-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from oramasearch/feat/trim
BREAKING: Adds trim function (implements #2)
- Loading branch information
Showing
5 changed files
with
113 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,93 @@ | ||
interface Highlight { | ||
positions: Array<{ start: number, end: number }> | ||
toString: () => string | ||
} | ||
|
||
export interface HighlightOptions { | ||
caseSensitive?: boolean | ||
wholeWords?: boolean | ||
HTMLTag?: string | ||
CSSClass?: string | ||
} | ||
|
||
type Positions = Array<{ start: number, end: number }> | ||
|
||
const defaultOptions: HighlightOptions = { | ||
caseSensitive: false, | ||
wholeWords: false, | ||
HTMLTag: 'mark', | ||
CSSClass: 'orama-highlight' | ||
} | ||
|
||
export function highlight (text: string, searchTerm: string, options: HighlightOptions = defaultOptions): Highlight { | ||
const caseSensitive = options.caseSensitive ?? defaultOptions.caseSensitive | ||
const wholeWords = options.wholeWords ?? defaultOptions.wholeWords | ||
const HTMLTag = options.HTMLTag ?? defaultOptions.HTMLTag | ||
const CSSClass = options.CSSClass ?? defaultOptions.CSSClass | ||
const regexFlags = caseSensitive ? 'g' : 'gi' | ||
const boundary = wholeWords ? '\\b' : '' | ||
const searchTerms = (caseSensitive ? searchTerm : searchTerm.toLowerCase()).trim().split(/\s+/).join('|') | ||
const regex = new RegExp(`${boundary}${searchTerms}${boundary}`, regexFlags) | ||
const positions: Array<{ start: number, end: number }> = [] | ||
const highlightedParts: string[] = [] | ||
|
||
let match | ||
let lastEnd = 0 | ||
let previousLastIndex = -1 | ||
|
||
while ((match = regex.exec(text)) !== null) { | ||
if (regex.lastIndex === previousLastIndex) { | ||
break | ||
export class Highlight { | ||
private readonly options: HighlightOptions | ||
private _positions: Positions = [] | ||
private _HTML: string = '' | ||
private _searchTerm: string = '' | ||
private _originalText: string = '' | ||
|
||
constructor (options: HighlightOptions = defaultOptions) { | ||
this.options = { ...defaultOptions, ...options } | ||
} | ||
|
||
public highlight (text: string, searchTerm: string): Highlight { | ||
this._searchTerm = searchTerm | ||
this._originalText = text | ||
|
||
const caseSensitive = this.options.caseSensitive ?? defaultOptions.caseSensitive | ||
const wholeWords = this.options.wholeWords ?? defaultOptions.wholeWords | ||
const HTMLTag = this.options.HTMLTag ?? defaultOptions.HTMLTag | ||
const CSSClass = this.options.CSSClass ?? defaultOptions.CSSClass | ||
const regexFlags = caseSensitive ? 'g' : 'gi' | ||
const boundary = wholeWords ? '\\b' : '' | ||
const searchTerms = (caseSensitive ? searchTerm : searchTerm.toLowerCase()).trim().split(/\s+/).join('|') | ||
const regex = new RegExp(`${boundary}${searchTerms}${boundary}`, regexFlags) | ||
const positions: Array<{ start: number, end: number }> = [] | ||
const highlightedParts: string[] = [] | ||
|
||
let match | ||
let lastEnd = 0 | ||
let previousLastIndex = -1 | ||
|
||
while ((match = regex.exec(text)) !== null) { | ||
if (regex.lastIndex === previousLastIndex) { | ||
break | ||
} | ||
previousLastIndex = regex.lastIndex | ||
|
||
const start = match.index | ||
const end = start + match[0].length - 1 | ||
|
||
positions.push({ start, end }) | ||
|
||
highlightedParts.push(text.slice(lastEnd, start)) | ||
highlightedParts.push(`<${HTMLTag} class="${CSSClass}">${match[0]}</${HTMLTag}>`) | ||
|
||
lastEnd = end + 1 | ||
} | ||
previousLastIndex = regex.lastIndex | ||
|
||
const start = match.index | ||
const end = start + match[0].length - 1 | ||
highlightedParts.push(text.slice(lastEnd)) | ||
|
||
positions.push({ start, end }) | ||
this._positions = positions | ||
this._HTML = highlightedParts.join('') | ||
|
||
highlightedParts.push(text.slice(lastEnd, start)) | ||
highlightedParts.push(`<${HTMLTag} class="${CSSClass}">${match[0]}</${HTMLTag}>`) | ||
return this | ||
} | ||
|
||
lastEnd = end + 1 | ||
public trim (trimLength: number, ellipsis: boolean = true): string { | ||
if (this._positions.length === 0 || this._originalText.length <= trimLength) { | ||
return this._HTML | ||
} | ||
|
||
const firstMatch = this._positions[0].start | ||
const start = Math.max(firstMatch - Math.floor(trimLength / 2), 0) | ||
const end = Math.min(start + trimLength, this._originalText.length) | ||
const trimmedContent = `${start === 0 || !ellipsis ? '' : '...'}${this._originalText.slice(start, end)}${end < this._originalText.length && ellipsis ? '...' : ''}` | ||
|
||
this.highlight(trimmedContent, this._searchTerm) | ||
return this._HTML | ||
} | ||
|
||
highlightedParts.push(text.slice(lastEnd)) | ||
get positions (): Positions { | ||
return this._positions | ||
} | ||
|
||
return { | ||
positions, | ||
toString: () => highlightedParts.join('') | ||
get HTML (): string { | ||
return this._HTML | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.