From 17046be05eba6613befe6e0ec7a7b19b0d3283e1 Mon Sep 17 00:00:00 2001 From: noumantahir Date: Thu, 27 Jan 2022 14:16:29 +0500 Subject: [PATCH] improved word extractor --- src/utils/editor.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utils/editor.ts b/src/utils/editor.ts index c97fde485..ffabb7aa0 100644 --- a/src/utils/editor.ts +++ b/src/utils/editor.ts @@ -45,18 +45,23 @@ export const generatePermlink = (title, random = false) => { }; export const extractWordAtIndex = (text:string, index:number) => { + const END_REGEX = /[\s,]/ 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]){ word += text[i]; } } word = word.split('').reverse().join(''); - for(let i = index + 1; i < text.length && text[i] !== ' '; i++){ - if(text[i]){ - word += text[i]; + + if(!END_REGEX.test(text[index])){ + for(let i = index + 1; i < text.length && !END_REGEX.test(text[i]); i++){ + if(text[i]){ + word += text[i]; + } } } + return word; }