safe-stringify
Advanced tools
Comparing version
55
index.js
@@ -1,33 +0,28 @@ | ||
/* @flow */ | ||
function safeStringifyReplacer(seen) { | ||
return function (key, value) { | ||
if (value !== null && typeof value === 'object') { | ||
if (seen.has(value)) { | ||
return '[Circular]'; | ||
} | ||
/** | ||
* Handles cycles in an object. If there are self references then this will | ||
* force that value to not appear in the output. See https://goo.gl/FLN94e for | ||
* more information on this function. | ||
* | ||
* NOTE: that this does run in n^2 of the total number of unique objects | ||
* recursively within the object being stringified. | ||
*/ | ||
const decycler: Function = () => { | ||
const cache: Array<Object> = []; | ||
return (key: string, val: any) => { | ||
if (typeof val === 'object' && val !== null) { | ||
if (cache.indexOf(val) > -1) { | ||
// Return undefined as specified in the spec (see link above). | ||
return undefined; | ||
} else { | ||
cache.push(val); | ||
} | ||
} | ||
return val; | ||
} | ||
seen.add(value); | ||
const newValue = Array.isArray(value) ? [] : {}; | ||
for (const [key2, value2] of Object.entries(value)) { | ||
newValue[key2] = safeStringifyReplacer(seen)(key2, value2); | ||
} | ||
seen.delete(value); | ||
return newValue; | ||
} | ||
return value; | ||
}; | ||
} | ||
/** | ||
* Passes the above decycler function into the `JSON.stringify` function in | ||
* order to disallow cycles. | ||
* | ||
* @param {any} input Any primitive input type. | ||
* @return {string} Stringified result from the input param. | ||
*/ | ||
export default (input: any): string => JSON.stringify(input, decycler()); | ||
export default function safeStringify(object, {indentation} = {}) { | ||
const seen = new WeakSet(); | ||
return JSON.stringify(object, safeStringifyReplacer(seen), indentation); | ||
} |
{ | ||
"name": "safe-stringify", | ||
"version": "0.0.1", | ||
"description": "A safe substitute for JSON.stringify", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nedrocks/safe-stringify.git" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"json", | ||
"stringify", | ||
"cycle" | ||
], | ||
"author": "Ned Rockson <nedrocks@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nedrocks/safe-stringify/issues" | ||
}, | ||
"homepage": "https://github.com/nedrocks/safe-stringify#readme", | ||
"devDependencies": { | ||
"babel": "^6.5.2", | ||
"babel-cli": "^6.10.1", | ||
"babel-jest": "^13.2.2", | ||
"babel-plugin-transform-flow-strip-types": "^6.8.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"jest-cli": "^13.2.3" | ||
}, | ||
"jest": { | ||
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest", | ||
"testFileExtensions": [ | ||
"es6", | ||
"js" | ||
], | ||
"moduleFileExtensions": [ | ||
"js", | ||
"json", | ||
"es6" | ||
], | ||
"unmockedModulePathPatterns": [ | ||
"<rootDir>/node_modules/babel-runtime" | ||
] | ||
} | ||
"name": "safe-stringify", | ||
"version": "1.1.0", | ||
"description": "Serialize objects to JSON with handling for circular references", | ||
"license": "MIT", | ||
"repository": "sindresorhus/safe-stringify", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"json", | ||
"stringify", | ||
"safe", | ||
"circular", | ||
"destroy", | ||
"serialize", | ||
"encode", | ||
"cyclic", | ||
"object" | ||
], | ||
"devDependencies": { | ||
"ava": "^5.2.0", | ||
"tsd": "^0.28.1", | ||
"xo": "^0.54.2" | ||
} | ||
} |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3
-57.14%59
436.36%Yes
NaN4492
-9.64%5
-37.5%43
-34.85%1
Infinity%1
Infinity%1
Infinity%