Skip to content

Commit d292761

Browse files
authored
Merge pull request #11 from askorama/non-fail-on-null
2 parents ed75646 + 00c4272 commit d292761

File tree

2 files changed

+290
-201
lines changed

2 files changed

+290
-201
lines changed

src/index.test.ts

Lines changed: 208 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,216 @@
1-
import { describe, it, beforeEach, afterEach } from 'bun:test'
2-
import assert from 'node:assert'
3-
import sinon from 'sinon'
4-
import { Highlight } from './index.js'
5-
6-
describe('default configuration', () => {
7-
it('should correctly highlight a text', () => {
8-
const text1 = 'The quick brown fox jumps over the lazy dog'
9-
const searchTerm1 = 'fox'
10-
const expectedResult1 = 'The quick brown <mark class="orama-highlight">fox</mark> jumps over the lazy dog'
11-
12-
const text2 = 'Yesterday all my troubles seemed so far away, now it looks as though they\'re here to stay oh, I believe in yesterday'
13-
const searchTerm2 = 'yesterday I was in trouble'
14-
const expectedResult2 = '<mark class="orama-highlight">Yesterday</mark> all my <mark class="orama-highlight">trouble</mark>s seemed so far away, now <mark class="orama-highlight">i</mark>t looks as though they\'re here to stay oh, <mark class="orama-highlight">I</mark> bel<mark class="orama-highlight">i</mark>eve <mark class="orama-highlight">i</mark>n <mark class="orama-highlight">yesterday</mark>'
15-
16-
const highlighter = new Highlight()
17-
18-
assert.strictEqual(highlighter.highlight(text1, searchTerm1).HTML, expectedResult1)
19-
assert.strictEqual(highlighter.highlight(text2, searchTerm2).HTML, expectedResult2)
20-
})
21-
22-
it('should return the correct positions', () => {
23-
const text = 'The quick brown fox jumps over the lazy dog'
24-
const searchTerm = 'fox'
25-
const expectedPositions = [{ start: 16, end: 18 }]
26-
27-
const highlighter = new Highlight()
28-
29-
assert.deepStrictEqual(highlighter.highlight(text, searchTerm).positions, expectedPositions)
30-
})
31-
32-
it('should return multiple positions', () => {
33-
const text = 'The quick brown fox jumps over the lazy dog'
34-
const searchTerm = 'the'
35-
const expectedPositions = [{ start: 0, end: 2 }, { start: 31, end: 33 }]
36-
37-
const highlighter = new Highlight()
38-
39-
assert.deepStrictEqual(highlighter.highlight(text, searchTerm).positions, expectedPositions)
40-
})
41-
})
42-
43-
describe('custom configuration', () => {
44-
it('should correctly highlight a text (case sensitive)', () => {
45-
const text1 = 'The quick brown fox jumps over the lazy dog'
46-
const searchTerm1 = 'Fox'
47-
const expectedResult1 = 'The quick brown fox jumps over the lazy dog'
48-
49-
const text2 = 'Yesterday all my troubles seemed so far away, now it looks as though they\'re here to stay oh, I believe in yesterday'
50-
const searchTerm2 = 'yesterday I was in trouble'
51-
const expectedResult2 = 'Yesterday all my <mark class="orama-highlight">trouble</mark>s seemed so far away, now it looks as though they\'re here to stay oh, <mark class="orama-highlight">I</mark> believe <mark class="orama-highlight">in</mark> <mark class="orama-highlight">yesterday</mark>'
52-
53-
const highlighter = new Highlight({ caseSensitive: true })
54-
55-
assert.strictEqual(highlighter.highlight(text1, searchTerm1).HTML, expectedResult1)
56-
assert.strictEqual(highlighter.highlight(text2, searchTerm2).HTML, expectedResult2)
57-
})
58-
59-
it('should correctly set a custom CSS class', () => {
60-
const text = 'The quick brown fox jumps over the lazy dog'
61-
const searchTerm = 'fox'
62-
const expectedResult = 'The quick brown <mark class="custom-class">fox</mark> jumps over the lazy dog'
63-
64-
const highlighter = new Highlight({ CSSClass: 'custom-class' })
65-
66-
assert.strictEqual(highlighter.highlight(text, searchTerm).HTML, expectedResult)
67-
})
68-
69-
it('should correctly use a custom HTML tag', () => {
70-
const text = 'The quick brown fox jumps over the lazy dog'
71-
const searchTerm = 'fox'
72-
const expectedResult = 'The quick brown <div class="orama-highlight">fox</div> jumps over the lazy dog'
73-
74-
const highlighter = new Highlight({ HTMLTag: 'div' })
75-
76-
assert.strictEqual(highlighter.highlight(text, searchTerm).HTML, expectedResult)
77-
})
78-
79-
it('should correctly highlight whole words only', () => {
80-
const text = 'The quick brown fox jumps over the lazy dog'
81-
const searchTerm = 'fox jump'
82-
const expectedResult = 'The quick brown <mark class="orama-highlight">fox</mark> jumps over the lazy dog'
83-
84-
const highlighter = new Highlight({ wholeWords: true })
85-
86-
assert.strictEqual(highlighter.highlight(text, searchTerm).HTML, expectedResult)
87-
})
88-
})
89-
90-
describe('highlight function - infinite loop protection', () => {
91-
let regexExecStub: sinon.SinonStub
1+
import { describe, it, beforeEach, afterEach } from "bun:test";
2+
import assert from "node:assert";
3+
import sinon from "sinon";
4+
import { Highlight } from "./index.js";
5+
6+
describe("default configuration", () => {
7+
it("should correctly highlight a text", () => {
8+
const text1 = "The quick brown fox jumps over the lazy dog";
9+
const searchTerm1 = "fox";
10+
const expectedResult1 =
11+
'The quick brown <mark class="orama-highlight">fox</mark> jumps over the lazy dog';
12+
13+
const text2 =
14+
"Yesterday all my troubles seemed so far away, now it looks as though they're here to stay oh, I believe in yesterday";
15+
const searchTerm2 = "yesterday I was in trouble";
16+
const expectedResult2 =
17+
'<mark class="orama-highlight">Yesterday</mark> all my <mark class="orama-highlight">trouble</mark>s seemed so far away, now <mark class="orama-highlight">i</mark>t looks as though they\'re here to stay oh, <mark class="orama-highlight">I</mark> bel<mark class="orama-highlight">i</mark>eve <mark class="orama-highlight">i</mark>n <mark class="orama-highlight">yesterday</mark>';
18+
19+
const highlighter = new Highlight();
20+
21+
assert.strictEqual(
22+
highlighter.highlight(text1, searchTerm1).HTML,
23+
expectedResult1
24+
);
25+
assert.strictEqual(
26+
highlighter.highlight(text2, searchTerm2).HTML,
27+
expectedResult2
28+
);
29+
});
30+
31+
it("should return the correct positions", () => {
32+
const text = "The quick brown fox jumps over the lazy dog";
33+
const searchTerm = "fox";
34+
const expectedPositions = [{ start: 16, end: 18 }];
35+
36+
const highlighter = new Highlight();
37+
38+
assert.deepStrictEqual(
39+
highlighter.highlight(text, searchTerm).positions,
40+
expectedPositions
41+
);
42+
});
43+
44+
it("should return multiple positions", () => {
45+
const text = "The quick brown fox jumps over the lazy dog";
46+
const searchTerm = "the";
47+
const expectedPositions = [
48+
{ start: 0, end: 2 },
49+
{ start: 31, end: 33 },
50+
];
51+
52+
const highlighter = new Highlight();
53+
54+
assert.deepStrictEqual(
55+
highlighter.highlight(text, searchTerm).positions,
56+
expectedPositions
57+
);
58+
});
59+
});
60+
61+
describe("custom configuration", () => {
62+
it("should correctly highlight a text (case sensitive)", () => {
63+
const text1 = "The quick brown fox jumps over the lazy dog";
64+
const searchTerm1 = "Fox";
65+
const expectedResult1 = "The quick brown fox jumps over the lazy dog";
66+
67+
const text2 =
68+
"Yesterday all my troubles seemed so far away, now it looks as though they're here to stay oh, I believe in yesterday";
69+
const searchTerm2 = "yesterday I was in trouble";
70+
const expectedResult2 =
71+
'Yesterday all my <mark class="orama-highlight">trouble</mark>s seemed so far away, now it looks as though they\'re here to stay oh, <mark class="orama-highlight">I</mark> believe <mark class="orama-highlight">in</mark> <mark class="orama-highlight">yesterday</mark>';
72+
73+
const highlighter = new Highlight({ caseSensitive: true });
74+
75+
assert.strictEqual(
76+
highlighter.highlight(text1, searchTerm1).HTML,
77+
expectedResult1
78+
);
79+
assert.strictEqual(
80+
highlighter.highlight(text2, searchTerm2).HTML,
81+
expectedResult2
82+
);
83+
});
84+
85+
it("should correctly set a custom CSS class", () => {
86+
const text = "The quick brown fox jumps over the lazy dog";
87+
const searchTerm = "fox";
88+
const expectedResult =
89+
'The quick brown <mark class="custom-class">fox</mark> jumps over the lazy dog';
90+
91+
const highlighter = new Highlight({ CSSClass: "custom-class" });
92+
93+
assert.strictEqual(
94+
highlighter.highlight(text, searchTerm).HTML,
95+
expectedResult
96+
);
97+
});
98+
99+
it("should correctly use a custom HTML tag", () => {
100+
const text = "The quick brown fox jumps over the lazy dog";
101+
const searchTerm = "fox";
102+
const expectedResult =
103+
'The quick brown <div class="orama-highlight">fox</div> jumps over the lazy dog';
104+
105+
const highlighter = new Highlight({ HTMLTag: "div" });
106+
107+
assert.strictEqual(
108+
highlighter.highlight(text, searchTerm).HTML,
109+
expectedResult
110+
);
111+
});
112+
113+
it("should correctly highlight whole words only", () => {
114+
const text = "The quick brown fox jumps over the lazy dog";
115+
const searchTerm = "fox jump";
116+
const expectedResult =
117+
'The quick brown <mark class="orama-highlight">fox</mark> jumps over the lazy dog';
118+
119+
const highlighter = new Highlight({ wholeWords: true });
120+
121+
assert.strictEqual(
122+
highlighter.highlight(text, searchTerm).HTML,
123+
expectedResult
124+
);
125+
});
126+
});
127+
128+
describe("highlight function - infinite loop protection", () => {
129+
let regexExecStub: sinon.SinonStub;
92130

93131
beforeEach(() => {
94-
regexExecStub = sinon.stub(RegExp.prototype, 'exec')
95-
})
132+
regexExecStub = sinon.stub(RegExp.prototype, "exec");
133+
});
96134

97135
afterEach(() => {
98-
regexExecStub.restore()
99-
})
136+
regexExecStub.restore();
137+
});
100138

