Skip to content

Create objects that can be freezed and un-freezed

License

Notifications You must be signed in to change notification settings

Bellisario/IceCube

Repository files navigation

Ice Cube

Ice Cube

CI NPM Node.js Version License

Create objects that can be freezed and un-freezed

Installation

npm i @bellisario/icecube

Example

import { IceCube } from '@bellisario/icecube';
const obj = new IceCube({ test: true });

obj.test; // returns true
obj.test = false; // throws an error

obj.iced = false; // disables freezing
obj.test = false; // succeeds
obj.test; // false

Usage

new IceCube(obj, options?)

Creates a new instance of the IceCube class.
Returns an usable object that can be freezed/un-freezed.

obj

Type: object
The object to be freezed. Can also be an array.

options

Type: object
Required: false
Initialization options.

iced

Type: boolean
Default: true
If true, the object will be freezed when created.
If false, the object will not be freezed when created and can be edited.

Object returned

Type: object
The object that can be freezed/un-freezed.

Properties
  • iced: boolean
    Default: specified by the user iced option
    Controls whether or not the object is frozen.
    Edit this property to freeze/un-freeze the object.

Why?

Since I started learning JavaScript, I noticed that freezing objects will not allow you to un-freeze them later, this is very useful in certain situations, but some others you just want to prevent the user from editing the object now, but allow them to edit it later...
So I created this library to solve this problem.

How?

You can see on the source code of the library that I used Proxy to "watch" the object, then allow editing (or throw an error) by looking for the iced property (which is instead always editable).

Contributing

We ❤️ contributions!
Feel free to open an issue or a pull request but follow Contributing Guidelines.

Tip: if you don't know where to start, check out the help wanted issues!

License

MIT License here.


Ice Cube original image from vexels.com

About

Create objects that can be freezed and un-freezed

Resources

License

Stars

Watchers

Forks

Sponsor this project