Git Product home page Git Product logo

copy-object-graph's Introduction

Copy Object Graphs

Supports copying of

  • nested objects
  • nested arrays
  • self-referencing objects
  • self-referencing arrays
  • numbers
  • strings
  • null
  • undefined

Things to be aware of

  • The copy gets references to functions (not copies of functions)
  • There is a dependency on WeakMap for storing self-reference identities
  • Object properties are copied into new objects using Object.assign({}, original_obj). This operation has side effects ~
    • it will activate any getters on the original object
    • the new object will inherit Object prototype

Test Documentation

TOC

Single Primitives

returns the same boolean when given a boolean.

(0, _assert.strictEqual)(false, (0, _2.default)(false));
(0, _assert.strictEqual)(true, (0, _2.default)(true));

returns the same number when given a number.

(0, _assert.strictEqual)(0, (0, _2.default)(0));
(0, _assert.strictEqual)(1, (0, _2.default)(1));
(0, _assert.strictEqual)(-1, (0, _2.default)(-1));

returns the same string when given a string.

(0, _assert.strictEqual)('', (0, _2.default)(''));
(0, _assert.strictEqual)('hello', (0, _2.default)('hello'));

returns undefined when given undefined.

(0, _assert.strictEqual)(undefined, (0, _2.default)(undefined));

returns null when given null.

(0, _assert.strictEqual)(null, (0, _2.default)(null));

returns the same symbol when given a symbol.

const sym = Symbol();
(0, _assert.strictEqual)(sym, (0, _2.default)(sym));

Single-Depth Copy

returns an array copy when given an array.

const arr = [];
(0, _assert.notStrictEqual)(arr, (0, _2.default)(arr));

returns an object copy when given an object.

const obj = {};
(0, _assert.notStrictEqual)(obj, (0, _2.default)(obj));

Deep Copy

returns a deep copy of nested array when given a nested array.

const arr = [[1], [[2]], [[[3]]]];
const res = (0, _2.default)(arr);
(0, _assert.notStrictEqual)(arr, res);
(0, _assert.notStrictEqual)(arr[0], res[0]);
(0, _assert.notStrictEqual)(arr[1], res[1]);
(0, _assert.notStrictEqual)(arr[1][0], res[1][0]);
(0, _assert.notStrictEqual)(arr[2], res[2]);
(0, _assert.notStrictEqual)(arr[2][0], res[2][0]);
(0, _assert.notStrictEqual)(arr[2][0][0], res[2][0][0]);

returns a deep copy of nested object when given a nested object.

const obj = {
  a: { a: 1 },
  b: { b: { b: 2 } },
  c: { c: { c: { c: 3 } } }
};
const res = (0, _2.default)(obj);
(0, _assert.notStrictEqual)(obj, res);
(0, _assert.notStrictEqual)(obj.a, res.a);
(0, _assert.notStrictEqual)(obj.b, res.b);
(0, _assert.notStrictEqual)(obj.b.b, res.b.b);
(0, _assert.notStrictEqual)(obj.c, res.c);
(0, _assert.notStrictEqual)(obj.c.c, res.c.c);
(0, _assert.notStrictEqual)(obj.c.c.c, res.c.c.c);

Self-Referencing Copy

returns a copy of self-referencing array when given a self-referencing array.

const arr = [1, 2, 3];
arr[0] = arr; // self-reference
(0, _assert.strictEqual)(arr, arr[0]);
const res = (0, _2.default)(arr);
(0, _assert.strictEqual)(res, res[0]);
(0, _assert.notStrictEqual)(arr, res);
(0, _assert.notStrictEqual)(arr[0], res[0]);

returns a copy of self-referencing object when given a self-referencing object.

const obj = { a: {}, b: {}, c: {} };
obj.a = obj; // self-reference
(0, _assert.strictEqual)(obj, obj.a);
const res = (0, _2.default)(obj);
(0, _assert.strictEqual)(res, res.a);
(0, _assert.notStrictEqual)(obj, res);
(0, _assert.notStrictEqual)(obj.a, res.a);

Deep/Self-Referencing Copy

returns a deep copy of nested/self-referencing array/object when given a nested/self-referencing array/object.

const arr = [{ a: [] }, [{}]];
arr[0].a = arr[1];
(0, _assert.strictEqual)(arr[0].a, arr[1]);
arr[1][0].b = 2;
(0, _assert.strictEqual)(arr[0].a[0].b, 2);
const res = (0, _2.default)(arr);
(0, _assert.strictEqual)(res[0].a, res[1]);
(0, _assert.strictEqual)(res[0].a[0].b, 2);
(0, _assert.notStrictEqual)(arr, res);
(0, _assert.notStrictEqual)(arr[0].a, res[0].a);
(0, _assert.notStrictEqual)(arr[0].a[0], res[0].a[0]);
(0, _assert.strictEqual)(arr[0].a[0].b, res[0].a[0].b); // 2 === 2

copy-object-graph's People

Contributors

thurt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.