0.1.4 • Published 4 years ago
lazymacro v0.1.4
lazymacro
macros for javascript
tutorial
install
npm install lazymacrorequire
lazymacroin 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!"));
reference
the macros provided in lazymacro
basic macros
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) === 6WAIT:O.WAIT(F)awaitsO.then(F)returnsOawait 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)whereoandkare 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 macro
LAZY will automatically choose one from PIPE, WITH, THEN and WAIT for you
for O.LAZY(F):
WAITis chosen ifOis aPromiseandO.then(F)is an empty promiseTHENis chosen ifOis aPromiseandO.then(F)is not an empty promiseWITHis chosen ifOis not aPromiseandF(O)is an empty promisePIPEis chosen ifOis not aPromiseandF(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!"));this macros
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!'THENTHISawait Promise.resolve("OK").THENTHIS(function () { return this.toLowerCase() }) == "ok"WAITTHISawait 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"