Skip to content

Deeply clone arrays, maps, sets, dates, plain objects, and class instances.

Notifications You must be signed in to change notification settings

visusnet/clone-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clone Lab

npm version

Deeply clone arrays, maps, sets, dates, plain objects, and class instances.

Install

With npm:

npm i --save clone-lab

Or with yarn:

yarn add -D clone-lab

Example

// @flow
import deepClone from 'clone-lab';

class Person {
    name: string;

    constructor(name: string) {
        this.name = name;
    }
}

class Parent extends Person {
    dateOfBirth: Date;
    character: Set<string>;

    constructor(name: string, dateOfBirth: Date, character: Set<string>) {
        super(name);
        this.dateOfBirth = dateOfBirth;
        this.character = character;
    }
}

class Child extends Person {
    parents: Map<string, Parent> = new Map();
}

const child = new Child('Holly');
const mother = new Parent('Offred', new Date('1970-01-01T03:24:00'), new Set(['kind', 'devotional']));
const father = new Parent('Nick', new Date('1975-01-01T05:12:00'), new Set(['calm', 'brave']));
child.parents.set('mother', mother);
child.parents.set('father', father);

const clonedChild = deepClone(child);

Limitations

Cycles are not supported at this moment, i.e. this does not work:

class Node {
    next: Node;
}

const node = new Node();
node.next = node;

deepClone(node); // error

License

MIT

About

Deeply clone arrays, maps, sets, dates, plain objects, and class instances.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published