https://github.com/dogma-io/freezly
Small utility for deeply freezing objects in JavaScript.
https://github.com/dogma-io/freezly
deep freeze immutable javascript js object utility
Last synced: 6 months ago
JSON representation
Small utility for deeply freezing objects in JavaScript.
- Host: GitHub
- URL: https://github.com/dogma-io/freezly
- Owner: dogma-io
- License: mit
- Created: 2017-11-08T14:07:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:50:42.000Z (almost 3 years ago)
- Last Synced: 2025-03-29T07:51:26.898Z (6 months ago)
- Topics: deep, freeze, immutable, javascript, js, object, utility
- Language: JavaScript
- Homepage:
- Size: 1.52 MB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# freezly [![NPM][npm-img]][npm-url] [![Coverage][cov-img]][cov-url]
Small utility for deeply freezing objects in JavaScript.
## Table of Contents
* [Installation](#installation)
* [Documentation](#documentation)
* [Code of Conduct](#code-of-conduct)
* [Contributing](#contributing)
* [License](#license)## Installation
**npm**
```bash
npm install freezly
```**yarn**
```bash
yarn add freezly
```## Documentation
This project literally exports a single function which is used to deeply freeze any Javascript object. All nested arrays and objects become frozen, ensuring the entire object can't be mutated. This is primarily useful in functional programming, if you want to guarantee an object passed into a function isn't mutated by said function. However in practice deeply freezing objects comes with some amount of a performance cost so it is recommended to only deep freeze objects in a development environment and/or tests.
Below is an example of how to use **freezly**.
```js
import deepFreeze from 'freezly'const frozenObject = deepFreeze({
foo: ['bar'],
})// The following will throw an error as you can't mutate frozen objects
frozenObject.baz = 'spam' // Can't add properties to frozen object
frozenObject.foo.push('baz') // Can't push items to frozen array
```> Note: Once you freeze an object any attempts to mutate any part of the object will result in an error being thrown.
## Code of Conduct
Please see the [code of conduct](CODE_OF_CONDUCT.md).
## Contributing
Please see the [contributing guide](CONTRIBUTING.md).
## License
[MIT](LICENSE.md)
[cov-img]: https://img.shields.io/codecov/c/github/dogma-io/freezly.svg "Code Coverage"
[cov-url]: https://codecov.io/gh/dogma-io/freezly[npm-img]: https://img.shields.io/npm/v/freezly.svg "NPM Version"
[npm-url]: https://www.npmjs.com/package/freezly