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

functools

v3.4.0

Published

Utilities for working with functions in JavaScript, with TypeScript

Downloads

86

Readme

Functools

NPM version NPM downloads Build status Test coverage

Utilities for working with functions in JavaScript, with TypeScript.

(Inspired by functools of the same name)

Installation

npm install functools --save

Usage

identity<T>(arg: T) => T

Always returns the same value supplied to it.

identity(42); //=> 42

always<T>(arg: T) => () => T

Returns a function that always returns the same value supplied to it.

identity(42); //=> 42

memoize<T, U>(fn: (x: T) => U, cache?: Cache) => (x: T) => U

Optimize a function to speed up consecutive calls by caching the result of calls with identical input arguments. The cache can be overridden for features such as an LRU cache.

let i = 0;
const fn = memoize(() => ++i);

fn("foo"); //=> 1
fn("foo"); //=> 1

fn("bar"); //=> 2
fn("bar"); //=> 2

memoize0<T>(fn: () => T): () => T

Memoize the result of fn after the first invocation.

let i = 0;
const fn = memoize0(() => ++i);

fn(); // => 1
fn(); // => 1
fn(); // => 1

memoizeOne<T, R>(fn: (...args: T) => R) => (...args: T) => R

Memoize the result of a function based on the most recent arguments.

let i = 0;
const fn = memoize(() => ++i);

fn("foo"); //=> 1
fn("foo"); //=> 1

fn("bar"); //=> 2
fn("bar"); //=> 2

fn("foo"); //=> 3
fn("foo"); //=> 3

prop<K>(key: K) => (obj: T) => T[K]

Return a function that fetches key from its operand.

prop("foo")({ foo: 123 }); //=> 123

invoke<K, A, T>(key: K, ...args: A) => (obj: T) => ReturnType<T[K]>

Return a function that calls the method name on its operand. If additional arguments are given, they will be given to the method as well.

invoke("add", 5, 5)({ add: (a, b) => a + b }); //=> 10

throttle<T>(fn: (...args: T) => void, ms: number, { leading, trailing, debounce }) => (...args: T) => void

Wrap a function to rate-limit the function executions to once every ms milliseconds.

let i = 0
const fn = throttle(() => ++i, 100)

fn() // i == 1
fn() // i == 1
fn() // i == 1

setTimeout(() => /* i == 2 */, 200)

Tip: Use fn.clear and fn.flush for finer execution control.

  • fn.clear Unconditionally clears the current timeout
  • fn.flush When fn is pending, executes fn() and starts a new interval

spread<T, R>(fn: (...args: T) => R) => (args: T) => R

Given a fn, return a wrapper that accepts an array of fn arguments.

Promise.all([1, 2, 3]).then(spread(add));

flip<T1, T2, R>(fn: (arg1: T1, arg2: T2) => R) => (arg2: T2, arg1: T1) => R

Flip a binary fn argument order.

flip(subtract)(5, 10); //=> 5

partial<T, U, R>(fn: (...args1: T, ...args2: U) => R) => (...args: U) => R

Returns a partially applied fn with the supplied arguments.

partial(subtract, 10)(5); //=> 5

sequence<T>(...fns: Array<(input: T) => T>) => (input: T) => T

Left-to-right function composition.

sequence(partial(add, 10), partial(multiply, 5))(5); //=> 75

compose<T>(...fns: Array<(input: T) => T>) => (input: T) => T

Right-to-left function composition.

compose(partial(add, 10), partial(multiply, 5))(5); //=> 35

nary<T, R>(n: number, fn: (...args: T) => R) => (...args: T) => R

Fix the number of receivable arguments in origFn to n.

["1", "2", "3"].map(nary(1, fn)); //=> [1, 2, 3]

TypeScript

This module uses TypeScript and publishes type definitions on NPM.

License

Apache 2.0