feat: universal search.

This commit is contained in:
a.bouhuolia
2021-08-21 18:59:49 +02:00
parent a7b0f1a8d2
commit 79c1b2ab67
82 changed files with 2497 additions and 317 deletions

View File

@@ -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;
}