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

@anthonypena/fp

v1.0.1

Published

Basic functional programming for TypeScript coders.

Downloads

24

Readme

@anthonypena/fp

Install

npm i @anthonypena/fp

Examples

Simple array filtering

import { isDefined, isDefinedAndNotEmpty, not } from '@anthonypena/fp';

const data = [
    { id: 'A', height: 10 },
    { id: 'B', name: 'B', height: 10 },
    { id: 'C', name: 'C', height: 12 },
    { id: 'C', name: '', height: 9 },
    null
];

function isTall(x: { height: number }) {
    return x.height > 10;
}

const res = data
    .filter(isDefined)
    .filter(not(isTall))
    .map(x => x.name)
    .filter(isDefinedAndNotEmpty);
// res = [ { id: 'B', name: 'B', height: 10 } ]

Docs

deepClone

This function will create a copy the passed value in most of the case.

Exception: undefined, null, number, boolean, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY

Parameters:

  • x: any value

Exemples:

deepClone(1);
deepClone(Number.NEGATIVE_INFINITY);
deepClone('A');
deepClone({ a: 1 });
deepClone({ a: { b: 'C' } });
deepClone(['A']);
deepClone([[[['A']]]]);

identity

This function will return the first parameter.

Parameters:

  • x: the first parameter

Exemples:

const res = identity(1); // res = 1

[1, 2, 3].map(identity); // [ 1, 2, 3 ]

isDifferent

This function compare two value of the same type.

Parameters:

  • a: one value
  • b: another value

Exemples:

isDifferent(1,1) // false
isDifferent(1,2) // true
isDifferent({ a: 'A' }, { a: 'A' }) // false
isDifferent({ a: 'A' }, { a: 'B' }) // true

Note: opposite of isEqual

isDefined

This function check passed value is defined with correct TypeScript type assertion.

Parameters:

  • x: one value which may be undefined or null

Exemples:

isDefined(1) // true
isDefined('A') // true
isDefined({ a: 'A' }) // true
isDefined([]) // true
isDefined(undefined) // false
isDefined(null) // false

isDefinedAndNotEmpty

This function check passed value is defined and is not empty with correct TypeScript type assertion.

Parameters:

  • x: one value which have a length and may be undefined or null

Exemples:

isDefinedAndNotEmpty('') // false
isDefinedAndNotEmpty('A') // true
isDefinedAndNotEmpty([]) // false
isDefinedAndNotEmpty(['A']) // true
isDefinedAndNotEmpty(undefined) // false
isDefinedAndNotEmpty(null) // false

isEmpty

This function check if passed value is empty with correct TypeScript type assertion.

Parameters:

  • x: one value which have a length

Exemples:

isEmpty([]) // true
isEmpty('') // true
isEmpty(['A']) // false
isEmpty('A') // false

isEqual

This function compare two value of the same type.

Parameters:

  • a: one value
  • b: another value

Exemples:

isEqual(1,1) // true
isEqual(1,2) // false
isEqual({ a: 'A' }, { a: 'A' }) // true
isEqual({ a: 'A' }, { a: 'B' }) // false

isNotDefined

This function check passed value is not defined with correct TypeScript type assertion.

Parameters:

  • x: one value which may be undefined or null

Exemples:

isNotDefined(1) // false
isNotDefined('A') // false
isNotDefined({ a: 'A' }) // false
isNotDefined([]) // false
isNotDefined(undefined) // true
isNotDefined(null) // true

Note: opposite of isDefined

isNotDefinedOrEmpty

This function check passed value is not defined or is empty with correct TypeScript type assertion.

Parameters:

  • x: one value which have a length and may be undefined or null

Exemples:

isNotDefinedOrEmpty('') // true
isNotDefinedOrEmpty('A') // false
isNotDefinedOrEmpty([]) // true
isNotDefinedOrEmpty(['A']) // false
isNotDefinedOrEmpty(undefined) // true
isNotDefinedOrEmpty(null) // true

isNotEmpty

This function check if passed value is not empty with correct TypeScript type assertion.

Parameters:

  • x: one value which have a length

Exemples:

isNotEmpty([]) // false
isNotEmpty('') // false
isNotEmpty(['A']) // true
isNotEmpty('A') // true

Note: opposite of isEmpty

noop

This function do nothing.

Exemples:

noop();
[1, 2, 3].forEach(noop);

not

This function will negate the boolean value in parameter.

Parameters:

  • x: a boolean value

Exemples:

not(true); // false
not(false); // true

notf

This function will negate the result of the function in parameter.

Parameters:

  • f: a function which return boolean

Exemples:

[1,2,3].map(notf(isEven)); // [true, false, true]
// or
const isOdd = notf(isEven);
[1,2,3].map(isOdd); // [true, false, true]