updeep-remeda/src/ifElse.ts

20 lines
582 B
TypeScript
Raw Permalink Normal View History

2023-01-03 18:51:35 +00:00
import update from "./update.js";
import wrap from "./wrap.js";
function updateIfElse(object, predicate, trueUpdates, falseUpdates) {
const test = typeof predicate === "function" ? predicate(object) : predicate;
const updates = test ? trueUpdates : falseUpdates;
return update(object, updates);
}
type Predicate = ((source: any) => boolean) | boolean;
2023-01-03 19:31:53 +00:00
export interface IfElse {
2023-01-03 18:51:35 +00:00
(object, predicate: Predicate, trueUpdates, falseUpdates): unknown;
(predicate: Predicate, trueUpdates, falseUpdates): (unknown) => unknown;
}
export default wrap(updateIfElse) as IfElse;