fast-stringify
Advanced tools
Comparing version 1.1.2 to 2.0.0
# fast-stringify CHANGELOG | ||
## 2.0.0 | ||
- Rewritten in TypeScript | ||
- Better reference key identification | ||
### BREAKING CHANGES | ||
- CommonJS builds no longer need `.default` (`const stringify = require('fast-stringify');`) | ||
- Reference keys on circular objects now reflect the key structure leading to the object | ||
## 1.1.2 | ||
@@ -4,0 +14,0 @@ |
@@ -0,6 +1,9 @@ | ||
type StandardReplacer = (key: string, value: any) => any; | ||
type CircularReplacer = (key: string, value: any, referenceKey: string) => any; | ||
export default function stringify( | ||
object: any, | ||
replacer?: Function, | ||
value: any, | ||
replacer?: StandardReplacer, | ||
indent?: number, | ||
circularReplacer?: Function | ||
circularReplacer?: CircularReplacer, | ||
): string; |
{ | ||
"author": "tony_quetano@planttheidea.com", | ||
"ava": { | ||
"failFast": true, | ||
"files": [ | ||
"test/*.js" | ||
], | ||
"require": [ | ||
"@babel/register", | ||
"test/helpers/setup-browser-env.js" | ||
], | ||
"sources": [ | ||
"src/*.js" | ||
], | ||
"verbose": true | ||
}, | ||
"browser": "dist/index.js", | ||
"bugs": { | ||
@@ -22,12 +9,13 @@ "url": "https://github.com/planttheidea/fast-stringify/issues" | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/plugin-proposal-class-properties": "^7.4.4", | ||
"@babel/cli": "^7.5.0", | ||
"@babel/core": "^7.5.0", | ||
"@babel/plugin-proposal-class-properties": "^7.5.0", | ||
"@babel/plugin-proposal-json-strings": "^7.2.0", | ||
"@babel/plugin-transform-runtime": "^7.4.4", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/plugin-transform-runtime": "^7.5.0", | ||
"@babel/preset-env": "^7.5.0", | ||
"@babel/preset-react": "^7.0.0", | ||
"@babel/register": "^7.4.4", | ||
"@babel/runtime": "^7.4.5", | ||
"ava": "^2.1.0", | ||
"@babel/preset-typescript": "^7.3.3", | ||
"@babel/runtime": "^7.5.0", | ||
"@types/jest": "^24.0.15", | ||
"@types/react": "^16.8.23", | ||
"babel-eslint": "^10.0.2", | ||
@@ -37,3 +25,2 @@ "babel-loader": "^8.0.6", | ||
"benchmark": "^2.1.4", | ||
"browser-env": "^3.2.6", | ||
"decircularize": "^1.0.0", | ||
@@ -43,13 +30,10 @@ "eslint": "^6.0.1", | ||
"eslint-friendly-formatter": "^4.0.1", | ||
"eslint-loader": "^2.2.0", | ||
"eslint-loader": "^2.2.1", | ||
"eslint-plugin-import": "^2.18.0", | ||
"fast-json-stable-stringify": "^2.0.0", | ||
"fs-extra": "^8.1.0", | ||
"html-webpack-plugin": "^3.2.0", | ||
"in-publish": "^2.0.0", | ||
"jest": "^24.8.0", | ||
"json-cycle": "^1.3.0", | ||
"json-stable-stringify": "^1.0.1", | ||
"json-stringify-safe": "^5.0.1", | ||
"nyc": "^14.1.1", | ||
"optimize-js-plugin": "^0.0.4", | ||
"react": "^16.8.6", | ||
@@ -59,5 +43,6 @@ "react-dom": "^16.8.6", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-uglify": "^6.0.2", | ||
"rollup-plugin-terser": "^5.1.0", | ||
"sinon": "^7.3.2", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.2", | ||
"webpack": "^4.35.2", | ||
@@ -75,4 +60,4 @@ "webpack-cli": "^3.3.5", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"module": "es/index.js", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"name": "fast-stringify", | ||
@@ -84,25 +69,23 @@ "repository": { | ||
"scripts": { | ||
"benchmark": "NODE_ENV=production BABEL_ENV=benchmark babel src --out-dir lib --no-comments && node benchmark/index.js", | ||
"benchmark": "npm run build && node benchmark/index.js", | ||
"build": "NODE_ENV=production rollup -c", | ||
"clean": "npm run clean:es && npm run clean:lib && npm run clean:dist", | ||
"clean": "npm run clean:dist && npm run clean:mjs", | ||
"clean:dist": "rimraf dist", | ||
"clean:es": "rimraf es", | ||
"clean:lib": "rimraf lib", | ||
"clean:mjs": "rimraf mjs", | ||
"copy:mjs": "npm run clean:mjs && node ./es-to-mjs.js", | ||
"copy:mjs": "node ./es-to-mjs.js", | ||
"dev": "NODE_ENV=development webpack-dev-server --colors --progress --config=webpack/webpack.config.dev.js", | ||
"dist": "npm run clean:dist && npm run build", | ||
"lint": "NODE_ENV=test eslint src --max-warnings 0", | ||
"lint:fix": "NODE_ENV=test eslint src --fix", | ||
"prepublish": "if in-publish; then npm run prepublish:compile; fi", | ||
"prepublish:compile": "npm run lint && npm run test:coverage && npm run transpile:lib && npm run transpile:es && npm run copy:mjs && npm run dist", | ||
"dist": "npm run clean && npm run build && npm run copy:mjs", | ||
"lint": "NODE_ENV=test eslint src/*.ts --max-warnings 0", | ||
"lint:fix": "npm run lint -- --fix", | ||
"prepublishOnly": "npm run lint && npm run typecheck && npm run test:coverage && npm run dist", | ||
"release": "release-it", | ||
"release:beta": "release-it --config=.release-it.beta.json", | ||
"start": "npm run dev", | ||
"test": "NODE_PATH=. BABEL_ENV=test ava", | ||
"test:coverage": "nyc npm test", | ||
"test": "NODE_PATH=. BABEL_ENV=test jest", | ||
"test:coverage": "npm test -- --coverage", | ||
"test:watch": "npm test -- --watch", | ||
"transpile:es": "npm run clean:es && BABEL_ENV=es babel src --out-dir es", | ||
"transpile:lib": "npm run clean:lib && BABEL_ENV=lib babel src --out-dir lib" | ||
"typecheck": "tsc src/* --noEmit" | ||
}, | ||
"types": "index.d.ts", | ||
"version": "1.1.2" | ||
"version": "2.0.0" | ||
} |
132
README.md
# fast-stringify | ||
A [blazing fast](#benchmarks) stringifier that safely handles circular objects | ||
A tiny, [blazing fast](#benchmarks) stringifier that safely handles circular objects | ||
@@ -27,17 +27,17 @@ ## Table of contents | ||
```javascript | ||
import stringify from "fast-stringify"; | ||
import stringify from 'fast-stringify'; | ||
const object = { | ||
foo: "bar", | ||
foo: 'bar', | ||
deeply: { | ||
recursive: { | ||
object: {} | ||
} | ||
} | ||
object: {}, | ||
}, | ||
}, | ||
}; | ||
object.deeply.recursive.object = object; | ||
object.deeply.recursive.object = object.deeply.recursive; | ||
console.log(stringify(object)); | ||
// {"foo":"bar","deeply":{"recursive":{"object":"[ref-0]"}}} | ||
// {"foo":"bar","deeply":{"recursive":{"object":"[ref=.deeply.recursive]"}}} | ||
``` | ||
@@ -47,30 +47,35 @@ | ||
`stringify(object: any, replacer: ?function, indent: ?number, circularReplacer: ?function): string` | ||
```ts | ||
type StandardReplacer = (key: string, value: any) => any; | ||
type CircularReplacer = (key: string, value: any, referenceKey: string) => any; | ||
function stringify( | ||
value: any, | ||
replacer?: StandardReplacer, | ||
indent?: number, | ||
circularReplacer: CircularReplacer, | ||
): string; | ||
``` | ||
Stringifies the object passed based on the parameters you pass. The only required value is the `object`. The additional parameters passed will customize how the string is compiled. | ||
- `replacer` => function to customize how the value for each key is stringified (see [the documentation for JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) for more details) | ||
- `value` => the value to stringify | ||
- `replacer` => function to customize how the non-circular value is stringified (see [the documentation for JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) for more details) | ||
- `indent` => number of spaces to indent the stringified object for pretty-printing (see [the documentation for JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) for more details) | ||
- `circularReplacer` => function to customize how the circular reference is stringified (defaults to `[ref-##]` where `##` is the reference count) | ||
- `circularReplacer` => function to customize how the circular value is stringified (defaults to `[ref=##]` where `##` is the `referenceKey`) | ||
- `referenceKey` is a dot-separated key list reflecting the nested key the object was originally declared at | ||
## Importing | ||
ESM in browsers: | ||
```javascript | ||
import stringify from "fast-stringify"; | ||
``` | ||
// ESM in browsers | ||
import stringify from 'fast-stringify'; | ||
ESM in NodeJS: | ||
// ESM in NodeJS | ||
import stringify from 'fast-stringify/mjs'; | ||
```javascript | ||
import stringify from "fast-stringify/mjs"; | ||
// CommonJS | ||
const stringify = require('fast-stringify'); | ||
``` | ||
CommonJS: | ||
```javascript | ||
const stringify = require("fast-stringify").default; | ||
``` | ||
## Benchmarks | ||
@@ -84,8 +89,8 @@ | ||
| -------------------------- | ------------------- | ------------------------ | | ||
| **fast-stringify** | **679,923** | **0.58%** | | ||
| fast-json-stable-stringify | 341,889 | 0.61% | | ||
| json-stringify-safe | 334,542 | 0.44% | | ||
| json-stable-stringify | 254,565 | 0.69% | | ||
| json-cycle | 196,417 | 0.56% | | ||
| decircularize | 145,771 | 0.47% | | ||
| **fast-stringify** | **598,072** | **0.59%** | | ||
| fast-json-stable-stringify | 339,082 | 0.86% | | ||
| json-stringify-safe | 333,447 | 0.46% | | ||
| json-stable-stringify | 255,619 | 0.71% | | ||
| json-cycle | 194,553 | 0.60% | | ||
| decircularize | 141,821 | 1.35% | | ||
@@ -98,8 +103,8 @@ #### Complex objects | ||
| -------------------------- | ------------------- | ------------------------ | | ||
| **fast-stringify** | **128,056** | **0.50%** | | ||
| json-stringify-safe | 59,644 | 0.54% | | ||
| fast-json-stable-stringify | 57,641 | 1.07% | | ||
| json-cycle | 50,753 | 0.69% | | ||
| json-stable-stringify | 39,456 | 0.95% | | ||
| decircularize | 26,572 | 0.74% | | ||
| **fast-stringify** | **97,559** | **0.32%** | | ||
| json-stringify-safe | 59,948 | 0.44% | | ||
| fast-json-stable-stringify | 57,656 | 1.14% | | ||
| json-cycle | 51,892 | 0.59% | | ||
| json-stable-stringify | 39,180 | 1.01% | | ||
| decircularize | 27,047 | 0.84% | | ||
@@ -112,6 +117,6 @@ #### Circular objects | ||
| ------------------------------------------ | ------------------- | ------------------------ | | ||
| **fast-stringify** | **116,247** | **0.52%** | | ||
| json-stringify-safe | 56,599 | 0.44% | | ||
| json-cycle | 47,026 | 0.91% | | ||
| decircularize | 25,084 | 0.72% | | ||
| **fast-stringify** | **87,030** | **0.51%** | | ||
| json-stringify-safe | 56,329 | 0.49% | | ||
| json-cycle | 48,116 | 0.77% | | ||
| decircularize | 25,240 | 0.68% | | ||
| fast-json-stable-stringify (not supported) | 0 | 0.00% | | ||
@@ -126,8 +131,8 @@ | json-stable-stringify (not supported) | 0 | 0.00% | | ||
| -------------------------- | ------------------- | ------------------------ | | ||
| **fast-stringify** | **34,901** | **0.54%** | | ||
| json-stringify-safe | 19,514 | 0.31% | | ||
| json-cycle | 18,329 | 0.68% | | ||
| fast-json-stable-stringify | 18,258 | 0.81% | | ||
| json-stable-stringify | 13,059 | 0.83% | | ||
| decircularize | 9,168 | 1.15% | | ||
| **fast-stringify** | **24,250** | **0.38%** | | ||
| json-stringify-safe | 19,526 | 0.52% | | ||
| json-cycle | 18,433 | 0.74% | | ||
| fast-json-stable-stringify | 18,202 | 0.73% | | ||
| json-stable-stringify | 13,041 | 0.87% | | ||
| decircularize | 9,175 | 0.82% | | ||
@@ -138,18 +143,19 @@ ## Development | ||
- benchmark => run benchmark tests against other equality libraries | ||
- build => build dist files with `rollup` | ||
- clean => run `clean:dist`, `clean:es`, and `clean:lib` scripts | ||
- clean:dist => run `rimraf` on the `dist` folder | ||
- clean:es => run `rimraf` on the `es` folder | ||
- clean:lib => run `rimraf` on the `lib` folder | ||
- dev => start webpack playground App | ||
- dist => run `build` and `build:minified` scripts | ||
- lint => run ESLint on all files in `src` folder (also runs on `dev` script) | ||
- lint:fix => run `lint` script, but with auto-fixer | ||
- prepublish:compile => run `lint`, `test:coverage`, `transpile:lib`, `transpile:es`, and `dist` scripts | ||
- start => run `dev` | ||
- test => run AVA with NODE_ENV=test on all files in `test` folder | ||
- test:coverage => run same script as `test` with code coverage calculation via `nyc` | ||
- test:watch => run same script as `test` but keep persistent watcher | ||
- transpile:es => run Babel on all files in `src` folder (transpiled to `es` folder without transpilation of ES2015 export syntax) | ||
- transpile:lib => run Babel on all files in `src` folder (transpiled to `lib` folder) | ||
- `benchmark` => run benchmark tests against other equality libraries | ||
- `build` => build dist files with `rollup` | ||
- `clean` => run `clean:dist` and `clean:mjs` scripts | ||
- `clean:dist` => run `rimraf` on the `dist` folder | ||
- `clean:mjs` => run `rimraf` on the `mjs` folder | ||
- `copy:mjs` => copy and transform the ESM file generated by `dist` to be consumable as an `.mjs` file | ||
- `dev` => start webpack playground App | ||
- `dist` => run `clean`, `build`, and `copy:mjs` scripts | ||
- `lint` => run ESLint on all files in `src` folder (also runs on `dev` script) | ||
- `lint:fix` => run `lint` script, but with auto-fixer | ||
- `prepublishOnly` => run `lint`, `typecheck`, `test:coverage`, and `dist` scripts | ||
- `release` => run `release-it` for standard versions (expected to be installed globally) | ||
- `release:beta` => run `release-it` for beta versions (expected to be installed globally) | ||
- `start` => run `dev` | ||
- `test` => run Jest with NODE_ENV=test on all files in `__tests__` folder | ||
- `test:coverage` => run same script as `test` with code coverage calculation | ||
- `test:watch` => run same script as `test` but keep persistent watcher | ||
- `typecheck` => run TypeScript types validation |
Sorry, the diff of this file is not supported yet
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
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
44939
38
16
155
0
382
1