diff --git a/client/components/utils/lists.jsx b/client/components/utils/lists.jsx new file mode 100644 index 0000000..5d77a61 --- /dev/null +++ b/client/components/utils/lists.jsx @@ -0,0 +1,28 @@ + +/** + * Intersect lists: [x | x <- A, x `elem` B] + */ +export function intersect(a, b) { + let res = []; + for (const e of a) { + for (const k of b) { + if (e==k) { + res.push(e); + break; + } + } + } + return res; +} + +/** + * Resolve list with dict: [dict[x]||x | x <- L] + */ +export function resolve(dict, list) { + let result = []; + for (const e of list) { + result.push(dict[e]||e); + } + return result; +} + diff --git a/client/components/utils/text.jsx b/client/components/utils/text.jsx new file mode 100644 index 0000000..ce4e794 --- /dev/null +++ b/client/components/utils/text.jsx @@ -0,0 +1,19 @@ + +/** + * Join list of words with ',' and provide a glue + * for the last element. + * + * Example: + * humanizedJoin(["foo", "bar", "baz"], "or") -> + * "foo, bar or baz" + */ +export function humanizedJoin(list, glue="and") { + // Doing this the other way round in one step would be nice. + let [last, ...init] = list.reverse(); + init = init.reverse(); + if (init.length == 0) { + return last; + } + return init.join(", ") + ` ${glue} ${last}`; +} +