Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 1.11 KB

README.md

File metadata and controls

71 lines (53 loc) · 1.11 KB

deep-freeze

Just a small function to freeze the entire object avoiding mutability.

Use

npm install deep-freezer
import deepFreeze from 'deep-freezer';

let person = {
  firstName: 'name',
  lastName: 'lastName'
};

deepFreeze(person);

Handle Errors

To throw error when you try to mutate a frozen object, just use the deep-freezer in strict mode.

'use strict'

import deepFreeze from 'deep-freezer';

let person = {
  firstName: 'name',
  lastName: 'lastName'
};

deepFreeze(person);

Examples

More examples could be find at example folder.

///Throwing Errors.
'use strict'

import deepFreeze from 'deep-freezer';

let person = {
  firstName: 'name',
  lastName: 'lastName'
};

person.firstName = 'New Name'; //==> this line will throw an error.

deepFreeze(person);

OR

///Ignoring the errors, but still keeping the object frozen.
import deepFreeze from 'deep-freezer';

let person = {
  firstName: 'name',
  lastName: 'lastName'
};

person.firstName = 'New Name'; //==> this line will be ignored.

deepFreeze(person);

Test

npm t