pax_global_header00006660000000000000000000000064143324007730014515gustar00rootroot0000000000000052 comment=4fdc427435d546aba5c6d272d6edd27411388625 json-stable-stringify-1.0.2/000077500000000000000000000000001433240077300157525ustar00rootroot00000000000000json-stable-stringify-1.0.2/.eslintrc000066400000000000000000000007731433240077300176050ustar00rootroot00000000000000{ "root": true, "extends": "@ljharb", "rules": { "consistent-return": 1, "max-lines-per-function": 0, "max-params": 1, "max-statements-per-line": [2, { "max": 2 }], "no-continue": 1, "no-negated-condition": 1, "no-param-reassign": 1, "no-redeclare": 1, "no-restricted-syntax": 1, "object-curly-newline": 0, "max-statements": 1, "operator-linebreak": 0, "sort-keys": 1, }, "overrides": [ { "files": "example/**", "rules": { "no-console": 0, }, }, ], } json-stable-stringify-1.0.2/.github/000077500000000000000000000000001433240077300173125ustar00rootroot00000000000000json-stable-stringify-1.0.2/.github/FUNDING.yml000066400000000000000000000011201433240077300211210ustar00rootroot00000000000000# These are supported funding model platforms github: [ljharb] patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: npm/json-stable-stringify community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] json-stable-stringify-1.0.2/.github/workflows/000077500000000000000000000000001433240077300213475ustar00rootroot00000000000000json-stable-stringify-1.0.2/.github/workflows/node-aught.yml000066400000000000000000000005161433240077300241270ustar00rootroot00000000000000name: 'Tests: node.js < 10' on: [pull_request, push] jobs: tests: uses: ljharb/actions/.github/workflows/node.yml@main with: range: '< 10' type: minors command: npm run tests-only node: name: 'node < 10' needs: [tests] runs-on: ubuntu-latest steps: - run: 'echo tests completed' json-stable-stringify-1.0.2/.github/workflows/node-pretest.yml000066400000000000000000000002061433240077300245010ustar00rootroot00000000000000name: 'Tests: pretest/posttest' on: [pull_request, push] jobs: tests: uses: ljharb/actions/.github/workflows/pretest.yml@main json-stable-stringify-1.0.2/.github/workflows/node-tens.yml000066400000000000000000000005211433240077300237640ustar00rootroot00000000000000name: 'Tests: node.js >= 10' on: [pull_request, push] jobs: tests: uses: ljharb/actions/.github/workflows/node.yml@main with: range: '>= 10' type: minors command: npm run tests-only node: name: 'node >= 10' needs: [tests] runs-on: ubuntu-latest steps: - run: 'echo tests completed' json-stable-stringify-1.0.2/.github/workflows/rebase.yml000066400000000000000000000007001433240077300233300ustar00rootroot00000000000000name: Automatic Rebase on: [pull_request_target] permissions: contents: read jobs: _: permissions: contents: write # for ljharb/rebase to push code to rebase pull-requests: read # for ljharb/rebase to get info about PR name: "Automatic Rebase" runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: ljharb/rebase@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} json-stable-stringify-1.0.2/.github/workflows/require-allow-edits.yml000066400000000000000000000003031433240077300257640ustar00rootroot00000000000000name: Require “Allow Edits” on: [pull_request_target] jobs: _: name: "Require “Allow Edits”" runs-on: ubuntu-latest steps: - uses: ljharb/require-allow-edits@main json-stable-stringify-1.0.2/.gitignore000066400000000000000000000001711433240077300177410ustar00rootroot00000000000000# gitignore node_modules # Only apps should have lockfiles npm-shrinkwrap.json package-lock.json yarn.lock .npmignore json-stable-stringify-1.0.2/.npmrc000066400000000000000000000000671433240077300170750ustar00rootroot00000000000000package-lock=false allow-same-version=true message=v%s json-stable-stringify-1.0.2/.travis.yml000066400000000000000000000000601433240077300200570ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" json-stable-stringify-1.0.2/CHANGELOG.md000066400000000000000000000147261433240077300175750ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [v1.0.2](https://github.com/ljharb/json-stable-stringify/compare/v1.0.1...v1.0.2) - 2022-11-07 ### Commits - [eslint] fix indentation and whitespace [`c97e78c`](https://github.com/ljharb/json-stable-stringify/commit/c97e78cf3c0695701095dc0036681182585a6392) - [eslint] more cleanup [`c162117`](https://github.com/ljharb/json-stable-stringify/commit/c162117489c6dc63ece402b4a9b6e566f109fa65) - [meta] add `auto-changelog` [`83934ff`](https://github.com/ljharb/json-stable-stringify/commit/83934ffbbb3e72b9da09bf6436e1f86e7dce3b74) - [actions] add reusable workflows [`7b24830`](https://github.com/ljharb/json-stable-stringify/commit/7b248309f6ba87e2e52f99485c1f8b209b5788dc) - [readme] rename, add badges [`5433588`](https://github.com/ljharb/json-stable-stringify/commit/5433588781ebd98e41c81b5bfed1fb67520cf171) - [eslint] add eslint [`7be6c27`](https://github.com/ljharb/json-stable-stringify/commit/7be6c2755a7e2ead43017761b248a21511e457a0) - [meta] create FUNDING.yml; add `funding` in package.json [`6edbece`](https://github.com/ljharb/json-stable-stringify/commit/6edbece874fb656b9957b7bb362cf492f95fe259) - [meta] use `npmignore` to autogenerate an npmignore file [`b5d7d3a`](https://github.com/ljharb/json-stable-stringify/commit/b5d7d3abbe3d3a653e9ed511ab1b48940c5eb126) - [Dev Deps] update `tape` [`2200cf1`](https://github.com/ljharb/json-stable-stringify/commit/2200cf1e5822a4dd928541c3122a0922703c951f) - [actions] update rebase action [`e41ac00`](https://github.com/ljharb/json-stable-stringify/commit/e41ac000fb633d3df7c1e417ffd6213d885b64a1) - [meta] update URLs [`f17e490`](https://github.com/ljharb/json-stable-stringify/commit/f17e49038cf39a84a8a2677cc6445fad54902766) - Only apps should have lockfiles [`4f052f4`](https://github.com/ljharb/json-stable-stringify/commit/4f052f4ebf722024bc3827064b2d823f405ff2f6) - add breaking test (acyclic detected as cyclic) [`7f5f443`](https://github.com/ljharb/json-stable-stringify/commit/7f5f443e90402a520f1413833318b02bbb11ad67) - [meta] add `safe-publish-latest` [`ddb843f`](https://github.com/ljharb/json-stable-stringify/commit/ddb843f678bfe5145afaf03d811701c5ce4a17a6) - [Tests] add `aud` in `posttest` [`245c9bf`](https://github.com/ljharb/json-stable-stringify/commit/245c9bfa291d6d33813d44941d7639494fa8579a) - [Deps] update `jsonify` [`7b79a68`](https://github.com/ljharb/json-stable-stringify/commit/7b79a686f1ccda88b3ab20549840764c9b6f74eb) - fix conflict [`e43ca2a`](https://github.com/ljharb/json-stable-stringify/commit/e43ca2a1dcfc39bf1514684492767ef6040d1f3e) ## [v1.0.1](https://github.com/ljharb/json-stable-stringify/compare/v1.0.0...v1.0.1) - 2016-02-02 ### Commits - Correctly stringify non-cyclic shared references [`c26c700`](https://github.com/ljharb/json-stable-stringify/commit/c26c700f0b1d078512d2eba0eb16d6e5110a5538) ## [v1.0.0](https://github.com/ljharb/json-stable-stringify/compare/v0.1.3...v1.0.0) - 2014-05-27 ### Commits - Added options.replacer for custom object serialization [`ccf5e63`](https://github.com/ljharb/json-stable-stringify/commit/ccf5e636803a55d062e97aaf4e2c27d5c787aff0) - document replacer [`894f43b`](https://github.com/ljharb/json-stable-stringify/commit/894f43b633724bf0c6c2741143addfe20e149015) ## [v0.1.3](https://github.com/ljharb/json-stable-stringify/compare/v0.1.2...v0.1.3) - 2014-05-27 ### Commits - Enable toJSON function to return different types [`de0debf`](https://github.com/ljharb/json-stable-stringify/commit/de0debff3a36604010279af1868c6172674f9cc9) ## [v0.1.2](https://github.com/ljharb/json-stable-stringify/compare/v0.1.1...v0.1.2) - 2014-04-02 ### Commits - Should call 'toJSON' if it is defined on the object being stringified. [`c1de9d1`](https://github.com/ljharb/json-stable-stringify/commit/c1de9d193e8d6755d6ea2c2e5ead0544a8122040) - guard the reference [`a723f70`](https://github.com/ljharb/json-stable-stringify/commit/a723f705dd13fcbab1aa0ffa51849395712aaa13) - reindent [`7ff314f`](https://github.com/ljharb/json-stable-stringify/commit/7ff314fabf3b40074a4aff906b16e087897c6040) ## [v0.1.1](https://github.com/ljharb/json-stable-stringify/compare/v0.1.0...v0.1.1) - 2013-12-21 ### Commits - fixed merge conflicts [`7e139e8`](https://github.com/ljharb/json-stable-stringify/commit/7e139e8bbeb37b4dfd44991f4d6c98bba446b949) - fix formatting [`b5df6b9`](https://github.com/ljharb/json-stable-stringify/commit/b5df6b9ec0f5a5826eebb5d93424923041e43405) ## [v0.1.0](https://github.com/ljharb/json-stable-stringify/compare/v0.0.1...v0.1.0) - 2013-12-21 ### Commits - New “space” option to enable pretty printing (same as ES5) [`e6815c9`](https://github.com/ljharb/json-stable-stringify/commit/e6815c9dd8ca4052023d2bbd5c5b78b44f0efef0) - formatting [`962edf4`](https://github.com/ljharb/json-stable-stringify/commit/962edf4abb96189546b4f78f8719d747fd90fd43) ## [v0.0.1](https://github.com/ljharb/json-stable-stringify/compare/v0.0.0...v0.0.1) - 2013-07-17 ### Commits - don't choke on null [`3f4e9c7`](https://github.com/ljharb/json-stable-stringify/commit/3f4e9c78befc32f7d36af68e408e25cdc84be202) ## v0.0.0 - 2013-07-17 ### Commits - docs, more examples [`81f36c1`](https://github.com/ljharb/json-stable-stringify/commit/81f36c1aa645a75ebefa6d66d9cf41660439ebfe) - package.json etc [`98c5fd6`](https://github.com/ljharb/json-stable-stringify/commit/98c5fd6f9b12e1679b90777b9f6384203a05e983) - working implementation with 2 examples [`3e5363a`](https://github.com/ljharb/json-stable-stringify/commit/3e5363ac542fa3bf0bdef51034ca9201648f9839) - turn examples into tests, everything passes [`cccbd24`](https://github.com/ljharb/json-stable-stringify/commit/cccbd24c1a1a6318e3c004c86ae032db98a9abf8) - badges [`f8ff127`](https://github.com/ljharb/json-stable-stringify/commit/f8ff127df9f05d0b238bae8f91e483a755e0069e) - comparison test now passes [`8ab93e2`](https://github.com/ljharb/json-stable-stringify/commit/8ab93e2273ec530990e28233fcb96fde548ab16c) - failing custom comparison test [`3af627d`](https://github.com/ljharb/json-stable-stringify/commit/3af627d0d367451a98fc9cec6580760ade8f9bae) - fix object.keys shim [`7c16662`](https://github.com/ljharb/json-stable-stringify/commit/7c16662bc1cc6ecfa64159f9277e067cb1bec505) - fix for the other tests that don't use a cmp function [`f7b9a47`](https://github.com/ljharb/json-stable-stringify/commit/f7b9a476fd3ce9ec09b2c0588605e6c7c053e9ed) json-stable-stringify-1.0.2/LICENSE000066400000000000000000000020611433240077300167560ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. json-stable-stringify-1.0.2/README.md000066400000000000000000000077651433240077300172500ustar00rootroot00000000000000# json-stable-stringify [![Version Badge][npm-version-svg]][package-url] [![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] [![npm badge][npm-badge-png]][package-url] deterministic version of `JSON.stringify()` so you can get a consistent hash from stringified results You can also pass in a custom comparison function. [![browser support](https://ci.testling.com/ljharb/json-stable-stringify.png)](https://ci.testling.com/ljharb/json-stable-stringify) [![build status](https://secure.travis-ci.org/ljharb/json-stable-stringify.png)](http://travis-ci.org/ljharb/json-stable-stringify) # example ``` js var stringify = require('json-stable-stringify'); var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; console.log(stringify(obj)); ``` output: ``` {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8} ``` # methods ``` js var stringify = require('json-stable-stringify') ``` ## var str = stringify(obj, opts) Return a deterministic stringified string `str` from the object `obj`. ## options ### 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: ``` js opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue }) ``` For example, to sort on the object key names in reverse order you could write: ``` js var stringify = require('json-stable-stringify'); var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 }; var s = stringify(obj, function (a, b) { return b.key.localeCompare(a.key); }); console.log(s); ``` 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: ``` var stringify = require('json-stable-stringify'); 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; }); console.log(s); ``` which outputs: ``` {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10} ``` ### space If you specify `opts.space`, it will indent the output for pretty-printing. Valid values are strings (e.g. `{space: \t}`) or a number of spaces (`{space: 3}`). For example: ```js var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }; var s = stringify(obj, { space: ' ' }); console.log(s); ``` which outputs: ``` { "a": { "and": [ 1, 2, 3 ], "foo": "bar" }, "b": 1 } ``` ### replacer The replacer parameter is a function `opts.replacer(key, value)` that behaves the same as the replacer [from the core JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter). # install With [npm](https://npmjs.org) do: ``` npm install json-stable-stringify ``` # license MIT [package-url]: https://npmjs.org/package/json-stable-stringify [npm-version-svg]: https://versionbadg.es/ljharb/json-stable-stringify.svg [deps-svg]: https://david-dm.org/ljharb/json-stable-stringify.svg [deps-url]: https://david-dm.org/ljharb/json-stable-stringify [dev-deps-svg]: https://david-dm.org/ljharb/json-stable-stringify/dev-status.svg [dev-deps-url]: https://david-dm.org/ljharb/json-stable-stringify#info=devDependencies [npm-badge-png]: https://nodei.co/npm/json-stable-stringify.png?downloads=true&stars=true [license-image]: https://img.shields.io/npm/l/json-stable-stringify.svg [license-url]: LICENSE [downloads-image]: https://img.shields.io/npm/dm/json-stable-stringify.svg [downloads-url]: https://npm-stat.com/charts.html?package=json-stable-stringify [codecov-image]: https://codecov.io/gh/ljharb/json-stable-stringify/branch/main/graphs/badge.svg [codecov-url]: https://app.codecov.io/gh/ljharb/json-stable-stringify/ [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/json-stable-stringify [actions-url]: https://github.com/ljharb/json-stable-stringify/actions json-stable-stringify-1.0.2/example/000077500000000000000000000000001433240077300174055ustar00rootroot00000000000000json-stable-stringify-1.0.2/example/key_cmp.js000066400000000000000000000003111433240077300213650ustar00rootroot00000000000000'use strict'; var stringify = require('../'); var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; var s = stringify(obj, function (a, b) { return b.key.localeCompare(a.key); }); console.log(s); json-stable-stringify-1.0.2/example/nested.js000066400000000000000000000002061433240077300212230ustar00rootroot00000000000000'use strict'; var stringify = require('../'); var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; console.log(stringify(obj)); json-stable-stringify-1.0.2/example/str.js000066400000000000000000000001631433240077300205530ustar00rootroot00000000000000'use strict'; var stringify = require('../'); var obj = { c: 6, b: [4, 5], a: 3 }; console.log(stringify(obj)); json-stable-stringify-1.0.2/example/value_cmp.js000066400000000000000000000003221433240077300217130ustar00rootroot00000000000000'use strict'; var stringify = require('../'); 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; }); console.log(s); json-stable-stringify-1.0.2/index.js000066400000000000000000000045131433240077300174220ustar00rootroot00000000000000'use strict'; var json = typeof JSON !== 'undefined' ? JSON : require('jsonify'); var isArray = Array.isArray || function (x) { return {}.toString.call(x) === '[object Array]'; }; var objectKeys = Object.keys || function (obj) { var has = Object.prototype.hasOwnProperty || function () { return true; }; var keys = []; for (var key in obj) { if (has.call(obj, key)) { keys.push(key); } } return keys; }; module.exports = function (obj, opts) { if (!opts) { opts = {}; } if (typeof opts === 'function') { opts = { cmp: opts }; } var space = opts.space || ''; if (typeof space === 'number') { space = Array(space + 1).join(' '); } var cycles = typeof opts.cycles === 'boolean' ? opts.cycles : false; var replacer = opts.replacer || function (key, value) { return value; }; var cmp = opts.cmp && (function (f) { return function (node) { return function (a, b) { var aobj = { key: a, value: node[a] }; var bobj = { key: b, value: node[b] }; return f(aobj, bobj); }; }; }(opts.cmp)); var seen = []; return (function stringify(parent, key, node, level) { var indent = space ? '\n' + new Array(level + 1).join(space) : ''; var colonSeparator = space ? ': ' : ':'; if (node && node.toJSON && typeof node.toJSON === 'function') { node = node.toJSON(); } node = replacer.call(parent, key, node); if (node === undefined) { return; } if (typeof node !== 'object' || node === null) { return json.stringify(node); } if (isArray(node)) { var out = []; for (var i = 0; i < node.length; i++) { var item = stringify(node, i, node[i], level + 1) || json.stringify(null); out.push(indent + space + item); } return '[' + out.join(',') + indent + ']'; } if (seen.indexOf(node) !== -1) { if (cycles) { return json.stringify('__cycle__'); } throw new TypeError('Converting circular structure to JSON'); } else { seen.push(node); } var keys = objectKeys(node).sort(cmp && cmp(node)); var out = []; for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = stringify(node, key, node[key], level + 1); if (!value) { continue; } var keyValue = json.stringify(key) + colonSeparator + value; out.push(indent + space + keyValue); } seen.splice(seen.indexOf(node), 1); return '{' + out.join(',') + indent + '}'; }({ '': obj }, '', obj, 0)); }; json-stable-stringify-1.0.2/package.json000066400000000000000000000036241433240077300202450ustar00rootroot00000000000000{ "name": "json-stable-stringify", "version": "1.0.2", "description": "deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results", "main": "index.js", "dependencies": { "jsonify": "^0.0.1" }, "devDependencies": { "@ljharb/eslint-config": "^21.0.0", "aud": "^2.0.1", "auto-changelog": "^2.4.0", "eslint": "=8.8.0", "in-publish": "^2.0.1", "npmignore": "^0.3.0", "safe-publish-latest": "^2.0.0", "tape": "^5.6.1" }, "scripts": { "prepack": "npmignore --auto --commentLines=autogenerated", "prepublishOnly": "safe-publish-latest", "prepublish": "not-in-publish || npm run prepublishOnly", "lint": "eslint --ext=js,mjs .", "pretest": "npm run lint", "tests-only": "tape 'test/**/*.js'", "test": "npm run tests-only", "posttest": "aud --production", "version": "auto-changelog && git add CHANGELOG.md", "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" }, "testling": { "files": "test/*.js", "browsers": [ "ie/8..latest", "ff/5", "ff/latest", "chrome/15", "chrome/latest", "safari/latest", "opera/latest" ] }, "repository": { "type": "git", "url": "git://github.com/ljharb/json-stable-stringify.git" }, "homepage": "https://github.com/ljharb/json-stable-stringify", "keywords": [ "json", "stringify", "deterministic", "hash", "sort", "stable" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "funding": { "url": "https://github.com/sponsors/ljharb" }, "license": "MIT", "auto-changelog": { "output": "CHANGELOG.md", "template": "keepachangelog", "unreleased": false, "commitLimit": false, "backfillLimit": false, "hideCredit": true }, "publishConfig": { "ignore": [ ".github/workflows" ] } } json-stable-stringify-1.0.2/test/000077500000000000000000000000001433240077300167315ustar00rootroot00000000000000json-stable-stringify-1.0.2/test/cmp.js000066400000000000000000000005211433240077300200440ustar00rootroot00000000000000'use strict'; var test = require('tape'); var stringify = require('../'); test('custom comparison function', function (t) { t.plan(1); 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; }); t.equal(s, '{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}'); }); json-stable-stringify-1.0.2/test/nested.js000066400000000000000000000020661433240077300205550ustar00rootroot00000000000000'use strict'; var test = require('tape'); var stringify = require('../'); test('nested', function (t) { t.plan(1); var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; t.equal(stringify(obj), '{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}'); }); test('cyclic (default)', function (t) { t.plan(1); var one = { a: 1 }; var two = { a: 2, one: one }; one.two = two; try { stringify(one); } catch (ex) { t.equal(ex.toString(), 'TypeError: Converting circular structure to JSON'); } }); test('cyclic (specifically allowed)', function (t) { t.plan(1); var one = { a: 1 }; var two = { a: 2, one: one }; one.two = two; t.equal(stringify(one, { cycles: true }), '{"a":1,"two":{"a":2,"one":"__cycle__"}}'); }); test('repeated non-cyclic value', function (t) { t.plan(1); var one = { x: 1 }; var two = { a: one, b: one }; t.equal(stringify(two), '{"a":{"x":1},"b":{"x":1}}'); }); test('acyclic but with reused obj-property pointers', function (t) { t.plan(1); var x = { a: 1 }; var y = { b: x, c: x }; t.equal(stringify(y), '{"b":{"a":1},"c":{"a":1}}'); }); json-stable-stringify-1.0.2/test/replacer.js000066400000000000000000000034051433240077300210660ustar00rootroot00000000000000'use strict'; var test = require('tape'); var stringify = require('../'); test('replace root', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function () { return 'one'; }; t.equal(stringify(obj, { replacer: replacer }), '"one"'); }); test('replace numbers', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function (key, value) { if (value === 1) { return 'one'; } if (value === 2) { return 'two'; } return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":"two","c":false}'); }); test('replace with object', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function (key, value) { if (key === 'b') { return { d: 1 }; } if (value === 1) { return 'one'; } return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":{"d":"one"},"c":false}'); }); test('replace with undefined', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function (key, value) { if (value === false) { return; } return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":1,"b":2}'); }); test('replace with array', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function (key, value) { if (key === 'b') { return ['one', 'two']; } return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":1,"b":["one","two"],"c":false}'); }); test('replace array item', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: [1, 2] }; var replacer = function (key, value) { if (value === 1) { return 'one'; } if (value === 2) { return 'two'; } return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":"two","c":["one","two"]}'); }); json-stable-stringify-1.0.2/test/space.js000066400000000000000000000024471433240077300203710ustar00rootroot00000000000000'use strict'; var test = require('tape'); var stringify = require('../'); test('space parameter', function (t) { t.plan(1); var obj = { one: 1, two: 2 }; t.equal( stringify(obj, { space: ' ' }), '' + '{\n' + ' "one": 1,\n' + ' "two": 2\n' + '}' ); }); test('space parameter (with tabs)', function (t) { t.plan(1); var obj = { one: 1, two: 2 }; t.equal( stringify(obj, { space: '\t' }), '' + '{\n' + '\t"one": 1,\n' + '\t"two": 2\n' + '}' ); }); test('space parameter (with a number)', function (t) { t.plan(1); var obj = { one: 1, two: 2 }; t.equal( stringify(obj, { space: 3 }), '' + '{\n' + ' "one": 1,\n' + ' "two": 2\n' + '}' ); }); test('space parameter (nested objects)', function (t) { t.plan(1); var obj = { one: 1, two: { b: 4, a: [2, 3] } }; t.equal( stringify(obj, { space: ' ' }), '' + '{\n' + ' "one": 1,\n' + ' "two": {\n' + ' "a": [\n' + ' 2,\n' + ' 3\n' + ' ],\n' + ' "b": 4\n' + ' }\n' + '}' ); }); test('space parameter (same as native)', function (t) { t.plan(1); // for this test, properties need to be in alphabetical order var obj = { one: 1, two: { a: [2, 3], b: 4 } }; t.equal( stringify(obj, { space: ' ' }), JSON.stringify(obj, null, ' ') ); }); json-stable-stringify-1.0.2/test/str.js000066400000000000000000000013751433240077300201050ustar00rootroot00000000000000'use strict'; var test = require('tape'); var stringify = require('../'); test('simple object', function (t) { t.plan(1); var obj = { c: 6, b: [4, 5], a: 3, z: null }; t.equal(stringify(obj), '{"a":3,"b":[4,5],"c":6,"z":null}'); }); test('object with undefined', function (t) { t.plan(1); var obj = { a: 3, z: undefined }; t.equal(stringify(obj), '{"a":3}'); }); test('array with undefined', function (t) { t.plan(1); var obj = [4, undefined, 6]; t.equal(stringify(obj), '[4,null,6]'); }); test('object with empty string', function (t) { t.plan(1); var obj = { a: 3, z: '' }; t.equal(stringify(obj), '{"a":3,"z":""}'); }); test('array with empty string', function (t) { t.plan(1); var obj = [4, '', 6]; t.equal(stringify(obj), '[4,"",6]'); }); json-stable-stringify-1.0.2/test/to-json.js000066400000000000000000000011061433240077300206560ustar00rootroot00000000000000'use strict'; var test = require('tape'); var stringify = require('../'); test('toJSON function', function (t) { t.plan(1); var obj = { one: 1, two: 2, toJSON: function () { return { one: 1 }; } }; t.equal(stringify(obj), '{"one":1}'); }); test('toJSON returns string', function (t) { t.plan(1); var obj = { one: 1, two: 2, toJSON: function () { return 'one'; } }; t.equal(stringify(obj), '"one"'); }); test('toJSON returns array', function (t) { t.plan(1); var obj = { one: 1, two: 2, toJSON: function () { return ['one']; } }; t.equal(stringify(obj), '["one"]'); });