macros for javascript
-
install
npm install lazymacro
-
require
lazymacro
in your source coderequire('lazymacro');
-
enjoy the macros:
["I", "am"].WITH(v => v.push("lazynode.")).WITH(console.log).PIPE(v => v.join(" ")).PIPE(v => console.log(`${v}`))?.PIPE(v => console.log("null safety features can be used together!"));
the macros provided in lazymacro
-
PIPE
:O.PIPE(F)
returnsF(O)
"OK".PIPE(v => v + "!") == "OK!"
-
WITH
:O.WITH(F)
runsF(O)
and returnsO
["O", "K"].WITH(v => v.push("!")).PIPE(v => v.join('')) == 'OK!'
"OK".WITH(v => v + "!") == "OK!"
-
THEN
:O.THEN(F)
returnsO.then(F)
await Promise.resolve(5).THEN(v => v + 1) === 6
-
WAIT
:O.WAIT(F)
awaitsO.then(F)
returnsO
await Promise.resolve(["O", "K"]).WAIT(async v => await new Promise(resolve => setTimeout(() => resolve(v.push("!")), 1000))).THEN(v => v.join('')) == "OK!"
-
XMAP
:O.XMAP(F)
returns an array ofF(o, k)
whereo
andk
are each element ofO
["O", "K"].XMAP(v => v.toLowerCase()).PIPE(v => v.join('')) == "ok"
({ o: "O", k: "K" }).XMAP((v, k) => k + v.toLowerCase()).PIPE(v => v.join('')) == "ookk"
LAZY
will automatically choose one from PIPE
, WITH
, THEN
and WAIT
for you
for O.LAZY(F)
:
WAIT
is chosen ifO
is aPromise
andO.then(F)
is an empty promiseTHEN
is chosen ifO
is aPromise
andO.then(F)
is not an empty promiseWITH
is chosen ifO
is not aPromise
andF(O)
is an empty promisePIPE
is chosen ifO
is not aPromise
andF(O)
is not an empty promise
example:
["I", "am"].LAZY(v => { v.push("lazynode.") }).LAZY(v => { console.log(v) }).LAZY(v => v.join(" ")).LAZY(v => { console.log(`${v}`) })?.LAZY(v => console.log("null safety features can be used together!"));
same as the related basic macro but use this
instead of the function paramter
arrow fuction expressions cannot be used as parameter of this macros because this
is lost in arrow function expressions
-
PIPETHIS
"OK".PIPETHIS(function () { return this.toLowerCase() }) == "ok"
-
WITHTHIS
["O", "K"].WITHTHIS(function () { this.push("!") }).PIPE(v => v.join('')) == 'OK!'
-
THENTHIS
await Promise.resolve("OK").THENTHIS(function () { return this.toLowerCase() }) == "ok"
-
WAITTHIS
await Promise.resolve(["O", "K"]).WAITTHIS(async function () { await new Promise(resolve => setTimeout(resolve, 1000)); this.push("!") }).THEN(v => v.join('')) == "OK!"
-
XMAPTHIS
["O", "K"].XMAPTHIS(function () { return this.toLowerCase() }).PIPE(v => v.join('')) == "ok"