Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Set, Map #16

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add support for Set Map
- refactor: code
- refactor: tests
- chore: add mocha.opts to package.json
- docs: update README.md
  • Loading branch information
commenthol committed Jan 27, 2020
commit 661c2fc2845f1075442c200312a3ebd1cdb9b2bd
50 changes: 18 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,15 @@ The following Objects are supported
- Int16Array, Uint16Array
- Int32Array, Uint32Array, Float32Array
- Float64Array

> **Note:** Version >3.0.0 has moved the serializeToModule method into its own
> package at [serialize-to-module][]
>
> Migrating from 2.x to 3.x for serialize:
> ```js
> // v2.x
> const serialize = require('serialize-to-js').serialize
> // >v3.x
> const serialize = require('serialize-to-js')
> ```
- Set
- Map

## Table of Contents

<!-- !toc (minlevel=2 omit="Table of Contents") -->

* [Methods](#methods)
* [serialize](#serialize)
* [serializeToModule](#serializetomodule)
* [Contribution and License Agreement](#contribution-and-license-agreement)
* [License](#license)

Expand All @@ -57,8 +47,8 @@ serializes an object to javascript
#### Example - serializing regex, date, buffer, ...

```js
var serialize = require('serialize-to-js')
var obj = {
const serialize = require('serialize-to-js')
const obj = {
str: '<script>var a = 0 > 1</script>',
num: 3.1415,
bool: true,
Expand All @@ -68,10 +58,17 @@ var obj = {
arr: [1, '2'],
regexp: /^test?$/,
date: new Date(),
buffer: Buffer.from('data'),
buffer: new Buffer('data'),
set: new Set([1, 2, 3]),
map: new Map([['a': 1],['b': 2]])
}
console.log(serialize(obj))
// > {str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E", num: 3.1415, bool: true, nil: null, undef: undefined, obj: {foo: "bar"}, arr: [1, "2"], regexp: /^test?$/, date: new Date("2016-04-15T16:22:52.009Z"), buffer: new Buffer('ZGF0YQ==', 'base64')}
//> '{str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E",
//> num: 3.1415, bool: true, nil: null, undef: undefined,
//> obj: {foo: "bar"}, arr: [1, "2"], regexp: new RegExp("^test?$", ""),
//> date: new Date("2019-12-29T10:37:36.613Z"),
//> buffer: Buffer.from("ZGF0YQ==", "base64"), set: new Set([1, 2, 3]),
//> map: new Map([["a", 1], ["b", 2]])}'
```

#### Example - serializing while respecting references
Expand All @@ -89,24 +86,14 @@ console.log(opts.references);

**Parameters**

**source**: `Object | Array | function | Any`, source to serialize

**opts**: `Object`, options

**opts.ignoreCircular**: `Boolean`, ignore circular objects

**opts.reference**: `Boolean`, reference instead of a copy (requires post-processing of opts.references)

**opts.unsafe**: `Boolean`, do not escape chars `<>/`

**source**: `Object | Array | function | Any`, source to serialize
**opts**: `Object`, options
**opts.ignoreCircular**: `Boolean`, ignore circular objects
**opts.reference**: `Boolean`, reference instead of a copy (requires post-processing of opts.references)
**opts.unsafe**: `Boolean`, do not escape chars `<>/`
**Returns**: `String`, serialized representation of `source`


### serializeToModule

The `serializeToModule` has been moved to it\`s own repository at [serialize-to-module][].


## Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your
Expand All @@ -121,4 +108,3 @@ Copyright (c) 2016- commenthol (MIT License)
See [LICENSE][] for more info.

[LICENSE]: ./LICENSE
[serialize-to-module]: https://npmjs.com/package/serialize-to-module
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@
"engines": {
"node": ">=4.0.0"
},
"maintainers": "commenthol <commenthol@gmail.com>"
"maintainers": "commenthol <commenthol@gmail.com>",
"mocha": {
"check-leaks": true
}
}
159 changes: 98 additions & 61 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Ref = require('./internal/reference')
* serializes an object to javascript
*
* @example <caption>serializing regex, date, buffer, ...</caption>
* const serialize = require('serialize-to-js').serialize;
* const serialize = require('serialize-to-js')
* const obj = {
* str: '<script>var a = 0 > 1</script>',
* num: 3.1415,
Expand All @@ -25,12 +25,19 @@ const Ref = require('./internal/reference')
* regexp: /^test?$/,
* date: new Date(),
* buffer: new Buffer('data'),
* set: new Set([1, 2, 3]),
* map: new Map([['a': 1],['b': 2]])
* }
* console.log(serialize(obj))
* // > {str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E", num: 3.1415, bool: true, nil: null, undef: undefined, obj: {foo: "bar"}, arr: [1, "2"], regexp: /^test?$/, date: new Date("2016-04-15T16:22:52.009Z"), buffer: new Buffer('ZGF0YQ==', 'base64')}
* //> '{str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E",
* //> num: 3.1415, bool: true, nil: null, undef: undefined,
* //> obj: {foo: "bar"}, arr: [1, "2"], regexp: new RegExp("^test?$", ""),
* //> date: new Date("2019-12-29T10:37:36.613Z"),
* //> buffer: Buffer.from("ZGF0YQ==", "base64"), set: new Set([1, 2, 3]),
* //> map: new Map([["a", 1], ["b", 2]])}'
*
* @example <caption>serializing while respecting references</caption>
* const serialize = require('serialize-to-js').serialize;
* const serialize = require('serialize-to-js')
* const obj = { object: { regexp: /^test?$/ } };
* obj.reference = obj.object;
* const opts = { reference: true };
Expand All @@ -46,79 +53,109 @@ const Ref = require('./internal/reference')
* @param {Boolean} opts.unsafe - do not escape chars `<>/`
* @return {String} serialized representation of `source`
*/
function serialize (source, opts) {
let type

function serialize (source, opts = {}) {
opts = opts || {}
if (!opts._visited) {
opts._visited = []
}
if (!opts._refs) {
opts.references = []
opts._refs = new Ref(opts.references, opts)
}

if (utils.isNull(source)) {
return 'null'
} else if (Array.isArray(source)) {
const tmp = source.map(item => serialize(item, opts))
return `[${tmp.join(', ')}]`
} else if (utils.isFunction(source)) {
// serializes functions only in unsafe mode!
const _tmp = source.toString()
const tmp = opts.unsafe ? _tmp : utils.saferFunctionString(_tmp, opts)
// append function to es6 function within obj
return !/^\s*(function|\([^)]*?\)\s*=>)/m.test(tmp) ? 'function ' + tmp : tmp
} else if (utils.isObject(source)) {
if (utils.isRegExp(source)) {
return `new RegExp(${utils.quote(source.source, opts)}, "${source.flags}")`
} else if (utils.isDate(source)) {
return `new Date(${utils.quote(source.toJSON(), opts)})`
} else if (utils.isError(source)) {
return `new Error(${utils.quote(source.message, opts)})`
} else if (utils.isBuffer(source)) {
// check for buffer first otherwise tests fail on node@4.4
// looks like buffers are accidentially detected as typed arrays
return `Buffer.from('${source.toString('base64')}', 'base64')`
} else if ((type = utils.isTypedArray(source))) {
const tmp = []
for (let i = 0; i < source.length; i++) {
tmp.push(source[i])
const visited = new Set()
opts.references = []
const refs = new Ref(opts.references, opts)

function stringify (source, opts) {
const type = utils.toType(source)

if (visited.has(source)) {
if (opts.ignoreCircular) {
switch (type) {
case 'Array':
return '[/*[Circular]*/]'
case 'Object':
return '{/*[Circular]*/}'
default:
return 'undefined /*[Circular]*/'
}
} else {
throw new Error('can not convert circular structures.')
}
}

switch (type) {
case 'Null':
return 'null'
case 'String':
return utils.quote(source, opts)
case 'Function': {
const _tmp = source.toString()
const tmp = opts.unsafe ? _tmp : utils.saferFunctionString(_tmp, opts)
// append function to es6 function within obj
return !/^\s*(function|\([^)]*?\)\s*=>)/m.test(tmp) ? 'function ' + tmp : tmp
}
case 'RegExp':
return `new RegExp(${utils.quote(source.source, opts)}, "${source.flags}")`
case 'Date':
if (utils.isInvalidDate(source)) return 'new Date("Invalid Date")'
return `new Date(${utils.quote(source.toJSON(), opts)})`
case 'Error':
return `new Error(${utils.quote(source.message, opts)})`
case 'Buffer':
return `Buffer.from("${source.toString('base64')}", "base64")`
case 'Array': {
visited.add(source)
const tmp = source.map(item => stringify(item, opts))
visited.delete(source)
return `[${tmp.join(', ')}]`
}
case 'Int8Array':
case 'Uint8Array':
case 'Uint8ClampedArray':
case 'Int16Array':
case 'Uint16Array':
case 'Int32Array':
case 'Uint32Array':
case 'Float32Array':
case 'Float64Array': {
const tmp = []
for (let i = 0; i < source.length; i++) {
tmp.push(source[i])
}
return `new ${type}([${tmp.join(', ')}])`
}
case 'Set': {
visited.add(source)
const tmp = Array.from(source).map(item => stringify(item, opts))
visited.delete(source)
return `new ${type}([${tmp.join(', ')}])`
}
return `new ${type}([${tmp.join(', ')}])`
} else {
const tmp = []
// copy properties if not circular
if (!~opts._visited.indexOf(source)) {
opts._visited.push(source)
case 'Map': {
visited.add(source)
const tmp = Array.from(source).map(([key, value]) => `[${stringify(key, opts)}, ${stringify(value, opts)}]`)
visited.delete(source)
return `new ${type}([${tmp.join(', ')}])`
}
case 'Object': {
visited.add(source)
const tmp = []
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
if (opts.reference && utils.isObject(source[key])) {
opts._refs.push(key)
if (!opts._refs.hasReference(source[key])) {
tmp.push(Ref.wrapkey(key, opts) + ': ' + serialize(source[key], opts))
refs.push(key)
if (!refs.hasReference(source[key])) {
tmp.push(Ref.wrapkey(key, opts) + ': ' + stringify(source[key], opts))
}
opts._refs.pop()
refs.pop()
} else {
tmp.push(Ref.wrapkey(key, opts) + ': ' + serialize(source[key], opts))
tmp.push(Ref.wrapkey(key, opts) + ': ' + stringify(source[key], opts))
}
}
}
opts._visited.pop()
visited.delete(source)
return `{${tmp.join(', ')}}`
} else {
if (opts.ignoreCircular) {
return '{/*[Circular]*/}'
} else {
throw new Error('can not convert circular structures.')
}
}
default:
return '' + source
}
} else if (utils.isString(source)) {
return utils.quote(source, opts)
} else {
return '' + source
}

return stringify(source, opts)
}

module.exports = serialize
69 changes: 11 additions & 58 deletions src/internal/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,79 +38,32 @@ function saferFunctionString (str, opts) {
: str.replace(/(<\/?)([a-z][^>]*?>)/ig, (m, m1, m2) => safeString(m1) + m2)
}

function objectToString (o) {
return Object.prototype.toString.call(o)
}

function toType (o) {
const type = objectToString(o)
return type.substring(8, type.length - 1)
}

function isString (arg) {
return typeof arg === 'string'
}

function isNull (arg) {
return arg === null
}

function isRegExp (re) {
return isObject(re) && objectToString(re) === '[object RegExp]'
}

function isObject (arg) {
return typeof arg === 'object' && arg !== null
}

function isDate (d) {
return isObject(d) && objectToString(d) === '[object Date]'
}

function isError (e) {
return isObject(e) &&
(objectToString(e) === '[object Error]' || e instanceof Error)
}

function isFunction (arg) {
return typeof arg === 'function'
}

function isBuffer (arg) {
return arg instanceof Buffer
}

const TYPED_ARRAYS = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
]
function isInvalidDate (arg) {
return isNaN(arg.getTime())
}

function isTypedArray (arg) {
const type = toType(arg)
if (TYPED_ARRAYS.indexOf(type) !== -1) {
return type
}
function toType (o) {
const _type = Object.prototype.toString.call(o)
const type = _type.substring(8, _type.length - 1)
if (type === 'Uint8Array' && isBuffer(o)) return 'Buffer'
return type
}

module.exports = {
safeString,
unsafeString,
quote,
saferFunctionString,
isString,
isNull,
isRegExp,
isObject,
isDate,
isError,
isFunction,
isBuffer,
isTypedArray
isObject,
isInvalidDate,
toType
}
Loading