Launch Week Day 1: Socket for Jira Is Now Available.Learn More
Socket
Book a DemoSign in
Socket

another-deep-freeze

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

another-deep-freeze

Util to make your objects and arrays purely immutable

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
357
-10.97%
Maintainers
1
Weekly downloads
 
Created
Source

Another Deep Freeze

This utility will recursively make the Object/Array and all it's properties/items immutable.

npm version Build Status code cove Codacy Badge another-deep-freeze

Javascript provides a built-in utility (Object.freeze) for freezing objects but it only freezes immediate properties of the object. If a property's depth is greater than 1 it will still remain mutable.

Installation

NPM

npm i -S another-deep-freeze

Yarn

yarn add another-deep-freeze --save

Usage

Pass the object to deepFreeze and generate an immutable clone.

import deepFreeze from 'another-deep-freeze'

const txn = {
  _id: 1,
  amount: 1000.0,
  branch: {
    _id: 1,
    tellers: [1, { _id: 2, code: 'XYZ001' }]
  }
}

// freeze txn in a cloned object
const frozenTxn = deepFreeze(txn)

txn === frozenTxn // false

The following operations will throw TypeError

// 1. mutating existing key
frozenTxn._id = 2
// 2. adding new key
frozenTxn.newKey = 1
// 3. deleting a key
delete forzenTxn.branch
// same behaviour at all depths
frozenTxn.branch._id = 2
// even for an array
frozenTxn.branch.tellers.push(0)
// or an array of objects
frozenTxn.branch.tellers[1].code = 'NEWCODE'

// the prototypes are immutable and any attempt of mutation will throw TypeError
Object.setPrototypeOf(frozenTxn, { foo: 'baz' })
txn.branch.__proto__.foo = 'baz'
txn.branch.tellers.__proto__.length = 0

You may also perform in-place deep freeze.

import deepFreeze from 'another-deep-freeze'

const txn = {
  _id: 1,
  amount: 1000.0,
  branch: {
    _id: 1,
    tellers: [1, { _id: 2, code: 'XYZ001' }]
  }
}

const sameTxn = deepFreeze(txn, /* in-place deep freeze*/ true)

sameTxn === txn // true

This will make txn immutable at all depths.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update the tests as appropriate.

License

MIT

Keywords

typescript

FAQs

Package last updated on 31 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts