https://github.com/nathanpb/lazy-as-fuck
Extremely simple lazy properties for Javascript
https://github.com/nathanpb/lazy-as-fuck
js lazy node
Last synced: 17 days ago
JSON representation
Extremely simple lazy properties for Javascript
- Host: GitHub
- URL: https://github.com/nathanpb/lazy-as-fuck
- Owner: NathanPB
- License: mit
- Created: 2020-08-27T20:43:07.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T21:30:12.000Z (over 3 years ago)
- Last Synced: 2025-10-13T10:15:37.690Z (8 months ago)
- Topics: js, lazy, node
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/lazyasfk
- Size: 90.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Really simple and dependency-free lazy properties for Javascript
```js
const lazy = require('lazyasfk')
const lazyAsync = require('lazyasfk/async')
const property = lazy(() => 'how could I be more lazy?')
console.log(property.isPresent()) // > false
console.log(property.get()) // > how could I be more lazy?
console.log(property.isPresent()) // > true
property.forget()
console.log(property.isPresent()) // > false
property.forceCompute()
console.log(property.isPresent()) // > true
// Also async
const expensiveAndTimeConsumingHttpRequestOrSomethingIdk = async () => 'foobar'
const lazyProperty = lazyAsync(expensiveAndTimeConsumingHttpRequestOrSomethingIdk)
console.log(lazyProperty.isPresent()) // > false
console.log(lazyProperty.get()) // > obviously not foobar
console.log(lazyProperty.isPresent()) // > true
lazyProperty.forget()
console.log(lazyProperty.isPresent()) // > false
lazyProperty.forceCompute()
console.log(lazyProperty.isPresent()) // > true
// Also works with params
// NOTE THAT THIS IS NOT MEMOIZATION!!!!!!!!!!!!!!!!!!!!!!!!
// THE PARAMS ARE USED ONLY FOR THE EVALUATION WHEN THE VALUE IS NOT PRESENT
// CHANGING THE PARAMS OF A PRESENT PROPERTY WILL NOT CHANGE THE OUTPUT
const withParams = lazy((a, b) => a + b) // Works with lazyAsync too
console.log(withParams.get(77, 33)) // > 100
```
The function names are pretty self-explanatory, I don't need I'll need to write function references for this.