101-
it('should exit the loop if regex.lastIndex does not advance', () => {
102-
const text = 'The quick brown fox jumps over the lazy dog'
103-
const searchTerm = 'fox'
139+
it("should exit the loop if regex.lastIndex does not advance", () => {
140+
const text = "The quick brown fox jumps over the lazy dog";
141+
const searchTerm = "fox";
104142

105143
regexExecStub.callsFake(function () {
106144
// @ts-expect-error
107-
this.lastIndex = 0
108-
return null
109-
})
110-
111-
const highlighter = new Highlight()
112-
const result = highlighter.highlight(text, searchTerm)
113-
114-
assert.strictEqual(result.HTML, text)
115-
116-
assert(regexExecStub.called)
117-
})
118-
})
119-
120-
describe('trim method', () => {
121-
it('should correctly trim the text', () => {
122-
const text = 'The quick brown fox jumps over the lazy dog'
123-
const searchTerm = 'fox'
124-
const highlighter = new Highlight()
125-
126-
assert.strictEqual(highlighter.highlight(text, searchTerm).trim(10), '...rown <mark class="orama-highlight">fox</mark> j...')
127-
assert.strictEqual(highlighter.highlight(text, searchTerm).trim(5), '...n <mark class="orama-highlight">fox</mark>...')
128-
assert.strictEqual(highlighter.highlight(text, 'the').trim(5), '<mark class="orama-highlight">The</mark> q...')
129-
assert.strictEqual(highlighter.highlight(text, 'dog').trim(5), '...y <mark class="orama-highlight">dog</mark>')
130-
assert.strictEqual(highlighter.highlight(text, 'dog').trim(5, false), 'y <mark class="orama-highlight">dog</mark>')
131-
assert.strictEqual(highlighter.highlight(text, 'the').trim(5, false), '<mark class="orama-highlight">The</mark> q')
132-
})
133-
})
134-
135-
describe('special characters', () => {
136-
it('should correctly highlight a text with special characters', () => {
137-
const text = 'C++ is a hell of a language'
138-
const searchTerm = 'C++'
139-
const expectedResult = '<mark class="orama-highlight">C++</mark> is a hell of a language'
140-
141-
const highlighter = new Highlight()
142-
143-
assert.strictEqual(highlighter.highlight(text, searchTerm).HTML, expectedResult)
144-
})
145-
})
145+
this.lastIndex = 0;
146+
return null;
147+
});
148+
149+
const highlighter = new Highlight();
150+
const result = highlighter.highlight(text, searchTerm);
151+
152+
assert.strictEqual(result.HTML, text);
153+
154+
assert(regexExecStub.called);
155+
});
156+
});
157+
158+
describe("trim method", () => {
159+
it("should correctly trim the text", () => {
160+
const text = "The quick brown fox jumps over the lazy dog";
161+
const searchTerm = "fox";
162+
const highlighter = new Highlight();
163+
164+
assert.strictEqual(
165+
highlighter.highlight(text, searchTerm).trim(10),
166+
'...rown <mark class="orama-highlight">fox</mark> j...'
167+
);
168+
assert.strictEqual(
169+
highlighter.highlight(text, searchTerm).trim(5),
170+
'...n <mark class="orama-highlight">fox</mark>...'
171+
);
172+
assert.strictEqual(
173+
highlighter.highlight(text, "the").trim(5),
174+
'<mark class="orama-highlight">The</mark> q...'
175+
);
176+
assert.strictEqual(
177+
highlighter.highlight(text, "dog").trim(5),
178+
'...y <mark class="orama-highlight">dog</mark>'
179+
);
180+
assert.strictEqual(
181+
highlighter.highlight(text, "dog").trim(5, false),
182+
'y <mark class="orama-highlight">dog</mark>'
183+
);
184+
assert.strictEqual(
185+
highlighter.highlight(text, "the").trim(5, false),
186+
'<mark class="orama-highlight">The</mark> q'
187+
);
188+
});
189+
});
190+
191+
describe("special characters", () => {
192+
it("should correctly highlight a text with special characters", () => {
193+
const text = "C++ is a hell of a language";
194+
const searchTerm = "C++";
195+
const expectedResult =
196+
'<mark class="orama-highlight">C++</mark> is a hell of a language';
197+
198+
const highlighter = new Highlight();
199+
200+
assert.strictEqual(
201+
highlighter.highlight(text, searchTerm).HTML,
202+
expectedResult
203+
);
204+
});
205+
});
206+
207+
describe("empty example", () => {
208+
it("should not break when text is null", () => {
209+
const searchTerm = "C";
210+
const highlighter = new Highlight();
211+
212+
// even though it is not expected we should make sure it won't break
213+
// @ts-expect-error
214+
assert.strictEqual(highlighter.highlight(null, searchTerm).HTML, "");
215+
});
216+
});

0 commit comments

Comments
 (0)