frozen-core

0.14.0 • Public • Published

frozen-core

Coverage Status Code Climate

General purpose lib to create maintainable and immutable Objects - the idea is to work without side effects .

  • Very small (4KB, raw)
  • Object properties are immutable
  • Optional state properties
  • Simple Typesafe inheritance
  • Hierarchic method bubbling
  • Enforces a specifc way to implement your Objects and Methods
var frozenCore = require('frozen-core');
var myObject = frozenCore.extend({
  state: {
    //mutable property values (the object mask)`
  },
  core: {
    // immutable property values here
    //only functions allowed (other types are ommited)
    // 'shares' the state object scope by obtaining a immutable copy (the snapshot) (lexical this refers to that snapshot)
    /* Every method here only can access that copy but no other method within 
    the object except the inherited public api methods (extend, bubble) */
  }
})
// the properties of the resulting object are always immutable, not configurable as the object itself too, one can't add new properties, remove or configure them

API

myObject.extend(/*options object*/);
myObject.bubble(/*method to call (which will be executed on every parent)*/)
myObject.getSnapshot() //returns a reference to the immutable state copy
myObject.parent // reference to the latest origin

Simple implementation example

var cube = frozenCore.extend({
  core: {
    mutate: function(){
      //'this' is safe to use here since it will be immutable - it will refer to a copy of the state object
      /* this will have no effect except throwing an error in strict mode !*/this.width = 1;
      return this.extend({
        state: {
          width: this.width+20,
          height: this.height+20,
          depth: this.depth+20
        }
      });
    }
  },
  state: {
    width: 0,
    height: 0,
    depth: 0
  }
});
 
var mutatedCube = cube.mutate();

Package Sidebar

Install

npm i frozen-core

Weekly Downloads

1

Version

0.14.0

License

MIT

Last publish

Collaborators

  • eimfach