SlimIO package to achieve Lazy evaluation on JavaScript Objects! It use getter/setter to evaluate a function which return the final value at runtime (only when the property is requested).
- Node.js v12 or higher
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @slimio/lazy
# or
$ yarn add @slimio/lazy
// lp stand for Lazy Proxy
const lp = Lazy.of({});
lp.set("foo", () => "bar");
lp.set("hello", () => {
// Do job here
});
module.exports = lp.value;
Under the hood the lib use the ECMAScript Reflection API to ensure that the property is set (else it will throw an Error).
const obj = Object.freeze({});
Lazy.defineProperty(obj, "foo", () => "bar"); // throw Error
Define a new lazy property with a given name on target. Similar to Object.defineProperty.
The property descriptors will be defined as follow:
{
"enumerable": true,
"writable": false
}
Create a lazy clojure described by the following interface:
interface LazyClojure<T> {
set(propertyName: string, lazyFunctionValue: lazyHandler): void;
value: T;
}
The set method is a mirror of the root defineProperty method.
const lp = Lazy.of({});
lp.set("foo", () => "bar");
const obj = lp.value;
Name | Refactoring | Security Risk | Usage |
---|---|---|---|
@slimio/is | Minor | Low | Type checker |
MIT