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

fjp

v0.2.4

Published

functional javascript playground

Downloads

1,721

Readme

Functional Javascript Playground

Maintainability Build Status Code Coverage NSP Status Last Commit PRs Welcome

Functional Programming was always a weird "why would you use this" way of coding as I've primarily been OOP. I set out to change my view on that and learn more about it and when to use it. These are some of the functions I collected/made along the way. They are all ES6/ESM, the parameter order for most lends itself to currying + composing. Still a little iffy on the Hindley-milner signatures so they may be slightly off.

This is very much a WIP

Functions

associate()

Sets the given property and value on the object. Returning a new object.

Kind: global function
Signature: associate :: String k -> {} -> v -> {k: v}
Example

const obj = associate('c', { a: b }, d)  // { a: b, c: d }

average()

Averages the given array values

Kind: global function
Signature: average :: [Number] -> Number
Example

average([ 1, 2, 3 ]); // 2
average(1, 2, 3); // 2
average(); // 0

A()

Calls the given function with the given value.

Kind: global function
Signature: A :: (a -> b) -> a -> b
Aka: apply

Fork()

Takes a joiner func, and two other funcs and a value. The value is given to both funcs and the results of each of these is given to the joiner func.

Kind: global function
Signature: Fork :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
Aka: join

I()

Returns the given value.

Kind: global function
Signature: I :: a -> a
Aka: identity

K()

Takes two values and returns the given first.

Kind: global function
Signature: K :: a -> b -> a
Aka: constant

OR()

Given two functions that take the same value, returns the first if the result is truthy, otherwise, the second.

Kind: global function
Signature: OR :: (a -> b) -> (a -> b) -> b
Aka: alternation

T()

Calls the given function with the given value. (Reverse order of apply)

Kind: global function
Signature: T :: a -> (a -> b) -> b
Aka: thrush, applyTo

compact()

Removes falsey values from an array.

Kind: global function
Signature: compact :: [a] -> [a]
Example

compact([ 0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34 ]); // [ 1, 2, 3, 'a', 's', 34 ]
compact(null) // []

compose()

Performs right-to-left function composition.

Kind: global function
Signature: compose :: [(m -> n), ..., (b -> c), (a -> b)] -> a -> n
Example

const addOne = x => x + 1;
const timeTen = x => x * 10;
const addOneTimeTen = compose(timeTen, addOne);
const result = addOneTimeTen(9); // 100

concat()

Concatenates two String|Arrays together. Returns empty array if value arent of the same type or not String|Array.

Kind: global function
Signature: concat :: a -> b -> c
Example

concat('foo', 'bar') // foobar
concat([1, 2], [3, 4]) // [1, 2, 3, 4]

concatN()

Concatenates N Arrays together.

Kind: global function
Signature: concat :: [a] -> [b] -> ...[n] -> [m]
Example

concatN([1, 2], [3, 4], [5, 6]) // [1, 2, 3, 4, 5, 6]

curry()

Wraps the given function, if the number of provided args is sufficient, call the passed function fn. Otherwise, return a wrapped function fn that expects the rest of the arguments. If you want to curry a function that accepts a variable number of arguments (a variadic function), you can optionally pass the number of arguments to the second parameter arity.

Kind: global function
Signature: curry :: ((a, b, ..., n) -> m) -> a -> b -> ...n -> m
Example

const add = curry((x, y) => x + y);
const addFiveTo = add(5);
addFiveTo(10); // 15

difference()

Returns the difference between two arrays.

Kind: global function
Signature: difference :: [a] -> [b] -> [c]
Example

difference([ 1, 2, 3 ], [ 1, 2, 4 ]) // [3]
difference([], [ 1, 2, 4 ]) // [ 1, 2, 4 ]
difference([ 1, 2, 3 ], []) // [ 1, 2, 3 ]
difference([ 1, 2, 3 ], null) // [ 1, 2, 3 ]

distinct()

Returns all of the distinct values of an array.

Kind: global function
Signature: distinct :: [a] -> [b]
Example

distinct([ 1, 2, 2, 3, 4, 4, 5 ]) // [ 1, 2, 3, 4, 5 ]

distinctN()

Returns all of the distinct values of the given arrays.

Kind: global function
Signature: distinctN :: [a] -> [b] -> ...[n] -> [m]
Example

distinctN([ 1, 2 ], [ 2, 3, 4 ], [ 4, 5 ]) // [ 1, 2, 3, 4, 5 ]

each()

Applies the given func to each element in the array.

Kind: global function
Signature: each :: (a -> b) -> [c] -> undefined
Example

difference(log, [1, 2, 3])

every()

Determines if all element in an array satisfy the given test function

Kind: global function
Signature: every :: (a -> Bool) -> [a] -> Bool
Aka: all
Example

every(Boolean, [1, 2, 3, 4]) // true
every(Boolean, [1, 2, null, 4]) // false

filter()

Filters the array using the given function.

Kind: global function
Signature: filter :: (a -> Boolean) -> [a] -> [a]
Example

filter(x => x > 5, [1, 2, 3, 5, 6, 7]) // [6, 7]

find()

Finds the first element that satisfies the given test func.

Kind: global function
Signature: find :: (a -> Boolean) -> [a] -> a
Example

find(x => x.score === 5, [{score: 1}, {score: 2}, {score: 5}, {score: 6}, {score: 7}]) // {score: 5}

flatten()

Flattens single nested array.

Kind: global function
Signature: flatten :: [a] -> [a]
Example

flatten([[ 1, 2 ], [ 3, 4 ]];); // [ 1, 2, 3, 4 ]
flatten(null) // []

isArray()

Determines if the given value is an array.

Kind: global function
Signature: isArray :: a -> Boolean
Example

isArray([1, 2, 3])  // true
isArray({ a: 'b' })  // false

isFunction()

Determines if the given value is a function.

Kind: global function
Signature: isFunction :: a -> Boolean
Example

isFunction(() => {})  // true
isFunction([1, 2, 3])  // false

isNumber()

Determines if the given value is a number.

Kind: global function
Signature: isNumber :: a -> Boolean
Example

isNumber(42)  // true
isNumber(8e5)  // true
isNumber(0x2F)  // true
isNumber('foo bar')  // false

isObject()

Determines if the given value is an object.

Kind: global function
Signature: isObject :: a -> Boolean
Example

isObject({ a: 'b' })  // true
isObject([1, 2, 3])  // false

isString()

Determines if the given value is a string.

Kind: global function
Signature: isString :: a -> Boolean
Example

isString('foo bar')  // true
isString({ a: 'b' })  // false

not()

Negates the given boolean-like value.

Kind: global function
Signature: not :: Boolean -> Boolean
Example

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

tap()

Calls the given function with the given value and returns the value.

Kind: global function
Signature: tap :: (a -> b) -> a -> a
Example

tap(console.log, 'foobar') // foobar

License

FJP is licensed under the MIT license.

Copyright © 2018 Bryan Kizer