1
0
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:
Matthias Hannig
2018-10-22 15:13:47 +02:00
parent 2d23646610
commit 848ae64e2f
2 changed files with 47 additions and 0 deletions

View 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;
}

View 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}`;
}