fuse.basic.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /**
  2. * Fuse.js v6.4.3 - Lightweight fuzzy-search (http://fusejs.io)
  3. *
  4. * Copyright (c) 2020 Kiro Risk (http://kiro.me)
  5. * All Rights Reserved. Apache Software License 2.0
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. (function (global, factory) {
  10. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  11. typeof define === 'function' && define.amd ? define(factory) :
  12. (global = global || self, global.Fuse = factory());
  13. }(this, (function () { 'use strict';
  14. function _typeof(obj) {
  15. "@babel/helpers - typeof";
  16. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  17. _typeof = function (obj) {
  18. return typeof obj;
  19. };
  20. } else {
  21. _typeof = function (obj) {
  22. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  23. };
  24. }
  25. return _typeof(obj);
  26. }
  27. function _classCallCheck(instance, Constructor) {
  28. if (!(instance instanceof Constructor)) {
  29. throw new TypeError("Cannot call a class as a function");
  30. }
  31. }
  32. function _defineProperties(target, props) {
  33. for (var i = 0; i < props.length; i++) {
  34. var descriptor = props[i];
  35. descriptor.enumerable = descriptor.enumerable || false;
  36. descriptor.configurable = true;
  37. if ("value" in descriptor) descriptor.writable = true;
  38. Object.defineProperty(target, descriptor.key, descriptor);
  39. }
  40. }
  41. function _createClass(Constructor, protoProps, staticProps) {
  42. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  43. if (staticProps) _defineProperties(Constructor, staticProps);
  44. return Constructor;
  45. }
  46. function _defineProperty(obj, key, value) {
  47. if (key in obj) {
  48. Object.defineProperty(obj, key, {
  49. value: value,
  50. enumerable: true,
  51. configurable: true,
  52. writable: true
  53. });
  54. } else {
  55. obj[key] = value;
  56. }
  57. return obj;
  58. }
  59. function ownKeys(object, enumerableOnly) {
  60. var keys = Object.keys(object);
  61. if (Object.getOwnPropertySymbols) {
  62. var symbols = Object.getOwnPropertySymbols(object);
  63. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  64. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  65. });
  66. keys.push.apply(keys, symbols);
  67. }
  68. return keys;
  69. }
  70. function _objectSpread2(target) {
  71. for (var i = 1; i < arguments.length; i++) {
  72. var source = arguments[i] != null ? arguments[i] : {};
  73. if (i % 2) {
  74. ownKeys(Object(source), true).forEach(function (key) {
  75. _defineProperty(target, key, source[key]);
  76. });
  77. } else if (Object.getOwnPropertyDescriptors) {
  78. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  79. } else {
  80. ownKeys(Object(source)).forEach(function (key) {
  81. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  82. });
  83. }
  84. }
  85. return target;
  86. }
  87. function _toConsumableArray(arr) {
  88. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
  89. }
  90. function _arrayWithoutHoles(arr) {
  91. if (Array.isArray(arr)) return _arrayLikeToArray(arr);
  92. }
  93. function _iterableToArray(iter) {
  94. if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
  95. }
  96. function _unsupportedIterableToArray(o, minLen) {
  97. if (!o) return;
  98. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  99. var n = Object.prototype.toString.call(o).slice(8, -1);
  100. if (n === "Object" && o.constructor) n = o.constructor.name;
  101. if (n === "Map" || n === "Set") return Array.from(o);
  102. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  103. }
  104. function _arrayLikeToArray(arr, len) {
  105. if (len == null || len > arr.length) len = arr.length;
  106. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  107. return arr2;
  108. }
  109. function _nonIterableSpread() {
  110. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  111. }
  112. function isArray(value) {
  113. return !Array.isArray ? getTag(value) === '[object Array]' : Array.isArray(value);
  114. } // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
  115. var INFINITY = 1 / 0;
  116. function baseToString(value) {
  117. // Exit early for strings to avoid a performance hit in some environments.
  118. if (typeof value == 'string') {
  119. return value;
  120. }
  121. var result = value + '';
  122. return result == '0' && 1 / value == -INFINITY ? '-0' : result;
  123. }
  124. function toString(value) {
  125. return value == null ? '' : baseToString(value);
  126. }
  127. function isString(value) {
  128. return typeof value === 'string';
  129. }
  130. function isNumber(value) {
  131. return typeof value === 'number';
  132. } // Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
  133. function isBoolean(value) {
  134. return value === true || value === false || isObjectLike(value) && getTag(value) == '[object Boolean]';
  135. }
  136. function isObject(value) {
  137. return _typeof(value) === 'object';
  138. } // Checks if `value` is object-like.
  139. function isObjectLike(value) {
  140. return isObject(value) && value !== null;
  141. }
  142. function isDefined(value) {
  143. return value !== undefined && value !== null;
  144. }
  145. function isBlank(value) {
  146. return !value.trim().length;
  147. } // Gets the `toStringTag` of `value`.
  148. // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
  149. function getTag(value) {
  150. return value == null ? value === undefined ? '[object Undefined]' : '[object Null]' : Object.prototype.toString.call(value);
  151. }
  152. var EXTENDED_SEARCH_UNAVAILABLE = 'Extended search is not available';
  153. var LOGICAL_SEARCH_UNAVAILABLE = 'Logical search is not available';
  154. var INCORRECT_INDEX_TYPE = "Incorrect 'index' type";
  155. var LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY = function LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key) {
  156. return "Invalid value for key ".concat(key);
  157. };
  158. var PATTERN_LENGTH_TOO_LARGE = function PATTERN_LENGTH_TOO_LARGE(max) {
  159. return "Pattern length exceeds max of ".concat(max, ".");
  160. };
  161. var MISSING_KEY_PROPERTY = function MISSING_KEY_PROPERTY(name) {
  162. return "Missing ".concat(name, " property in key");
  163. };
  164. var INVALID_KEY_WEIGHT_VALUE = function INVALID_KEY_WEIGHT_VALUE(key) {
  165. return "Property 'weight' in key '".concat(key, "' must be a positive integer");
  166. };
  167. var hasOwn = Object.prototype.hasOwnProperty;
  168. var KeyStore = /*#__PURE__*/function () {
  169. function KeyStore(keys) {
  170. var _this = this;
  171. _classCallCheck(this, KeyStore);
  172. this._keys = [];
  173. this._keyMap = {};
  174. var totalWeight = 0;
  175. keys.forEach(function (key) {
  176. var obj = createKey(key);
  177. totalWeight += obj.weight;
  178. _this._keys.push(obj);
  179. _this._keyMap[obj.id] = obj;
  180. totalWeight += obj.weight;
  181. }); // Normalize weights so that their sum is equal to 1
  182. this._keys.forEach(function (key) {
  183. key.weight /= totalWeight;
  184. });
  185. }
  186. _createClass(KeyStore, [{
  187. key: "get",
  188. value: function get(keyId) {
  189. return this._keyMap[keyId];
  190. }
  191. }, {
  192. key: "keys",
  193. value: function keys() {
  194. return this._keys;
  195. }
  196. }, {
  197. key: "toJSON",
  198. value: function toJSON() {
  199. return JSON.stringify(this._keys);
  200. }
  201. }]);
  202. return KeyStore;
  203. }();
  204. function createKey(key) {
  205. var path = null;
  206. var id = null;
  207. var src = null;
  208. var weight = 1;
  209. if (isString(key) || isArray(key)) {
  210. src = key;
  211. path = createKeyPath(key);
  212. id = createKeyId(key);
  213. } else {
  214. if (!hasOwn.call(key, 'name')) {
  215. throw new Error(MISSING_KEY_PROPERTY('name'));
  216. }
  217. var name = key.name;
  218. src = name;
  219. if (hasOwn.call(key, 'weight')) {
  220. weight = key.weight;
  221. if (weight <= 0) {
  222. throw new Error(INVALID_KEY_WEIGHT_VALUE(name));
  223. }
  224. }
  225. path = createKeyPath(name);
  226. id = createKeyId(name);
  227. }
  228. return {
  229. path: path,
  230. id: id,
  231. weight: weight,
  232. src: src
  233. };
  234. }
  235. function createKeyPath(key) {
  236. return isArray(key) ? key : key.split('.');
  237. }
  238. function createKeyId(key) {
  239. return isArray(key) ? key.join('.') : key;
  240. }
  241. function get(obj, path) {
  242. var list = [];
  243. var arr = false;
  244. var deepGet = function deepGet(obj, path, index) {
  245. if (!isDefined(obj)) {
  246. return;
  247. }
  248. if (!path[index]) {
  249. // If there's no path left, we've arrived at the object we care about.
  250. list.push(obj);
  251. } else {
  252. var key = path[index];
  253. var value = obj[key];
  254. if (!isDefined(value)) {
  255. return;
  256. } // If we're at the last value in the path, and if it's a string/number/bool,
  257. // add it to the list
  258. if (index === path.length - 1 && (isString(value) || isNumber(value) || isBoolean(value))) {
  259. list.push(toString(value));
  260. } else if (isArray(value)) {
  261. arr = true; // Search each item in the array.
  262. for (var i = 0, len = value.length; i < len; i += 1) {
  263. deepGet(value[i], path, index + 1);
  264. }
  265. } else if (path.length) {
  266. // An object. Recurse further.
  267. deepGet(value, path, index + 1);
  268. }
  269. }
  270. }; // Backwards compatibility (since path used to be a string)
  271. deepGet(obj, isString(path) ? path.split('.') : path, 0);
  272. return arr ? list : list[0];
  273. }
  274. var MatchOptions = {
  275. // Whether the matches should be included in the result set. When `true`, each record in the result
  276. // set will include the indices of the matched characters.
  277. // These can consequently be used for highlighting purposes.
  278. includeMatches: false,
  279. // When `true`, the matching function will continue to the end of a search pattern even if
  280. // a perfect match has already been located in the string.
  281. findAllMatches: false,
  282. // Minimum number of characters that must be matched before a result is considered a match
  283. minMatchCharLength: 1
  284. };
  285. var BasicOptions = {
  286. // When `true`, the algorithm continues searching to the end of the input even if a perfect
  287. // match is found before the end of the same input.
  288. isCaseSensitive: false,
  289. // When true, the matching function will continue to the end of a search pattern even if
  290. includeScore: false,
  291. // List of properties that will be searched. This also supports nested properties.
  292. keys: [],
  293. // Whether to sort the result list, by score
  294. shouldSort: true,
  295. // Default sort function: sort by ascending score, ascending index
  296. sortFn: function sortFn(a, b) {
  297. return a.score === b.score ? a.idx < b.idx ? -1 : 1 : a.score < b.score ? -1 : 1;
  298. }
  299. };
  300. var FuzzyOptions = {
  301. // Approximately where in the text is the pattern expected to be found?
  302. location: 0,
  303. // At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
  304. // (of both letters and location), a threshold of '1.0' would match anything.
  305. threshold: 0.6,
  306. // Determines how close the match must be to the fuzzy location (specified above).
  307. // An exact letter match which is 'distance' characters away from the fuzzy location
  308. // would score as a complete mismatch. A distance of '0' requires the match be at
  309. // the exact location specified, a threshold of '1000' would require a perfect match
  310. // to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.
  311. distance: 100
  312. };
  313. var AdvancedOptions = {
  314. // When `true`, it enables the use of unix-like search commands
  315. useExtendedSearch: false,
  316. // The get function to use when fetching an object's properties.
  317. // The default will search nested paths *ie foo.bar.baz*
  318. getFn: get,
  319. // When `true`, search will ignore `location` and `distance`, so it won't matter
  320. // where in the string the pattern appears.
  321. // More info: https://fusejs.io/concepts/scoring-theory.html#fuzziness-score
  322. ignoreLocation: false,
  323. // When `true`, the calculation for the relevance score (used for sorting) will
  324. // ignore the field-length norm.
  325. // More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
  326. ignoreFieldNorm: false
  327. };
  328. var Config = _objectSpread2({}, BasicOptions, {}, MatchOptions, {}, FuzzyOptions, {}, AdvancedOptions);
  329. var SPACE = /[^ ]+/g; // Field-length norm: the shorter the field, the higher the weight.
  330. // Set to 3 decimals to reduce index size.
  331. function norm() {
  332. var mantissa = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
  333. var cache = new Map();
  334. return {
  335. get: function get(value) {
  336. var numTokens = value.match(SPACE).length;
  337. if (cache.has(numTokens)) {
  338. return cache.get(numTokens);
  339. }
  340. var n = parseFloat((1 / Math.sqrt(numTokens)).toFixed(mantissa));
  341. cache.set(numTokens, n);
  342. return n;
  343. },
  344. clear: function clear() {
  345. cache.clear();
  346. }
  347. };
  348. }
  349. var FuseIndex = /*#__PURE__*/function () {
  350. function FuseIndex() {
  351. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  352. _ref$getFn = _ref.getFn,
  353. getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn;
  354. _classCallCheck(this, FuseIndex);
  355. this.norm = norm(3);
  356. this.getFn = getFn;
  357. this.isCreated = false;
  358. this.setIndexRecords();
  359. }
  360. _createClass(FuseIndex, [{
  361. key: "setSources",
  362. value: function setSources() {
  363. var docs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  364. this.docs = docs;
  365. }
  366. }, {
  367. key: "setIndexRecords",
  368. value: function setIndexRecords() {
  369. var records = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  370. this.records = records;
  371. }
  372. }, {
  373. key: "setKeys",
  374. value: function setKeys() {
  375. var _this = this;
  376. var keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  377. this.keys = keys;
  378. this._keysMap = {};
  379. keys.forEach(function (key, idx) {
  380. _this._keysMap[key.id] = idx;
  381. });
  382. }
  383. }, {
  384. key: "create",
  385. value: function create() {
  386. var _this2 = this;
  387. if (this.isCreated || !this.docs.length) {
  388. return;
  389. }
  390. this.isCreated = true; // List is Array<String>
  391. if (isString(this.docs[0])) {
  392. this.docs.forEach(function (doc, docIndex) {
  393. _this2._addString(doc, docIndex);
  394. });
  395. } else {
  396. // List is Array<Object>
  397. this.docs.forEach(function (doc, docIndex) {
  398. _this2._addObject(doc, docIndex);
  399. });
  400. }
  401. this.norm.clear();
  402. } // Adds a doc to the end of the index
  403. }, {
  404. key: "add",
  405. value: function add(doc) {
  406. var idx = this.size();
  407. if (isString(doc)) {
  408. this._addString(doc, idx);
  409. } else {
  410. this._addObject(doc, idx);
  411. }
  412. } // Removes the doc at the specified index of the index
  413. }, {
  414. key: "removeAt",
  415. value: function removeAt(idx) {
  416. this.records.splice(idx, 1); // Change ref index of every subsquent doc
  417. for (var i = idx, len = this.size(); i < len; i += 1) {
  418. this.records[i].i -= 1;
  419. }
  420. }
  421. }, {
  422. key: "getValueForItemAtKeyId",
  423. value: function getValueForItemAtKeyId(item, keyId) {
  424. return item[this._keysMap[keyId]];
  425. }
  426. }, {
  427. key: "size",
  428. value: function size() {
  429. return this.records.length;
  430. }
  431. }, {
  432. key: "_addString",
  433. value: function _addString(doc, docIndex) {
  434. if (!isDefined(doc) || isBlank(doc)) {
  435. return;
  436. }
  437. var record = {
  438. v: doc,
  439. i: docIndex,
  440. n: this.norm.get(doc)
  441. };
  442. this.records.push(record);
  443. }
  444. }, {
  445. key: "_addObject",
  446. value: function _addObject(doc, docIndex) {
  447. var _this3 = this;
  448. var record = {
  449. i: docIndex,
  450. $: {}
  451. }; // Iterate over every key (i.e, path), and fetch the value at that key
  452. this.keys.forEach(function (key, keyIndex) {
  453. // console.log(key)
  454. var value = _this3.getFn(doc, key.path);
  455. if (!isDefined(value)) {
  456. return;
  457. }
  458. if (isArray(value)) {
  459. (function () {
  460. var subRecords = [];
  461. var stack = [{
  462. nestedArrIndex: -1,
  463. value: value
  464. }];
  465. while (stack.length) {
  466. var _stack$pop = stack.pop(),
  467. nestedArrIndex = _stack$pop.nestedArrIndex,
  468. _value = _stack$pop.value;
  469. if (!isDefined(_value)) {
  470. continue;
  471. }
  472. if (isString(_value) && !isBlank(_value)) {
  473. var subRecord = {
  474. v: _value,
  475. i: nestedArrIndex,
  476. n: _this3.norm.get(_value)
  477. };
  478. subRecords.push(subRecord);
  479. } else if (isArray(_value)) {
  480. _value.forEach(function (item, k) {
  481. stack.push({
  482. nestedArrIndex: k,
  483. value: item
  484. });
  485. });
  486. }
  487. }
  488. record.$[keyIndex] = subRecords;
  489. })();
  490. } else if (!isBlank(value)) {
  491. var subRecord = {
  492. v: value,
  493. n: _this3.norm.get(value)
  494. };
  495. record.$[keyIndex] = subRecord;
  496. }
  497. });
  498. this.records.push(record);
  499. }
  500. }, {
  501. key: "toJSON",
  502. value: function toJSON() {
  503. return {
  504. keys: this.keys,
  505. records: this.records
  506. };
  507. }
  508. }]);
  509. return FuseIndex;
  510. }();
  511. function createIndex(keys, docs) {
  512. var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  513. _ref2$getFn = _ref2.getFn,
  514. getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn;
  515. var myIndex = new FuseIndex({
  516. getFn: getFn
  517. });
  518. myIndex.setKeys(keys.map(createKey));
  519. myIndex.setSources(docs);
  520. myIndex.create();
  521. return myIndex;
  522. }
  523. function parseIndex(data) {
  524. var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  525. _ref3$getFn = _ref3.getFn,
  526. getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn;
  527. var keys = data.keys,
  528. records = data.records;
  529. var myIndex = new FuseIndex({
  530. getFn: getFn
  531. });
  532. myIndex.setKeys(keys);
  533. myIndex.setIndexRecords(records);
  534. return myIndex;
  535. }
  536. function transformMatches(result, data) {
  537. var matches = result.matches;
  538. data.matches = [];
  539. if (!isDefined(matches)) {
  540. return;
  541. }
  542. matches.forEach(function (match) {
  543. if (!isDefined(match.indices) || !match.indices.length) {
  544. return;
  545. }
  546. var indices = match.indices,
  547. value = match.value;
  548. var obj = {
  549. indices: indices,
  550. value: value
  551. };
  552. if (match.key) {
  553. obj.key = match.key.src;
  554. }
  555. if (match.idx > -1) {
  556. obj.refIndex = match.idx;
  557. }
  558. data.matches.push(obj);
  559. });
  560. }
  561. function transformScore(result, data) {
  562. data.score = result.score;
  563. }
  564. function computeScore(pattern) {
  565. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  566. _ref$errors = _ref.errors,
  567. errors = _ref$errors === void 0 ? 0 : _ref$errors,
  568. _ref$currentLocation = _ref.currentLocation,
  569. currentLocation = _ref$currentLocation === void 0 ? 0 : _ref$currentLocation,
  570. _ref$expectedLocation = _ref.expectedLocation,
  571. expectedLocation = _ref$expectedLocation === void 0 ? 0 : _ref$expectedLocation,
  572. _ref$distance = _ref.distance,
  573. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  574. _ref$ignoreLocation = _ref.ignoreLocation,
  575. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  576. var accuracy = errors / pattern.length;
  577. if (ignoreLocation) {
  578. return accuracy;
  579. }
  580. var proximity = Math.abs(expectedLocation - currentLocation);
  581. if (!distance) {
  582. // Dodge divide by zero error.
  583. return proximity ? 1.0 : accuracy;
  584. }
  585. return accuracy + proximity / distance;
  586. }
  587. function convertMaskToIndices() {
  588. var matchmask = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  589. var minMatchCharLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Config.minMatchCharLength;
  590. var indices = [];
  591. var start = -1;
  592. var end = -1;
  593. var i = 0;
  594. for (var len = matchmask.length; i < len; i += 1) {
  595. var match = matchmask[i];
  596. if (match && start === -1) {
  597. start = i;
  598. } else if (!match && start !== -1) {
  599. end = i - 1;
  600. if (end - start + 1 >= minMatchCharLength) {
  601. indices.push([start, end]);
  602. }
  603. start = -1;
  604. }
  605. } // (i-1 - start) + 1 => i - start
  606. if (matchmask[i - 1] && i - start >= minMatchCharLength) {
  607. indices.push([start, i - 1]);
  608. }
  609. return indices;
  610. }
  611. // Machine word size
  612. var MAX_BITS = 32;
  613. function search(text, pattern, patternAlphabet) {
  614. var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
  615. _ref$location = _ref.location,
  616. location = _ref$location === void 0 ? Config.location : _ref$location,
  617. _ref$distance = _ref.distance,
  618. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  619. _ref$threshold = _ref.threshold,
  620. threshold = _ref$threshold === void 0 ? Config.threshold : _ref$threshold,
  621. _ref$findAllMatches = _ref.findAllMatches,
  622. findAllMatches = _ref$findAllMatches === void 0 ? Config.findAllMatches : _ref$findAllMatches,
  623. _ref$minMatchCharLeng = _ref.minMatchCharLength,
  624. minMatchCharLength = _ref$minMatchCharLeng === void 0 ? Config.minMatchCharLength : _ref$minMatchCharLeng,
  625. _ref$includeMatches = _ref.includeMatches,
  626. includeMatches = _ref$includeMatches === void 0 ? Config.includeMatches : _ref$includeMatches,
  627. _ref$ignoreLocation = _ref.ignoreLocation,
  628. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  629. if (pattern.length > MAX_BITS) {
  630. throw new Error(PATTERN_LENGTH_TOO_LARGE(MAX_BITS));
  631. }
  632. var patternLen = pattern.length; // Set starting location at beginning text and initialize the alphabet.
  633. var textLen = text.length; // Handle the case when location > text.length
  634. var expectedLocation = Math.max(0, Math.min(location, textLen)); // Highest score beyond which we give up.
  635. var currentThreshold = threshold; // Is there a nearby exact match? (speedup)
  636. var bestLocation = expectedLocation; // Performance: only computer matches when the minMatchCharLength > 1
  637. // OR if `includeMatches` is true.
  638. var computeMatches = minMatchCharLength > 1 || includeMatches; // A mask of the matches, used for building the indices
  639. var matchMask = computeMatches ? Array(textLen) : [];
  640. var index; // Get all exact matches, here for speed up
  641. while ((index = text.indexOf(pattern, bestLocation)) > -1) {
  642. var score = computeScore(pattern, {
  643. currentLocation: index,
  644. expectedLocation: expectedLocation,
  645. distance: distance,
  646. ignoreLocation: ignoreLocation
  647. });
  648. currentThreshold = Math.min(score, currentThreshold);
  649. bestLocation = index + patternLen;
  650. if (computeMatches) {
  651. var i = 0;
  652. while (i < patternLen) {
  653. matchMask[index + i] = 1;
  654. i += 1;
  655. }
  656. }
  657. } // Reset the best location
  658. bestLocation = -1;
  659. var lastBitArr = [];
  660. var finalScore = 1;
  661. var binMax = patternLen + textLen;
  662. var mask = 1 << patternLen - 1;
  663. for (var _i = 0; _i < patternLen; _i += 1) {
  664. // Scan for the best match; each iteration allows for one more error.
  665. // Run a binary search to determine how far from the match location we can stray
  666. // at this error level.
  667. var binMin = 0;
  668. var binMid = binMax;
  669. while (binMin < binMid) {
  670. var _score2 = computeScore(pattern, {
  671. errors: _i,
  672. currentLocation: expectedLocation + binMid,
  673. expectedLocation: expectedLocation,
  674. distance: distance,
  675. ignoreLocation: ignoreLocation
  676. });
  677. if (_score2 <= currentThreshold) {
  678. binMin = binMid;
  679. } else {
  680. binMax = binMid;
  681. }
  682. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  683. } // Use the result from this iteration as the maximum for the next.
  684. binMax = binMid;
  685. var start = Math.max(1, expectedLocation - binMid + 1);
  686. var finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen; // Initialize the bit array
  687. var bitArr = Array(finish + 2);
  688. bitArr[finish + 1] = (1 << _i) - 1;
  689. for (var j = finish; j >= start; j -= 1) {
  690. var currentLocation = j - 1;
  691. var charMatch = patternAlphabet[text.charAt(currentLocation)];
  692. if (computeMatches) {
  693. // Speed up: quick bool to int conversion (i.e, `charMatch ? 1 : 0`)
  694. matchMask[currentLocation] = +!!charMatch;
  695. } // First pass: exact match
  696. bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch; // Subsequent passes: fuzzy match
  697. if (_i) {
  698. bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
  699. }
  700. if (bitArr[j] & mask) {
  701. finalScore = computeScore(pattern, {
  702. errors: _i,
  703. currentLocation: currentLocation,
  704. expectedLocation: expectedLocation,
  705. distance: distance,
  706. ignoreLocation: ignoreLocation
  707. }); // This match will almost certainly be better than any existing match.
  708. // But check anyway.
  709. if (finalScore <= currentThreshold) {
  710. // Indeed it is
  711. currentThreshold = finalScore;
  712. bestLocation = currentLocation; // Already passed `loc`, downhill from here on in.
  713. if (bestLocation <= expectedLocation) {
  714. break;
  715. } // When passing `bestLocation`, don't exceed our current distance from `expectedLocation`.
  716. start = Math.max(1, 2 * expectedLocation - bestLocation);
  717. }
  718. }
  719. } // No hope for a (better) match at greater error levels.
  720. var _score = computeScore(pattern, {
  721. errors: _i + 1,
  722. currentLocation: expectedLocation,
  723. expectedLocation: expectedLocation,
  724. distance: distance,
  725. ignoreLocation: ignoreLocation
  726. });
  727. if (_score > currentThreshold) {
  728. break;
  729. }
  730. lastBitArr = bitArr;
  731. }
  732. var result = {
  733. isMatch: bestLocation >= 0,
  734. // Count exact matches (those with a score of 0) to be "almost" exact
  735. score: Math.max(0.001, finalScore)
  736. };
  737. if (computeMatches) {
  738. var indices = convertMaskToIndices(matchMask, minMatchCharLength);
  739. if (!indices.length) {
  740. result.isMatch = false;
  741. } else if (includeMatches) {
  742. result.indices = indices;
  743. }
  744. }
  745. return result;
  746. }
  747. function createPatternAlphabet(pattern) {
  748. var mask = {};
  749. for (var i = 0, len = pattern.length; i < len; i += 1) {
  750. var char = pattern.charAt(i);
  751. mask[char] = (mask[char] || 0) | 1 << len - i - 1;
  752. }
  753. return mask;
  754. }
  755. var BitapSearch = /*#__PURE__*/function () {
  756. function BitapSearch(pattern) {
  757. var _this = this;
  758. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  759. _ref$location = _ref.location,
  760. location = _ref$location === void 0 ? Config.location : _ref$location,
  761. _ref$threshold = _ref.threshold,
  762. threshold = _ref$threshold === void 0 ? Config.threshold : _ref$threshold,
  763. _ref$distance = _ref.distance,
  764. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  765. _ref$includeMatches = _ref.includeMatches,
  766. includeMatches = _ref$includeMatches === void 0 ? Config.includeMatches : _ref$includeMatches,
  767. _ref$findAllMatches = _ref.findAllMatches,
  768. findAllMatches = _ref$findAllMatches === void 0 ? Config.findAllMatches : _ref$findAllMatches,
  769. _ref$minMatchCharLeng = _ref.minMatchCharLength,
  770. minMatchCharLength = _ref$minMatchCharLeng === void 0 ? Config.minMatchCharLength : _ref$minMatchCharLeng,
  771. _ref$isCaseSensitive = _ref.isCaseSensitive,
  772. isCaseSensitive = _ref$isCaseSensitive === void 0 ? Config.isCaseSensitive : _ref$isCaseSensitive,
  773. _ref$ignoreLocation = _ref.ignoreLocation,
  774. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  775. _classCallCheck(this, BitapSearch);
  776. this.options = {
  777. location: location,
  778. threshold: threshold,
  779. distance: distance,
  780. includeMatches: includeMatches,
  781. findAllMatches: findAllMatches,
  782. minMatchCharLength: minMatchCharLength,
  783. isCaseSensitive: isCaseSensitive,
  784. ignoreLocation: ignoreLocation
  785. };
  786. this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
  787. this.chunks = [];
  788. if (!this.pattern.length) {
  789. return;
  790. }
  791. var addChunk = function addChunk(pattern, startIndex) {
  792. _this.chunks.push({
  793. pattern: pattern,
  794. alphabet: createPatternAlphabet(pattern),
  795. startIndex: startIndex
  796. });
  797. };
  798. var len = this.pattern.length;
  799. if (len > MAX_BITS) {
  800. var i = 0;
  801. var remainder = len % MAX_BITS;
  802. var end = len - remainder;
  803. while (i < end) {
  804. addChunk(this.pattern.substr(i, MAX_BITS), i);
  805. i += MAX_BITS;
  806. }
  807. if (remainder) {
  808. var startIndex = len - MAX_BITS;
  809. addChunk(this.pattern.substr(startIndex), startIndex);
  810. }
  811. } else {
  812. addChunk(this.pattern, 0);
  813. }
  814. }
  815. _createClass(BitapSearch, [{
  816. key: "searchIn",
  817. value: function searchIn(text) {
  818. var _this$options = this.options,
  819. isCaseSensitive = _this$options.isCaseSensitive,
  820. includeMatches = _this$options.includeMatches;
  821. if (!isCaseSensitive) {
  822. text = text.toLowerCase();
  823. } // Exact match
  824. if (this.pattern === text) {
  825. var _result = {
  826. isMatch: true,
  827. score: 0
  828. };
  829. if (includeMatches) {
  830. _result.indices = [[0, text.length - 1]];
  831. }
  832. return _result;
  833. } // Otherwise, use Bitap algorithm
  834. var _this$options2 = this.options,
  835. location = _this$options2.location,
  836. distance = _this$options2.distance,
  837. threshold = _this$options2.threshold,
  838. findAllMatches = _this$options2.findAllMatches,
  839. minMatchCharLength = _this$options2.minMatchCharLength,
  840. ignoreLocation = _this$options2.ignoreLocation;
  841. var allIndices = [];
  842. var totalScore = 0;
  843. var hasMatches = false;
  844. this.chunks.forEach(function (_ref2) {
  845. var pattern = _ref2.pattern,
  846. alphabet = _ref2.alphabet,
  847. startIndex = _ref2.startIndex;
  848. var _search = search(text, pattern, alphabet, {
  849. location: location + startIndex,
  850. distance: distance,
  851. threshold: threshold,
  852. findAllMatches: findAllMatches,
  853. minMatchCharLength: minMatchCharLength,
  854. includeMatches: includeMatches,
  855. ignoreLocation: ignoreLocation
  856. }),
  857. isMatch = _search.isMatch,
  858. score = _search.score,
  859. indices = _search.indices;
  860. if (isMatch) {
  861. hasMatches = true;
  862. }
  863. totalScore += score;
  864. if (isMatch && indices) {
  865. allIndices = [].concat(_toConsumableArray(allIndices), _toConsumableArray(indices));
  866. }
  867. });
  868. var result = {
  869. isMatch: hasMatches,
  870. score: hasMatches ? totalScore / this.chunks.length : 1
  871. };
  872. if (hasMatches && includeMatches) {
  873. result.indices = allIndices;
  874. }
  875. return result;
  876. }
  877. }]);
  878. return BitapSearch;
  879. }();
  880. var registeredSearchers = [];
  881. function createSearcher(pattern, options) {
  882. for (var i = 0, len = registeredSearchers.length; i < len; i += 1) {
  883. var searcherClass = registeredSearchers[i];
  884. if (searcherClass.condition(pattern, options)) {
  885. return new searcherClass(pattern, options);
  886. }
  887. }
  888. return new BitapSearch(pattern, options);
  889. }
  890. var LogicalOperator = {
  891. AND: '$and',
  892. OR: '$or'
  893. };
  894. var KeyType = {
  895. PATH: '$path',
  896. PATTERN: '$val'
  897. };
  898. var isExpression = function isExpression(query) {
  899. return !!(query[LogicalOperator.AND] || query[LogicalOperator.OR]);
  900. };
  901. var isPath = function isPath(query) {
  902. return !!query[KeyType.PATH];
  903. };
  904. var isLeaf = function isLeaf(query) {
  905. return !isArray(query) && isObject(query) && !isExpression(query);
  906. };
  907. var convertToExplicit = function convertToExplicit(query) {
  908. return _defineProperty({}, LogicalOperator.AND, Object.keys(query).map(function (key) {
  909. return _defineProperty({}, key, query[key]);
  910. }));
  911. }; // When `auto` is `true`, the parse function will infer and initialize and add
  912. // the appropriate `Searcher` instance
  913. function parse(query, options) {
  914. var _ref3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  915. _ref3$auto = _ref3.auto,
  916. auto = _ref3$auto === void 0 ? true : _ref3$auto;
  917. var next = function next(query) {
  918. var keys = Object.keys(query);
  919. var isQueryPath = isPath(query);
  920. if (!isQueryPath && keys.length > 1 && !isExpression(query)) {
  921. return next(convertToExplicit(query));
  922. }
  923. if (isLeaf(query)) {
  924. var key = isQueryPath ? query[KeyType.PATH] : keys[0];
  925. var pattern = isQueryPath ? query[KeyType.PATTERN] : query[key];
  926. if (!isString(pattern)) {
  927. throw new Error(LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key));
  928. }
  929. var obj = {
  930. keyId: createKeyId(key),
  931. pattern: pattern
  932. };
  933. if (auto) {
  934. obj.searcher = createSearcher(pattern, options);
  935. }
  936. return obj;
  937. }
  938. var node = {
  939. children: [],
  940. operator: keys[0]
  941. };
  942. keys.forEach(function (key) {
  943. var value = query[key];
  944. if (isArray(value)) {
  945. value.forEach(function (item) {
  946. node.children.push(next(item));
  947. });
  948. }
  949. });
  950. return node;
  951. };
  952. if (!isExpression(query)) {
  953. query = convertToExplicit(query);
  954. }
  955. return next(query);
  956. }
  957. var Fuse = /*#__PURE__*/function () {
  958. function Fuse(docs) {
  959. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  960. var index = arguments.length > 2 ? arguments[2] : undefined;
  961. _classCallCheck(this, Fuse);
  962. this.options = _objectSpread2({}, Config, {}, options);
  963. if (this.options.useExtendedSearch && !false) {
  964. throw new Error(EXTENDED_SEARCH_UNAVAILABLE);
  965. }
  966. this._keyStore = new KeyStore(this.options.keys);
  967. this.setCollection(docs, index);
  968. }
  969. _createClass(Fuse, [{
  970. key: "setCollection",
  971. value: function setCollection(docs, index) {
  972. this._docs = docs;
  973. if (index && !(index instanceof FuseIndex)) {
  974. throw new Error(INCORRECT_INDEX_TYPE);
  975. }
  976. this._myIndex = index || createIndex(this.options.keys, this._docs, {
  977. getFn: this.options.getFn
  978. });
  979. }
  980. }, {
  981. key: "add",
  982. value: function add(doc) {
  983. if (!isDefined(doc)) {
  984. return;
  985. }
  986. this._docs.push(doc);
  987. this._myIndex.add(doc);
  988. }
  989. }, {
  990. key: "remove",
  991. value: function remove() {
  992. var predicate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
  993. return (
  994. /* doc, idx */
  995. false
  996. );
  997. };
  998. var results = [];
  999. for (var i = 0, len = this._docs.length; i < len; i += 1) {
  1000. var doc = this._docs[i];
  1001. if (predicate(doc, i)) {
  1002. this.removeAt(i);
  1003. i -= 1;
  1004. len -= 1;
  1005. results.push(doc);
  1006. }
  1007. }
  1008. return results;
  1009. }
  1010. }, {
  1011. key: "removeAt",
  1012. value: function removeAt(idx) {
  1013. this._docs.splice(idx, 1);
  1014. this._myIndex.removeAt(idx);
  1015. }
  1016. }, {
  1017. key: "getIndex",
  1018. value: function getIndex() {
  1019. return this._myIndex;
  1020. }
  1021. }, {
  1022. key: "search",
  1023. value: function search(query) {
  1024. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  1025. _ref$limit = _ref.limit,
  1026. limit = _ref$limit === void 0 ? -1 : _ref$limit;
  1027. var _this$options = this.options,
  1028. includeMatches = _this$options.includeMatches,
  1029. includeScore = _this$options.includeScore,
  1030. shouldSort = _this$options.shouldSort,
  1031. sortFn = _this$options.sortFn,
  1032. ignoreFieldNorm = _this$options.ignoreFieldNorm;
  1033. var results = isString(query) ? isString(this._docs[0]) ? this._searchStringList(query) : this._searchObjectList(query) : this._searchLogical(query);
  1034. computeScore$1(results, {
  1035. ignoreFieldNorm: ignoreFieldNorm
  1036. });
  1037. if (shouldSort) {
  1038. results.sort(sortFn);
  1039. }
  1040. if (isNumber(limit) && limit > -1) {
  1041. results = results.slice(0, limit);
  1042. }
  1043. return format(results, this._docs, {
  1044. includeMatches: includeMatches,
  1045. includeScore: includeScore
  1046. });
  1047. }
  1048. }, {
  1049. key: "_searchStringList",
  1050. value: function _searchStringList(query) {
  1051. var searcher = createSearcher(query, this.options);
  1052. var records = this._myIndex.records;
  1053. var results = []; // Iterate over every string in the index
  1054. records.forEach(function (_ref2) {
  1055. var text = _ref2.v,
  1056. idx = _ref2.i,
  1057. norm = _ref2.n;
  1058. if (!isDefined(text)) {
  1059. return;
  1060. }
  1061. var _searcher$searchIn = searcher.searchIn(text),
  1062. isMatch = _searcher$searchIn.isMatch,
  1063. score = _searcher$searchIn.score,
  1064. indices = _searcher$searchIn.indices;
  1065. if (isMatch) {
  1066. results.push({
  1067. item: text,
  1068. idx: idx,
  1069. matches: [{
  1070. score: score,
  1071. value: text,
  1072. norm: norm,
  1073. indices: indices
  1074. }]
  1075. });
  1076. }
  1077. });
  1078. return results;
  1079. }
  1080. }, {
  1081. key: "_searchLogical",
  1082. value: function _searchLogical(query) {
  1083. {
  1084. throw new Error(LOGICAL_SEARCH_UNAVAILABLE);
  1085. }
  1086. }
  1087. }, {
  1088. key: "_searchObjectList",
  1089. value: function _searchObjectList(query) {
  1090. var _this2 = this;
  1091. var searcher = createSearcher(query, this.options);
  1092. var _this$_myIndex = this._myIndex,
  1093. keys = _this$_myIndex.keys,
  1094. records = _this$_myIndex.records;
  1095. var results = []; // List is Array<Object>
  1096. records.forEach(function (_ref5) {
  1097. var item = _ref5.$,
  1098. idx = _ref5.i;
  1099. if (!isDefined(item)) {
  1100. return;
  1101. }
  1102. var matches = []; // Iterate over every key (i.e, path), and fetch the value at that key
  1103. keys.forEach(function (key, keyIndex) {
  1104. matches.push.apply(matches, _toConsumableArray(_this2._findMatches({
  1105. key: key,
  1106. value: item[keyIndex],
  1107. searcher: searcher
  1108. })));
  1109. });
  1110. if (matches.length) {
  1111. results.push({
  1112. idx: idx,
  1113. item: item,
  1114. matches: matches
  1115. });
  1116. }
  1117. });
  1118. return results;
  1119. }
  1120. }, {
  1121. key: "_findMatches",
  1122. value: function _findMatches(_ref6) {
  1123. var key = _ref6.key,
  1124. value = _ref6.value,
  1125. searcher = _ref6.searcher;
  1126. if (!isDefined(value)) {
  1127. return [];
  1128. }
  1129. var matches = [];
  1130. if (isArray(value)) {
  1131. value.forEach(function (_ref7) {
  1132. var text = _ref7.v,
  1133. idx = _ref7.i,
  1134. norm = _ref7.n;
  1135. if (!isDefined(text)) {
  1136. return;
  1137. }
  1138. var _searcher$searchIn2 = searcher.searchIn(text),
  1139. isMatch = _searcher$searchIn2.isMatch,
  1140. score = _searcher$searchIn2.score,
  1141. indices = _searcher$searchIn2.indices;
  1142. if (isMatch) {
  1143. matches.push({
  1144. score: score,
  1145. key: key,
  1146. value: text,
  1147. idx: idx,
  1148. norm: norm,
  1149. indices: indices
  1150. });
  1151. }
  1152. });
  1153. } else {
  1154. var text = value.v,
  1155. norm = value.n;
  1156. var _searcher$searchIn3 = searcher.searchIn(text),
  1157. isMatch = _searcher$searchIn3.isMatch,
  1158. score = _searcher$searchIn3.score,
  1159. indices = _searcher$searchIn3.indices;
  1160. if (isMatch) {
  1161. matches.push({
  1162. score: score,
  1163. key: key,
  1164. value: text,
  1165. norm: norm,
  1166. indices: indices
  1167. });
  1168. }
  1169. }
  1170. return matches;
  1171. }
  1172. }]);
  1173. return Fuse;
  1174. }(); // Practical scoring function
  1175. function computeScore$1(results, _ref8) {
  1176. var _ref8$ignoreFieldNorm = _ref8.ignoreFieldNorm,
  1177. ignoreFieldNorm = _ref8$ignoreFieldNorm === void 0 ? Config.ignoreFieldNorm : _ref8$ignoreFieldNorm;
  1178. results.forEach(function (result) {
  1179. var totalScore = 1;
  1180. result.matches.forEach(function (_ref9) {
  1181. var key = _ref9.key,
  1182. norm = _ref9.norm,
  1183. score = _ref9.score;
  1184. var weight = key ? key.weight : null;
  1185. totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
  1186. });
  1187. result.score = totalScore;
  1188. });
  1189. }
  1190. function format(results, docs) {
  1191. var _ref10 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  1192. _ref10$includeMatches = _ref10.includeMatches,
  1193. includeMatches = _ref10$includeMatches === void 0 ? Config.includeMatches : _ref10$includeMatches,
  1194. _ref10$includeScore = _ref10.includeScore,
  1195. includeScore = _ref10$includeScore === void 0 ? Config.includeScore : _ref10$includeScore;
  1196. var transformers = [];
  1197. if (includeMatches) transformers.push(transformMatches);
  1198. if (includeScore) transformers.push(transformScore);
  1199. return results.map(function (result) {
  1200. var idx = result.idx;
  1201. var data = {
  1202. item: docs[idx],
  1203. refIndex: idx
  1204. };
  1205. if (transformers.length) {
  1206. transformers.forEach(function (transformer) {
  1207. transformer(result, data);
  1208. });
  1209. }
  1210. return data;
  1211. });
  1212. }
  1213. Fuse.version = '6.4.3';
  1214. Fuse.createIndex = createIndex;
  1215. Fuse.parseIndex = parseIndex;
  1216. Fuse.config = Config;
  1217. {
  1218. Fuse.parseQuery = parse;
  1219. }
  1220. return Fuse;
  1221. })));