Skip to content

Just a small function to freeze the entire object avoiding mutability

Notifications You must be signed in to change notification settings

abdalla/deep-freeze

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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