Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
aab1c68
Improve typescript typing
sxwei123 Nov 15, 2020
3878959
Rewrite project with typescript
sxwei123 Dec 31, 2020
a9df899
Setup github action for unit test
sxwei123 Dec 31, 2020
542920e
Setup CI to update npm package when there is new commit to master branch
sxwei123 Dec 31, 2020
40c449f
Improve document
sxwei123 Dec 31, 2020
bb3be27
Merge pull request #1 from sxwei123/develop
sxwei123 Dec 31, 2020
53c7b24
Add more examples into document
sxwei123 Dec 31, 2020
3453ccb
Remove unused files
sxwei123 Dec 31, 2020
bc83882
v0.0.2
sxwei123 Dec 31, 2020
daed177
Merge pull request #2 from sxwei123/develop
sxwei123 Dec 31, 2020
c37841c
Improve document
sxwei123 Dec 31, 2020
3f9b7d2
v0.0.3
sxwei123 Dec 31, 2020
f22cccf
Merge pull request #3 from sxwei123/develop
sxwei123 Dec 31, 2020
ebe52e7
Update license
sxwei123 Jan 1, 2021
bbf74d9
Add test status badge to document
sxwei123 Jan 1, 2021
aaa0094
Add test coverage
sxwei123 Jan 1, 2021
f348d04
Improve github workflow
sxwei123 Jan 1, 2021
89ab2ab
Run lint before test
sxwei123 Jan 1, 2021
57cc593
v0.1.0
sxwei123 Jan 1, 2021
87e9cb5
Merge pull request #4 from sxwei123/develop
sxwei123 Jan 1, 2021
399d6fe
Fix coverage flow
sxwei123 Jan 1, 2021
8de2e9b
v0.1.1
sxwei123 Jan 1, 2021
ed1d4f8
Merge pull request #5 from sxwei123/develop
sxwei123 Jan 1, 2021
db8488f
v0.1.2
sxwei123 Jan 1, 2021
dc5074e
Merge pull request #6 from sxwei123/develop
sxwei123 Jan 1, 2021
5778ed3
Remove dist directory from git
sxwei123 Jan 1, 2021
871f71c
v0.1.3
sxwei123 Jan 1, 2021
126a921
Merge pull request #7 from sxwei123/develop
sxwei123 Jan 1, 2021
7c0f4f6
Improve code readability
sxwei123 Jan 2, 2021
ca9740c
Setup git pre-commit and pre-push
sxwei123 Jan 2, 2021
f4fec62
Update readme
sxwei123 Jan 2, 2021
36070dd
v0.1.4
sxwei123 Jan 2, 2021
e705c0a
Merge pull request #8 from sxwei123/develop
sxwei123 Jan 2, 2021
b6efe80
Update readme
sxwei123 Jan 2, 2021
fc9c422
v0.1.5
sxwei123 Jan 2, 2021
c0de6fd
Merge pull request #9 from sxwei123/develop
sxwei123 Jan 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
benchmark
60 changes: 35 additions & 25 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
extends: eslint:recommended
env:
node: true
browser: true
---
parser: "@typescript-eslint/parser"
plugins:
- "@typescript-eslint"
extends:
- "plugin:@typescript-eslint/recommended"
- eslint-config-prettier
- plugin:import/errors
- plugin:import/warnings
- plugin:import/typescript
rules:
block-scoped-var: 2
callback-return: 2
dot-notation: 2
indent: 2
linebreak-style: [2, unix]
new-cap: 2
no-console: [2, allow: [warn, error]]
no-else-return: 2
no-eq-null: 2
no-fallthrough: 2
no-invalid-this: 2
no-return-assign: 2
no-shadow: 1
no-trailing-spaces: 2
no-use-before-define: [2, nofunc]
quotes: [2, single, avoid-escape]
semi: [2, always]
strict: [2, global]
valid-jsdoc: [2, requireReturn: false]
no-control-regex: 0
no-useless-escape: 2
"@typescript-eslint/no-unused-vars":
- warn
- argsIgnorePattern: "^_"
no-use-before-define:
- warn
"@typescript-eslint/no-use-before-define":
- warn
no-unused-expressions:
- error
no-underscore-dangle:
- error
- # allow couchdb stuff
allow:
- _id
- _rev
- _deleted
- __typename
allowAfterThis: true
allowAfterSuper: true
enforceInMethodNames: true
one-var:
- error
- never
class-methods-use-this:
- error
2 changes: 0 additions & 2 deletions .github/FUNDING.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: "Coverage"

on:
pull_request:
branches:
- master

jobs:
release:
name: Run test coverage
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup node.js
uses: actions/setup-node@v1
with:
node-version: "14.x"
registry-url: "https://registry.npmjs.org"
- run: yarn install
- run: yarn coverage
env:
NODE_ENV: test
- name: Add coverage to comment
uses: romeovs/lcov-reporter-action@v0.2.16
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Publish"

on:
push:
branches:
- master

jobs:
release:
name: Build and publish
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup node.js
uses: actions/setup-node@v1
with:
node-version: "10.x"
registry-url: "https://registry.npmjs.org"
- run: yarn install
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Lint and Test"

on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
release:
name: Run lint and test
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
registry-url: "https://registry.npmjs.org"
- run: yarn install
- run: yarn lint
- run: yarn test
env:
NODE_ENV: test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
node_modules
.nyc_output/
coverage/
.DS_Store

.vscode

dist
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
This software is released under the MIT license:

