0.0.7 • Published 11 months ago

@jrc03c/freeze v0.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

freeze is a little function that returns an immutable copy of a JS object or array.

Install:

npm install --save https://github.com/jrc03c/freeze

Use:

const freeze = require("@jrc03c/freeze")

const person = freeze({ name: "Alice" })
person.name = "Bob"
console.log(person.name)
// Alice

const myList = freeze([1, 2, 3])
myList.push(42)
console.log(myList)
// [1, 2, 3]

By default, the above operations fail silently. However, if you'd prefer for them to throw errors, you can pass a true value as the second argument:

const shouldThrowErrors = true
const person = freeze({ name: "Alice" }, shouldThrowErrors)
person.name = "Bob"
// Uncaught Error: The target object is read-only, so its "name" property
// cannot be set!

Notes:

There might be sneaky ways of modifying "frozen" objects or arrays that haven't occurred to me yet. If you find any, please let me know!

0.0.7

11 months ago

0.0.6

12 months ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago