@joookiwi/lazy v1.7.0
Lazy (javascript version)
Table of content
This project is an implementation based on the Kotlin Lazy. All of this with additional features to help the reusing of some values.
Installation
npm install @joookiwi/lazy
npm i @joookiwi/lazy
npm install --save @joookiwi/lazy
npm i -S @joookiwi/lazy
npm install --save-dev @joookiwi/lazy
npm i -D @joookiwi/lazyUsage
The usage is similar to the kotlin usage like val value by lazy { someValue } in kotlin
as well as the non-lazy implementation like val value = lazyOf(someValue).
But since there is no significant dependency toward this specifically. Here is an implementation of the lazy via 2 functions
lazyto load the value laterlazyOfto have aLazybut with an already loaded value
Lazy value
Using the lazy value will always return a Lazy object holding a value.
import {lazy} from "@joookiwi/lazy"
const firstValue = lazy(() => 2)
firstValue.isInitialized // false
firstValue.value // 2
firstValue.isInitialized // trueNon-lazy value
If the value is already known, then just use lazyOf() in order to have a Lazy with an already loaded value.
import {lazyOf} from "@joookiwi/lazy"
const firstValue = lazyOf(2)
firstValue.isInitialized // true
firstValue.value // 2
firstValue.isInitialized // trueSpecialized non-lazy value
There are some specialized lazyOf()-like Lazy to reuse the CommonLazy values depending on what it has received:
booleanLazyOffor thebooleannumberLazyOffor thenumberbigintLazyOffor thebigintdateLazyOffor theDate
Common lazy
When using the lazy methods, it always creates a new instance.
But, there could be some cases where an already known value is there.
Here is the full list of the common Lazy instances:
| Value | With string | With character | With BigInt |
|---|---|---|---|
null | yes | ||
undefined | yes | ||
true / false | yes | ||
-1 / 0 / 1 / 2 | yes | yes | yes |
NaN / ∞ (infinity) / -∞ (-infinity) | yes | ||
ln(2) / ln(10) | |||
log₂(E) / log₁₀(E) | |||
E, π (pi), τ (tau) | |||
/ \t / \n | yes | ||
| epoch date | |||
| invalid date | yes | ||
empty String → '' | |||
empty object → Readonly<{}> | |||
empty Array → readonly [] | |||
empty Set → ReadonlySet<never> | |||
empty WeakSet → Readonly<WeakSet<WeakKey>> | |||
empty Map → ReadonlyMap<unknown, never> | |||
empty WeakMap → Readonly<WeakMap<WeakKey, never>> |
Contribution
You can contribute to great simple packages. All with similar behaviour across different languages (like Java, Kotlin, C# and PHP). It can be done 2 different ways: