2017-02-10 20:04:52 +03:00
|
|
|
//@flow
|
2017-06-23 20:30:16 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Aviral Dasgupta
|
2018-06-23 04:20:41 +03:00
|
|
|
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
|
2017-06-23 20:30:16 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2017-02-10 20:04:52 +03:00
|
|
|
|
|
|
|
import _at from 'lodash/at';
|
|
|
|
import _flatMap from 'lodash/flatMap';
|
|
|
|
import _sortBy from 'lodash/sortBy';
|
2017-07-18 18:23:54 +03:00
|
|
|
import _uniq from 'lodash/uniq';
|
2017-02-10 20:04:52 +03:00
|
|
|
import _keys from 'lodash/keys';
|
|
|
|
|
|
|
|
class KeyMap {
|
|
|
|
keys: Array<String>;
|
|
|
|
objectMap: {[String]: Array<Object>};
|
|
|
|
priorityMap = new Map();
|
|
|
|
}
|
|
|
|
|
2018-06-23 04:20:41 +03:00
|
|
|
function stripDiacritics(str: string): string {
|
|
|
|
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
|
|
|
|
}
|
|
|
|
|
2017-02-10 20:04:52 +03:00
|
|
|
export default class QueryMatcher {
|
|
|
|
/**
|
2017-06-23 19:35:07 +03:00
|
|
|
* @param {object[]} objects the objects to perform a match on
|
|
|
|
* @param {string[]} keys an array of keys within each object to match on
|
2017-02-10 20:04:52 +03:00
|
|
|
* Keys can refer to object properties by name and as in JavaScript (for nested properties)
|
|
|
|
*
|
|
|
|
* To use, simply presort objects by required criteria, run through this function and create a QueryMatcher with the
|
|
|
|
* resulting KeyMap.
|
|
|
|
*
|
|
|
|
* TODO: Handle arrays and objects (Fuse did this, RoomProvider uses it)
|
2017-06-23 19:35:07 +03:00
|
|
|
* @return {KeyMap}
|
2017-02-10 20:04:52 +03:00
|
|
|
*/
|
|
|
|
static valuesToKeyMap(objects: Array<Object>, keys: Array<String>): KeyMap {
|
|
|
|
const keyMap = new KeyMap();
|
|
|
|
const map = {};
|
|
|
|
|
|
|
|
objects.forEach((object, i) => {
|
|
|
|
const keyValues = _at(object, keys);
|
|
|
|
for (const keyValue of keyValues) {
|
2018-06-23 04:20:41 +03:00
|
|
|
const key = stripDiacritics(keyValue).toLowerCase();
|
|
|
|
if (!map.hasOwnProperty(key)) {
|
|
|
|
map[key] = [];
|
2017-02-10 20:04:52 +03:00
|
|
|
}
|
2018-06-23 04:20:41 +03:00
|
|
|
map[key].push(object);
|
2017-02-10 20:04:52 +03:00
|
|
|
}
|
|
|
|
keyMap.priorityMap.set(object, i);
|
|
|
|
});
|
|
|
|
|
|
|
|
keyMap.objectMap = map;
|
|
|
|
keyMap.keys = _keys(map);
|
|
|
|
return keyMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(objects: Array<Object>, options: {[Object]: Object} = {}) {
|
|
|
|
this.options = options;
|
|
|
|
this.keys = options.keys;
|
|
|
|
this.setObjects(objects);
|
2017-06-29 13:29:55 +03:00
|
|
|
|
|
|
|
// By default, we remove any non-alphanumeric characters ([^A-Za-z0-9_]) from the
|
|
|
|
// query and the value being queried before matching
|
|
|
|
if (this.options.shouldMatchWordsOnly === undefined) {
|
|
|
|
this.options.shouldMatchWordsOnly = true;
|
|
|
|
}
|
2017-07-04 18:24:59 +03:00
|
|
|
|
|
|
|
// By default, match anywhere in the string being searched. If enabled, only return
|
|
|
|
// matches that are prefixed with the query.
|
|
|
|
if (this.options.shouldMatchPrefix === undefined) {
|
|
|
|
this.options.shouldMatchPrefix = false;
|
|
|
|
}
|
2017-02-10 20:04:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setObjects(objects: Array<Object>) {
|
|
|
|
this.keyMap = QueryMatcher.valuesToKeyMap(objects, this.keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
match(query: String): Array<Object> {
|
2018-06-23 04:20:41 +03:00
|
|
|
query = stripDiacritics(query).toLowerCase();
|
2017-06-29 13:29:55 +03:00
|
|
|
if (this.options.shouldMatchWordsOnly) {
|
|
|
|
query = query.replace(/[^\w]/g, '');
|
|
|
|
}
|
2017-07-04 19:32:07 +03:00
|
|
|
if (query.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2017-07-04 15:53:06 +03:00
|
|
|
const results = [];
|
|
|
|
this.keyMap.keys.forEach((key) => {
|
2018-06-23 04:20:41 +03:00
|
|
|
let resultKey = key;
|
2017-06-29 13:29:55 +03:00
|
|
|
if (this.options.shouldMatchWordsOnly) {
|
|
|
|
resultKey = resultKey.replace(/[^\w]/g, '');
|
|
|
|
}
|
2017-07-04 15:53:06 +03:00
|
|
|
const index = resultKey.indexOf(query);
|
2017-07-04 18:24:59 +03:00
|
|
|
if (index !== -1 && (!this.options.shouldMatchPrefix || index === 0)) {
|
2017-07-04 15:53:06 +03:00
|
|
|
results.push({key, index});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 18:23:54 +03:00
|
|
|
return _uniq(_flatMap(_sortBy(results, (candidate) => {
|
2017-07-04 15:53:06 +03:00
|
|
|
return candidate.index;
|
|
|
|
}).map((candidate) => {
|
|
|
|
// return an array of objects (those given to setObjects) that have the given
|
|
|
|
// key as a property.
|
|
|
|
return this.keyMap.objectMap[candidate.key];
|
|
|
|
})));
|
2017-02-10 20:04:52 +03:00
|
|
|
}
|
|
|
|
}
|