Skip to content
/ Lazy Public

SlimIO Little lib to set Lazy Properties on JavaScript Objects!

License

Notifications You must be signed in to change notification settings

SlimIO/Lazy

Repository files navigation

Lazy

Version Maintenance MIT size dep Known Vulnerabilities Build Status

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).

Requirements

Getting Started

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

Usage example

// 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

API

defineProperty(target, propertyName, lazyFunctionValue): void

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
}

lazy.of< T >(target: T): LazyClojure< T >

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;

Dependencies

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

License

MIT

About

SlimIO Little lib to set Lazy Properties on JavaScript Objects!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published