Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Schniz/cmd-ts. 💻 A type-driven command line argument parser cmd-ts.now.sh
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061export type Highlighted = { text: string; highlighted: boolean };import flatMap from 'lodash/flatMap';import { findAll } from 'highlight-words-core';
export function highlight( text: string, query: string, padding?: number): Highlighted[] { const result = findAll({ searchWords: query.split(/\s+/), textToHighlight: text, }).map(indices => { return { text: text.slice(indices.start, indices.end), highlighted: indices.highlight, }; });
const shorterResults = flatMap(result, (t, index, arr) => { if (!padding) return [t]; if (t.highlighted) { return [t]; }
const results: Highlighted[] = [];
if ( arr[index - 1]?.highlighted && arr[index + 1]?.highlighted && t.text.length < padding + 6 ) { results.push(t); } else if (arr[index - 1]?.highlighted) { let text = padding > t.text.length ? t.text : t.text.slice(0, padding) + '...'; results.push({ text, highlighted: false, }); } else if (arr[index + 1]?.highlighted) { let text = padding > t.text.length ? t.text : '...' + t.text.slice(t.text.length - padding); results.push({ text, highlighted: false, }); }
return results; });
if (shorterResults.length === 0) { return [{ text: text.slice(0, (padding ?? 25) * 4), highlighted: false }]; }
return shorterResults;}