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

pipx

v1.0.1

Published

Provide pipe function for js/node. Handles async progress, exception throwing, conditional call, branching

Downloads

55

Readme

Pipe (Functional Programming)

P(propertyName)

Get object property

const obj = { name: "Peter", address: { street: "123" } };

const getName = P("name");
const getStreet = P("address", "street");

console.log(getName(obj)); // => Peter
console.log(getStreet(obj)); // => 123

P.invoke(methodNameOrFunction, ...args)

Invoke object method without parameters

const obj = { getName: () => "Peter" };

const getName = P("getName()");

console.log(getName(obj)); // => Peter

Invoke object method with specified parameters

const obj = { getPhone: type => (type === "home" ? "1234" : "4321") };

const getHomePhone = P.invoke("getPhone", "home");
const getWorkingPhone = P.invoke("getPhone", "working");

console.log(getHomePhone(obj)); // => 1234
console.log(getWorkingPhone(obj)); // => 4321

Invoke object method with specified parameters and placeholder

const wrapMe = P.invoke(console.log, "Before", P, "After");

console.log(wrapMe("Hello World")); // => Before Hello World After

P.if(condition, thenHandler, elseHandler)

Single Conditional

import P from "pipx";

const isOdd = x => x % 2;

const left = x => `${x} is odd`;
const right = x => `${x} is even`;

const fn = P.if(isOdd, left, right);

console.log(fn(2)); // returns 2 is even
console.log(fn(1)); // returns 1 is odd

P.when(conditions[], unlessHandler)

Multiple Conditionals

const waterWithTemperature = P.when(
  [
    [P.is("equalTo", 0), P.always("water freezes at 0°C")],
    [P.is("equalTo", 100), P.always("water boils at 100°C")]
    /*
    // other method
    [
        P.case(P.is('equalTo', 0), P.always('water freezes at 0°C')),
        P.case(P.is('equalTo', 100), P.always('water boils at 100°C'))
    ],
     */
  ],
  temperature => `nothing special happens at ${temperature}°C`
);

print(waterWithTemperature(0));
print(waterWithTemperature(50));
print(waterWithTemperature(100));

P.always(value)

Returns a function that always returns the given value

const t = R.always("Tee");
t(); //=> 'Tee'

// There are some shorthands:
P.true; // => true
P.null; // => null
P.undefined; // => undefined
P.empty; // => ''
P.zero; // => 0

P.is(operator, value)

Compare value

const equalToZero = P.is("equalTo", 0); // or P.is('=', 0)
console.log(equalToZero(0)); // => true
console.log(equalToZero(1)); // => false

There are some various comparisons:

P.is('notEqualTo', value)

P.is('!=', value)

P.is('greaterThan', value)

P.is('>', value)

P.is('lessThan', value)

P.is('<', value)

P.is('greaterOrEqualTo', value)

P.is('>=', value)

P.is('lessOrEqualTo', value)

P.is('<=', value)

P.is('null')

P.is('undefined')

P.is('nil') null or undefined

P.is('empty')

P.is('zero')

P.await(funcReturnsPromise, failHandler)

const login = P(
  P.await(
    submitCredential,
    // handle promise fail
    P.invoke(
      console.error,
      "User name or pass does not match. The exception is: ",
      P
    )
  ),
  action1,
  action2
);

login({ user: "admin", pass: "admin" });

P.and(...conditions)

Returns true if all results are true.

const userNameMustHaveAtLeastSixCharacters = username =>
  username && username.length >= 6;
const usernameMustNotContainSpecialCharacters = username =>
  /^\w+$/.test(username);
const registerUser = P(
  P.and(
    userNameMustHaveAtLeastSixCharacters,
    usernameMustNotContainSpecialCharacters
  ),
  P.if(
    null,
    () => console.log("User registered"),
    () => console.log("Username is not valid")
  )
);

/* method 2
  const registerUser = P.if(
     P.and(
       userNameMustHaveAtLeastSixCharacters,
       usernameMustNotContainSpecialCharacters
     ),
     () => console.log('User registered'),
     () => console.log('Username is not valid')
   );
*/

registerUser("test");
registerUser("validuser");
registerUser("invalid$%");

There are some various of logical functions

P.every(...conditions) ~ P.and

p.some(...conditions) ~ P.or

P.not or P.not(condition)

P.catch(handler)

const doSomethingWrongIf = flag => {
  if (!flag) {
    throw new Error('Invalid operation');
  }
  return flag;
};

const doSomething = P(P.catch(console.error), doSomethingWrongIf, value =>
  console.log(`Result is ${value}`)
);

doSomething(true); // => Result is true
doSomething(false); // Invalid operation exception thrown