Skip to content

Instantly share code, notes, and snippets.

@kurone-kito
Last active June 19, 2019 01:55
Show Gist options
  • Save kurone-kito/ceb9b82ac6829eb8c6cbb7821a88248b to your computer and use it in GitHub Desktop.
Save kurone-kito/ceb9b82ac6829eb8c6cbb7821a88248b to your computer and use it in GitHub Desktop.
Group duplicate prefixes in the strings: (src: string[]) => string[]
const getInitials = (src: string[], length = 1) =>
Array.from(new Set(src.map(v => v.substring(0, length))));
export default (src: string[]) =>
getInitials(src).map(initial => {
const list = src.filter(v => v.match(`^${initial}`));
const rec = (prev: string, length = 2): string => {
const [word, ...{ length: l }] = getInitials(list, length);
return l || list[0].length < length ? prev : rec(word, length + 1);
};
return rec(initial);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment