mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat: universal search.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import React from 'react';
|
||||
import moment from 'moment';
|
||||
import _ from 'lodash';
|
||||
import * as R from 'ramda';
|
||||
@@ -751,4 +752,39 @@ export const RESORUCE_TYPE = {
|
||||
ACCOUNTS: 'account',
|
||||
ITEMS: 'items',
|
||||
|
||||
}
|
||||
|
||||
function escapeRegExpChars(text) {
|
||||
return text.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
|
||||
}
|
||||
|
||||
export function highlightText(text, query) {
|
||||
let lastIndex = 0;
|
||||
const words = query
|
||||
.split(/\s+/)
|
||||
.filter((word) => word.length > 0)
|
||||
.map(escapeRegExpChars);
|
||||
if (words.length === 0) {
|
||||
return [text];
|
||||
}
|
||||
const regexp = new RegExp(words.join('|'), 'gi');
|
||||
const tokens = [];
|
||||
while (true) {
|
||||
const match = regexp.exec(text);
|
||||
if (!match) {
|
||||
break;
|
||||
}
|
||||
const length = match[0].length;
|
||||
const before = text.slice(lastIndex, regexp.lastIndex - length);
|
||||
if (before.length > 0) {
|
||||
tokens.push(before);
|
||||
}
|
||||
lastIndex = regexp.lastIndex;
|
||||
tokens.push(<strong key={lastIndex}>{match[0]}</strong>);
|
||||
}
|
||||
const rest = text.slice(lastIndex);
|
||||
if (rest.length > 0) {
|
||||
tokens.push(rest);
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
Reference in New Issue
Block a user