0.0.1 • Published 8 years ago

object-freezer v0.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
8 years ago

Object Freezer

The purpose of this module is to freeze an object and all its nested objects. What differentiates this project from others, such as deep-freeze and deep-freeze-strict, is the ability to flag specific nested objects as antifreeze to avoid freezing.

Disclaimer

This is not a new idea or a common need. The project originated from a single very specific use case. Your chances of needing this module are slim.

Installation

npm install object-freezer --save

Use Strict

Object.freeze() is the method used to freeze the objects. It is highly recommended to use strict mode. With strict mode, frozen objects will throw an TypeError on a change attempt. Without strict mode, frozen objects silently ignore changes.

Examples

The following is a basic example of how to deepFreeze an object and what happens when changes are attempted.

'use strict';
import { deepFreeze } from 'object-freezer';

const frozen = deepFreeze({
  int: 2016,
  str: 'example',
  arr: [ 1, 2, 3 ],
  obj: { a: 1, b: 2, c: 3 },
});

frozen.int = 2017;     // throws a TypeError
frozen.str = 'sample'; // throws a TypeError
frozen.arr = [ 1 ];    // throws a TypeError
frozen.obj = {};       // throws a TypeError

The following is an example of how to antiFreeze nested objects to avoid a deepFreeze.

'use strict';
import { antiFreeze, deepFreeze } from 'object-freezer';

const frozen = deepFreeze({
  obj: antiFreeze({ a: 1, b: 2, c: 3 }),
});

frozen.obj = {}; // success!