@ungap/structured-clone
Advanced tools
Comparing version 0.0.1 to 0.0.2
'use strict'; | ||
const _item = ([type, value], info, as) => { | ||
const _deserialize = (index, $, _) => { | ||
if ($.has(index)) | ||
return $.get(index); | ||
const [type, value] = _[index]; | ||
const as = deserialized => { | ||
$.set(index, deserialized); | ||
return deserialized; | ||
}; | ||
switch (type) { | ||
case 'primitive': | ||
return as(value); | ||
case 'Date': | ||
return as(new Date(value)); | ||
case 'RegExp': { | ||
const {source, flags} = value; | ||
return as(new RegExp(source, flags)); | ||
case 'Array': { | ||
const arr = as([]); | ||
for (const index of value) | ||
arr.push(_deserialize(index, $, _)); | ||
return arr; | ||
} | ||
@@ -15,10 +25,10 @@ case 'Object': { | ||
for (const [key, index] of value) | ||
object[key] = _deserialize(index, info); | ||
object[key] = _deserialize(index, $, _); | ||
return object; | ||
} | ||
case 'Array': { | ||
const arr = as([]); | ||
for (const index of value) | ||
arr.push(_deserialize(index, info)); | ||
return arr; | ||
case 'Date': | ||
return as(new Date(value)); | ||
case 'RegExp': { | ||
const {source, flags} = value; | ||
return as(new RegExp(source, flags)); | ||
} | ||
@@ -28,3 +38,3 @@ case 'Map': { | ||
for (const [key, index] of value) | ||
map.set(key, _deserialize(index, info)); | ||
map.set(key, _deserialize(index, $, _)); | ||
return map; | ||
@@ -35,7 +45,5 @@ } | ||
for (const index of value) | ||
set.add(_deserialize(index, info)); | ||
set.add(_deserialize(index, $, _)); | ||
return set; | ||
} | ||
case 'BigInt': | ||
return as(BigInt(value)); | ||
case 'Error': { | ||
@@ -51,2 +59,4 @@ const {name, message} = value; | ||
return as(new String(value)); | ||
case 'BigInt': | ||
return as(BigInt(value)); | ||
} | ||
@@ -56,13 +66,6 @@ return as(new globalThis[type](value)); | ||
const _deserialize = (index, info) => { | ||
const {$, _} = info; | ||
if ($.has(index)) | ||
return $.get(index); | ||
/** | ||
* @typedef {Array<string,any>} Record a type representation | ||
*/ | ||
return _item(_[index], info, deserialized => { | ||
$.set(index, deserialized); | ||
return deserialized; | ||
}); | ||
}; | ||
/** | ||
@@ -73,3 +76,3 @@ * Returns a deserialized value from a serialized array of Records. | ||
*/ | ||
const deserialize = (_) => _deserialize(0, {$: new Map, _}); | ||
const deserialize = serialized => _deserialize(0, new Map, serialized); | ||
exports.deserialize = deserialize; |
'use strict'; | ||
const {deserialize} = require('./deserialize.js'); | ||
const {serialize} = require('./serialize.js'); | ||
const dflt = {transfer: []}; | ||
Object.defineProperty(exports, '__esModule', {value: true}).default = (any, options = {transfer: []}) => | ||
deserialize(serialize(any, options)); | ||
/** | ||
* @typedef {Array<string,any>} Record a type representation | ||
*/ | ||
/** | ||
* Returns an array of serialized Records. | ||
* @param {any} any a serializable value. | ||
* @param {{transfer: any[]}?} options an object with a transfoer property. | ||
* This is currently not supported, all values are always cloned. | ||
* @returns {Record[]} | ||
*/ | ||
Object.defineProperty(exports, '__esModule', {value: true}).default = (any, options = dflt) => deserialize(serialize(any, options)); | ||
exports.deserialize = deserialize; | ||
exports.serialize = serialize; |
'use strict'; | ||
const {toString} = {}; | ||
const {keys} = Object; | ||
const defaultOptions = {transfer: []}; | ||
const _serialize = (value, info) => { | ||
const {$, _} = info; | ||
const _serialize = (value, $, _) => { | ||
if ($.has(value)) | ||
return $.get(value); | ||
const i = _.push(value) - 1; | ||
$.set(value, i); | ||
const index = _.push(value) - 1; | ||
$.set(value, index); | ||
const as = serialized => { | ||
_[i] = serialized; | ||
return i; | ||
_[index] = serialized; | ||
return index; | ||
}; | ||
switch (typeof value) { | ||
case 'bigint': | ||
return as(['BigInt', value.toString()]); | ||
case 'object': | ||
@@ -26,12 +22,10 @@ if (value !== null) { | ||
switch (type) { | ||
case 'Array': | ||
return as([type, value.map(entry => _serialize(entry, $, _))]); | ||
case 'Object': { | ||
const entries = []; | ||
for (const key of keys(value)) | ||
entries.push([key, _serialize(value[key], info)]); | ||
entries.push([key, _serialize(value[key], $, _)]); | ||
return as([type, entries]); | ||
} | ||
case 'Boolean': | ||
case 'Number': | ||
case 'String': | ||
return as([type, value.valueOf()]); | ||
case 'Date': | ||
@@ -46,17 +40,15 @@ return as([type, value.toISOString()]); | ||
for (const [key, entry] of value) | ||
entries.push([_serialize(key, info), _serialize(entry, info)]); | ||
entries.push([_serialize(key, $, _), _serialize(entry, $, _)]); | ||
return as([type, entries]); | ||
} | ||
case 'Array': { | ||
const arr = []; | ||
for (const entry of value) | ||
arr.push(_serialize(entry, info)); | ||
return as([type, arr]); | ||
} | ||
case 'Set': { | ||
const values = []; | ||
for (const entry of value) | ||
values.push(_serialize(entry, info)); | ||
values.push(_serialize(entry, $, _)); | ||
return as([type, values]); | ||
} | ||
case 'Boolean': | ||
case 'Number': | ||
case 'String': | ||
return as([type, value.valueOf()]); | ||
} | ||
@@ -79,2 +71,4 @@ | ||
return as(['primitive', value]); | ||
case 'bigint': | ||
return as(['BigInt', value.toString()]); | ||
default: | ||
@@ -86,12 +80,14 @@ throw new TypeError; | ||
/** | ||
* @typedef {Array<string,any>} Record a type representation | ||
*/ | ||
/** | ||
* Returns an array of serialized Records. | ||
* @param {any} any a serializable value. | ||
* @param {object?} options an object with a transfoer property. | ||
* This is currently not supported, all values are always cloned. | ||
* @returns | ||
* @param {any} serializable a serializable value. | ||
* @returns {Record[]} | ||
*/ | ||
const serialize = (any, {transfer} = defaultOptions) => { | ||
const serialize = serializable => { | ||
const _ = []; | ||
return _serialize(any, {transfer, $: new Map, _}), _; | ||
return _serialize(serializable, new Map, _), _; | ||
}; | ||
exports.serialize = serialize; |
@@ -1,10 +0,20 @@ | ||
const _item = ([type, value], info, as) => { | ||
const _deserialize = (index, $, _) => { | ||
if ($.has(index)) | ||
return $.get(index); | ||
const [type, value] = _[index]; | ||
const as = deserialized => { | ||
$.set(index, deserialized); | ||
return deserialized; | ||
}; | ||
switch (type) { | ||
case 'primitive': | ||
return as(value); | ||
case 'Date': | ||
return as(new Date(value)); | ||
case 'RegExp': { | ||
const {source, flags} = value; | ||
return as(new RegExp(source, flags)); | ||
case 'Array': { | ||
const arr = as([]); | ||
for (const index of value) | ||
arr.push(_deserialize(index, $, _)); | ||
return arr; | ||
} | ||
@@ -14,10 +24,10 @@ case 'Object': { | ||
for (const [key, index] of value) | ||
object[key] = _deserialize(index, info); | ||
object[key] = _deserialize(index, $, _); | ||
return object; | ||
} | ||
case 'Array': { | ||
const arr = as([]); | ||
for (const index of value) | ||
arr.push(_deserialize(index, info)); | ||
return arr; | ||
case 'Date': | ||
return as(new Date(value)); | ||
case 'RegExp': { | ||
const {source, flags} = value; | ||
return as(new RegExp(source, flags)); | ||
} | ||
@@ -27,3 +37,3 @@ case 'Map': { | ||
for (const [key, index] of value) | ||
map.set(key, _deserialize(index, info)); | ||
map.set(key, _deserialize(index, $, _)); | ||
return map; | ||
@@ -34,7 +44,5 @@ } | ||
for (const index of value) | ||
set.add(_deserialize(index, info)); | ||
set.add(_deserialize(index, $, _)); | ||
return set; | ||
} | ||
case 'BigInt': | ||
return as(BigInt(value)); | ||
case 'Error': { | ||
@@ -50,2 +58,4 @@ const {name, message} = value; | ||
return as(new String(value)); | ||
case 'BigInt': | ||
return as(BigInt(value)); | ||
} | ||
@@ -55,13 +65,6 @@ return as(new globalThis[type](value)); | ||
const _deserialize = (index, info) => { | ||
const {$, _} = info; | ||
if ($.has(index)) | ||
return $.get(index); | ||
/** | ||
* @typedef {Array<string,any>} Record a type representation | ||
*/ | ||
return _item(_[index], info, deserialized => { | ||
$.set(index, deserialized); | ||
return deserialized; | ||
}); | ||
}; | ||
/** | ||
@@ -72,2 +75,2 @@ * Returns a deserialized value from a serialized array of Records. | ||
*/ | ||
export const deserialize = (_) => _deserialize(0, {$: new Map, _}); | ||
export const deserialize = serialized => _deserialize(0, new Map, serialized); |
import {deserialize} from './deserialize.js'; | ||
import {serialize} from './serialize.js'; | ||
const dflt = {transfer: []}; | ||
export default (any, options = {transfer: []}) => | ||
deserialize(serialize(any, options)); | ||
/** | ||
* @typedef {Array<string,any>} Record a type representation | ||
*/ | ||
/** | ||
* Returns an array of serialized Records. | ||
* @param {any} any a serializable value. | ||
* @param {{transfer: any[]}?} options an object with a transfoer property. | ||
* This is currently not supported, all values are always cloned. | ||
* @returns {Record[]} | ||
*/ | ||
export default (any, options = dflt) => deserialize(serialize(any, options)); | ||
export {deserialize, serialize}; |
const {toString} = {}; | ||
const {keys} = Object; | ||
const defaultOptions = {transfer: []}; | ||
const _serialize = (value, info) => { | ||
const {$, _} = info; | ||
const _serialize = (value, $, _) => { | ||
if ($.has(value)) | ||
return $.get(value); | ||
const i = _.push(value) - 1; | ||
$.set(value, i); | ||
const index = _.push(value) - 1; | ||
$.set(value, index); | ||
const as = serialized => { | ||
_[i] = serialized; | ||
return i; | ||
_[index] = serialized; | ||
return index; | ||
}; | ||
switch (typeof value) { | ||
case 'bigint': | ||
return as(['BigInt', value.toString()]); | ||
case 'object': | ||
@@ -25,12 +21,10 @@ if (value !== null) { | ||
switch (type) { | ||
case 'Array': | ||
return as([type, value.map(entry => _serialize(entry, $, _))]); | ||
case 'Object': { | ||
const entries = []; | ||
for (const key of keys(value)) | ||
entries.push([key, _serialize(value[key], info)]); | ||
entries.push([key, _serialize(value[key], $, _)]); | ||
return as([type, entries]); | ||
} | ||
case 'Boolean': | ||
case 'Number': | ||
case 'String': | ||
return as([type, value.valueOf()]); | ||
case 'Date': | ||
@@ -45,17 +39,15 @@ return as([type, value.toISOString()]); | ||
for (const [key, entry] of value) | ||
entries.push([_serialize(key, info), _serialize(entry, info)]); | ||
entries.push([_serialize(key, $, _), _serialize(entry, $, _)]); | ||
return as([type, entries]); | ||
} | ||
case 'Array': { | ||
const arr = []; | ||
for (const entry of value) | ||
arr.push(_serialize(entry, info)); | ||
return as([type, arr]); | ||
} | ||
case 'Set': { | ||
const values = []; | ||
for (const entry of value) | ||
values.push(_serialize(entry, info)); | ||
values.push(_serialize(entry, $, _)); | ||
return as([type, values]); | ||
} | ||
case 'Boolean': | ||
case 'Number': | ||
case 'String': | ||
return as([type, value.valueOf()]); | ||
} | ||
@@ -78,2 +70,4 @@ | ||
return as(['primitive', value]); | ||
case 'bigint': | ||
return as(['BigInt', value.toString()]); | ||
default: | ||
@@ -85,11 +79,13 @@ throw new TypeError; | ||
/** | ||
* @typedef {Array<string,any>} Record a type representation | ||
*/ | ||
/** | ||
* Returns an array of serialized Records. | ||
* @param {any} any a serializable value. | ||
* @param {object?} options an object with a transfoer property. | ||
* This is currently not supported, all values are always cloned. | ||
* @returns | ||
* @param {any} serializable a serializable value. | ||
* @returns {Record[]} | ||
*/ | ||
export const serialize = (any, {transfer} = defaultOptions) => { | ||
export const serialize = serializable => { | ||
const _ = []; | ||
return _serialize(any, {transfer, $: new Map, _}), _; | ||
return _serialize(serializable, new Map, _), _; | ||
}; |
{ | ||
"name": "@ungap/structured-clone", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A structuredClone polyfill", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10131
322