Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular references, flcone and build improvements #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
nodejs: [8, 10, 12]
nodejs: [8, 10, 12, 14, 16, 18]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
/json
/lite
/full
/circular
5 changes: 5 additions & 0 deletions bench/fixtures/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const full = require("./full");

full.full = full;

module.exports = full;
24 changes: 24 additions & 0 deletions bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ console.time('clone');
const clone = require('clone');
console.timeEnd('clone');

console.time('fclone');
const fclone = require('fclone');
console.timeEnd('fclone');

console.time('clone-deep');
const clonedeep = require('clone-deep');
console.timeEnd('clone-deep');
Expand All @@ -39,6 +43,10 @@ console.time('klona/json');
const json = require('klona/json');
console.timeEnd('klona/json');

console.time('klona/circular');
const circular = require('klona/circular');
console.timeEnd('klona/circular');

const naiive = x => JSON.parse(JSON.stringify(x));
const clone_full = x => clone(x, { includeNonEnumerable: true });

Expand Down Expand Up @@ -84,35 +92,51 @@ runner('json', {
'rfdc': rfdc(),
'clone': clone,
'clone/include': clone_full,
'fclone': fclone,
'clone-deep': clonedeep,
'deep-copy': deepcopy,
'klona/full': full.klona,
'klona': klona.klona,
'klona/lite': lite.klona,
'klona/json': json.klona,
'klona/circular': circular.klona,
});

runner('lite', {
'lodash': lodash,
'clone': clone,
'clone/include': clone_full,
'fclone': fclone,
'clone-deep': clonedeep,
'klona/full': full.klona,
'klona': klona.klona,
'klona/lite': lite.klona,
'klona/circular': circular.klona,
});

runner('default', {
'lodash': lodash, // FAIL @ Buffer, Map keys
'clone': clone, // FAIL @ DataView
'clone/include': clone_full, // FAIL @ DataView
'fclone': fclone,
// FAIL @ "Set #2" & "Map #2" :: 'clone-deep': clonedeep,
'klona/full': full.klona,
'klona': klona.klona,
'klona/circular': circular.klona,
});

runner('full', {
'lodash': lodash, // FAIL @ Buffer, Map keys, non-enumerable properties,
'clone/include': clone_full, // FAIL @ DataView, non-enumerable descriptors
'fclone': fclone,
'klona/full': full.klona,
'klona/circular': circular.klona,
});

runner('circular', {
'lodash': lodash,
'clone/include': clone_full, // FAIL @ DataView, non-enumerable descriptors
'fclone': fclone,
'klona/full': full.klona,
'klona/circular': circular.klona,
});
6 changes: 5 additions & 1 deletion bench/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"private": true,
"scripts": {
"start": "node index.js"
},
"devDependencies": {
"benchmark": "2.1.4",
"clone": "2.1.2",
Expand All @@ -8,6 +11,7 @@
"deepcopy": "2.0.0",
"klona": "file:../",
"lodash": "4.17.19",
"rfdc": "1.1.4"
"rfdc": "1.1.4",
"fclone": "^1.0.11"
}
}
5 changes: 5 additions & 0 deletions bench/validate/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const assert = require('assert');

module.exports = function (_, copy) {
assert.ok(["[object Circular]", "[Circular]"].includes(copy.full));
}
28 changes: 24 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dist",
"full",
"json",
"circular",
"lite"
],
"exports": {
Expand All @@ -37,22 +38,39 @@
"import": "./full/index.mjs",
"require": "./full/index.js"
},
"./circular": {
"import": "./circular/index.mjs",
"require": "./circular/index.js"
},
"./package.json": "./package.json"
},
"modes": {
"json": "src/json.js",
"lite": "src/lite.js",
"default": "src/index.js",
"full": "src/full.js"
"full": "src/full.js",
"circular": "src/circular.js"
},
"engines": {
"node": ">= 8"
},
"scripts": {
"build": "bundt",
"build": "npm run clear && bundt && npm run postbuild",
"pretest": "npm run build",
"postbuild": "echo \"lite full json\" | xargs -n1 cp -v index.d.ts",
"test": "uvu -r esm test -i suites"
"postbuild": "echo \"dist lite full json circular\" | xargs -n1 cp -v index.d.ts",
"test": "uvu -r esm test -i suites",
"clear": "rm -rf dist full json lite circular",
"build2": "npm run clear && npm run run-p build2:* && npm run postbuild",
"build2:json-esm": "esbuild --minify --format=esm --platform=neutral --outfile=json/index.mjs src/json.js",
"build2:json-cjs": "esbuild --minify --format=cjs --platform=neutral --outfile=json/index.js src/json.js",
"build2:lite-esm": "esbuild --minify --format=esm --platform=neutral --outfile=lite/index.mjs src/lite.js",
"build2:lite-cjs": "esbuild --minify --format=cjs --platform=neutral --outfile=lite/index.js src/lite.js",
"build2:dflt-esm": "esbuild --minify --format=esm --platform=neutral --outfile=dist/index.mjs src/index.js",
"build2:dflt-cjs": "esbuild --minify --format=cjs --platform=neutral --outfile=dist/index.js src/index.js",
"build2:full-esm": "esbuild --minify --format=esm --platform=neutral --outfile=full/index.mjs src/full.js",
"build2:full-cjs": "esbuild --minify --format=cjs --platform=neutral --outfile=full/index.js src/full.js",
"build2:circ-esm": "esbuild --minify --format=esm --platform=neutral --outfile=circular/index.mjs src/circular.js",
"build2:circ-cjs": "esbuild --minify --format=cjs --platform=neutral --outfile=circular/index.js src/circular.js"
},
"keywords": [
"clone",
Expand All @@ -64,7 +82,9 @@
],
"devDependencies": {
"bundt": "1.0.2",
"esbuild": "^0.14.46",
"esm": "3.2.25",
"npm-run-all": "^4.1.5",
"uvu": "0.5.2"
}
}
71 changes: 71 additions & 0 deletions src/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function set(obj, key, val, opt) {
if (typeof val.value === 'object') val.value = klona(val.value, opt);
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === '__proto__') {
Object.defineProperty(obj, key, val);
} else obj[key] = val.value;
}

