mirror of
https://github.com/alice-lg/alice-lg.git
synced 2024-05-11 05:55:03 +00:00
added list and text helpers
This commit is contained in:
28
client/components/utils/lists.jsx
Normal file
28
client/components/utils/lists.jsx
Normal file
@ -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;
|
||||
}
|
||||
|
19
client/components/utils/text.jsx
Normal file
19
client/components/utils/text.jsx
Normal file
@ -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}`;
|
||||
}
|
||||
|
Reference in New Issue
Block a user