Skip to content

Commit

Permalink
Merge pull request #12 from askorama/failsafe-when-search-null
Browse files Browse the repository at this point in the history
fix: should not break when search is null
  • Loading branch information
micheleriva committed Mar 25, 2024
2 parents c2a0487 + 266c807 commit 10bd111
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,21 @@ describe("special characters", () => {
});

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

// even though it is not expected we should make sure it won't break
// @ts-expect-error
assert.strictEqual(highlighter.highlight(null, searchTerm).HTML, "");
});
it("should not break when search term is null", () => {
const highlighter = new Highlight();

assert.strictEqual(
// @ts-expect-error
highlighter.highlight(null, null).HTML,
'<mark class="orama-highlight"></mark>'
);
});
});
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Highlight {
}

public highlight(text: string, searchTerm: string): Highlight {
this._searchTerm = searchTerm;
this._searchTerm = searchTerm ?? "";
this._originalText = text ?? "";

const caseSensitive =
Expand All @@ -38,7 +38,7 @@ export class Highlight {
const regexFlags = caseSensitive ? "g" : "gi";
const boundary = wholeWords ? "\\b" : "";
const searchTerms = this.escapeRegExp(
caseSensitive ? searchTerm : searchTerm.toLowerCase()
caseSensitive ? this._searchTerm : this._searchTerm.toLowerCase()
)
.trim()
.split(/\s+/)
Expand Down

0 comments on commit 10bd111

Please sign in to comment.