From cdf191336aa616a206b977ba3beeb1233cf41c32 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 28 Jun 2020 12:38:34 -0400 Subject: Clone function (#624) * Add clone function * Replace utilIsolate with clone * Replace JsonSchema.isolate with clone function * Include core.js for tests which use json-schema.js * Update visisted set --- ext/mixed/js/core.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'ext/mixed') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 21b7bf5e..9b3ea3e2 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -157,6 +157,73 @@ function getSetDifference(set1, set2) { ); } +const clone = (() => { + // eslint-disable-next-line no-shadow + function clone(value) { + if (value === null) { return null; } + switch (typeof value) { + case 'boolean': + case 'number': + case 'string': + case 'bigint': + case 'symbol': + case 'undefined': + return value; + default: + return cloneInternal(value, new Set()); + } + } + + function cloneInternal(value, visited) { + if (value === null) { return null; } + switch (typeof value) { + case 'boolean': + case 'number': + case 'string': + case 'bigint': + case 'symbol': + case 'undefined': + return value; + case 'function': + return cloneObject(value, visited); + case 'object': + return Array.isArray(value) ? cloneArray(value, visited) : cloneObject(value, visited); + } + } + + function cloneArray(value, visited) { + if (visited.has(value)) { throw new Error('Circular'); } + try { + visited.add(value); + const result = []; + for (const item of value) { + result.push(cloneInternal(item, visited)); + } + return result; + } finally { + visited.delete(value); + } + } + + function cloneObject(value, visited) { + if (visited.has(value)) { throw new Error('Circular'); } + try { + visited.add(value); + const result = {}; + for (const key in value) { + if (Object.prototype.hasOwnProperty.call(value, key)) { + result[key] = cloneInternal(value[key], visited); + } + } + return result; + } finally { + visited.delete(value); + } + } + + return clone; +})(); + /* * Async utilities -- cgit v1.2.3