Skip to content

flotwig/js-refrigerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

refrigerator

Gitlab pipeline status (self-hosted) npm bundle size npm

refrigerator is a tool that can be used to make all property changes on an object throw an error.

It's like Object.freeze, except it also blocks modifying all child objects, and will throw an error even when not in strict mode.

This can be useful when you want to validate that your JS code does not ever modify the arguments passed in to it.

Installation

npm i --save refrigerator

Usage

const { refrigerate } = require('refrigerator')

const obj = {
  lisa: 'marge',
  homer: {
    bart: 'maggie'
  }
}

const frozen = refrigerate(obj)

frozen.lisa = 'apu' // throws an Error!
frozen.homer.bart = 'skinner' // throws an Error!

Why make yet another object-freezing module?

Most existing packages will recursively walk your input object and Object.freeze it. Object.freeze only throws an error if use strict is set; otherwise, it will silently discard the new property value. This makes it hard to add to an existing sloppy mode (non-strict) codebase.

Also, this package does NOT recursively walk your object; instead, it wraps the base layer with a Proxy, and wraps any object you access off of that layer with the same Proxy, and so on. This means that the performance of refrigerator is better than is possible with a recursive Object.freeze.

About

0-dependency JS module that can be used to make all property changes on an object throw an error.

Resources

Stars

Watchers

Forks

Packages

No packages published