@@ -5,15 +5,20 @@ import {
55 highlightTermInString ,
66 highlightTermInTextTag ,
77 highlightTermsInTextTag ,
8+ type TermData ,
89 type TextTag ,
910} from "./highlightTermsInTextTag" ;
1011
1112describe ( "highlightTermInTextTag" , ( ) => {
1213 describe ( "highlightTermInString" , ( ) => {
1314 it ( "should return original fragment in at #text tag if termsRegexp has no match" , ( ) => {
1415 const text = "This is a sample text." ;
15- const term : RegExp = / ^ s e a r c h t e x t $ / gi;
16- const result = highlightTermInString ( text , term , [ "word" ] , undefined ) ;
16+ const termData : TermData = {
17+ termRegex : / ^ s e a r c h t e x t $ / gi,
18+ term : "search text" ,
19+ groups : [ "word" ] ,
20+ } ;
21+ const result = highlightTermInString ( text , termData ) ;
1722 expect ( result ) . toStrictEqual ( [
1823 {
1924 tag : "#text" ,
@@ -24,8 +29,13 @@ describe("highlightTermInTextTag", () => {
2429
2530 it ( "should highlight single term in text" , ( ) => {
2631 const text = "This is a sample text." ;
27- const term : RegExp = / s a m p l e / gi;
28- const result = highlightTermInString ( text , term , [ "word" ] , undefined ) ;
32+ const termData : TermData = {
33+ termRegex : / s a m p l e / gi,
34+ term : "sample" ,
35+ groups : [ "word" ] ,
36+ } ;
37+
38+ const result = highlightTermInString ( text , termData ) ;
2939
3040 expect ( result ) . toStrictEqual ( [
3141 {
@@ -51,8 +61,13 @@ describe("highlightTermInTextTag", () => {
5161
5262 it ( "should highlight all occurrences of a single term in text" , ( ) => {
5363 const text = "This is a sample text for testing called sample." ;
54- const term : RegExp = / s a m p l e / gi;
55- const result = highlightTermInString ( text , term , [ "group" ] , undefined ) ;
64+
65+ const termData : TermData = {
66+ termRegex : / s a m p l e / gi,
67+ term : "sample" ,
68+ groups : [ "group" ] ,
69+ } ;
70+ const result = highlightTermInString ( text , termData ) ;
5671 expect ( result ) . toStrictEqual ( [
5772 {
5873 tag : "#text" ,
@@ -75,8 +90,12 @@ describe("highlightTermInTextTag", () => {
7590
7691 it ( "should preserve space between words when highlighting" , ( ) => {
7792 const text = "Highlight this term." ;
78- const term : RegExp = / t h i s / gi;
79- const result = highlightTermInString ( text , term , [ "group" ] , undefined ) ;
93+ const termData : TermData = {
94+ termRegex : / t h i s / gi,
95+ term : "this" ,
96+ groups : [ "group" ] ,
97+ } ;
98+ const result = highlightTermInString ( text , termData ) ;
8099 expect ( result ) . toStrictEqual ( [
81100 { tag : "#text" , value : "Highlight " } ,
82101 {
@@ -92,6 +111,30 @@ describe("highlightTermInTextTag", () => {
92111 { tag : "#text" , value : " term." } ,
93112 ] ) ;
94113 } ) ;
114+
115+ it ( "should set term in attributes term and keep matched value in value" , ( ) => {
116+ const text = "Testing matching\nTERM with complex regex." ;
117+ const termData : TermData = {
118+ termRegex : / m a t c h i n g \s t e r m / gi,
119+ term : "term" ,
120+ groups : [ "group" ] ,
121+ } ;
122+ const result = highlightTermInString ( text , termData ) ;
123+ expect ( result ) . toStrictEqual ( [
124+ { tag : "#text" , value : "Testing " } ,
125+ {
126+ tag : "highlight" ,
127+ value : [
128+ {
129+ tag : "#text" ,
130+ value : "matching\nTERM" ,
131+ } ,
132+ ] ,
133+ attributes : { groups : [ "group" ] , term : "term" } ,
134+ } ,
135+ { tag : "#text" , value : " with complex regex." } ,
136+ ] ) ;
137+ } ) ;
95138 } ) ;
96139
97140 describe ( "highlightTermInTextTag" , ( ) => {
@@ -116,8 +159,12 @@ describe("highlightTermInTextTag", () => {
116159 } as HighlightTag ,
117160 { tag : "#text" , value : "Another sample here." } ,
118161 ] ;
119- const term : RegExp = / s a m p l e / gi;
120- const result = highlightTermInTextTag ( fragments , term , [ "group2" ] ) ;
162+ const termData : TermData = {
163+ termRegex : / s a m p l e / gi,
164+ term : "sample" ,
165+ groups : [ "group2" ] ,
166+ } ;
167+ const result = highlightTermInTextTag ( fragments , termData ) ;
121168 expect ( result ) . toStrictEqual ( [
122169 {
123170 tag : "#text" ,
@@ -190,8 +237,12 @@ describe("highlightTermInTextTag", () => {
190237 value : "Another sample here." ,
191238 } ,
192239 ] ;
193- const term : RegExp = / s a m p l e / gi;
194- const result = highlightTermInTextTag ( fragments , term , [ "group2" ] ) ;
240+ const termData : TermData = {
241+ termRegex : / s a m p l e / gi,
242+ term : "sample" ,
243+ groups : [ "group2" ] ,
244+ } ;
245+ const result = highlightTermInTextTag ( fragments , termData ) ;
195246 expect ( result ) . toStrictEqual ( [
196247 {
197248 tag : "#text" ,
@@ -234,8 +285,12 @@ describe("highlightTermInTextTag", () => {
234285
235286 it ( "should return empty array when given empty fragments" , ( ) => {
236287 const fragments : ( HighlightTag | TextTag ) [ ] = [ ] ;
237- const term : RegExp = / s a m p l e / gi;
238- const result = highlightTermInTextTag ( fragments , term , [ "group" ] ) ;
288+ const termData : TermData = {
289+ termRegex : / s a m p l e / gi,
290+ term : "sample" ,
291+ groups : [ "group" ] ,
292+ } ;
293+ const result = highlightTermInTextTag ( fragments , termData ) ;
239294 expect ( result ) . toStrictEqual ( [ ] ) ;
240295 } ) ;
241296 } ) ;
@@ -248,12 +303,12 @@ describe("highlightTermInTextTag", () => {
248303 } ,
249304 value : "This is a sample text for testing." ,
250305 } ;
251- const terms = [
252- { termRegex : / s a m p l e / gi, groups : [ "group1" ] } ,
253- { termRegex : / t e s t i n g / gi, groups : [ "group2" ] } ,
254- { termRegex : / e x a m p l e / gi, groups : [ "group3" ] } ,
306+ const termDataList = [
307+ { termRegex : / s a m p l e / gi, term : "sample" , groups : [ "group1" ] } ,
308+ { termRegex : / t e s t i n g / gi, term : "testing" , groups : [ "group2" ] } ,
309+ { termRegex : / e x a m p l e / gi, term : "example" , groups : [ "group3" ] } ,
255310 ] ;
256- const result = highlightTermsInTextTag ( textTag , terms ) ;
311+ const result = highlightTermsInTextTag ( textTag , termDataList ) ;
257312 expect ( result ) . toStrictEqual ( {
258313 tag : "highlightedText" ,
259314 attributes : { lang : "en" } ,
@@ -302,11 +357,11 @@ describe("highlightTermInTextTag", () => {
302357 } ,
303358 value : "This is a sample text for testing." ,
304359 } ;
305- const terms = [
306- { termRegex : / e x a m p l e / gi, groups : [ "group1" ] } ,
307- { termRegex : / d e m o / gi, groups : [ "group2" ] } ,
360+ const termDataList : TermData [ ] = [
361+ { termRegex : / e x a m p l e / gi, term : "example" , groups : [ "group1" ] } ,
362+ { termRegex : / d e m o / gi, term : "demo" , groups : [ "group2" ] } ,
308363 ] ;
309- const result = highlightTermsInTextTag ( fragments as any , terms ) ;
364+ const result = highlightTermsInTextTag ( fragments as any , termDataList ) ;
310365 expect ( result ) . toStrictEqual ( {
311366 tag : "highlightedText" ,
312367 attributes : { lang : "en" } ,
@@ -346,11 +401,11 @@ describe("highlightTermInTextTag", () => {
346401 tag : "#text" ,
347402 value : "Term1 Term2" ,
348403 } ;
349- const terms = [
350- { termRegex : / T e r m 1 / gi, groups : [ "group1" ] } ,
351- { termRegex : / T e r m 2 / gi, groups : [ "group2" ] } ,
404+ const termDataList : TermData [ ] = [
405+ { termRegex : / T e r m 1 / gi, term : "term1" , groups : [ "group1" ] } ,
406+ { termRegex : / T e r m 2 / gi, term : "term2" , groups : [ "group2" ] } ,
352407 ] ;
353- const result = highlightTermsInTextTag ( fragments , terms ) ;
408+ const result = highlightTermsInTextTag ( fragments , termDataList ) ;
354409 expect ( result ) . toStrictEqual ( {
355410 tag : "highlightedText" ,
356411 attributes : undefined ,
0 commit comments