1.0.18 • Published 5 years ago

cloneable-ts v1.0.18

Weekly downloads
153
License
MIT
Repository
github
Last release
5 years ago

About

Abstract class that provides a deep clone method for classes in Typescript. Inspired by the copy method of a case class in Scala language.

How to use

Install

npm install cloneable-ts --save

Clone object

import {clone} from 'cloneable-ts';

// Interface that will be used as named arguments to initialize and clone an object
interface Person {
    readonly name: string;
    readonly age: number;
    readonly tags: string[];
}

const a: Person = {name: 'Alice', age: 28, tags: ['tag1']};
const b = clone(a, {name: 'Bob'});

a.name // Alice
b.name // Bob
b.age // 28
a.tags === b.tags //false

Clone instance of a class

import {Cloneable, CloneableArgs} from 'cloneable-ts';

// Interface that will be used as named arguments to initialize and clone an object
interface PersonArgs {
    readonly name: string;
    readonly age: number;
}

// CloneableArgs interface ensures that all properties defined in the argument interface are defined in the class
class Person extends Cloneable<TestArgs>  implements CloneableArgs<PersonArgs> {
    readonly name: string;
    readonly age: number;
    
    constructor(args: TestArgs) {
        // Cloneable abstract class initializes the object with super method and adds the clone method
        super(args);
    }
}

const a = new Person({name: 'Alice', age: 28});
const b = a.clone({name: 'Bob'})
a.name // Alice
b.name // Bob
b.age // 28
1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago