immutable-class
This library provides helper functions to extend class-transformer
functionality to create immutable classes. It also provides helper functions to work with immutable classes using immer
.
instanceToImmutableInstance
Converts a class (constructor) object to a new immutable class (constructor) object. Also works with arrays.
class MyClass {
foo!: string
}
const existing = new MyClass();
existing.foo = 'bar';
const instance = instanceToImmutableInstance(existing);
instance.foo = 'foo';
instanceToImmutablePlain
Converts a class (constructor) object to an immutable plain (literal) object. Also works with arrays.
class MyClass {
foo!: string
}
const instance = new MyClass();
instance.foo = 'bar';
const plain = instanceToImmutablePlain(instance);
plain.foo = 'foo';
patchImmutableInstance
Produce the next immutable class (constructor) object by patching the existing class (constructor) object. Also works with arrays.
class MyClass {
foo!: string
}
const instance = new MyClass();
instance.foo = 'bar';
const updated = patchImmutableInstance(instance, { foo: 'some value' });
instance.foo = 'foo';
plainToImmutableInstance
Converts a plain (literal) object to an immutable class (constructor) object. Also works with arrays.
class MyClass {
foo!: string
}
const plain = { foo: 'example' };
plain.foo = 'bar';
const instance = plainToImmutableInstance(MyClass, plain);
instance.foo = 'foo';
produceImmutableInstance
Produce the next immutable class (constructor) object by modifying the current class (constructor) object with a recipe function. Also works with arrays.
class MyClass {
foo!: string
}
const instance = new MyClass();
instance.foo = 'bar';
const updated = produceImmutableInstance(instance, draft => {
draft.foo = 'some value'
});
instance.foo = 'foo';
Contributing
Running unit tests
Run nx test immutable-class
to execute the unit tests via
Jest.- immutable-class