Last active
June 19, 2019 01:55
-
-
Save kurone-kito/ceb9b82ac6829eb8c6cbb7821a88248b to your computer and use it in GitHub Desktop.
Group duplicate prefixes in the strings: (src: string[]) => string[]
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
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