You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
describe('getSpecialCharEscapedRegex',()=>{it.each(['tests','test@@','+test@!','test.'])(`should be return true, keyword is '%s'`,(keyword)=>{constre=getSpecialCharEscapedRegex(keyword);expect(re.test(`xxx${keyword}abc`)).toBe(true);});});
import{isPhoneNumberString,isKeywordMatched}from'./isKeywordMatched';describe('isPhoneNumberString',()=>{it.each(['66 21 234 681','21-234-681','21 234-681','21 234 681','010-1234-1234','010 1234','+82 010','1544-1234','1234 123 1234','911',])(`should be return true, keyword is phone number '%s'`,(keyword)=>{constresult=isPhoneNumberString(keyword);expect(result).toBe(true);});it.each(['66*21*234*681'])(`should be return false, keyword is not phone number '%s'`,(keyword)=>{constresult=isPhoneNumberString(keyword);expect(result).toBe(false);});});describe('isKeywordMatched',()=>{it.each(['',null,undefined])(`should be return false, title is '%s'`,(title)=>{constresult=isKeywordMatched(title,'keyword');expect(result).toBe(false);});it.each(['',null,undefined])(`should be return false, keyword is '%s'`,(keyword)=>{constresult=isKeywordMatched('title',keyword);expect(result).toBe(false);});it('should match normal keyword correctly',()=>{constmessageWithKeyword='messagetest';constmeessageNoKeyword='message';constkeyword='test';constresultOne=isKeywordMatched(messageWithKeyword,keyword);constresultTwo=isKeywordMatched(meessageNoKeyword,keyword);expect(resultOne).toBe(true);expect(resultTwo).toBe(false);});it('should match special character keyword correctly',()=>{constmessageWithKeyword='message@!';constmeessageNoKeyword='message';constkeyword='@!';constresultOne=isKeywordMatched(messageWithKeyword,keyword);constresultTwo=isKeywordMatched(meessageNoKeyword,keyword);expect(resultOne).toBe(true);expect(resultTwo).toBe(false);});it.each(['0101234','010-1234','010 1234'])(`should be return true, title is phone number and keyword is phone number '%s'`,(keyword)=>{constresult=isKeywordMatched('82-010-1234-5678',keyword);expect(result).toBe(true);});it.each(['82*010-1234-5678','test'])(`should be return false, title is '%s' and keyword is phone number`,(title)=>{constresult=isKeywordMatched(title,'0101234');expect(result).toBe(false);});});
marking with react
importReactfrom'react';typeIOjbectType<T>=T|null|undefined;constisNull=<T>(obj: IOjbectType<T>): obj is null =>{returnobj===null||(typeofobj==='string'&&obj==='null');};
const isUndefined = <T>(obj: IOjbectType<T>): obj is undefined =>{returntypeofobj==='undefined'||(typeofobj==='string'&&obj==='undefined');};
const isNullOrUndefined = <T>(obj: IOjbectType<T>): obj is null | undefined =>{returnisNull(obj)||isUndefined(obj);};
const isEmptyString = (v: unknown): v is null | undefined =>{returnisNullOrUndefined(v)||(isString(v)&&v.trim()==='');};
function generateFromGeneral(title: string, keyword: string) {letmatches;letstartIdx=0;constmarkedComponent=[];constkeywordRegex=getSpecialCharEscapedRegex(keyword);while((matches=keywordRegex.exec(title))!==null){const matchIndex =matches.index;if(matchIndex!==0){markedComponent.push(title.substr(startIdx,matchIndex-startIdx));}markedComponent.push(<mark>{matches[0]}</mark>);startIdx=keywordRegex.lastIndex;}
if (startIdx <title.length){markedComponent.push(title.substr(startIdx));}returnmarkedComponent.map((item,index)=>{return<React.Fragmentkey={index}>{item}</React.Fragment>;});}functiongenerateFromPhoneNumber(title: string,keyword: string){constmarkedComponent=[];constkeywordNumStr=NumberUtils.getNumberByRegex(keyword);constkeywordRegex=getSpecialCharEscapedRegex(keywordNumStr);constmatchesWithOnlyNumber=keywordRegex.exec(getNumberByRegex(title));constgetActualStringLength=(startIndex: number,length: number)=>{letcount=0;constincludedCharMatched=newRegExp('\\D+','gi').exec(title.substr(startIndex,length));if(includedCharMatched){includedCharMatched.map((item)=>{count+=item.length;});}returnlength+count;};if(matchesWithOnlyNumber){// check index for before matchedletactualMatchedStartIndex=getActualStringLength(0,matchesWithOnlyNumber.index);while(keywordNumStr.substr(0,1)!==title.substr(actualMatchedStartIndex,1)){actualMatchedStartIndex++;}// check matched keyword lengthconstactualMatchedLength=getActualStringLength(actualMatchedStartIndex,keywordNumStr.length);// check index for after matchedconstactualMatchedEndIndex=actualMatchedStartIndex+actualMatchedLength;// console.log('### check - titleSplitByMatchedIndex', {// title,// keywordNumStr,// actualMatchedStartIndex,// actualMatchedEndIndex,// actualMatchedLength,// beforeMactched: title.substr(0, actualMatchedStartIndex),// matched: title.substr(actualMatchedStartIndex, actualMatchedLength),// afterMactched: title.substr(actualMatchedEndIndex),// });if(actualMatchedStartIndex!==0){markedComponent.push(title.substr(0,actualMatchedStartIndex));}markedComponent.push(<mark>{title.substr(actualMatchedStartIndex,actualMatchedLength)}</mark>);
if (actualMatchedEndIndex <title.length){markedComponent.push(title.substr(actualMatchedEndIndex));}}returnmarkedComponent.map((item,index)=>{return<React.Fragmentkey={index}>{item}</React.Fragment>;});}exportfunctiongenerateMarkedComponent(title: string,keyword?: string|null){if(isEmptyString(title)||isEmptyString(keyword)||!isKeywordMatched(title,keyword)){returntitle;}returnisPhoneNumberString(keyword) ? generateFromPhoneNumber(title,keyword) : generateFromGeneral(title,keyword);}
importReactfrom'react';import{generateMarkedComponent}from'./generateMarkedComponent';describe('generateMarkedComponent',function(){it(`should be the plain text, if title is empty string`,()=>{consttitle='';constresult=generateMarkedComponent(title,'keyword');expect(result).toBe(title);});it.each(['',null,undefined])(`should be the plain text, if keyword is empty('%s)`,(keyword)=>{consttitle='Hello, world!';constresult=generateMarkedComponent(title,keyword);expect(result).toBe(title);});it(`should be return the plain text, if keyword is not matched`,()=>{consttitle='Starbucks Coffee';constresult=generateMarkedComponent(title,'ABC');expect(result).toBe(title);});it(`should be add mark, if upper keyword is matched`,()=>{constresult=generateMarkedComponent('Starbucks Coffee','Star');expect(result.length).toBe(2);expect(React.isValidElement(result[0])).toBeTruthy();expect(result).toMatchSnapshot();});it(`should be add mark, if lower keyword is matched`,()=>{constresult=generateMarkedComponent('Starbucks Coffee','coff');expect(result.length).toBe(3);expect(React.isValidElement(result[1])).toBeTruthy();expect(result).toMatchSnapshot();});it.each(['+66223','66'])(`should be add mark in the first word, if phone keyword '%s' is matched`,(keyword: string)=>{constresult=generateMarkedComponent('66-223-1234',keyword);expect(result.length).toBe(2);expect(React.isValidElement(result[0])).toBeTruthy();expect(result).toMatchSnapshot();});it.each(['223','23','312','123','6223','3123'])(`should be add mark in the middle word, if phone keyword '%s' is matched`,(keyword: string)=>{constresult=generateMarkedComponent('66-223-1234',keyword);expect(result.length).toBe(3);expect(React.isValidElement(result[1])).toBeTruthy();expect(result).toMatchSnapshot();});it.each(['234','3-1234'])(`should be add mark in the last word, if phone keyword '%s' is matched`,(keyword: string)=>{constmessage='66-223-1234';constresult=generateMarkedComponent(message,keyword);expect(result.length).toBe(2);// @ts-ignoreexpect(result[0].props.children).toBe(message.split(keyword)[0]);expect(React.isValidElement(result[1])).toBeTruthy();expect(result).toMatchSnapshot();});it.each(['66 123 1234','66 123 1234','66--123--1234','66 - 123 - 1234','+66-10-123-1234'])(`should be add mark, if message is '%s' matched with phone keyword`,(message: string)=>{constkeyword='123';constresult=generateMarkedComponent(message,keyword);constfirstWord=message.split(newRegExp(keyword))[0];expect(result.length).toBe(3);// @ts-ignoreexpect(result[0].props.children).toBe(firstWord);expect(React.isValidElement(result[1])).toBeTruthy();// @ts-ignoreexpect(result[2].props.children).toBe(message.split(`${firstWord}${keyword}`)[1]);expect(result).toMatchSnapshot();});});
The text was updated successfully, but these errors were encountered:
keywrod matching
getSpecialCharEscapedRegex
getNumberByRegex
isKeywordMatched / isPhoneNumberString
marking with react
The text was updated successfully, but these errors were encountered: