Skip to content

Latest commit

 

History

History
83 lines (65 loc) · 2.64 KB

README.md

File metadata and controls

83 lines (65 loc) · 2.64 KB

Immutable

version Maintenance MIT dep size Known Vulnerabilities Build Status

Group of useful functions that allow to create and manage Immutable and sealed properties and objects.

⚠️ Experimental Project (Please do not use in production)

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/immutable
# or
$ yarn add @slimio/immutable

Usage example

const { freezedProperty, seal } = require("@slimio/immutable");

class User {
    constructor(name, age) {
        // Produce readyonly properties
        freezedProperty(this, "name", name);
        freezedProperty(this, "age", age);
    }
}
User.properties = seal({
    name: "param as a string"
});
Object.preventExtensions(User);

const obj = new User("fraxken", 16);
console.log(obj.name);
console.log(obj.age);

API

seal< T extends object >(target: T): T

Same as Object.seal() but doesn't allow to cast the original property type:

const obj = Immutable.seal({
    foo: "bar"
});
obj.foo = "world"; // ok
obj.foo = 10; // Error: Unable to cast string to number for propertyKey foo
freezedProperty(target: object, propertyKey: symbol | string, value?: any): void

Setup a freezed property on a given target (Same behavior as Object.freeze but for all kind of values).

const obj = {};
Immutable.freezedProperty(obj, "foo", "bar");
console.log(obj.foo); // stdout bar
delete obj.foo; // Error

Dependencies

Name Refactoring Security Risk Usage
@slimio/is Minor Low Type checker

License

MIT