funnl

1.0.1 • Public • Published

funnl

The main purpose of this package is to have a utility which can pipe data easily and flexibly.

To build

Clone the repo, then run npm install or yarn init

Code examples

// Import
const funnl = require('funnl');
 
/* OR w/ ES6 */
 
import funnel from 'funnl'
 
 
 
// Two ways:
 
// 1.
// First is to use callback functions
const res = funnl("Hello")(
    (hello) => `${hello} World!`,
    (message) => {
      return { message }
    }
  );
console.log(res); // { message: "Hello World!" }
...
 
// 2.
// Second, define, and then pass functions
// Notice, when a function has more than one argument,
// we pass an array with the first element being the function.
// The result of the first element in the pipe is then passed
// as the first arg of the next function call, then
// the rest of the args get passed along, so the eventual
// function call looks like:
//   add(2, 8)
// given the example below.
const add(a, b) => a + b;
const res = funnl(2)(
    [add, 8]
  );
console.log(res) // 10

async/await

funnl also supports async/await functionality.

To calculate async values, you can declare async/await in the chain, or inside a function.

Example of async/await in a function:

 
funnl(10)(
  async (value) => {
    await setTimeout(() => {}, 200); // Pause for 200ms to mock network call
    return { users: value };
  },
  (data) => data.then(({ users }) => console.log(`There are ${ users } users online!`))
); // There are 10 users online!
 
// OR
 
funnl(100)(
  async (value) => {
    await setTimeout(() => {}, 200); // Pause for 200ms to mock network call
    return { users: value };
  },
  async (data) => {
    const { users } = await data;
    console.log(`There are ${ users } users online!`)
  }
); // There are 100 users online!
 
// OR
 
const add = (...args) => args.reduce((acc, curr) => acc + curr, 0);
const calc = async (num) => await funnl(num)(
  [add, 1, 2, 3, 4],
  (res) => res * 2,
  (res) => ({ data: res })
)
 
calc(10).then(r => console.log(r)) // { data: 40 }

Readme

Keywords

none

Package Sidebar

Install

npm i funnl

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

146 kB

Total Files

7

Last publish

Collaborators

  • brent-soles