Skip to content

Commit 10bd111

Browse files
authored
Merge pull request #12 from askorama/failsafe-when-search-null
fix: should not break when search is null
2 parents c2a0487 + 266c807 commit 10bd111

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/index.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,21 @@ describe("special characters", () => {
205205
});
206206

207207
describe("empty example", () => {
208+
// even though it is not expected we should make sure it won't break
208209
it("should not break when text is null", () => {
209210
const searchTerm = "C";
210211
const highlighter = new Highlight();
211212

212-
// even though it is not expected we should make sure it won't break
213213
// @ts-expect-error
214214
assert.strictEqual(highlighter.highlight(null, searchTerm).HTML, "");
215215
});
216+
it("should not break when search term is null", () => {
217+
const highlighter = new Highlight();
218+
219+
assert.strictEqual(
220+
// @ts-expect-error
221+
highlighter.highlight(null, null).HTML,
222+
'<mark class="orama-highlight"></mark>'
223+
);
224+
});
216225
});

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class Highlight {
2727
}
2828

2929
public highlight(text: string, searchTerm: string): Highlight {
30-
this._searchTerm = searchTerm;
30+
this._searchTerm = searchTerm ?? "";
3131
this._originalText = text ?? "";
3232

3333
const caseSensitive =
@@ -38,7 +38,7 @@ export class Highlight {
3838
const regexFlags = caseSensitive ? "g" : "gi";
3939
const boundary = wholeWords ? "\\b" : "";
4040
const searchTerms = this.escapeRegExp(
41-
caseSensitive ? searchTerm : searchTerm.toLowerCase()
41+
caseSensitive ? this._searchTerm : this._searchTerm.toLowerCase()
4242
)
4343
.trim()
4444
.split(/\s+/)

0 commit comments

Comments
 (0)