Copyright (c) 2021 Leon Song
Copyright (c) 2017 Evgeny Poberezkin
Copyright (c) 2013 James Halliday

Expand Down
127 changes: 67 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,79 @@
# fast-json-stable-stringify
# quick-stable-stringify

Deterministic `JSON.stringify()` - a faster version of [@substack](https://github.com/substack)'s json-stable-strigify without [jsonify](https://github.com/substack/jsonify).
![Test status](https://github.com/sxwei123/quick-stable-stringify/workflows/Lint%20and%20Test/badge.svg?branch=master)

You can also pass in a custom comparison function.
Deterministic `JSON.stringify()` - a faster version of [@epoberezkin](https://github.com/epoberezkin)'s [fast-json-stable-strigify](https://github.com/epoberezkin/fast-json-stable-stringify). Built with Typescript and modern Javascript.

[![Build Status](https://travis-ci.org/epoberezkin/fast-json-stable-stringify.svg?branch=master)](https://travis-ci.org/epoberezkin/fast-json-stable-stringify)
[![Coverage Status](https://coveralls.io/repos/github/epoberezkin/fast-json-stable-stringify/badge.svg?branch=master)](https://coveralls.io/github/epoberezkin/fast-json-stable-stringify?branch=master)
## Compatibility

# example
NodeJS 10+ or browser with ES2015 support.

``` js
var stringify = require('fast-json-stable-stringify');
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
console.log(stringify(obj));
## Features

- Support both browser and NodeJS
- Support custom comparator function
- No dependencies

## Install

With npm do:

```
npm install quick-stable-stringify
```

output:
Or install with yarn:

```
{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
yarn add quick-stable-stringify
```

## Examples

# methods
Node.JS with CommonJS:

``` js
var stringify = require('fast-json-stable-stringify')
```js
const stringify = require("quick-stable-stringify");

const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
console.log(stringify(obj));
```

## var str = stringify(obj, opts)
Typescript or modern Javascript environment:

```ts
import stringify from "quick-stable-stringify";

Return a deterministic stringified string `str` from the object `obj`.
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
console.log(stringify(obj));
```

## Options

## options
Options can be a comparator function or an object which has two optional properties: `cmp` and `cycles`.

### cmp

If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
function for object keys. Your function `opts.cmp` is called with these
parameters:
`opts.cmp` is the custom comparator function that user can specify. If custom comparator function is not provided, the JSON string of an object will be sorted by the alphanumeric order of object keys.
The type of the comparator function is defined as:

```ts
interface KeyValue {
key: string;
value: any;
}

``` js
opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })
type ComparatorFunction = (a: KeyValue, b: KeyValue) => number;
```

For example, to sort on the object key names in reverse order you could write:
For example, to sort by the object keys in reverse order:

``` js
var stringify = require('fast-json-stable-stringify');
```js
var stringify = require("quick-stable-stringify");

var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
var s = stringify(obj, function (a, b) {
return a.key < b.key ? 1 : -1;
return a.key < b.key ? 1 : -1;
});
console.log(s);
```
Expand All @@ -63,14 +84,14 @@ which results in the output string:
{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
```

Or if you wanted to sort on the object values in reverse order, you could write:
To sort by the object values in reverse order:

```
var stringify = require('fast-json-stable-stringify');
```js
var stringify = require("quick-stable-stringify");

var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
var obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
var s = stringify(obj, function (a, b) {
return a.value < b.value ? 1 : -1;
return a.value < b.value ? 1 : -1;
});
console.log(s);
```
Expand All @@ -87,45 +108,31 @@ Pass `true` in `opts.cycles` to stringify circular property as `__cycle__` - the

TypeError will be thrown in case of circular object without this option.

## Benchmark

# install

With [npm](https://npmjs.org) do:

```
npm install fast-json-stable-stringify
```


# benchmark
To run benchmark (requires Node.js 10+):

To run benchmark (requires Node.js 6+):
```
node benchmark
```

Results:

```
fast-json-stable-stringify x 17,189 ops/sec ±1.43% (83 runs sampled)
json-stable-stringify x 13,634 ops/sec ±1.39% (85 runs sampled)
fast-stable-stringify x 20,212 ops/sec ±1.20% (84 runs sampled)
faster-stable-stringify x 15,549 ops/sec ±1.12% (84 runs sampled)
The fastest is fast-stable-stringify
quick-stable-stringify x 25,499 ops/sec ±1.79% (82 runs sampled)
fast-json-stable-stringify x 18,566 ops/sec ±0.43% (89 runs sampled)
json-stable-stringify x 14,453 ops/sec ±0.62% (88 runs sampled)
fast-stable-stringify x 20,763 ops/sec ±0.58% (88 runs sampled)
faster-stable-stringify x 18,320 ops/sec ±0.90% (88 runs sampled)
The fastest is quick-stable-stringify
```


## Enterprise support

fast-json-stable-stringify package is a part of [Tidelift enterprise subscription](https://tidelift.com/subscription/pkg/npm-fast-json-stable-stringify?utm_source=npm-fast-json-stable-stringify&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.


## Security contact

To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.
Mail to: [sxwei321@gmail.com](mailto:sxwei321@gmail.com)

Please do NOT report security vulnerability via GitHub issues.

# license
## License

[MIT](https://github.com/epoberezkin/fast-json-stable-stringify/blob/master/LICENSE)
[MIT](https://github.com/sxwei123/quick-stable-stringify/blob/master/LICENSE)
Loading