mirror of
https://github.com/LukeHagar/arbiter.git
synced 2025-12-07 12:27:49 +00:00
Saving all progress
This commit is contained in:
6
node_modules/json-stringify-deterministic/lib/defaults.js
generated
vendored
Normal file
6
node_modules/json-stringify-deterministic/lib/defaults.js
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
space: '',
|
||||
cycles: false,
|
||||
replacer: (k, v) => v,
|
||||
stringify: JSON.stringify
|
||||
}
|
||||
46
node_modules/json-stringify-deterministic/lib/index.d.ts
generated
vendored
Normal file
46
node_modules/json-stringify-deterministic/lib/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
interface Options {
|
||||
/**
|
||||
* Determinate how to stringify primitives values.
|
||||
* @default JSON.stringify
|
||||
*/
|
||||
stringify?: typeof JSON["stringify"];
|
||||
|
||||
/**
|
||||
* Determinate how to resolve cycles.
|
||||
* Under true, when a cycle is detected, [Circular] will be inserted in the node.
|
||||
* @default false
|
||||
*/
|
||||
cycles?: boolean;
|
||||
|
||||
/**
|
||||
* Custom comparison function for object keys.
|
||||
* @param a first key-value pair.
|
||||
* @param b second key-value pair.
|
||||
* @returns a number whose sign indicates the relative order of the two elements.
|
||||
*/
|
||||
compare?: (a: KeyValue, b: KeyValue) => number;
|
||||
|
||||
/**
|
||||
* Indent the output for pretty-printing.
|
||||
*/
|
||||
space?: string;
|
||||
|
||||
/**
|
||||
* Replacer function that behaves the same as the replacer from the core JSON object.
|
||||
*/
|
||||
replacer?: (key: string, value: unknown) => unknown;
|
||||
}
|
||||
|
||||
interface KeyValue {
|
||||
key: string;
|
||||
value: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic version of JSON.stringify(), so you can get a consistent hash from stringified results.
|
||||
* @param obj The input object to be serialized.
|
||||
* @param opts options.
|
||||
*/
|
||||
declare function stringify(obj: unknown, opts?: Options): string;
|
||||
|
||||
export = stringify;
|
||||
87
node_modules/json-stringify-deterministic/lib/index.js
generated
vendored
Normal file
87
node_modules/json-stringify-deterministic/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict'
|
||||
|
||||
const DEFAULTS = require('./defaults')
|
||||
const isFunction = require('./util').isFunction
|
||||
const isBoolean = require('./util').isBoolean
|
||||
const isObject = require('./util').isObject
|
||||
const isArray = require('./util').isArray
|
||||
const isRegex = require('./util').isRegex
|
||||
const assign = require('./util').assign
|
||||
const keys = require('./util').keys
|
||||
|
||||
function serialize (obj) {
|
||||
if (obj === null || obj === undefined) return obj
|
||||
if (isRegex(obj)) return obj.toString()
|
||||
return obj.toJSON ? obj.toJSON() : obj
|
||||
}
|
||||
|
||||
function stringifyDeterministic (obj, opts) {
|
||||
opts = opts || assign({}, DEFAULTS)
|
||||
|
||||
if (isFunction(opts)) opts = { compare: opts }
|
||||
|
||||
const space = opts.space || DEFAULTS.space
|
||||
const cycles = isBoolean(opts.cycles) ? opts.cycles : DEFAULTS.cycles
|
||||
const replacer = opts.replacer || DEFAULTS.replacer
|
||||
const stringify = opts.stringify || DEFAULTS.stringify
|
||||
|
||||
const compare = opts.compare && (function (f) {
|
||||
return function (node) {
|
||||
return function (a, b) {
|
||||
const aobj = { key: a, value: node[a] }
|
||||
const bobj = { key: b, value: node[b] }
|
||||
return f(aobj, bobj)
|
||||
}
|
||||
}
|
||||
})(opts.compare)
|
||||
|
||||
// Detect circular structure in obj and raise error efficiently.
|
||||
if (!cycles) stringify(obj)
|
||||
|
||||
const seen = []
|
||||
|
||||
return (function _deterministic (parent, key, node, level) {
|
||||
const indent = space ? ('\n' + new Array(level + 1).join(space)) : ''
|
||||
const colonSeparator = space ? ': ' : ':'
|
||||
|
||||
node = serialize(node)
|
||||
node = replacer.call(parent, key, node)
|
||||
|
||||
if (node === undefined) return
|
||||
|
||||
if (!isObject(node) || node === null) return stringify(node)
|
||||
|
||||
if (isArray(node)) {
|
||||
const out = []
|
||||
for (let i = 0; i < node.length; i++) {
|
||||
const item = _deterministic(node, i, node[i], level + 1) || stringify(null)
|
||||
out.push(indent + space + item)
|
||||
}
|
||||
return '[' + out.join(',') + indent + ']'
|
||||
} else {
|
||||
if (cycles) {
|
||||
if (seen.indexOf(node) !== -1) {
|
||||
return stringify('[Circular]')
|
||||
} else {
|
||||
seen.push(node)
|
||||
}
|
||||
}
|
||||
|
||||
const nodeKeys = keys(node).sort(compare && compare(node))
|
||||
const out = []
|
||||
for (let i = 0; i < nodeKeys.length; i++) {
|
||||
const key = nodeKeys[i]
|
||||
const value = _deterministic(node, key, node[key], level + 1)
|
||||
|
||||
if (!value) continue
|
||||
|
||||
const keyValue = stringify(key) + colonSeparator + value
|
||||
out.push(indent + space + keyValue)
|
||||
}
|
||||
seen.splice(seen.indexOf(node), 1)
|
||||
return '{' + out.join(',') + indent + '}'
|
||||
}
|
||||
})({ '': obj }, '', obj, 0)
|
||||
}
|
||||
|
||||
module.exports = stringifyDeterministic
|
||||
11
node_modules/json-stringify-deterministic/lib/util.js
generated
vendored
Normal file
11
node_modules/json-stringify-deterministic/lib/util.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
isArray: Array.isArray,
|
||||
assign: Object.assign,
|
||||
isObject: v => typeof v === 'object',
|
||||
isFunction: v => typeof v === 'function',
|
||||
isBoolean: v => typeof v === 'boolean',
|
||||
isRegex: v => v instanceof RegExp,
|
||||
keys: Object.keys
|
||||
}
|
||||
Reference in New Issue
Block a user