I am not sure what would I call this but I am trying to get a collection of substrings from a string. What I mean by that is, let's say I have a string, draft question
and If I pass it in the function it will return me something like
["", "d", "dr", "dra", "draf", "draft", "draft ", "draft q", "draft qu", "draft que", "draft ques", "draft quest", "draft questi", "draft questio", "draft question", "q", "qu", "que", "ques", "quest", "questi", "questio", "question", "question ", "question d", "question dr", "question dra", "question draf", "question draft"]
I wrote a function which giving me a some extend to what I want but not exactly. Any idea how do I do this? Is it possible by regex? Please note that it can any number of words. Like generateKeywords('put returns between paragraphs')
.
var createKeywords = (name) => {
const arrName = [];
let curName = '';
name.split('').forEach((letter) => {
curName += letter;
arrName.push(curName);
});
return arrName;
};
var generateKeywords = (names) => {
const [first, middle, last = '', sfx] = names;
const suffix = sfx && sfx.length > 0 ? ` ${sfx}.` : '';
const keywordNameWithoutMiddleName = createKeywords(`${first} ${last}${suffix}`);
const keywordFullName = createKeywords(`${first} ${middle} ${last}${suffix}`);
const keywordLastNameFirst = createKeywords(`${last}, ${first} ${middle}${suffix}`);
const middleInitial = middle.length > 0 ? ` ${middle[0]}.` : '';
const keywordFullNameMiddleInitial = createKeywords(`${first}${middleInitial} ${last}${suffix}`);
const keywordLastNameFirstMiddleInitial = createKeywords(`${last}, ${first} ${middleInitial}${suffix}`);
return [
...new Set([
'',
...keywordFullName,
...keywordLastNameFirst,
...keywordFullNameMiddleInitial,
...keywordLastNameFirstMiddleInitial,
...keywordNameWithoutMiddleName
])
];
};