lazy-o
Painless chains that are lazy, functional, and fast.
var lazy = var foo = lazy 'map' x + 3 // Queue a call 'slice' 3 5 // Queue more calls consolelog // Use plain function // Apply calls on a value with '.run'foo// => [ 7, 8 ]
A small utility for creating execution chains that are functional.
Note: If you are creating JS APIs with this, you want to only expose .run
to the user. So they can call it like a normal function (foo()
vs foo.run()
), and also to prevent them putting extra calls on the lazy stack.
Installation
$ npm install --save lazy-o
API
lazy(method, [...args])
Queue a method to run on the value.
name
(String
|Token
): The operation you want to queue.args
: Arguments to supply to the method during execution.
Returns self, so you can chain more methods.
var foo = lazy 'map' x + 1 'slice' 1run // => [3, 4]
lazy(fn)
Queue a plain function to run, the return replaces value
.
fn
(Function
): A function that is run with thevalue
Returns self, so you can chain more methods.
var foo = run // => 11
lazy.run(value)
lazy.run(...values)
Run all the queued calls on value
or values
.
Returns the resulting a modified value or array of values after all the calls have been applied.
Example with a single value:
var foo = run // => [0, 1, 2, 3, 6, 7, 8, 9]
Multiple values:
var bar = 'toUpperCase'run bar// => ['OOBA', 'AZQU', 'ELLO WORL']
Meta
npm test
: Run tests.npm run bench
: Run benchmarks.
License
MIT © Jamen Marz