Skip to content

Commit

Permalink
Merge pull request #1 from ungap/packed-release
Browse files Browse the repository at this point in the history
Packed all the strings
  • Loading branch information
WebReflection authored Oct 30, 2021
2 parents a80f26b + a02d59d commit b7b023e
Show file tree
Hide file tree
Showing 12 changed files with 2,366 additions and 60 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: build

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
- run: npm run coverage --if-present
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 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.

* [Supported Types](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types)
Expand Down
32 changes: 15 additions & 17 deletions cjs/deserialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
'use strict';
const {
PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT
} = require('./types.js');

const env = typeof self === 'object' ? self : globalThis;

const _deserialize = (index, $, _) => {
Expand All @@ -13,49 +17,43 @@ const _deserialize = (index, $, _) => {
};

switch (type) {
case 'primitive':
case PRIMITIVE:
return as(value);
case 'Array': {
case ARRAY: {
const arr = as([]);
for (const index of value)
arr.push(_deserialize(index, $, _));
return arr;
}
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);
for (const index of value)
set.add(_deserialize(index, $, _));
return set;
}
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));
}
return as(new env[type](value));
Expand Down
24 changes: 14 additions & 10 deletions cjs/serialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
'use strict';
const {
PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT
} = require('./types.js');

const {toString} = {};
const {keys} = Object;

Expand All @@ -20,30 +24,30 @@ const _serialize = (value, $, _) => {
const type = toString.call(value).slice(8, -1);
switch (type) {
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}]);
}
case 'Map': {
const entries = [];
for (const [key, entry] of value)
entries.push([_serialize(key, $, _), _serialize(entry, $, _)]);
return as([type, entries]);
return as([MAP, entries]);
}
case 'Set': {
const values = [];
for (const entry of value)
values.push(_serialize(entry, $, _));
return as([type, values]);
return as([SET, values]);
}
case 'Boolean':
case 'Number':
Expand All @@ -56,7 +60,7 @@ const _serialize = (value, $, _) => {

if (type.includes('Error')) {
const {message} = value;
return as(['Error', {name: type, message}]);
return as([ERROR, {name: type, message}]);
}

throw new TypeError;
Expand All @@ -65,9 +69,9 @@ const _serialize = (value, $, _) => {
case 'number':
case 'string':
case 'undefined':
return as(['primitive', value]);
return as([PRIMITIVE, value]);
case 'bigint':
return as(['BigInt', value.toString()]);
return as([BIGINT, value.toString()]);
default:
throw new TypeError;
}
Expand Down
21 changes: 21 additions & 0 deletions cjs/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
let i = 0;

const PRIMITIVE = i++;
exports.PRIMITIVE = PRIMITIVE;
const ARRAY = i++;
exports.ARRAY = ARRAY;
const OBJECT = i++;
exports.OBJECT = OBJECT;
const DATE = i++;
exports.DATE = DATE;
const REGEXP = i++;
exports.REGEXP = REGEXP;
const MAP = i++;
exports.MAP = MAP;
const SET = i++;
exports.SET = SET;
const ERROR = i++;
exports.ERROR = ERROR;
const BIGINT = i++;
exports.BIGINT = BIGINT;
34 changes: 17 additions & 17 deletions esm/deserialize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
PRIMITIVE, ARRAY, OBJECT,
DATE, REGEXP, MAP, SET,
ERROR, BIGINT
} from './types.js';

const env = typeof self === 'object' ? self : globalThis;

const _deserialize = (index, $, _) => {
Expand All @@ -12,49 +18,43 @@ const _deserialize = (index, $, _) => {
};

switch (type) {
case 'primitive':
case PRIMITIVE:
return as(value);
case 'Array': {
case ARRAY: {
const arr = as([]);
for (const index of value)
arr.push(_deserialize(index, $, _));
return arr;
}
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);
for (const index of value)
set.add(_deserialize(index, $, _));
return set;
}
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));
}
return as(new env[type](value));
Expand Down
26 changes: 16 additions & 10 deletions esm/serialize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
PRIMITIVE, ARRAY, OBJECT,
DATE, REGEXP, MAP, SET,
ERROR, BIGINT
} from './types.js';

const {toString} = {};
const {keys} = Object;

Expand All @@ -19,30 +25,30 @@ const _serialize = (value, $, _) => {
const type = toString.call(value).slice(8, -1);
switch (type) {
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}]);
}
case 'Map': {
const entries = [];
for (const [key, entry] of value)
entries.push([_serialize(key, $, _), _serialize(entry, $, _)]);
return as([type, entries]);
return as([MAP, entries]);
}
case 'Set': {
const values = [];
for (const entry of value)
values.push(_serialize(entry, $, _));
return as([type, values]);
return as([SET, values]);
}
case 'Boolean':
case 'Number':
Expand All @@ -55,7 +61,7 @@ const _serialize = (value, $, _) => {

if (type.includes('Error')) {
const {message} = value;
return as(['Error', {name: type, message}]);
return as([ERROR, {name: type, message}]);
}

throw new TypeError;
Expand All @@ -64,9 +70,9 @@ const _serialize = (value, $, _) => {
case 'number':
case 'string':
case 'undefined':
return as(['primitive', value]);
return as([PRIMITIVE, value]);
case 'bigint':
return as(['BigInt', value.toString()]);
return as([BIGINT, value.toString()]);
default:
throw new TypeError;
}
Expand Down
11 changes: 11 additions & 0 deletions esm/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let i = 0;

export const PRIMITIVE = i++;
export const ARRAY = i++;
export const OBJECT = i++;
export const DATE = i++;
export const REGEXP = i++;
export const MAP = i++;
export const SET = i++;
export const ERROR = i++;
export const BIGINT = i++;
Loading

0 comments on commit b7b023e

Please sign in to comment.