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

tk-functional

v0.2.4

Published

Typescript/Javascript helpers for functional-style programming

Downloads

3

Readme

tk-functional

pipeline status coverage report npm version npm size

About

Provides some common helpers for functional-style programming.

Typescript definitions are included.

Issues can be reported on GitLab.

Functions

always and never return true and false respectively

always()  // => true
never()  // => false

and, or and not allow to combine or negate predicates

const a = and((x: number) => x > 2, (x: number) => x < 5);
a(2)  // => false
a(3)  // => true
a(4)  // => true
a(5)  // => false
const o = or((x: number) => x < 2, (x: number) => x > 3);
o(1)  // => true
o(2)  // => false
o(3)  // => false
o(4)  // => true
const n = not((x: number) => x == 1);
n(0)  // => true
n(1)  // => false
n(2)  // => true

at gets an array's item by position:

const getthird = at(2);
getthird([2, 4, 8, 16]);      // => 8
getthird([2, 4]);             // => undefined
const getsecondlast = at(-2);
getsecondlast([1, 2, 3, 4]);  // => 3
getsecondlast([1]);           // => undefined

attr gets an object's attribute:

const getx = attr("x");
getx({x: 3});  // => 3

bool checks for boolean equivalence (in a broader sense than !(!(val))):

bool(undefined);  // => false
bool(null);  // => false

bool(-1);  // => true
bool(0);  // => false
bool(1);  // => true

bool("");  // => false
bool(" ");  // => true
bool("abc");  // => true

bool([]);  // => false
bool([1, 2, 3]);  // => true

bool({});  // => false
bool({x: 1});  // => true

cmp simplifies the use of array sorting:

[8, 3, 5].sort(cmp())                            // => [3, 5, 8]
[8, 3, 5].sort(cmp({ reverse: true }))           // => [8, 5, 3]
[-2, 8, -7].sort(cmp({ key: Math.abs }))         // => [-2, -7, 8]

first and last return the first or last item of an array:

first([1, 2, 3])  // => 1
first([])         // => undefined
last([1, 2, 3])   // => 3
last([])          // => undefined

identity returns its argument untouched:

a === identity(a)  // => true

is and isinstance checks for strict equality and inheritance:

const f = is(8);
f(8)      // => true
f(5 + 3)  // => true
f("8")    // => false
f(null)   // => false

class A { }
class A1 extends A { }
class A2 extends A { }
class B { }
const f: any[] = [5, null, undefined, new A, new A1, new B, new A2];
const result: A[] = f.filter(isinstance(A));  // => [f[3], f[4], f[6]]

nn, nu and nnu checks at run-time for null or undefined:

nn(undefined)  // => undefined
nn(null)  // => Error
nn(1)  // => 1

nu(undefined)  // => Error
nu(null)  // => null
nu(1)  // => 1

nnu(undefined)  // => Error
nnu(null)  // => Error
nnu(1)  // => 1

nop does nothing (useful for some callbacks):

new ConstructorWithMandatoryCallback(nop)

partial applies a partial configuration object as first argument of compatible functions:

const sum = (args: {a: number, b: number}) => args.a + args.b
const plus1 = partial({a: 1}, sum);
plus1({b: 8})  // => 9

pipe chains two functions as one:

const f = pipe((x: number) => x * 2, (x: number) => x + 1);
f(3)  // => 7 ((3 * 2) + 1)