| 1 | /**
|
|---|
| 2 | * Creates a function that checks conditions one by one and runs the matching function.
|
|---|
| 3 | *
|
|---|
| 4 | * Each pair consists of a condition (predicate) and a function to run.
|
|---|
| 5 | * The function goes through each condition in order until it finds one that's true.
|
|---|
| 6 | * When it finds a true condition, it runs the corresponding function and returns its result.
|
|---|
| 7 | * If none of the conditions are true, it returns undefined.
|
|---|
| 8 | *
|
|---|
| 9 | * @param {Array<Array>} pairs - Array of pairs. Each pair consists of a predicate function and a function to run.
|
|---|
| 10 | * @returns {(...args: any[]) => unknown} A new composite function that checks conditions and runs the matching function.
|
|---|
| 11 | * @example
|
|---|
| 12 | *
|
|---|
| 13 | * const func = cond([
|
|---|
| 14 | * [matches({ a: 1 }), constant('matches A')],
|
|---|
| 15 | * [conforms({ b: isNumber }), constant('matches B')],
|
|---|
| 16 | * [stubTrue, constant('no match')]
|
|---|
| 17 | * ]);
|
|---|
| 18 | *
|
|---|
| 19 | * func({ a: 1, b: 2 });
|
|---|
| 20 | * // => 'matches A'
|
|---|
| 21 | *
|
|---|
| 22 | * func({ a: 0, b: 1 });
|
|---|
| 23 | * // => 'matches B'
|
|---|
| 24 | *
|
|---|
| 25 | * func({ a: '1', b: '2' });
|
|---|
| 26 | * // => 'no match'
|
|---|
| 27 | */
|
|---|
| 28 | declare function cond<R>(pairs: Array<[truthy: () => boolean, falsey: () => R]>): () => R;
|
|---|
| 29 | /**
|
|---|
| 30 | * Creates a function that checks conditions one by one and runs the matching function.
|
|---|
| 31 | *
|
|---|
| 32 | * Each pair consists of a condition (predicate) and a function to run.
|
|---|
| 33 | * The function goes through each condition in order until it finds one that's true.
|
|---|
| 34 | * When it finds a true condition, it runs the corresponding function and returns its result.
|
|---|
| 35 | * If none of the conditions are true, it returns undefined.
|
|---|
| 36 | *
|
|---|
| 37 | * @param {Array<Array>} pairs - Array of pairs. Each pair consists of a predicate function and a function to run.
|
|---|
| 38 | * @returns {(...args: any[]) => unknown} A new composite function that checks conditions and runs the matching function.
|
|---|
| 39 | * @example
|
|---|
| 40 | *
|
|---|
| 41 | * const func = cond([
|
|---|
| 42 | * [matches({ a: 1 }), constant('matches A')],
|
|---|
| 43 | * [conforms({ b: isNumber }), constant('matches B')],
|
|---|
| 44 | * [stubTrue, constant('no match')]
|
|---|
| 45 | * ]);
|
|---|
| 46 | *
|
|---|
| 47 | * func({ a: 1, b: 2 });
|
|---|
| 48 | * // => 'matches A'
|
|---|
| 49 | *
|
|---|
| 50 | * func({ a: 0, b: 1 });
|
|---|
| 51 | * // => 'matches B'
|
|---|
| 52 | *
|
|---|
| 53 | * func({ a: '1', b: '2' });
|
|---|
| 54 | * // => 'no match'
|
|---|
| 55 | */
|
|---|
| 56 | declare function cond<T, R>(pairs: Array<[truthy: (val: T) => boolean, falsey: (val: T) => R]>): (val: T) => R;
|
|---|
| 57 |
|
|---|
| 58 | export { cond };
|
|---|