npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@keepzen/pipe.js

v2.1.0

Published

pipeline for Js

Downloads

4

Readme

img img img

Pipe

Make a pipable

Pipable is a object, which have a method named .pipe(fun,...args).

You can get a new pipable object as fellow ways:

const {
  Pipe,
  pipe,
  from,
  _
} = require('./pipe.js')

test('make pipable', () => {
  const properties = ['valueOf', 'pipe', 'map', "promise_rescue"].sort();
  const keys = Object.keys.bind(Object);
  expect(keys(Pipe(1)).sort()).toMatchObject(properties);
  expect(keys(pipe(1)).sort()).toMatchObject(properties);
  expect(keys(from(1)).sort()).toMatchObject(properties);
  expect(keys(from(1).pipe(a => a + 1)).sort()).toMatchObject(properties);
})

As then snippet showed, a pipable object have methods as fellow:

  • pipable.valueOf() :

    Get the v you want to pipe to other function.

    So pipe(a).valueOf() === a is always true:

    test('pipe(any).valueOf() == any', () => { expect(pipe(1).valueOf() == 1).toBe(true) expect(pipe("hello").valueOf() == "hello").toBe(true); expect(pipe(true).valueOf() == true).toBe(true); expect(pipe(false).valueOf() == false).toBe(true); let str = new String('Hello'); expect(pipe(str).valueOf() == str).toBe(true); expect(pipe(null).valueOf() == null).toBe(true); expect(pipe(undefined).valueOf() == undefined).toBe(true); let a = { hello: "a" }; expect(pipe(a).valueOf() == a).toBe(true) })

If a is primary type but neither null nor undefined then pipe(a) == a is true.

test('pipe(primary) == primary', () => {
  expect(pipe(1) == 1).toBe(true)
  expect(pipe("hello") == "hello").toBe(true);
  expect(pipe(true) == true).toBe(true);
  expect(pipe(false) == false).toBe(true);
  let str = new String('Hello')
  expect(pipe(str) == str).toBe(false);
  expect(pipe(null) == null).toBe(false);
  expect(pipe(undefined) == undefined).toBe(false);
  let a = { hello: "a" };
  expect(pipe(a) == a).toBe(false);
})
  • pipable.pipe(fun,...args) : Return a pipable object.

    fun is a function, args are the rest arguments you want pipe to fun.

    • If args do not included placeholder _, fun will be called as fun(...[v,...args]).

    • If args included one and only one placeholder _, pipable.valueOf() will instead of the placehoder, and fun will be called as fun(...args).

  • pipable.map(fun,...args) : pipable.map is the alias of pipable.pipe.

  • pipable.promise_rescue(fun,...args): return a new pipable object.

    • when the pipable.vlaueOf() is a promise and the promise is reject, the reject reason will pipe to fun.
    • when pipeable.valueOf() is not a promise, fun will been skiped and return copy of pipable.

Use placeholder set which argument of fun take the piped value

test('pipe with placeholder', () => {
  let fn = jest.fn((a, b, c) => a + b + c);
  let z = pipe('a').pipe(fn, 'b', 'c');
  expect(fn).not.toHaveBeenCalled();
  expect(z == 'abc').toBe(true);
  expect(fn).toBeCalledWith('a', 'b', 'c');
  z = pipe("2").pipe(fn, "1", _, "3");
  expect(z == '123').toBe(true);
  expect(fn).toBeCalledWith('1', '2', '3');
  z = pipe('e').pipe(fn, 'a', 'b', 'c', 'd', _);
  expect(z == 'abc');
  expect(fn).toHaveBeenLastCalledWith('a', 'b', 'c', 'd', 'e');
})

pipable.pipe(fun,...args) keep pipable object immutable.

pipable.pipe(fn,...args) will return a new pipable object,don't change the old one.

test('pipable is independ', () => {
  let a = 1;
  let fn1 = jest.fn(a => a + 1);
  let fn2 = jest.fn((b, c) => b + c);
  let pipable0 = pipe(a);
  let pipable1 = pipable0.pipe(fn1);
  expect(pipable1 == a + 1).toBe(true);
  expect(pipable0.valueOf()).toBe(a);
  let pipable2 = pipable1.pipe(fn2, 'c');
  expect(pipable2 == '2c').toBe(true);
  expect(pipable1.valueOf()).toBe(a + 1);
})

pipable.pipe(fn,...args) is lazy

Call pipeable.pipe(fn,...args) do not call fn, but pipable.valueOf() will call the fn if there have one. As fellow show:

test(`pipable is layze`, () => {
  const fn = jest.fn(a => a + 1);
  let pipable0 = pipe(1);
  let ret = pipable0.pipe(fn).pipe(fn).pipe(fn);
  expect(fn).not.toHaveBeenCalled();
  expect(ret.valueOf()).toBe(1 + 1 + 1 + 1);
  expect(fn.mock.calls).toMatchObject([[1], [2], [3]]);
})

pipable<Promise>.pipe(fn,...args)

If the value of a pipable is a Promise, pipable.pipe(fn) pipe the resolve value of the promise to fn, and return a new promise pipable.

Examaples

test('pipe promise resolve ', (done) => {
  let a = from(Promise.resolve(1));
  expect(a.valueOf()).toBeInstanceOf(Promise);
  let fn = jest.fn(a => a + 1);
  let ret = a.pipe(fn).
    promise_rescue(fn).//skip
    pipe(fn).valueOf();
  expect(ret).toBeInstanceOf(Promise);
  ret.then(() => {
    expect(fn.mock.calls).toMatchObject([[1], [2]]);
    done();
  });
})

pipable<Promise>.promise_rescue:

Return a new pipable.

If value of pipable<Promise> is reject, this method can rescue from the error.

test('pipe promise reject', (done) => {
  let a = from(Promise.reject('hello'));
  let fn1 = jest.fn(a => 'fn1');
  let fn2 = jest.fn(a => 'fn2');
  let fn3 = jest.fn(a => "fn3");
  let promise = a.pipe(fn1).//skip
    promise_rescue(fn2).//call
    pipe(fn3).valueOf();
  expect(promise).toBeInstanceOf(Promise);

  promise.then(
    (a) => {
      expect(fn1).not.toHaveBeenCalled();
      expect(fn2).toHaveBeenCalledWith('hello');
      expect(fn3).toHaveBeenCalledWith('fn2');
      expect(a).toBe('fn3');
      done();
    },
    (error) => {
      console.error(error);
      expect('not run to here').toBe('but it is here');
    }
  )
})

pipable.inspect(fn, ...args)

Return a new pipable.

Run then function fn, ignor the return value of fn, so the value of the pipable will keep not change.

The purpose of this method is helpfully debuging the pipeable chain.