Last active
August 30, 2017 01:35
-
-
Save vasilionjea/3d79e78ecb9552876fee2b3650d7cec4 to your computer and use it in GitHub Desktop.
Concat list of names in various ways
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fuzzyNames = { | |
_andConcat({ list, last }) { | |
return `${list.join(', ')} and ${last.join('')}`; | |
}, | |
_lengthConcat({ list, rest }) { | |
let others = rest.length === 1 ? "other" : "others"; | |
return `${list.join(', ')} and ${rest.length} ${others}`; | |
}, | |
humanize(names, opts={}) { | |
if (!names || !Array.isArray(names) || !names.length) { | |
throw TypeError(`An array with 1 or more names is expected but received: ${names}`); | |
} | |
// Don't accidentaly modify original | |
let namesCopy = names.concat(); | |
// Just return it | |
if (namesCopy.length === 1) { | |
return namesCopy[0]; | |
} | |
// Concat with opts | |
if (opts) { | |
// Maybe sort | |
opts.sort && namesCopy.sort(); | |
// Maybe length is in range | |
if (opts.length > 0 && opts.length < namesCopy.length) { | |
return this._lengthConcat({ | |
list: namesCopy.slice(0, opts.length), | |
rest: namesCopy.slice(opts.length) | |
}); | |
} | |
} | |
// Default concat | |
return this._andConcat({ | |
list: namesCopy.slice(0, namesCopy.length - 1), | |
last: namesCopy.slice(namesCopy.length - 1) | |
}); | |
} | |
}; | |
// TESTS | |
// --------------------------------- | |
// Invalid input | |
// --------------------------------- | |
fuzzyNames.humanize(); //=> TypeError: An array with 1 or more... | |
fuzzyNames.humanize([]); //=> TypeError: An array with 1 or more... | |
fuzzyNames.humanize(null); //=> TypeError: An array with 1 or more... | |
// --------------------------------- | |
// Without opts | |
// --------------------------------- | |
[ | |
fuzzyNames.humanize(['Alice']) === "Alice", | |
fuzzyNames.humanize(['Alice','Mary']) === "Alice and Mary", | |
fuzzyNames.humanize(['Alice','Bob','Mary','Tom','John']) === "Alice, Bob, Mary, Tom and John" | |
].forEach(console.assert); | |
// --------------------------------- | |
// With opts | |
// --------------------------------- | |
let names = ['Alice','Bob','Mary','Tom','John']; | |
[ | |
fuzzyNames.humanize(names, {}) === "Alice, Bob, Mary, Tom and John", | |
fuzzyNames.humanize(names, { length: 0 }) === "Alice, Bob, Mary, Tom and John", | |
fuzzyNames.humanize(names, { length: 99 }) === "Alice, Bob, Mary, Tom and John", | |
fuzzyNames.humanize(names, { length: -3 }) === "Alice, Bob, Mary, Tom and John", | |
fuzzyNames.humanize(names, { length: 1 }) === "Alice and 4 others", | |
fuzzyNames.humanize(names, { length: 3 }) === "Alice, Bob, Mary and 2 others", | |
fuzzyNames.humanize(names, { length: 4 }) === "Alice, Bob, Mary, Tom and 1 other", | |
fuzzyNames.humanize(names, { length: 3, sort: true }) === "Alice, Bob, John and 2 others", | |
fuzzyNames.humanize(names, { sort: true }) === "Alice, Bob, John, Mary and Tom" | |
].forEach(console.assert); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment