Skip to content

Latest commit

 

History

History
120 lines (78 loc) · 2.92 KB

README.md

File metadata and controls

120 lines (78 loc) · 2.92 KB

lazymacro

macros for javascript

tutorial

  1. install

    npm install lazymacro
  2. require lazymacro in your source code

    require('lazymacro');
  3. 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) returns F(O)

    "OK".PIPE(v => v + "!") == "OK!"
  • WITH: O.WITH(F) runs F(O) and returns O

    ["O", "K"].WITH(v => v.push("!")).PIPE(v => v.join('')) == 'OK!'
    "OK".WITH(v => v + "!") == "OK!"
  • THEN: O.THEN(F) returns O.then(F)

    await Promise.resolve(5).THEN(v => v + 1) === 6
  • WAIT: O.WAIT(F) awaits O.then(F) returns O

    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 of F(o, k) where o and k are each element of O

    ["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 if O is a Promise and O.then(F) is an empty promise
  • THENis chosen if O is a Promise and O.then(F) is not an empty promise
  • WITHis chosen if O is not a Promise and F(O) is an empty promise
  • PIPEis chosen if O is not a Promise and F(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!'
  • 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"