1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
2018-10-22 15:13:47 +02:00

20 lines
449 B
JavaScript

/**
* 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}`;
}