Skip to content

Commit e3aad78

Browse files
authored
Merge pull request #5 from oramasearch/fix/always_return_lowercase
fix: returning always lowercase inside toString method
2 parents 8f86c62 + 7e0e617 commit e3aad78

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/index.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('default configuration', () => {
1010

1111
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'
1212
const searchTerm2 = 'yesterday I was in trouble'
13-
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>'
13+
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>'
1414

1515
assert.strictEqual(highlight(text1, searchTerm1).toString(), expectedResult1)
1616
assert.strictEqual(highlight(text2, searchTerm2).toString(), expectedResult2)
@@ -34,6 +34,19 @@ describe('default configuration', () => {
3434
})
3535

3636
describe('custom configuration', () => {
37+
it('should correctly highlight a text (case sensitive)', () => {
38+
const text1 = 'The quick brown fox jumps over the lazy dog'
39+
const searchTerm1 = 'Fox'
40+
const expectedResult1 = 'The quick brown fox jumps over the lazy dog'
41+
42+
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'
43+
const searchTerm2 = 'yesterday I was in trouble'
44+
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>'
45+
46+
assert.strictEqual(highlight(text1, searchTerm1, { caseSensitive: true }).toString(), expectedResult1)
47+
assert.strictEqual(highlight(text2, searchTerm2, { caseSensitive: true }).toString(), expectedResult2)
48+
})
49+
3750
it('should correctly set a custom CSS class', () => {
3851
const text = 'The quick brown fox jumps over the lazy dog'
3952
const searchTerm = 'fox'

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ export function highlight (text: string, searchTerm: string, options: HighlightO
3030
const highlightedParts: string[] = []
3131

3232
let match
33-
34-
const sourceText = caseSensitive ? text : text.toLowerCase()
3533
let lastEnd = 0
3634

37-
while ((match = regex.exec(sourceText)) !== null) {
35+
while ((match = regex.exec(text)) !== null) {
3836
const start = match.index
3937
const end = start + match[0].length - 1
4038

0 commit comments

Comments
 (0)