serialize-to-js
Advanced tools
Comparing version 3.0.3 to 3.1.0
212
lib/index.js
@@ -7,2 +7,10 @@ /* | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var utils = require('./internal/utils'); | ||
@@ -15,3 +23,3 @@ | ||
* @example <caption>serializing regex, date, buffer, ...</caption> | ||
* const serialize = require('serialize-to-js').serialize; | ||
* const serialize = require('serialize-to-js') | ||
* const obj = { | ||
@@ -28,8 +36,15 @@ * str: '<script>var a = 0 > 1</script>', | ||
* 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?$/ } }; | ||
@@ -52,89 +67,150 @@ * obj.reference = obj.object; | ||
function serialize(source, opts) { | ||
var type; | ||
function serialize(source) { | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
opts = opts || {}; | ||
var visited = new Set(); | ||
opts.references = []; | ||
var refs = new Ref(opts.references, opts); | ||
if (!opts._visited) { | ||
opts._visited = []; | ||
} | ||
function stringify(source, opts) { | ||
var type = utils.toType(source); | ||
if (!opts._refs) { | ||
opts.references = []; | ||
opts._refs = new Ref(opts.references, opts); | ||
} | ||
if (visited.has(source)) { | ||
if (opts.ignoreCircular) { | ||
switch (type) { | ||
case 'Array': | ||
return '[/*[Circular]*/]'; | ||
if (utils.isNull(source)) { | ||
return 'null'; | ||
} else if (Array.isArray(source)) { | ||
var tmp = source.map(function (item) { | ||
return serialize(item, opts); | ||
}); | ||
return "[".concat(tmp.join(', '), "]"); | ||
} else if (utils.isFunction(source)) { | ||
// serializes functions only in unsafe mode! | ||
var _tmp = source.toString(); | ||
case 'Object': | ||
return '{/*[Circular]*/}'; | ||
var _tmp2 = opts.unsafe ? _tmp : utils.saferFunctionString(_tmp, opts); // append function to es6 function within obj | ||
default: | ||
return 'undefined /*[Circular]*/'; | ||
} | ||
} else { | ||
throw new Error('can not convert circular structures.'); | ||
} | ||
} | ||
switch (type) { | ||
case 'Null': | ||
return 'null'; | ||
return !/^\s*(function|\([^)]*?\)\s*=>)/m.test(_tmp2) ? 'function ' + _tmp2 : _tmp2; | ||
} else if (utils.isObject(source)) { | ||
if (utils.isRegExp(source)) { | ||
return "new RegExp(".concat(utils.quote(source.source, opts), ", \"").concat(source.flags, "\")"); | ||
} else if (utils.isDate(source)) { | ||
return "new Date(".concat(utils.quote(source.toJSON(), opts), ")"); | ||
} else if (utils.isError(source)) { | ||
return "new Error(".concat(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('".concat(source.toString('base64'), "', 'base64')"); | ||
} else if (type = utils.isTypedArray(source)) { | ||
var _tmp3 = []; | ||
case 'String': | ||
return utils.quote(source, opts); | ||
for (var i = 0; i < source.length; i++) { | ||
_tmp3.push(source[i]); | ||
} | ||
case 'Function': | ||
{ | ||
var _tmp = source.toString(); | ||
return "new ".concat(type, "([").concat(_tmp3.join(', '), "])"); | ||
} else { | ||
var _tmp4 = []; // copy properties if not circular | ||
var tmp = opts.unsafe ? _tmp : utils.saferFunctionString(_tmp, opts); // append function to es6 function within obj | ||
if (!~opts._visited.indexOf(source)) { | ||
opts._visited.push(source); | ||
return !/^\s*(function|\([^)]*?\)\s*=>)/m.test(tmp) ? 'function ' + tmp : tmp; | ||
} | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
if (opts.reference && utils.isObject(source[key])) { | ||
opts._refs.push(key); | ||
case 'RegExp': | ||
return "new RegExp(".concat(utils.quote(source.source, opts), ", \"").concat(source.flags, "\")"); | ||
if (!opts._refs.hasReference(source[key])) { | ||
_tmp4.push(Ref.wrapkey(key, opts) + ': ' + serialize(source[key], opts)); | ||
} | ||
case 'Date': | ||
if (utils.isInvalidDate(source)) return 'new Date("Invalid Date")'; | ||
return "new Date(".concat(utils.quote(source.toJSON(), opts), ")"); | ||
opts._refs.pop(); | ||
} else { | ||
_tmp4.push(Ref.wrapkey(key, opts) + ': ' + serialize(source[key], opts)); | ||
} | ||
case 'Error': | ||
return "new Error(".concat(utils.quote(source.message, opts), ")"); | ||
case 'Buffer': | ||
return "Buffer.from(\"".concat(source.toString('base64'), "\", \"base64\")"); | ||
case 'Array': | ||
{ | ||
visited.add(source); | ||
var _tmp2 = source.map(function (item) { | ||
return stringify(item, opts); | ||
}); | ||
visited["delete"](source); | ||
return "[".concat(_tmp2.join(', '), "]"); | ||
} | ||
case 'Int8Array': | ||
case 'Uint8Array': | ||
case 'Uint8ClampedArray': | ||
case 'Int16Array': | ||
case 'Uint16Array': | ||
case 'Int32Array': | ||
case 'Uint32Array': | ||
case 'Float32Array': | ||
case 'Float64Array': | ||
{ | ||
var _tmp3 = []; | ||
for (var i = 0; i < source.length; i++) { | ||
_tmp3.push(source[i]); | ||
} | ||
return "new ".concat(type, "([").concat(_tmp3.join(', '), "])"); | ||
} | ||
opts._visited.pop(); | ||
case 'Set': | ||
{ | ||
visited.add(source); | ||
return "{".concat(_tmp4.join(', '), "}"); | ||
} else { | ||
if (opts.ignoreCircular) { | ||
return '{/*[Circular]*/}'; | ||
} else { | ||
throw new Error('can not convert circular structures.'); | ||
var _tmp4 = Array.from(source).map(function (item) { | ||
return stringify(item, opts); | ||
}); | ||
visited["delete"](source); | ||
return "new ".concat(type, "([").concat(_tmp4.join(', '), "])"); | ||
} | ||
} | ||
case 'Map': | ||
{ | ||
visited.add(source); | ||
var _tmp5 = Array.from(source).map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
key = _ref2[0], | ||
value = _ref2[1]; | ||
return "[".concat(stringify(key, opts), ", ").concat(stringify(value, opts), "]"); | ||
}); | ||
visited["delete"](source); | ||
return "new ".concat(type, "([").concat(_tmp5.join(', '), "])"); | ||
} | ||
case 'Object': | ||
{ | ||
visited.add(source); | ||
var _tmp6 = []; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
if (opts.reference && utils.isObject(source[key])) { | ||
refs.push(key); | ||
if (!refs.hasReference(source[key])) { | ||
_tmp6.push(Ref.wrapkey(key, opts) + ': ' + stringify(source[key], opts)); | ||
} | ||
refs.pop(); | ||
} else { | ||
_tmp6.push(Ref.wrapkey(key, opts) + ': ' + stringify(source[key], opts)); | ||
} | ||
} | ||
} | ||
visited["delete"](source); | ||
return "{".concat(_tmp6.join(', '), "}"); | ||
} | ||
default: | ||
return '' + source; | ||
} | ||
} else if (utils.isString(source)) { | ||
return utils.quote(source, opts); | ||
} else { | ||
return '' + source; | ||
} | ||
return stringify(source, opts); | ||
} | ||
module.exports = serialize; |
@@ -44,23 +44,2 @@ 'use strict'; | ||
function objectToString(o) { | ||
return Object.prototype.toString.call(o); | ||
} | ||
function toType(o) { | ||
var 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) { | ||
@@ -70,26 +49,17 @@ return _typeof(arg) === 'object' && arg !== null; | ||
function isDate(d) { | ||
return isObject(d) && objectToString(d) === '[object Date]'; | ||
function isBuffer(arg) { | ||
return arg instanceof Buffer; | ||
} | ||
function isError(e) { | ||
return isObject(e) && (objectToString(e) === '[object Error]' || e instanceof Error); | ||
function isInvalidDate(arg) { | ||
return isNaN(arg.getTime()); | ||
} | ||
function isFunction(arg) { | ||
return typeof arg === 'function'; | ||
} | ||
function toType(o) { | ||
var _type = Object.prototype.toString.call(o); | ||
function isBuffer(arg) { | ||
return arg instanceof Buffer; | ||
} | ||
var type = _type.substring(8, _type.length - 1); | ||
var TYPED_ARRAYS = ['Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array']; | ||
function isTypedArray(arg) { | ||
var type = toType(arg); | ||
if (TYPED_ARRAYS.indexOf(type) !== -1) { | ||
return type; | ||
} | ||
if (type === 'Uint8Array' && isBuffer(o)) return 'Buffer'; | ||
return type; | ||
} | ||
@@ -102,11 +72,6 @@ | ||
saferFunctionString: saferFunctionString, | ||
isString: isString, | ||
isNull: isNull, | ||
isRegExp: isRegExp, | ||
isBuffer: isBuffer, | ||
isObject: isObject, | ||
isDate: isDate, | ||
isError: isError, | ||
isFunction: isFunction, | ||
isBuffer: isBuffer, | ||
isTypedArray: isTypedArray | ||
isInvalidDate: isInvalidDate, | ||
toType: toType | ||
}; |
{ | ||
"name": "serialize-to-js", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "serialize objects to javascript", | ||
@@ -55,13 +55,13 @@ "keywords": [ | ||
"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" | ||
@@ -72,3 +72,6 @@ }, | ||
}, | ||
"maintainers": "commenthol <commenthol@gmail.com>" | ||
"maintainers": "commenthol <commenthol@gmail.com>", | ||
"mocha": { | ||
"check-leaks": true | ||
} | ||
} |
@@ -25,14 +25,5 @@ # serialize-to-js | ||
- Float64Array | ||
- Set | ||
- Map | ||
> **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') | ||
> ``` | ||
## Table of Contents | ||
@@ -44,3 +35,2 @@ | ||
* [serialize](#serialize) | ||
* [serializeToModule](#serializetomodule) | ||
* [Contribution and License Agreement](#contribution-and-license-agreement) | ||
@@ -62,4 +52,4 @@ * [License](#license) | ||
```js | ||
var serialize = require('serialize-to-js') | ||
var obj = { | ||
const serialize = require('serialize-to-js') | ||
const obj = { | ||
str: '<script>var a = 0 > 1</script>', | ||
@@ -74,6 +64,13 @@ num: 3.1415, | ||
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]])}' | ||
``` | ||
@@ -96,20 +93,10 @@ | ||
**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 | ||
@@ -129,2 +116,1 @@ | ||
[LICENSE]: ./LICENSE | ||
[serialize-to-module]: https://npmjs.com/package/serialize-to-module |
159
src/index.js
@@ -16,3 +16,3 @@ /* | ||
* @example <caption>serializing regex, date, buffer, ...</caption> | ||
* const serialize = require('serialize-to-js').serialize; | ||
* const serialize = require('serialize-to-js') | ||
* const obj = { | ||
@@ -29,8 +29,15 @@ * str: '<script>var a = 0 > 1</script>', | ||
* 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?$/ } }; | ||
@@ -51,77 +58,107 @@ * obj.reference = obj.object; | ||
*/ | ||
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.') | ||
} | ||
return `new ${type}([${tmp.join(', ')}])` | ||
} else { | ||
const tmp = [] | ||
// copy properties if not circular | ||
if (!~opts._visited.indexOf(source)) { | ||
opts._visited.push(source) | ||
} | ||
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(', ')}])` | ||
} | ||
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 |
@@ -41,23 +41,2 @@ 'use strict' | ||
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) { | ||
@@ -67,15 +46,2 @@ 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) { | ||
@@ -85,19 +51,11 @@ 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 | ||
} | ||
@@ -110,11 +68,6 @@ | ||
saferFunctionString, | ||
isString, | ||
isNull, | ||
isRegExp, | ||
isBuffer, | ||
isObject, | ||
isDate, | ||
isError, | ||
isFunction, | ||
isBuffer, | ||
isTypedArray | ||
isInvalidDate, | ||
toType | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
24843
9
613
111