{"id":15674947,"url":"https://github.com/blakeembrey/js-functools","last_synced_at":"2025-09-14T14:35:40.457Z","repository":{"id":57243087,"uuid":"139103873","full_name":"blakeembrey/js-functools","owner":"blakeembrey","description":"Utilities for working with functions in JavaScript, with TypeScript","archived":false,"fork":false,"pushed_at":"2023-12-12T06:52:06.000Z","size":470,"stargazers_count":14,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-06T22:13:50.980Z","etag":null,"topics":["functools","operand","throttle","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blakeembrey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-29T05:16:12.000Z","updated_at":"2023-12-12T06:52:10.000Z","dependencies_parsed_at":"2024-06-19T13:33:13.900Z","dependency_job_id":"2db5edb8-e133-412f-83c7-fb6e82cf79f7","html_url":"https://github.com/blakeembrey/js-functools","commit_stats":{"total_commits":29,"total_committers":2,"mean_commits":14.5,"dds":0.06896551724137934,"last_synced_commit":"0f26a083c8445ba229a42d90a03e1f4bb5fa32e8"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blakeembrey%2Fjs-functools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blakeembrey%2Fjs-functools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blakeembrey%2Fjs-functools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blakeembrey%2Fjs-functools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blakeembrey","download_url":"https://codeload.github.com/blakeembrey/js-functools/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252776600,"owners_count":21802469,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functools","operand","throttle","typescript"],"created_at":"2024-10-03T15:53:32.876Z","updated_at":"2025-05-06T22:13:57.348Z","avatar_url":"https://github.com/blakeembrey.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Functools\n\n[![NPM version][npm-image]][npm-url]\n[![NPM downloads][downloads-image]][downloads-url]\n[![Build status][build-image]][build-url]\n[![Build coverage][coverage-image]][coverage-url]\n\n\u003e Utilities for working with functions in JavaScript, with TypeScript.\n\n_(Inspired by [functools](https://docs.python.org/2/library/functools.html) of the same name)_\n\n## Installation\n\n```\nnpm install functools --save\n```\n\n## Usage\n\n### `identity\u003cT\u003e(arg: T) =\u003e T`\n\nAlways returns the same value supplied to it.\n\n```js\nidentity(42); //=\u003e 42\n```\n\n### `always\u003cT\u003e(arg: T) =\u003e () =\u003e T`\n\nReturns a function that always returns the same value supplied to it.\n\n```js\nidentity(42); //=\u003e 42\n```\n\n### `memoize\u003cT, U\u003e(fn: (x: T) =\u003e U, cache?: Cache) =\u003e (x: T) =\u003e U`\n\nOptimize 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.\n\n```js\nlet i = 0;\nconst fn = memoize(() =\u003e ++i);\n\nfn(\"foo\"); //=\u003e 1\nfn(\"foo\"); //=\u003e 1\n\nfn(\"bar\"); //=\u003e 2\nfn(\"bar\"); //=\u003e 2\n```\n\n### `memoize0\u003cT\u003e(fn: () =\u003e T): () =\u003e T`\n\nMemoize the result of `fn` after the first invocation.\n\n```js\nlet i = 0;\nconst fn = memoize0(() =\u003e ++i);\n\nfn(); // =\u003e 1\nfn(); // =\u003e 1\nfn(); // =\u003e 1\n```\n\n### `memoizeOne\u003cT, R\u003e(fn: (...args: T) =\u003e R) =\u003e (...args: T) =\u003e R`\n\nMemoize the result of a function based on the most recent arguments.\n\n```js\nlet i = 0;\nconst fn = memoize(() =\u003e ++i);\n\nfn(\"foo\"); //=\u003e 1\nfn(\"foo\"); //=\u003e 1\n\nfn(\"bar\"); //=\u003e 2\nfn(\"bar\"); //=\u003e 2\n\nfn(\"foo\"); //=\u003e 3\nfn(\"foo\"); //=\u003e 3\n```\n\n### `prop\u003cK\u003e(key: K) =\u003e (obj: T) =\u003e T[K]`\n\nReturn a function that fetches `key` from its operand.\n\n```js\nprop(\"foo\")({ foo: 123 }); //=\u003e 123\n```\n\n### `invoke\u003cK, A, T\u003e(key: K, ...args: A) =\u003e (obj: T) =\u003e ReturnType\u003cT[K]\u003e`\n\nReturn a function that calls the method name on its operand. If additional arguments are given, they will be given to the method as well.\n\n```js\ninvoke(\"add\", 5, 5)({ add: (a, b) =\u003e a + b }); //=\u003e 10\n```\n\n### `throttle\u003cT\u003e(fn: (...args: T) =\u003e void, ms: number, { leading, trailing, debounce }) =\u003e (...args: T) =\u003e void`\n\nWrap a function to rate-limit the function executions to once every `ms` milliseconds.\n\n```js\nlet i = 0\nconst fn = throttle(() =\u003e ++i, 100)\n\nfn() // i == 1\nfn() // i == 1\nfn() // i == 1\n\nsetTimeout(() =\u003e /* i == 2 */, 200)\n```\n\n**Tip:** Use `fn.clear` and `fn.flush` for finer execution control.\n\n- `fn.clear` Unconditionally clears the current timeout\n- `fn.flush` When `fn` is pending, executes `fn()` and starts a new interval\n\n### `spread\u003cT, R\u003e(fn: (...args: T) =\u003e R) =\u003e (args: T) =\u003e R`\n\nGiven a `fn`, return a wrapper that accepts an array of `fn` arguments.\n\n```js\nPromise.all([1, 2, 3]).then(spread(add));\n```\n\n### `flip\u003cT1, T2, R\u003e(fn: (arg1: T1, arg2: T2) =\u003e R) =\u003e (arg2: T2, arg1: T1) =\u003e R`\n\nFlip a binary `fn` argument order.\n\n```js\nflip(subtract)(5, 10); //=\u003e 5\n```\n\n### `partial\u003cT, U, R\u003e(fn: (...args1: T, ...args2: U) =\u003e R) =\u003e (...args: U) =\u003e R`\n\nReturns a partially applied `fn` with the supplied arguments.\n\n```js\npartial(subtract, 10)(5); //=\u003e 5\n```\n\n### `sequence\u003cT\u003e(...fns: Array\u003c(input: T) =\u003e T\u003e) =\u003e (input: T) =\u003e T`\n\nLeft-to-right function composition.\n\n```js\nsequence(partial(add, 10), partial(multiply, 5))(5); //=\u003e 75\n```\n\n### `compose\u003cT\u003e(...fns: Array\u003c(input: T) =\u003e T\u003e) =\u003e (input: T) =\u003e T`\n\nRight-to-left function composition.\n\n```js\ncompose(partial(add, 10), partial(multiply, 5))(5); //=\u003e 35\n```\n\n### `nary\u003cT, R\u003e(n: number, fn: (...args: T) =\u003e R) =\u003e (...args: T) =\u003e R`\n\nFix the number of receivable arguments in `origFn` to `n`.\n\n```js\n[\"1\", \"2\", \"3\"].map(nary(1, fn)); //=\u003e [1, 2, 3]\n```\n\n## TypeScript\n\nThis module uses [TypeScript](https://github.com/Microsoft/TypeScript) and publishes type definitions on NPM.\n\n## License\n\nApache 2.0\n\n[npm-image]: https://img.shields.io/npm/v/functools\n[npm-url]: https://npmjs.org/package/functools\n[downloads-image]: https://img.shields.io/npm/dm/functools\n[downloads-url]: https://npmjs.org/package/functools\n[build-image]: https://img.shields.io/github/workflow/status/blakeembrey/js-functools/CI/main\n[build-url]: https://github.com/blakeembrey/js-functools/actions/workflows/ci.yml?query=branch%3Amain\n[coverage-image]: https://img.shields.io/codecov/c/gh/blakeembrey/js-functools\n[coverage-url]: https://codecov.io/gh/blakeembrey/js-functools\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblakeembrey%2Fjs-functools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblakeembrey%2Fjs-functools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblakeembrey%2Fjs-functools/lists"}