const baseOptions = {
seen: new Set()
}

/**
* Clone any object deep without caring about circular references again.
*
* If you want to manually improve memory performance add the seen Set option
* yourself and clear it after the clone.
*
* @param {Object} x - object to clone
* @param {{ seen: Set }} opt - options
*
* @returns your object, but cloned
*/
export function klona(x, opt = baseOptions) {
if (typeof x !== 'object') return x;

var i=0, k, list, tmp, str=Object.prototype.toString.call(x);
opt.seen.add(x);

if (str === '[object Object]') {
tmp = Object.create(x.__proto__ || null);
} else if (str === '[object Array]') {
tmp = Array(x.length);
} else if (str === '[object Set]') {
tmp = new Set;
x.forEach(function (val) {
tmp.add(klona(val, opt));
});
} else if (str === '[object Map]') {
tmp = new Map;
x.forEach(function (val, key) {
tmp.set(klona(key, opt), klona(val, opt));
});
} else if (str === '[object Date]') {
tmp = new Date(+x);
} else if (str === '[object RegExp]') {
tmp = new RegExp(x.source, x.flags);
} else if (str === '[object DataView]') {
tmp = new x.constructor( klona(x.buffer, opt) );
} else if (str === '[object ArrayBuffer]') {
tmp = x.slice(0);
} else if (str.slice(-6) === 'Array]') {
tmp = x.constructor.from(x);
}

if (tmp) {
for (list=Object.getOwnPropertySymbols(x); i < list.length; i++) {
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]), opt);
}

for (i=0, list=Object.getOwnPropertyNames(x); i < list.length; i++) {
if (Object.hasOwnProperty.call(tmp, k=list[i]) && tmp[k] === x[k]) continue;
if (opt.seen.has(x[k])) {
tmp[k] = "[object Circular]";
} else {
set(tmp, k, Object.getOwnPropertyDescriptor(x, k), opt);
}
}
}

return tmp || x;
}
29 changes: 29 additions & 0 deletions test/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as suites from './suites';
import { klona } from '../src/circular';

suites.API(klona);

suites.Strings(klona);
suites.Booleans(klona);
suites.Numbers(klona);
suites.Nully(klona);

suites.Dates(klona);
suites.RegExps(klona);

suites.Objects(klona);
suites.Arrays(klona);

suites.Functions(klona);
suites.Pollutions(klona);
suites.Classes(klona);

suites.Maps(klona);
suites.Sets(klona);

suites.TypedArrays(klona);

suites.Symbols(klona);
suites.Descriptors(klona);
suites.Dicts(klona);
suites.Circular(klona);
19 changes: 19 additions & 0 deletions test/suites/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { suite } from 'uvu';
import * as assert from 'assert';

export default function (klona) {
const Circular = suite('Map');

Circular('base', () => {
const input = { a: 1 };
input.input = input;
const output = klona(input);

output.hello = "world"
assert.equal(input.hello, undefined);

assert.equal(output.input, "[object Circular]");
});

Circular.run();
}
1 change: 1 addition & 0 deletions test/suites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export { default as Arrays } from './array';
export { default as Functions } from './function';
export { default as Pollutions } from './pollution';
export { default as Classes } from './class';
export { default as Circular } from './circular';
4 changes: 2 additions & 2 deletions test/suites/typedarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function(klona) {
assert.equal(output[0], 0);
});

test('ArrayBuffer :: empty', () => {
test('ArrayBuffer :: empty', () => {
const input = new ArrayBuffer(6);
const output = klona(input);

Expand All @@ -85,7 +85,7 @@ export default function(klona) {
assert.equal(view2.getInt8(1), 8);
});

test('ArrayBuffer :: values', () => {
test('ArrayBuffer :: values', () => {
const input = new ArrayBuffer(3);
const view1 = new DataView(input);

Expand Down