Comparing version
@@ -5,3 +5,3 @@ /** | ||
export interface Cache<T, U> { | ||
get(key: T): U; | ||
get(key: T): U | undefined; | ||
set(key: T, value: U): void; | ||
@@ -20,1 +20,11 @@ has(key: T): boolean; | ||
export declare function identity<T>(value: T): T; | ||
/** | ||
* 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 type InvokeResult<T extends (...args: A) => any, A extends any[]> = T extends (...args: A) => infer R ? R : never; | ||
/** | ||
* 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. | ||
*/ | ||
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>; |
@@ -25,2 +25,17 @@ "use strict"; | ||
exports.identity = identity; | ||
/** | ||
* Return a function that fetches `key` from its operand. | ||
*/ | ||
function prop(key) { | ||
return (obj) => obj[key]; | ||
} | ||
exports.prop = prop; | ||
/** | ||
* 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. | ||
*/ | ||
function invoke(key, ...args) { | ||
return (obj) => obj[key](...args); | ||
} | ||
exports.invoke = invoke; | ||
//# sourceMappingURL=index.js.map |
@@ -20,3 +20,12 @@ "use strict"; | ||
}); | ||
describe('prop', () => { | ||
const getter = functools.prop('foo'); | ||
expect(getter({ foo: 123 })).toEqual(123); | ||
expect(getter({})).toEqual(undefined); | ||
}); | ||
describe('invoke', () => { | ||
expect(functools.invoke('foo')({ foo: () => 123 })).toEqual(123); | ||
expect(functools.invoke('add', 3, 7)({ add: (a, b) => a + b })).toEqual(10); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.spec.js.map |
{ | ||
"name": "functools", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Utilities for working with functions in JavaScript, with TypeScript", | ||
@@ -55,11 +55,11 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^22.2.2", | ||
"@types/jest": "^23.3.1", | ||
"@types/node": "^10.1.2", | ||
"jest": "^22.4.3", | ||
"jest": "^23.5.0", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^22.4.2", | ||
"tslint": "^5.9.1", | ||
"tslint-config-standard": "^7.0.0", | ||
"typescript": "^2.8.1" | ||
"ts-jest": "^23.1.4", | ||
"tslint": "^5.11.0", | ||
"tslint-config-standard": "^8.0.0", | ||
"typescript": "^3.0.3" | ||
} | ||
} |
@@ -20,3 +20,3 @@ # Functools | ||
### `identity<T>(arg: T): T` | ||
### `identity<T>(arg: T) => T` | ||
@@ -29,3 +29,3 @@ Always returns the same value that was used as the argument. | ||
### `memoize<T, U>(fn: (x: T) => U, cache?: Cache): (x: T) => U` | ||
### `memoize<T, U>(fn: (x: T) => U, cache?: Cache) => (x: T) => U` | ||
@@ -45,2 +45,18 @@ Optimize a function to speed up consecutive calls by caching the result of calls with identical input arguments. The cache can be overrriden to implement features such as LRU eviction. | ||
### `prop<K>(key: K) => (obj: T) => T[K]` | ||
Return a function that fetches `key` from its operand. | ||
```js | ||
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. | ||
```js | ||
invoke('add', 5, 5)({ add: (a, b) => a + b }) //=> 10 | ||
``` | ||
## TypeScript | ||
@@ -47,0 +63,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12072
47.45%97
53.97%66
32%0
-100%