You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

safe-stringify

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-stringify - npm Package Compare versions

Comparing version

to
1.1.0

index.d.ts

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"
}
}