New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@bearclaw/immutable-class

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bearclaw/immutable-class

This library provides helper functions to extend `class-transformer` functionality to make the generated classes immutable. It also provides helper functions to work with immutable classes using `immer`.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

immutable-class

This library provides helper functions to extend class-transformer functionality to make the generated classes immutable. It also provides helper functions to work with immutable classes using immer.

  • immutable-class
  • Contributing

fromJSON

Create an immutable class from a JSON object. Note that any fields that do not have the class-transformer @Expose decorator will be ignored by the function. The decorator is also exported from this library for convenience.

class Test {
  @Expose()
  readonly foo!: string;
  @Expose()
  readonly bar?: string
}

const cls = fromJSON(Test, { foo: 'asdf' });
// cls: Test {
//   foo: 'asdf',
//   bar: undefined,
// }

toJSON

Convert a class to an immutable plain object. Note that any fields that do not have the class-transformer @Expose decorator will be ignored by the function.

class Test {
  @Expose()
  foo!: string;
  @Expose()
  bar?: string
}

const cls = new Test();
const emptyJson = toJSON(cls);
// emptyJson: {
//   foo: undefined,
//   bar: undefined,
// }
cls.foo = 'abc'
cls.bar = '123'
const setJson = toJSON(cls)
// setJson: {
//    foo: 'abc',
//    bar: '123',
// }

deserialize

Create an immutable class from a JSON string. Note that any fields that do not have the class-transformer @Expose decorator will be ignored by the function.

class Test {
  @Expose()
  readonly foo!: string;
  @Expose()
  readonly bar?: string
}

const cls = fromJSON(Test, `{ "foo": "asdf" }`);
// cls: Test {
//   foo: 'asdf',
//   bar: undefined,
// }

serialize

Convert a class to an immutable plain object. Note that any fields that do not have the class-transformer @Expose decorator will be ignored by the function.

class Test {
  @Expose()
  foo!: string;
  @Expose()
  bar?: string
}

const cls = new Test();
const emptyJson = toJSON(cls);
// emptyJson: "{}""
cls.foo = 'abc'
cls.bar = '123'
const setJson = toJSON(cls)
// setJson: '{"foo":"abc","bar":"123"}'

clone

Clone an existing immutable class. Note that any fields that do not have the class-transformer @Expose decorator will be ignored by the function.

class Test {
  @Expose()
  foo!: string;
  @Expose()
  bar?: string
}

const cls = new Test();
cls.foo = 'test'
const copy = clone(cls)
// copy: Test {
//   foo: 'test',
//   bar: undefined
// }

update

Update an immutable class. This function will leave the original class unmodified and return an updated copy of the class. Note that classes must be marked with "[immerable]: true" in order to use this function.

class Test {
  [immerable] = true;
  @Expose()
  readonly foo!: string;
  @Expose()
  readonly bar?: string
}
const cls = create(Test)
const updated = update(cls, x => {
  x.foo = 'abc'
})
// updated: Test {
//   foo: 'abc',
//   bar: undefined
// }

patch

Patch an immutable class with a JSON value. This function will leave the original class unmodified and return an updated copy of the class. Note that classes must be marked with "[immerable]: true" in order to use this function. Like other functions, any fields that do not have the class-transformer @Expose decorator will be ignored by the function.

class Test {
  [immerable] = true;
  @Expose()
  readonly foo!: string;
  @Expose()
  readonly bar?: string
}
const cls = create(Test)
const patched = patch(cls, { foo: 'abc' })
// patched: Test {
//   foo: 'abc',
//   bar: undefined
// }

Contributing

Running unit tests

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

FAQs

Package last updated on 11 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc