UNPKG

2.6 kBJavaScriptView Raw
1import { latinMap } from './latin-map';
2export var TypeaheadUtils = (function () {
3 function TypeaheadUtils() {
4 }
5 TypeaheadUtils.latinize = function (str) {
6 if (!str) {
7 return '';
8 }
9 return str.replace(/[^A-Za-z0-9\[\] ]/g, function (a) {
10 return TypeaheadUtils.latinMap[a] || a;
11 });
12 };
13 TypeaheadUtils.escapeRegexp = function (queryToEscape) {
14 // Regex: capture the whole query string and replace it with the string
15 // that will be used to match the results, for example if the capture is
16 // 'a' the result will be \a
17 return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
18 };
19 /* tslint:disable */
20 TypeaheadUtils.tokenize = function (str, wordRegexDelimiters, phraseRegexDelimiters) {
21 if (wordRegexDelimiters === void 0) { wordRegexDelimiters = ' '; }
22 if (phraseRegexDelimiters === void 0) { phraseRegexDelimiters = ''; }
23 /* tslint:enable */
24 var regexStr = '(?:[' + phraseRegexDelimiters + '])([^' + phraseRegexDelimiters + ']+)(?:[' + phraseRegexDelimiters + '])|([^' + wordRegexDelimiters + ']+)';
25 var preTokenized = str.split(new RegExp(regexStr, 'g'));
26 var result = [];
27 var preTokenizedLength = preTokenized.length;
28 var token;
29 var replacePhraseDelimiters = new RegExp('[' + phraseRegexDelimiters + ']+', 'g');
30 for (var i = 0; i < preTokenizedLength; i += 1) {
31 token = preTokenized[i];
32 if (token && token.length && token !== wordRegexDelimiters) {
33 result.push(token.replace(replacePhraseDelimiters, ''));
34 }
35 }
36 return result;
37 };
38 TypeaheadUtils.getValueFromObject = function (object, option) {
39 if (!option || typeof object !== 'object') {
40 return object.toString();
41 }
42 if (option.endsWith('()')) {
43 var functionName = option.slice(0, option.length - 2);
44 return object[functionName]().toString();
45 }
46 var properties = option.replace(/\[(\w+)\]/g, '.$1')
47 .replace(/^\./, '');
48 var propertiesArray = properties.split('.');
49 for (var _i = 0, propertiesArray_1 = propertiesArray; _i < propertiesArray_1.length; _i++) {
50 var property = propertiesArray_1[_i];
51 if (property in object) {
52 object = object[property];
53 }
54 }
55 return object.toString();
56 };
57 TypeaheadUtils.latinMap = latinMap;
58 return TypeaheadUtils;
59}());
60//# sourceMappingURL=typeahead-utils.js.map
\No newline at end of file