improved word extractor

This commit is contained in:
noumantahir 2022-01-27 14:16:29 +05:00
parent ae34c14870
commit 17046be05e

View File

@ -45,18 +45,23 @@ export const generatePermlink = (title, random = false) => {
}; };
export const extractWordAtIndex = (text:string, index:number) => { export const extractWordAtIndex = (text:string, index:number) => {
const END_REGEX = /[\s,]/
let word = ''; let word = '';
for(let i = index; i >= 0 && text[i] !== ' '; i--){ for(let i = index; i >= 0 && (!END_REGEX.test(text[i]) || i === index); i--){
if(text[i]){ if(text[i]){
word += text[i]; word += text[i];
} }
} }
word = word.split('').reverse().join(''); word = word.split('').reverse().join('');
for(let i = index + 1; i < text.length && text[i] !== ' '; i++){
if(!END_REGEX.test(text[index])){
for(let i = index + 1; i < text.length && !END_REGEX.test(text[i]); i++){
if(text[i]){ if(text[i]){
word += text[i]; word += text[i];
} }
} }
}
return word; return word;
} }