Skip to content

Small javascript library for deep freezing/thawing objects.

Notifications You must be signed in to change notification settings

davemx/DeepFreezerJS

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

DeepFreezerJS

A library for [deep] freezing and [deep] thawing JavaScript objects.

Can be used from node or the browser.

This package is intended for freezing and thawing simple objects and arrays. In order to clone arrays properly is invokes objects' prototypes constructor in order to make copies, so it probably won't work with objects with complex custom constructors or properties that aren't carefully designed to work with it.

Examples

Freeze your pizza

var pizza = {
    "toppings": [
        {
            "name": "Broccoli"
        },
        {
            "name": "Onions"
        }
    ]
}

pizza.toppings.push({"name": "Mushrooms"});

// The second parameter, 'true', means 'allow freezing in-place'.
// i.e. we don't care about keeping the original object mutable.
var frozenPizza = DeepFreezer.deepFreeze(pizza, true);

frozenPizza.toppings.push({"name": "Olives"}); // an error!  Pizza's frozen
pizza.toppings.push({"name": "Olives"}); // Also an error!  That's the same instance as frozenPizza.

var thawedPizza = DeepFreezer.thaw(pizza);

thawedPizza.toppings.push({"name": "Olives"}); // That's okay!

thawedPizza.toppings[1].name = "Extra sauce"; // An error!  The toppings were also each frozen.

var totallyThawedPizza = DeepFreezer.deepThaw(thawedPizza);

thawedPizza.toppings[1].name = "Extra sauce"; // Ahh there we go.

pizza.toppings[1].name; // Still "Onions"

See test.html and test.js for more examples.

About

Small javascript library for deep freezing/thawing objects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 88.3%
  • HTML 11.3%
  • Makefile 0.4%