Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

immutable-class

Package Overview
Dependencies
Maintainers
2
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immutable-class

A template for creating immutable classes

  • 0.6.16
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

Immutable Class

A little library to facilitate creating and testing serializable, immutable classes.

This library is really just a set of templates and testing tools to allow for quick construction of immutable classes.

Templates

An object Blah is considered a immutable class of it meats the following criteria:

  • It is a JS 'class' that starts with an uppercase letter
  • It has a static Blah.isBlah method for checking if something is an instance of the given class
  • It has a static Blah.fromJS method for deserializing classes
  • It has an instance blah.valueOf method that return a minimally serialized object (preserving all sub classes as immutable classes)
  • It has an instance blah.toJS method that return a fully serialized object (recursively serializing all sub classes)
  • It has an instance blah.toJSON method that returns the same as the toJS method allowing the object to be passed into JSON.stringify
  • It has an instance blah.toString method that is implemented in some way (and returns a string)
  • It has an instance blah.equals method that can be used to compare this object to other classes to check for equivalence.

Testing tools

Immutable Class provides one testing function for testing potential immutable classes: testImmutableClass

It is used like so:

testImmutableClass(MyImmutableClassConstructor, [
  { "potential": 1 }
  { "distinct": 2 }
  { "js": 3 }
  { "immutable classes": 4 }
]);

This function will then try to call fromJS on each candidate and run it through a series of tests to ensure that it corresponds to the rules above. It will also do an equality check of every object with every other object and make sure that it only equals itself.

For an example of the usage of this library look at these files: source, tests.

Keywords

FAQs

Package last updated on 28 Sep 2016

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