diff options
Diffstat (limited to 'deps/npm/node_modules/json-stringify-nice')
8 files changed, 403 insertions, 0 deletions
diff --git a/deps/npm/node_modules/json-stringify-nice/.github/FUNDING.yml b/deps/npm/node_modules/json-stringify-nice/.github/FUNDING.yml new file mode 100644 index 0000000000..20d8c03a4d --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms + +github: [isaacs] diff --git a/deps/npm/node_modules/json-stringify-nice/.npmignore b/deps/npm/node_modules/json-stringify-nice/.npmignore new file mode 100644 index 0000000000..e69acb4cf4 --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/.npmignore @@ -0,0 +1,23 @@ +# ignore most things, include some others +/* +/.* + +!.github +!bin/ +!lib/ +!docs/ +!package.json +!package-lock.json +!README.md +!CONTRIBUTING.md +!LICENSE +!CHANGELOG.md +!example/ +!scripts/ +!tap-snapshots/ +!test/ +!.travis.yml +!.gitignore +!.gitattributes +!coverage-map.js +!index.js diff --git a/deps/npm/node_modules/json-stringify-nice/LICENSE b/deps/npm/node_modules/json-stringify-nice/LICENSE new file mode 100644 index 0000000000..19129e315f --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/LICENSE @@ -0,0 +1,15 @@ +The ISC License + +Copyright (c) Isaac Z. Schlueter and Contributors + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/deps/npm/node_modules/json-stringify-nice/README.md b/deps/npm/node_modules/json-stringify-nice/README.md new file mode 100644 index 0000000000..66cb1a7c53 --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/README.md @@ -0,0 +1,105 @@ +# json-stringify-nice + +Stringify an object sorting scalars before objects, and defaulting to +2-space indent. + +Sometimes you want to stringify an object in a consistent way, and for +human legibility reasons, you may want to put any non-object properties +ahead of any object properties, so that it's easier to track the nesting +level as you read through the object, but you don't want to have to be +meticulous about maintaining object property order as you're building up +the object, since it doesn't matter in code, it only matters in the output +file. Also, it'd be nice to have it default to reasonable spacing without +having to remember to add `, null, 2)` to all your `JSON.stringify()` +calls. + +If that is what you want, then this module is for you, because it does +all of that. + +## USAGE + +```js +const stringify = require('json-stringify-nice') +const obj = { + z: 1, + y: 'z', + obj: { a: {}, b: 'x' }, + a: { b: 1, a: { nested: true} }, + yy: 'a', +} + +console.log(stringify(obj)) +/* output: +{ + "y": "z", <-- alphabetical sorting like whoa! + "yy": "a", + "z": 1, + "a": { <-- a sorted before obj, because alphabetical, and both objects + "b": 1, + "a": { <-- note that a comes after b, because it's an object + "nested": true + } + }, + "obj": { + "b": "x", + "a": {} + } +} +*/ + +// specify an array of keys if you have some that you prefer +// to be sorted in a specific order. preferred keys come before +// any other keys, and in the order specified, but objects are +// still sorted AFTER scalars, so the preferences only apply +// when both values are objects or both are non-objects. +console.log(stringify(obj, ['z', 'yy', 'obj'])) +/* output +{ + "z": 1, <-- z comes before other scalars + "yy": "a", <-- yy comes after z, but before other scalars + "y": "z", <-- then all the other scalar values + "obj": { <-- obj comes before other objects, but after scalars + "b": "x", + "a": {} + }, + "a": { + "b": 1, + "a": { + "nested": true + } + } +} +*/ + +// can also specify a replacer or indent value like with JSON.stringify +// this turns all values with an 'a' key into a doggo meme from 2011 +const replacer = (key, val) => + key === 'a' ? { hello: 'π yes', 'this is': 'π', ...val } : val + +console.log(stringify(obj, replacer, 'ππΆ')) + +/* output: +{ +ππΆ"y": "z", +ππΆ"yy": "a", +ππΆ"z": 1, +ππΆ"a": { +ππΆππΆ"b": 1, +ππΆππΆ"hello": "π yes", +ππΆππΆ"this is": "π", +ππΆππΆ"a": { +ππΆππΆππΆ"hello": "π yes", +ππΆππΆππΆ"nested": true, +ππΆππΆππΆ"this is": "π" +ππΆππΆ} +ππΆ}, +ππΆ"obj": { +ππΆππΆ"b": "x", +ππΆππΆ"a": { +ππΆππΆππΆ"hello": "π yes", +ππΆππΆππΆ"this is": "π" +ππΆππΆ} +ππΆ} +} +*/ +``` diff --git a/deps/npm/node_modules/json-stringify-nice/index.js b/deps/npm/node_modules/json-stringify-nice/index.js new file mode 100644 index 0000000000..1ca7e14fa0 --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/index.js @@ -0,0 +1,38 @@ +const isObj = val => val && !Array.isArray(val) && typeof val === 'object' + +const compare = (ak, bk, prefKeys) => + prefKeys.includes(ak) && !prefKeys.includes(bk) ? -1 + : prefKeys.includes(bk) && !prefKeys.includes(ak) ? 1 + : prefKeys.includes(ak) && prefKeys.includes(bk) + ? prefKeys.indexOf(ak) - prefKeys.indexOf(bk) + : ak.localeCompare(bk) + +const sort = (replacer, seen) => (key, val) => { + const prefKeys = Array.isArray(replacer) ? replacer : [] + + if (typeof replacer === 'function') + val = replacer(key, val) + + if (!isObj(val)) + return val + + if (seen.has(val)) + return seen.get(val) + + const ret = Object.entries(val).sort( + ([ak, av], [bk, bv]) => + isObj(av) === isObj(bv) ? compare(ak, bk, prefKeys) + : isObj(av) ? 1 + : -1 + ).reduce((set, [k, v]) => { + set[k] = v + return set + }, {}) + + seen.set(val, ret) + return ret +} + +module.exports = (obj, replacer, space = 2) => + JSON.stringify(obj, sort(replacer, new Map()), space) + + (space ? '\n' : '') diff --git a/deps/npm/node_modules/json-stringify-nice/package.json b/deps/npm/node_modules/json-stringify-nice/package.json new file mode 100644 index 0000000000..e060b2ed70 --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/package.json @@ -0,0 +1,24 @@ +{ + "name": "json-stringify-nice", + "version": "1.1.1", + "description": "Stringify an object sorting scalars before objects, and defaulting to 2-space indent", + "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)", + "license": "ISC", + "scripts": { + "test": "tap", + "snap": "tap", + "preversion": "npm test", + "postversion": "npm publish", + "postpublish": "git push origin --follow-tags" + }, + "tap": { + "check-coverage": true + }, + "devDependencies": { + "tap": "^14.9.2" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + }, + "repository": "https://github.com/isaacs/json-stringify-nice" +} diff --git a/deps/npm/node_modules/json-stringify-nice/tap-snapshots/test-basic.js-TAP.test.js b/deps/npm/node_modules/json-stringify-nice/tap-snapshots/test-basic.js-TAP.test.js new file mode 100644 index 0000000000..53d5fda6c3 --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/tap-snapshots/test-basic.js-TAP.test.js @@ -0,0 +1,127 @@ +/* IMPORTANT + * This snapshot file is auto-generated, but designed for humans. + * It should be checked into source control and tracked carefully. + * Re-generate by setting TAP_SNAPSHOT=1 and running tests. + * Make sure to inspect the output below. Do not ignore changes! + */ +'use strict' +exports[`test/basic.js TAP basic sorting operation with default 2-space indent > mix of objects and out of order keys 1`] = ` +{ + "y": "z", + "yy": "a", + "z": 1, + "a": { + "a": 2, + "b": 1 + }, + "obj": { + "b": "x", + "a": {} + } +} + +` + +exports[`test/basic.js TAP replacer function is used > replace a val with phone doggo 1`] = ` +{ + "y": "z", + "yy": "a", + "z": 1, + "a": { + "b": 1, + "hello": "π yes", + "this is": "π", + "a": { + "hello": "π yes", + "nested": true, + "this is": "π" + } + }, + "obj": { + "b": "x", + "a": { + "hello": "π yes", + "this is": "π" + } + } +} + +` + +exports[`test/basic.js TAP sort keys explicitly with a preference list > replace a val with preferences 1`] = ` +{ + "z": 1, + "yy": "a", + "y": "z", + "obj": { + "b": "x", + "a": {} + }, + "a": { + "b": 1, + "a": { + "nested": true + } + } +} + +` + +exports[`test/basic.js TAP spaces can be set > boolean false 1`] = ` +{"y":"z","yy":"a","z":1,"a":{"a":2,"b":1},"obj":{"b":"x","a":{}}} +` + +exports[`test/basic.js TAP spaces can be set > empty string 1`] = ` +{"y":"z","yy":"a","z":1,"a":{"a":2,"b":1},"obj":{"b":"x","a":{}}} +` + +exports[`test/basic.js TAP spaces can be set > space face 1`] = ` +{ + ^_^ "y": "z", + ^_^ "yy": "a", + ^_^ "z": 1, + ^_^ "a": { + ^_^ ^_^ "a": 2, + ^_^ ^_^ "b": 1 + ^_^ }, + ^_^ "obj": { + ^_^ ^_^ "b": "x", + ^_^ ^_^ "a": {} + ^_^ } +} + +` + +exports[`test/basic.js TAP spaces can be set > tab 1`] = ` +{ + "y": "z", + "yy": "a", + "z": 1, + "a": { + "a": 2, + "b": 1 + }, + "obj": { + "b": "x", + "a": {} + } +} + +` + +exports[`test/basic.js TAP spaces can be set > the number 3 1`] = ` +{ + "y": "z", + "yy": "a", + "z": 1, + "a": { + "a": 2, + "b": 1 + }, + "obj": { + "b": "x", + "a": {} + } +} + +` diff --git a/deps/npm/node_modules/json-stringify-nice/test/basic.js b/deps/npm/node_modules/json-stringify-nice/test/basic.js new file mode 100644 index 0000000000..23c3ceb384 --- /dev/null +++ b/deps/npm/node_modules/json-stringify-nice/test/basic.js @@ -0,0 +1,68 @@ +const t = require('tap') +const stringify = require('../') + +t.test('basic sorting operation with default 2-space indent', t => { + t.plan(1) + t.matchSnapshot(stringify({ + z: 1, + y: 'z', + obj: { a: {}, b: 'x' }, + a: { b: 1, a: 2}, + yy: 'a', + }), 'mix of objects and out of order keys') +}) + +t.test('throws same error on cycles as JSON.stringify', t => { + t.plan(1) + const cycle = { a: { b: { c: {} } } } + cycle.a.b.c = cycle.a + try { + JSON.stringify(cycle) + } catch (builtinEr) { + t.throws(() => stringify(cycle), builtinEr, 'same error as builtin') + } +}) + +t.test('spaces can be set', t => { + t.plan(5) + const obj = { + z: 1, + y: 'z', + obj: { a: {}, b: 'x' }, + a: { b: 1, a: 2}, + yy: 'a', + } + t.matchSnapshot(stringify(obj, 0, '\t'), 'tab') + t.matchSnapshot(stringify(obj, null, ' ^_^ '), 'space face') + t.matchSnapshot(stringify(obj, false, 3), 'the number 3') + t.matchSnapshot(stringify(obj, false, ''), 'empty string') + t.matchSnapshot(stringify(obj, false, false), 'boolean false') +}) + +t.test('replacer function is used', t => { + t.plan(1) + const obj = { + z: 1, + y: 'z', + obj: { a: {}, b: 'x' }, + a: { b: 1, a: { nested: true} }, + yy: 'a', + } + const replacer = (key, val) => + key === 'a' ? { hello: 'π yes', 'this is': 'π', ...val } + : val + t.matchSnapshot(stringify(obj, replacer), 'replace a val with phone doggo') +}) + +t.test('sort keys explicitly with a preference list', t => { + t.plan(1) + const obj = { + z: 1, + y: 'z', + obj: { a: {}, b: 'x' }, + a: { b: 1, a: { nested: true} }, + yy: 'a', + } + const preference = ['obj', 'z', 'yy'] + t.matchSnapshot(stringify(obj, preference), 'replace a val with preferences') +}) |