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

go-fns

v1.0.0

Published

Utility functions for functions.

Downloads

242

Readme

Go Functions

Utility functions for functions.

codecov.io Code Coverage jsdoc donation

  • version: 1.0.0
  • license: GNU LGPLv3

Installation

npm i go-fns

or

yarn add go-fns

Usage

ES6

import { not } from 'go-fns'

[1, 2, 3.1].filter(not(Number.isInteger)); // => [3.1]

Node

const { not } = require('go-fns');

[1, 2, 3.1].filter(not(Number.isInteger)); // => [3.1]

Web browser

<script src="dist/go-fns.min.js"></script>
<script>
    const { not } = Functions;

	[1, 2, 3.1].filter(not(Number.isInteger)); // => [3.1]
</script>

Documentation

Table of Contents

Utility

pipe

Creates a function that executes a series of functions in the order provided. First it passes the parameters to the first function and then the output is used as the input for the next function in the sequence, and so on until it reaches the end and finally it returns the output of the last function.

Parameters
  • fns ...Function The functions to execute sequentially.
Examples
pipe(Math.floor)(4.5); // => 4

pipe(Math.floor, Math.sqrt)(4.5); // => 2
  • Throws TypeError if any of the given values is not a function, null or undefined.

Returns Function The new function that sequentially executes the given functions.

Meta

  • since: 1.0.0

spread

Creates a function that takes an array of values and passes the values to the given function as individual parameters.

Parameters
  • fn Function The function to execute with individual parameters.
Examples
function sum(a, b) {
   return a + b;
}

spread(sum)([1, 2]); // => 3

Returns Function The new function that passes an array of values to the given function as individual parameters.

Meta

  • since: 1.0.0

attempt

Attempts to execute a function and returns the result or if there was an error, the error object caught.

Parameters
  • fn Function The function to attempt.
  • args ...any? The arguments for the function.
Examples
attempt(JSON.parse, "1"); // => 1

attempt(JSON.parse, ""); // => SyntaxError

Returns (any | Error) The result of executing the function or the error object caught if there was an error.

Meta

  • since: 1.0.0

repeat

Executes a function for a specified number of times and returns the results in a new array.

Parameters
  • fn Function The function to execute.
  • count number The number of times to execute. (optional, default 0)
  • args ...any? The arguments for the function.
Examples
repeat(Math.floor, 3, 1.5); // => [1, 1, 1]

Returns Array The new array containing the results.

Meta

  • since: 1.0.0

delay

Executes a function after a specified number of milliseconds with the arguments provided.

Parameters
  • fn Function The function to execute after a delay.
  • delay number The number of milliseconds to wait before executing the function. (optional, default 0)
  • args ...any? The arguments for the function.
Examples
delay(log, 1000, "hi"); // => prints "hi" after 1 second.

Returns number The timer id.

Meta

  • since: 1.0.0

Logic

Propositional logic connectives

and

Creates a function that tests if all the given functions return a truthy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.

Parameters
Examples
and(isString, isPrimitive)("abc"); // => true

and(isString, isPrimitive)(new String("abc")); // => false
  • Throws TypeError if any of the given values is not a function, null or undefined.

Returns Function The new function that tests if all the given functions return a truthy value.

Meta

  • since: 1.0.0

or

Creates a function that tests if any of the given functions return a truthy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.

Parameters
Examples
or(isString, isNumber)("abc"); // => true

or(isString, isNumber)(true); // => false
  • Throws TypeError if any of the given values is not a function, null or undefined.

Returns Function The new function that tests if any of the functions return a truthy value.

Meta

  • since: 1.0.0

xor

Creates a function that tests if exactly one of the given functions returns a truthy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.

Parameters
Examples
xor(isString, isNumber)("abc"); // => true

xor(isString, isNumber)(1);  // => true

xor(isString, isNumber)(true); // => false
  • Throws TypeError if any of the given values is not a function, null or undefined.

Returns Function The new function that tests if exactly one of the given functions return a truthy value.

Meta

  • since: 1.0.0

nor

Creates a function that tests if all the given functions return a falsy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.

Parameters
Examples
nor(isString, isNumber)(true); // => true

nor(isString, isNumber)("abc"); // => false
  • Throws TypeError if any of the given values is not a function, null or undefined.

Returns Function The new function that tests if all the given functions return a falsy value.

Meta

  • since: 1.0.0

not

Creates a function that negates the result of the given function. The parameters you pass when invoking the created function will be passed down to the given function with the this binding of the given function.

Parameters
Examples
not(isString)(1); // => true

not(isString)("abc"); // => false
  • Throws TypeError if the given value is not a function.

Returns Function The new function that negates the result of the given function.

Meta

  • since: 1.0.0