@bearclaw/immutable-class
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

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'; // No errors
const instance = instanceToImmutableInstance(existing);
instance.foo = 'foo'; // throws a read only property error

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'; // No errors
const plain = instanceToImmutablePlain(instance);
plain.foo = 'foo'; // throws a read only property error

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'; // No errors
const updated = patchImmutableInstance(instance, { foo: 'some value' });
instance.foo = 'foo'; // throws a read only property error

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'; // No errors
const instance = plainToImmutableInstance(MyClass, plain);
instance.foo = 'foo'; // throws a read only property error

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'; // No errors
const updated = produceImmutableInstance(instance, draft => {
  draft.foo = 'some value'
});
instance.foo = 'foo'; // throws a read only property error

Contributing

Running unit tests

Run nx test immutable-class to execute the unit tests via Jest.- immutable-class

Readme

Keywords

none

Package Sidebar

Install

npm i @bearclaw/immutable-class

Weekly Downloads

0

Version

0.3.0

License

none

Unpacked Size

17.7 kB

Total Files

21

Last publish

Collaborators

  • jafaircl