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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

62b8611 · May 27, 2020

History

76 Commits
Apr 7, 2019
Dec 20, 2018
Nov 1, 2018
Jul 18, 2019
Dec 20, 2018
Apr 7, 2019
Nov 1, 2018
May 27, 2020
Nov 1, 2018
Oct 31, 2018
May 13, 2020
Jul 18, 2019
Apr 7, 2019
Jul 18, 2019
May 31, 2019
May 27, 2020
Apr 7, 2019

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