@ungap/structured-clone
Advanced tools
Comparing version 0.1.0 to 0.2.0
'use strict'; | ||
const { | ||
PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT | ||
} = require('./types.js'); | ||
const env = typeof self === 'object' ? self : globalThis; | ||
@@ -16,5 +20,5 @@ | ||
switch (type) { | ||
case 'primitive': | ||
case PRIMITIVE: | ||
return as(value); | ||
case 'Array': { | ||
case ARRAY: { | ||
const arr = as([]); | ||
@@ -25,21 +29,21 @@ for (const index of value) | ||
} | ||
case 'Object': { | ||
case OBJECT: { | ||
const object = as({}); | ||
for (const [key, index] of value) | ||
object[key] = _deserialize(index, $, _); | ||
object[_deserialize(key, $, _)] = _deserialize(index, $, _); | ||
return object; | ||
} | ||
case 'Date': | ||
case DATE: | ||
return as(new Date(value)); | ||
case 'RegExp': { | ||
case REGEXP: { | ||
const {source, flags} = value; | ||
return as(new RegExp(source, flags)); | ||
} | ||
case 'Map': { | ||
case MAP: { | ||
const map = as(new Map); | ||
for (const [key, index] of value) | ||
map.set(key, _deserialize(index, $, _)); | ||
map.set(_deserialize(key, $, _), _deserialize(index, $, _)); | ||
return map; | ||
} | ||
case 'Set': { | ||
case SET: { | ||
const set = as(new Set); | ||
@@ -50,13 +54,7 @@ for (const index of value) | ||
} | ||
case 'Error': { | ||
case ERROR: { | ||
const {name, message} = value; | ||
return as(new env[name](message)); | ||
} | ||
case 'Boolean': | ||
return as(new Boolean(value)); | ||
case 'Number': | ||
return as(new Number(value)); | ||
case 'String': | ||
return as(new String(value)); | ||
case 'BigInt': | ||
case BIGINT: | ||
return as(BigInt(value)); | ||
@@ -63,0 +61,0 @@ } |
'use strict'; | ||
const { | ||
PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT | ||
} = require('./types.js'); | ||
const {toString} = {}; | ||
@@ -23,14 +27,14 @@ const {keys} = Object; | ||
case 'Array': | ||
return as([type, value.map(entry => _serialize(entry, $, _))]); | ||
return as([ARRAY, value.map(entry => _serialize(entry, $, _))]); | ||
case 'Object': { | ||
const entries = []; | ||
for (const key of keys(value)) | ||
entries.push([key, _serialize(value[key], $, _)]); | ||
return as([type, entries]); | ||
entries.push([_serialize(key, $, _), _serialize(value[key], $, _)]); | ||
return as([OBJECT, entries]); | ||
} | ||
case 'Date': | ||
return as([type, value.toISOString()]); | ||
return as([DATE, value.toISOString()]); | ||
case 'RegExp': { | ||
const {source, flags} = value; | ||
return as([type, {source, flags}]); | ||
return as([REGEXP, {source, flags}]); | ||
} | ||
@@ -41,3 +45,3 @@ case 'Map': { | ||
entries.push([_serialize(key, $, _), _serialize(entry, $, _)]); | ||
return as([type, entries]); | ||
return as([MAP, entries]); | ||
} | ||
@@ -48,3 +52,3 @@ case 'Set': { | ||
values.push(_serialize(entry, $, _)); | ||
return as([type, values]); | ||
return as([SET, values]); | ||
} | ||
@@ -62,3 +66,3 @@ case 'Boolean': | ||
const {message} = value; | ||
return as(['Error', {name: type, message}]); | ||
return as([ERROR, {name: type, message}]); | ||
} | ||
@@ -72,5 +76,5 @@ | ||
case 'undefined': | ||
return as(['primitive', value]); | ||
return as([PRIMITIVE, value]); | ||
case 'bigint': | ||
return as(['BigInt', value.toString()]); | ||
return as([BIGINT, value.toString()]); | ||
default: | ||
@@ -77,0 +81,0 @@ throw new TypeError; |
@@ -0,1 +1,7 @@ | ||
import { | ||
PRIMITIVE, ARRAY, OBJECT, | ||
DATE, REGEXP, MAP, SET, | ||
ERROR, BIGINT | ||
} from './types.js'; | ||
const env = typeof self === 'object' ? self : globalThis; | ||
@@ -15,5 +21,5 @@ | ||
switch (type) { | ||
case 'primitive': | ||
case PRIMITIVE: | ||
return as(value); | ||
case 'Array': { | ||
case ARRAY: { | ||
const arr = as([]); | ||
@@ -24,21 +30,21 @@ for (const index of value) | ||
} | ||
case 'Object': { | ||
case OBJECT: { | ||
const object = as({}); | ||
for (const [key, index] of value) | ||
object[key] = _deserialize(index, $, _); | ||
object[_deserialize(key, $, _)] = _deserialize(index, $, _); | ||
return object; | ||
} | ||
case 'Date': | ||
case DATE: | ||
return as(new Date(value)); | ||
case 'RegExp': { | ||
case REGEXP: { | ||
const {source, flags} = value; | ||
return as(new RegExp(source, flags)); | ||
} | ||
case 'Map': { | ||
case MAP: { | ||
const map = as(new Map); | ||
for (const [key, index] of value) | ||
map.set(key, _deserialize(index, $, _)); | ||
map.set(_deserialize(key, $, _), _deserialize(index, $, _)); | ||
return map; | ||
} | ||
case 'Set': { | ||
case SET: { | ||
const set = as(new Set); | ||
@@ -49,13 +55,7 @@ for (const index of value) | ||
} | ||
case 'Error': { | ||
case ERROR: { | ||
const {name, message} = value; | ||
return as(new env[name](message)); | ||
} | ||
case 'Boolean': | ||
return as(new Boolean(value)); | ||
case 'Number': | ||
return as(new Number(value)); | ||
case 'String': | ||
return as(new String(value)); | ||
case 'BigInt': | ||
case BIGINT: | ||
return as(BigInt(value)); | ||
@@ -62,0 +62,0 @@ } |
@@ -0,1 +1,7 @@ | ||
import { | ||
PRIMITIVE, ARRAY, OBJECT, | ||
DATE, REGEXP, MAP, SET, | ||
ERROR, BIGINT | ||
} from './types.js'; | ||
const {toString} = {}; | ||
@@ -22,14 +28,14 @@ const {keys} = Object; | ||
case 'Array': | ||
return as([type, value.map(entry => _serialize(entry, $, _))]); | ||
return as([ARRAY, value.map(entry => _serialize(entry, $, _))]); | ||
case 'Object': { | ||
const entries = []; | ||
for (const key of keys(value)) | ||
entries.push([key, _serialize(value[key], $, _)]); | ||
return as([type, entries]); | ||
entries.push([_serialize(key, $, _), _serialize(value[key], $, _)]); | ||
return as([OBJECT, entries]); | ||
} | ||
case 'Date': | ||
return as([type, value.toISOString()]); | ||
return as([DATE, value.toISOString()]); | ||
case 'RegExp': { | ||
const {source, flags} = value; | ||
return as([type, {source, flags}]); | ||
return as([REGEXP, {source, flags}]); | ||
} | ||
@@ -40,3 +46,3 @@ case 'Map': { | ||
entries.push([_serialize(key, $, _), _serialize(entry, $, _)]); | ||
return as([type, entries]); | ||
return as([MAP, entries]); | ||
} | ||
@@ -47,3 +53,3 @@ case 'Set': { | ||
values.push(_serialize(entry, $, _)); | ||
return as([type, values]); | ||
return as([SET, values]); | ||
} | ||
@@ -61,3 +67,3 @@ case 'Boolean': | ||
const {message} = value; | ||
return as(['Error', {name: type, message}]); | ||
return as([ERROR, {name: type, message}]); | ||
} | ||
@@ -71,5 +77,5 @@ | ||
case 'undefined': | ||
return as(['primitive', value]); | ||
return as([PRIMITIVE, value]); | ||
case 'bigint': | ||
return as(['BigInt', value.toString()]); | ||
return as([BIGINT, value.toString()]); | ||
default: | ||
@@ -76,0 +82,0 @@ throw new TypeError; |
{ | ||
"name": "@ungap/structured-clone", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A structuredClone polyfill", | ||
@@ -9,3 +9,3 @@ "main": "./cjs/index.js", | ||
"cjs": "ascjs esm cjs", | ||
"coveralls": "c8 report --reporter=text-lcov | coveralls", | ||
"coverage": "c8 report --reporter=text-lcov > ./coverage/lcov.info", | ||
"test": "c8 node test/index.js" | ||
@@ -12,0 +12,0 @@ }, |
# structuredClone polyfill | ||
[![build status](https://github.com/WebReflection/structured-clone/actions/workflows/node.js.yml/badge.svg)](https://github.com/WebReflection/structured-clone/actions) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/structured-clone/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/structured-clone?branch=main) | ||
An env agnostic serializer and deserializer with recursion ability and types beyond *JSON* from the *HTML* standard itself. | ||
@@ -10,8 +12,19 @@ | ||
Serialized values can be safely stringified as *JSON* too, and deserialization resurrect all values, even recursive, or more complex than what *JSON* allows. | ||
```js | ||
// as default export | ||
import structuredClone from '@ungap/structured-clone'; | ||
const cloned = structuredClone({any: 'serializable'}); | ||
// as independent serializer/deserializer | ||
import {serialize, deserialize} from '@ungap/structured-clone'; | ||
// the result can be stringified as JSON without issues | ||
// even if there is recursive data, bigint values, | ||
// typed arrays, and so on | ||
const serialized = serialize({any: 'serializable'}); | ||
// the result will be a replica of the original object | ||
const deserialized = deserialize(serialized); | ||
``` |
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
14662
13
358
30