-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathcond.ts
More file actions
93 lines (85 loc) · 3.22 KB
/
Copy pathcond.ts
File metadata and controls
93 lines (85 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { iteratee } from './iteratee.ts';
import { isFunction } from '../../predicate/isFunction.ts';
/**
* Creates a function that checks conditions one by one and runs the matching function.
*
* Each pair consists of a condition (predicate) and a function to run.
* The function goes through each condition in order until it finds one that's true.
* When it finds a true condition, it runs the corresponding function and returns its result.
* If none of the conditions are true, it returns undefined.
*
* @param {Array<Array>} pairs - Array of pairs. Each pair consists of a predicate function and a function to run.
* @returns {(...args: any[]) => unknown} A new composite function that checks conditions and runs the matching function.
* @example
*
* const func = cond([
* [matches({ a: 1 }), constant('matches A')],
* [conforms({ b: isNumber }), constant('matches B')],
* [stubTrue, constant('no match')]
* ]);
*
* func({ a: 1, b: 2 });
* // => 'matches A'
*
* func({ a: 0, b: 1 });
* // => 'matches B'
*
* func({ a: '1', b: '2' });
* // => 'no match'
*/
export function cond<R>(pairs: Array<[truthy: () => boolean, falsey: () => R]>): () => R;
/**
* Creates a function that checks conditions one by one and runs the matching function.
*
* Each pair consists of a condition (predicate) and a function to run.
* The function goes through each condition in order until it finds one that's true.
* When it finds a true condition, it runs the corresponding function and returns its result.
* If none of the conditions are true, it returns undefined.
*
* @param {Array<Array>} pairs - Array of pairs. Each pair consists of a predicate function and a function to run.
* @returns {(...args: any[]) => unknown} A new composite function that checks conditions and runs the matching function.
* @example
*
* const func = cond([
* [matches({ a: 1 }), constant('matches A')],
* [conforms({ b: isNumber }), constant('matches B')],
* [stubTrue, constant('no match')]
* ]);
*
* func({ a: 1, b: 2 });
* // => 'matches A'
*
* func({ a: 0, b: 1 });
* // => 'matches B'
*
* func({ a: '1', b: '2' });
* // => 'no match'
*/
export function cond<T, R>(pairs: Array<[truthy: (val: T) => boolean, falsey: (val: T) => R]>): (val: T) => R;
/**
* Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
*
* @param {any[][]} pairs The predicate-function pairs.
* @returns {(...args: any[]) => unknown} Returns the new composite function.
*/
export function cond(pairs: any[][]): (...args: any[]) => unknown {
const length = pairs.length;
const processedPairs = pairs.map(pair => {
const predicate = pair[0];
const func = pair[1];
if (!isFunction(func)) {
throw new TypeError('Expected a function');
}
return [iteratee(predicate), func] as const;
});
return function (this: unknown, ...args: any[]): unknown {
for (let i = 0; i < length; i++) {
const pair = processedPairs[i];
const predicate = pair[0] as (this: unknown, ...args: any[]) => boolean;
const func = pair[1] as (this: unknown, ...args: any[]) => unknown;
if (predicate.apply(this, args)) {
return func.apply(this, args);
}
}
};
}