Package Exports
- constconst
- constconst/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (constconst) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ConstConst
ConstConst is a JavaScript package that provides two functions, freeze and deepFreeze, which allow you to freeze objects and prevent any mutation. These functions return a proxy that throws an error when attempting to modify or delete any property. The deepFreeze function handles circular references properly, ensuring that all nested objects are frozen.
New: fakeFreeze and fakeDeepFreeze
Unveiling an exciting new update to the ConstConst – the introduction of fakeFreeze and fakeDeepFreeze functions. This expansion provides enhanced flexibility while adhering to the core principles of immutability with controlled modification.
These functions augment your toolkit for immutability-like behavior while allowing controlled modification access.
Motivation
The main motivation behind ConstConst is to simplify the usage of constant information within modules. For example, when working with game development, you may have modules that store constant stats for characters or other game entities. With ConstConst, you can export and use these constant objects without the need for deep cloning them every time.
By freezing the objects, you ensure that their values remain unchanged throughout the execution of your code. This provides immutability and prevents accidental modifications that could lead to bugs or unexpected behavior.
Installation
You can install ConstConst using npm:
npm install constconstUsage
freeze: returns a proxy which throws error when trying to mutate
const { freeze } = require('constconst');
const myObject = {
prop1: 'value1',
prop2: 'value2'
};
const frozenObject = freeze(myObject);
frozenObject.prop1 = 'new value'; // ConstConstError: Cannot set property 'prop1' to value 'new value' since object is a constconstdeepFreeze: similar to freeze but recursively freezes nested objects
const { deepFreeze } = require('constconst');
const obj1 = {
prop1: 'value'
};
const obj2 = {
prop2: obj1
};
obj1.prop2 = obj2;
const frozenObject = deepFreeze(obj1);
frozenObject.prop2.prop1 = 'new value'; // ConstConstError: Cannot set property 'prop1' to value 'new value' since object is a constconst[!WARNING]
deepFreezefunction operates by modifying the fields of the passed object, replacing them with proxies to enforce immutability. To ensure the safest usage of this function, it's recommended to directly freeze the object without saving any reference to it in a variable.
[!NOTE]
- Any object which was frozen before passing to
freezeordeepFreezewill be returned as is and will not be proxied either. Hence they will not produce any errors on mutation.- freezing objects with setters and getters is not tested and is undefined behaviour.
fakeFreeze: Seals an object; modifications to the original object reflect in the returned proxy object. Modifications via the proxy trigger an error.
const { fakeFreeze } = require('constconst');
const myObject = {
prop1: 'value1'
};
const frozenObject = fakeFreeze(myObject);
myObject.prop1 = 'value2';
console.log(frozenObject.prop1) // value2
frozenObject.prop1 = 'new value'; // ConstConstError: Cannot set property 'prop1' to value 'new value' since object is a constconstfakeDeepFreeze: Extends fakeFreeze capabilities to nested object also handles circular references. Object sealing remains consistent.
const { fakeDeepFreeze } = require('constconst');
const obj1 = {
prop1: {
subProp1: 'value'
}
};
const frozenObject = fakeDeepFreeze(obj1);
obj1.prop1.subProp1 = "value2";
console.log(frozenObject.prop1.subProp1); // value2
frozenObject.prop1.subProp1 = 'new value'; // ConstConstError: Cannot set property 'prop1' to value 'new value' since object is a constconst[!NOTE]
fakeFreezeandfakeDeepFreezedo not replace the fields of the original object via proxies. The proxies returned by these functions are configured such that while accessing a property, a proxy of the property is returned.
Contribution Guidelines
To contribute to ConstConst, please follow these guidelines:
- Fork and clone the repository.
- Install project dependencies using
npm install. - Create a new branch for your contribution.
- Make your changes and ensure that tests pass (
npm test). - Add new tests if applicable.
- Use
npx changesetto create a changeset:- Select an appropriate bump type.
- Provide a summary (WHAT, WHY, HOW).
- Commit the changeset file and your code changes.
- Open a pull request against the
mainbranch.
For more information on using Changesets, refer to the Changesets documentation.