Comparing version 3.3.0 to 3.4.0
@@ -23,3 +23,3 @@ export declare const add: (a: number, b: number) => number; | ||
* Optimize a function to speed up consecutive calls by caching the result of | ||
* calls with identical input arguments. The cache can be overrriden to | ||
* calls with identical input arguments. The cache can be overridden to | ||
* implement features such as LRU eviction. | ||
@@ -29,9 +29,17 @@ */ | ||
/** | ||
* Memoize a 0-arg function call. | ||
* Memoize the result of `fn` after the first invocation. | ||
*/ | ||
export declare function memoize0<T>(fn: () => T): () => T | undefined; | ||
export declare function memoize0<T>(fn: () => T): () => T; | ||
/** | ||
* Compare two arrays for equality. | ||
*/ | ||
export declare function arrayEqual<T extends any[]>(prev: T, next: T): boolean; | ||
/** | ||
* Memoize the result of a function based on the most recent arguments. | ||
*/ | ||
export declare function memoizeOne<T extends any[], R>(fn: (...args: T) => R): (...args: T) => R; | ||
/** | ||
* Return a function that fetches `key` from its operand. | ||
*/ | ||
export declare function prop<K extends string | number | symbol>(key: K): <T extends { [T in K]?: any; }>(obj: T) => T[K]; | ||
export declare function prop<K extends string | number | symbol>(key: K): <T_1 extends { [T in K]?: any; }>(obj: T_1) => T_1[K]; | ||
export declare type InvokeResult<T extends (...args: A) => any, A extends any[]> = T extends (...args: A) => infer R ? R : never; | ||
@@ -43,2 +51,5 @@ /** | ||
export declare function invoke<K extends string | number | symbol, A extends any[]>(key: K, ...args: A): <T extends Record<K, (...args: A) => any>>(obj: T) => InvokeResult<T[K], A>; | ||
/** | ||
* Throttle configuration. | ||
*/ | ||
export interface ThrottleOptions { | ||
@@ -45,0 +56,0 @@ leading?: boolean; |
@@ -17,3 +17,3 @@ "use strict"; | ||
* Optimize a function to speed up consecutive calls by caching the result of | ||
* calls with identical input arguments. The cache can be overrriden to | ||
* calls with identical input arguments. The cache can be overridden to | ||
* implement features such as LRU eviction. | ||
@@ -32,3 +32,3 @@ */ | ||
/** | ||
* Memoize a 0-arg function call. | ||
* Memoize the result of `fn` after the first invocation. | ||
*/ | ||
@@ -48,2 +48,31 @@ function memoize0(fn) { | ||
/** | ||
* Compare two arrays for equality. | ||
*/ | ||
function arrayEqual(prev, next) { | ||
const len = next.length; | ||
if (prev.length !== len) | ||
return false; | ||
for (let i = 0; i < len; i++) { | ||
if (!Object.is(prev[i], next[i])) | ||
return false; | ||
} | ||
return true; | ||
} | ||
exports.arrayEqual = arrayEqual; | ||
/** | ||
* Memoize the result of a function based on the most recent arguments. | ||
*/ | ||
function memoizeOne(fn) { | ||
let prevArgs; | ||
let result; | ||
return (...args) => { | ||
if (prevArgs === undefined || !arrayEqual(args, prevArgs)) { | ||
prevArgs = args; | ||
result = fn(...args); | ||
} | ||
return result; | ||
}; | ||
} | ||
exports.memoizeOne = memoizeOne; | ||
/** | ||
* Return a function that fetches `key` from its operand. | ||
@@ -50,0 +79,0 @@ */ |
@@ -34,2 +34,14 @@ "use strict"; | ||
}); | ||
describe("memoizeOne", () => { | ||
it("should memoize a zero-length function", () => { | ||
let i = 0; | ||
const fn = functools.memoizeOne((x) => ++i); | ||
expect(fn("foo")).toEqual(1); | ||
expect(fn("foo")).toEqual(1); | ||
expect(fn("bar")).toEqual(2); | ||
expect(fn("bar")).toEqual(2); | ||
expect(fn("foo")).toEqual(3); | ||
expect(fn("foo")).toEqual(3); | ||
}); | ||
}); | ||
describe("prop", () => { | ||
@@ -36,0 +48,0 @@ const getter = functools.prop("foo"); |
{ | ||
"name": "functools", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "Utilities for working with functions in JavaScript, with TypeScript", | ||
@@ -71,15 +71,15 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.1", | ||
"@types/node": "^10.1.2", | ||
"husky": "^1.3.1", | ||
"jest": "^23.5.0", | ||
"lint-staged": "^8.1.0", | ||
"prettier": "^1.16.1", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^23.1.4", | ||
"tslint": "^5.11.0", | ||
"@types/jest": "^25.1.1", | ||
"@types/node": "^13.7.0", | ||
"husky": "^4.2.1", | ||
"jest": "^25.1.0", | ||
"lint-staged": "^10.0.7", | ||
"prettier": "^1.19.1", | ||
"rimraf": "^3.0.1", | ||
"ts-jest": "^25.2.0", | ||
"tslint": "^6.0.0", | ||
"tslint-config-prettier": "^1.17.0", | ||
"tslint-config-standard": "^8.0.0", | ||
"typescript": "^3.0.3" | ||
"tslint-config-standard": "^9.0.0", | ||
"typescript": "^3.7.5" | ||
} | ||
} |
@@ -51,4 +51,33 @@ # Functools | ||
**See also:** `memoize0` for zero-length function arguments. | ||
### `memoize0<T>(fn: () => T): () => T` | ||
Memoize the result of `fn` after the first invocation. | ||
```js | ||
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. | ||
```js | ||
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]` | ||
@@ -127,6 +156,3 @@ | ||
```js | ||
compose( | ||
partial(add, 10), | ||
partial(multiply, 5) | ||
)(5); //=> 35 | ||
compose(partial(add, 10), partial(multiply, 5))(5); //=> 35 | ||
``` | ||
@@ -133,0 +159,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
53173
460
173
0