Skip to content

Commit

Permalink
Merge pull request #14 from askorama/max-chars
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Apr 8, 2024
2 parents 2203e2b + ee12eb3 commit de47d7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ describe("trim method", () => {
'<mark class="orama-highlight">The</mark> q'
);
});
it("should correctly trim the text when no match is found", () => {
const text = "The quick brown dog jumps over the lazy dog in a forrest";
const searchTerm = "fox";
const highlighter = new Highlight();

assert.strictEqual(
highlighter.highlight(text, searchTerm).trim(10),
"The quick ..."
);
assert.strictEqual(
highlighter.highlight(text, searchTerm).trim(10, false),
"The quick "
);
});
});

describe("special characters", () => {
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ export class Highlight {
}

public trim(trimLength: number, ellipsis: boolean = true): string {
if (
this._positions.length === 0 ||
this._originalText.length <= trimLength
) {
if (this._positions.length === 0) {
return `${this._HTML.substring(0, trimLength)}${ellipsis ? `...` : ""}`;
}

if (this._originalText.length <= trimLength) {
return this._HTML;
}

Expand Down

0 comments on commit de47d7f

Please sign in to comment.