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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 18 additions & 32 deletions README.md
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
21 changes: 12 additions & 9 deletions package.json
Expand Up @@ -52,21 +52,24 @@
},
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/preset-env": "^7.7.6",
"eslint": "^6.7.2",
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"rimraf": "^3.0.0"
},
"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
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