// Have several things that you want to change into something else? Reduce it
const fullNames = [["s", "peter"], ["p", "athena"]]; // -> ["Peter S", "Athena P"]
const scores = [34, 33, 1, 0, 99, 123]; // 48.33
// with full names (from above) -> [{firstName: "peter", lastName: "s"},{firstName: "athena", lastName: "p"}, ]
// Want to test if one or more things are true? Write a predicate (or set of predicates)
R.map(startsWith("a"), names); // -> [false, true, true, bob]
R.map(isGreaterThan300, sizes); // -> [false, true, true, false, true]
R.map(isEven, percentages); // -> [true, false, false, false?, false, false] I'm not sure if 0 is even
// Do something only if a predicate passes? Use a when
const animals = [{ type: "dog" }, { type: "cat" }, { type: "snake" }, { type: "shark" }];
// -> [true, true, false, false]
const balance = [10, 0, -3, 4, 50]; // -> ["ok", "ok", "overdrawn", "ok", "ok"] if the balance dips below 0
// If a predicate passes, do something, if not something else? Use an ifThen
const heights = [[4, 10], [5, 10], [5, 3], [6, 2]]; // -> ["reject", "ride", "reject", "ride"] height limit 5'5
const configs = [{ type: "text" }, { type: "date" }, { type: "datetime" }, { type: "text" }]; // -> ["Text", "Default", "Default", "Text"] use default unless text
// Have branching cases where you're looking for the first predicate that passes? Do a cond
const betterConfigs = [
{ type: "text" },
{ type: "date" },
{ type: "datetime" },
{ type: "text" },
{ type: "textarea" },
]; // -> ["Text", "Date", "Date", "Text", "TextArea"] use default unless text
// Want only part of a list that passes a predicate? use Filter (or reject)
const evenBetterConfigs = [
{ name: "Field A", type: "text", isEnabled: true },
{ name: "Field B", type: "date", isEnabled: false },
{ name: "Field C", type: "datetime", isEnabled: false },
{ name: "Field D", type: "text", isEnabled: true },
{ name: "Field E", type: "textarea", isEnabled: true },
];
// all enabled
// all disabled
// all enabled text fields
// all where the type starts with date
0 Answer(s)