fuse.common.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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. 'use strict';
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  13. _typeof = function (obj) {
  14. return typeof obj;
  15. };
  16. } else {
  17. _typeof = function (obj) {
  18. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  19. };
  20. }
  21. return _typeof(obj);
  22. }
  23. function _classCallCheck(instance, Constructor) {
  24. if (!(instance instanceof Constructor)) {
  25. throw new TypeError("Cannot call a class as a function");
  26. }
  27. }
  28. function _defineProperties(target, props) {
  29. for (var i = 0; i < props.length; i++) {
  30. var descriptor = props[i];
  31. descriptor.enumerable = descriptor.enumerable || false;
  32. descriptor.configurable = true;
  33. if ("value" in descriptor) descriptor.writable = true;
  34. Object.defineProperty(target, descriptor.key, descriptor);
  35. }
  36. }
  37. function _createClass(Constructor, protoProps, staticProps) {
  38. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  39. if (staticProps) _defineProperties(Constructor, staticProps);
  40. return Constructor;
  41. }
  42. function _defineProperty(obj, key, value) {
  43. if (key in obj) {
  44. Object.defineProperty(obj, key, {
  45. value: value,
  46. enumerable: true,
  47. configurable: true,
  48. writable: true
  49. });
  50. } else {
  51. obj[key] = value;
  52. }
  53. return obj;
  54. }
  55. function ownKeys(object, enumerableOnly) {
  56. var keys = Object.keys(object);
  57. if (Object.getOwnPropertySymbols) {
  58. var symbols = Object.getOwnPropertySymbols(object);
  59. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  60. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  61. });
  62. keys.push.apply(keys, symbols);
  63. }
  64. return keys;
  65. }
  66. function _objectSpread2(target) {
  67. for (var i = 1; i < arguments.length; i++) {
  68. var source = arguments[i] != null ? arguments[i] : {};
  69. if (i % 2) {
  70. ownKeys(Object(source), true).forEach(function (key) {
  71. _defineProperty(target, key, source[key]);
  72. });
  73. } else if (Object.getOwnPropertyDescriptors) {
  74. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  75. } else {
  76. ownKeys(Object(source)).forEach(function (key) {
  77. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  78. });
  79. }
  80. }
  81. return target;
  82. }
  83. function _inherits(subClass, superClass) {
  84. if (typeof superClass !== "function" && superClass !== null) {
  85. throw new TypeError("Super expression must either be null or a function");
  86. }
  87. subClass.prototype = Object.create(superClass && superClass.prototype, {
  88. constructor: {
  89. value: subClass,
  90. writable: true,
  91. configurable: true
  92. }
  93. });
  94. if (superClass) _setPrototypeOf(subClass, superClass);
  95. }
  96. function _getPrototypeOf(o) {
  97. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
  98. return o.__proto__ || Object.getPrototypeOf(o);
  99. };
  100. return _getPrototypeOf(o);
  101. }
  102. function _setPrototypeOf(o, p) {
  103. _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
  104. o.__proto__ = p;
  105. return o;
  106. };
  107. return _setPrototypeOf(o, p);
  108. }
  109. function _isNativeReflectConstruct() {
  110. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  111. if (Reflect.construct.sham) return false;
  112. if (typeof Proxy === "function") return true;
  113. try {
  114. Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
  115. return true;
  116. } catch (e) {
  117. return false;
  118. }
  119. }
  120. function _assertThisInitialized(self) {
  121. if (self === void 0) {
  122. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  123. }
  124. return self;
  125. }
  126. function _possibleConstructorReturn(self, call) {
  127. if (call && (typeof call === "object" || typeof call === "function")) {
  128. return call;
  129. }
  130. return _assertThisInitialized(self);
  131. }
  132. function _createSuper(Derived) {
  133. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  134. return function _createSuperInternal() {
  135. var Super = _getPrototypeOf(Derived),
  136. result;
  137. if (hasNativeReflectConstruct) {
  138. var NewTarget = _getPrototypeOf(this).constructor;
  139. result = Reflect.construct(Super, arguments, NewTarget);
  140. } else {
  141. result = Super.apply(this, arguments);
  142. }
  143. return _possibleConstructorReturn(this, result);
  144. };
  145. }
  146. function _toConsumableArray(arr) {
  147. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
  148. }
  149. function _arrayWithoutHoles(arr) {
  150. if (Array.isArray(arr)) return _arrayLikeToArray(arr);
  151. }
  152. function _iterableToArray(iter) {
  153. if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
  154. }
  155. function _unsupportedIterableToArray(o, minLen) {
  156. if (!o) return;
  157. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  158. var n = Object.prototype.toString.call(o).slice(8, -1);
  159. if (n === "Object" && o.constructor) n = o.constructor.name;
  160. if (n === "Map" || n === "Set") return Array.from(o);
  161. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  162. }
  163. function _arrayLikeToArray(arr, len) {
  164. if (len == null || len > arr.length) len = arr.length;
  165. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  166. return arr2;
  167. }
  168. function _nonIterableSpread() {
  169. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  170. }
  171. function isArray(value) {
  172. return !Array.isArray ? getTag(value) === '[object Array]' : Array.isArray(value);
  173. } // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
  174. var INFINITY = 1 / 0;
  175. function baseToString(value) {
  176. // Exit early for strings to avoid a performance hit in some environments.
  177. if (typeof value == 'string') {
  178. return value;
  179. }
  180. var result = value + '';
  181. return result == '0' && 1 / value == -INFINITY ? '-0' : result;
  182. }
  183. function toString(value) {
  184. return value == null ? '' : baseToString(value);
  185. }
  186. function isString(value) {
  187. return typeof value === 'string';
  188. }
  189. function isNumber(value) {
  190. return typeof value === 'number';
  191. } // Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
  192. function isBoolean(value) {
  193. return value === true || value === false || isObjectLike(value) && getTag(value) == '[object Boolean]';
  194. }
  195. function isObject(value) {
  196. return _typeof(value) === 'object';
  197. } // Checks if `value` is object-like.
  198. function isObjectLike(value) {
  199. return isObject(value) && value !== null;
  200. }
  201. function isDefined(value) {
  202. return value !== undefined && value !== null;
  203. }
  204. function isBlank(value) {
  205. return !value.trim().length;
  206. } // Gets the `toStringTag` of `value`.
  207. // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
  208. function getTag(value) {
  209. return value == null ? value === undefined ? '[object Undefined]' : '[object Null]' : Object.prototype.toString.call(value);
  210. }
  211. var EXTENDED_SEARCH_UNAVAILABLE = 'Extended search is not available';
  212. var INCORRECT_INDEX_TYPE = "Incorrect 'index' type";
  213. var LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY = function LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key) {
  214. return "Invalid value for key ".concat(key);
  215. };
  216. var PATTERN_LENGTH_TOO_LARGE = function PATTERN_LENGTH_TOO_LARGE(max) {
  217. return "Pattern length exceeds max of ".concat(max, ".");
  218. };
  219. var MISSING_KEY_PROPERTY = function MISSING_KEY_PROPERTY(name) {
  220. return "Missing ".concat(name, " property in key");
  221. };
  222. var INVALID_KEY_WEIGHT_VALUE = function INVALID_KEY_WEIGHT_VALUE(key) {
  223. return "Property 'weight' in key '".concat(key, "' must be a positive integer");
  224. };
  225. var hasOwn = Object.prototype.hasOwnProperty;
  226. var KeyStore = /*#__PURE__*/function () {
  227. function KeyStore(keys) {
  228. var _this = this;
  229. _classCallCheck(this, KeyStore);
  230. this._keys = [];
  231. this._keyMap = {};
  232. var totalWeight = 0;
  233. keys.forEach(function (key) {
  234. var obj = createKey(key);
  235. totalWeight += obj.weight;
  236. _this._keys.push(obj);
  237. _this._keyMap[obj.id] = obj;
  238. totalWeight += obj.weight;
  239. }); // Normalize weights so that their sum is equal to 1
  240. this._keys.forEach(function (key) {
  241. key.weight /= totalWeight;
  242. });
  243. }
  244. _createClass(KeyStore, [{
  245. key: "get",
  246. value: function get(keyId) {
  247. return this._keyMap[keyId];
  248. }
  249. }, {
  250. key: "keys",
  251. value: function keys() {
  252. return this._keys;
  253. }
  254. }, {
  255. key: "toJSON",
  256. value: function toJSON() {
  257. return JSON.stringify(this._keys);
  258. }
  259. }]);
  260. return KeyStore;
  261. }();
  262. function createKey(key) {
  263. var path = null;
  264. var id = null;
  265. var src = null;
  266. var weight = 1;
  267. if (isString(key) || isArray(key)) {
  268. src = key;
  269. path = createKeyPath(key);
  270. id = createKeyId(key);
  271. } else {
  272. if (!hasOwn.call(key, 'name')) {
  273. throw new Error(MISSING_KEY_PROPERTY('name'));
  274. }
  275. var name = key.name;
  276. src = name;
  277. if (hasOwn.call(key, 'weight')) {
  278. weight = key.weight;
  279. if (weight <= 0) {
  280. throw new Error(INVALID_KEY_WEIGHT_VALUE(name));
  281. }
  282. }
  283. path = createKeyPath(name);
  284. id = createKeyId(name);
  285. }
  286. return {
  287. path: path,
  288. id: id,
  289. weight: weight,
  290. src: src
  291. };
  292. }
  293. function createKeyPath(key) {
  294. return isArray(key) ? key : key.split('.');
  295. }
  296. function createKeyId(key) {
  297. return isArray(key) ? key.join('.') : key;
  298. }
  299. function get(obj, path) {
  300. var list = [];
  301. var arr = false;
  302. var deepGet = function deepGet(obj, path, index) {
  303. if (!isDefined(obj)) {
  304. return;
  305. }
  306. if (!path[index]) {
  307. // If there's no path left, we've arrived at the object we care about.
  308. list.push(obj);
  309. } else {
  310. var key = path[index];
  311. var value = obj[key];
  312. if (!isDefined(value)) {
  313. return;
  314. } // If we're at the last value in the path, and if it's a string/number/bool,
  315. // add it to the list
  316. if (index === path.length - 1 && (isString(value) || isNumber(value) || isBoolean(value))) {
  317. list.push(toString(value));
  318. } else if (isArray(value)) {
  319. arr = true; // Search each item in the array.
  320. for (var i = 0, len = value.length; i < len; i += 1) {
  321. deepGet(value[i], path, index + 1);
  322. }
  323. } else if (path.length) {
  324. // An object. Recurse further.
  325. deepGet(value, path, index + 1);
  326. }
  327. }
  328. }; // Backwards compatibility (since path used to be a string)
  329. deepGet(obj, isString(path) ? path.split('.') : path, 0);
  330. return arr ? list : list[0];
  331. }
  332. var MatchOptions = {
  333. // Whether the matches should be included in the result set. When `true`, each record in the result
  334. // set will include the indices of the matched characters.
  335. // These can consequently be used for highlighting purposes.
  336. includeMatches: false,
  337. // When `true`, the matching function will continue to the end of a search pattern even if
  338. // a perfect match has already been located in the string.
  339. findAllMatches: false,
  340. // Minimum number of characters that must be matched before a result is considered a match
  341. minMatchCharLength: 1
  342. };
  343. var BasicOptions = {
  344. // When `true`, the algorithm continues searching to the end of the input even if a perfect
  345. // match is found before the end of the same input.
  346. isCaseSensitive: false,
  347. // When true, the matching function will continue to the end of a search pattern even if
  348. includeScore: false,
  349. // List of properties that will be searched. This also supports nested properties.
  350. keys: [],
  351. // Whether to sort the result list, by score
  352. shouldSort: true,
  353. // Default sort function: sort by ascending score, ascending index
  354. sortFn: function sortFn(a, b) {
  355. return a.score === b.score ? a.idx < b.idx ? -1 : 1 : a.score < b.score ? -1 : 1;
  356. }
  357. };
  358. var FuzzyOptions = {
  359. // Approximately where in the text is the pattern expected to be found?
  360. location: 0,
  361. // At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
  362. // (of both letters and location), a threshold of '1.0' would match anything.
  363. threshold: 0.6,
  364. // Determines how close the match must be to the fuzzy location (specified above).
  365. // An exact letter match which is 'distance' characters away from the fuzzy location
  366. // would score as a complete mismatch. A distance of '0' requires the match be at
  367. // the exact location specified, a threshold of '1000' would require a perfect match
  368. // to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.
  369. distance: 100
  370. };
  371. var AdvancedOptions = {
  372. // When `true`, it enables the use of unix-like search commands
  373. useExtendedSearch: false,
  374. // The get function to use when fetching an object's properties.
  375. // The default will search nested paths *ie foo.bar.baz*
  376. getFn: get,
  377. // When `true`, search will ignore `location` and `distance`, so it won't matter
  378. // where in the string the pattern appears.
  379. // More info: https://fusejs.io/concepts/scoring-theory.html#fuzziness-score
  380. ignoreLocation: false,
  381. // When `true`, the calculation for the relevance score (used for sorting) will
  382. // ignore the field-length norm.
  383. // More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
  384. ignoreFieldNorm: false
  385. };
  386. var Config = _objectSpread2({}, BasicOptions, {}, MatchOptions, {}, FuzzyOptions, {}, AdvancedOptions);
  387. var SPACE = /[^ ]+/g; // Field-length norm: the shorter the field, the higher the weight.
  388. // Set to 3 decimals to reduce index size.
  389. function norm() {
  390. var mantissa = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
  391. var cache = new Map();
  392. return {
  393. get: function get(value) {
  394. var numTokens = value.match(SPACE).length;
  395. if (cache.has(numTokens)) {
  396. return cache.get(numTokens);
  397. }
  398. var n = parseFloat((1 / Math.sqrt(numTokens)).toFixed(mantissa));
  399. cache.set(numTokens, n);
  400. return n;
  401. },
  402. clear: function clear() {
  403. cache.clear();
  404. }
  405. };
  406. }
  407. var FuseIndex = /*#__PURE__*/function () {
  408. function FuseIndex() {
  409. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  410. _ref$getFn = _ref.getFn,
  411. getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn;
  412. _classCallCheck(this, FuseIndex);
  413. this.norm = norm(3);
  414. this.getFn = getFn;
  415. this.isCreated = false;
  416. this.setIndexRecords();
  417. }
  418. _createClass(FuseIndex, [{
  419. key: "setSources",
  420. value: function setSources() {
  421. var docs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  422. this.docs = docs;
  423. }
  424. }, {
  425. key: "setIndexRecords",
  426. value: function setIndexRecords() {
  427. var records = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  428. this.records = records;
  429. }
  430. }, {
  431. key: "setKeys",
  432. value: function setKeys() {
  433. var _this = this;
  434. var keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  435. this.keys = keys;
  436. this._keysMap = {};
  437. keys.forEach(function (key, idx) {
  438. _this._keysMap[key.id] = idx;
  439. });
  440. }
  441. }, {
  442. key: "create",
  443. value: function create() {
  444. var _this2 = this;
  445. if (this.isCreated || !this.docs.length) {
  446. return;
  447. }
  448. this.isCreated = true; // List is Array<String>
  449. if (isString(this.docs[0])) {
  450. this.docs.forEach(function (doc, docIndex) {
  451. _this2._addString(doc, docIndex);
  452. });
  453. } else {
  454. // List is Array<Object>
  455. this.docs.forEach(function (doc, docIndex) {
  456. _this2._addObject(doc, docIndex);
  457. });
  458. }
  459. this.norm.clear();
  460. } // Adds a doc to the end of the index
  461. }, {
  462. key: "add",
  463. value: function add(doc) {
  464. var idx = this.size();
  465. if (isString(doc)) {
  466. this._addString(doc, idx);
  467. } else {
  468. this._addObject(doc, idx);
  469. }
  470. } // Removes the doc at the specified index of the index
  471. }, {
  472. key: "removeAt",
  473. value: function removeAt(idx) {
  474. this.records.splice(idx, 1); // Change ref index of every subsquent doc
  475. for (var i = idx, len = this.size(); i < len; i += 1) {
  476. this.records[i].i -= 1;
  477. }
  478. }
  479. }, {
  480. key: "getValueForItemAtKeyId",
  481. value: function getValueForItemAtKeyId(item, keyId) {
  482. return item[this._keysMap[keyId]];
  483. }
  484. }, {
  485. key: "size",
  486. value: function size() {
  487. return this.records.length;
  488. }
  489. }, {
  490. key: "_addString",
  491. value: function _addString(doc, docIndex) {
  492. if (!isDefined(doc) || isBlank(doc)) {
  493. return;
  494. }
  495. var record = {
  496. v: doc,
  497. i: docIndex,
  498. n: this.norm.get(doc)
  499. };
  500. this.records.push(record);
  501. }
  502. }, {
  503. key: "_addObject",
  504. value: function _addObject(doc, docIndex) {
  505. var _this3 = this;
  506. var record = {
  507. i: docIndex,
  508. $: {}
  509. }; // Iterate over every key (i.e, path), and fetch the value at that key
  510. this.keys.forEach(function (key, keyIndex) {
  511. // console.log(key)
  512. var value = _this3.getFn(doc, key.path);
  513. if (!isDefined(value)) {
  514. return;
  515. }
  516. if (isArray(value)) {
  517. (function () {
  518. var subRecords = [];
  519. var stack = [{
  520. nestedArrIndex: -1,
  521. value: value
  522. }];
  523. while (stack.length) {
  524. var _stack$pop = stack.pop(),
  525. nestedArrIndex = _stack$pop.nestedArrIndex,
  526. _value = _stack$pop.value;
  527. if (!isDefined(_value)) {
  528. continue;
  529. }
  530. if (isString(_value) && !isBlank(_value)) {
  531. var subRecord = {
  532. v: _value,
  533. i: nestedArrIndex,
  534. n: _this3.norm.get(_value)
  535. };
  536. subRecords.push(subRecord);
  537. } else if (isArray(_value)) {
  538. _value.forEach(function (item, k) {
  539. stack.push({
  540. nestedArrIndex: k,
  541. value: item
  542. });
  543. });
  544. }
  545. }
  546. record.$[keyIndex] = subRecords;
  547. })();
  548. } else if (!isBlank(value)) {
  549. var subRecord = {
  550. v: value,
  551. n: _this3.norm.get(value)
  552. };
  553. record.$[keyIndex] = subRecord;
  554. }
  555. });
  556. this.records.push(record);
  557. }
  558. }, {
  559. key: "toJSON",
  560. value: function toJSON() {
  561. return {
  562. keys: this.keys,
  563. records: this.records
  564. };
  565. }
  566. }]);
  567. return FuseIndex;
  568. }();
  569. function createIndex(keys, docs) {
  570. var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  571. _ref2$getFn = _ref2.getFn,
  572. getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn;
  573. var myIndex = new FuseIndex({
  574. getFn: getFn
  575. });
  576. myIndex.setKeys(keys.map(createKey));
  577. myIndex.setSources(docs);
  578. myIndex.create();
  579. return myIndex;
  580. }
  581. function parseIndex(data) {
  582. var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  583. _ref3$getFn = _ref3.getFn,
  584. getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn;
  585. var keys = data.keys,
  586. records = data.records;
  587. var myIndex = new FuseIndex({
  588. getFn: getFn
  589. });
  590. myIndex.setKeys(keys);
  591. myIndex.setIndexRecords(records);
  592. return myIndex;
  593. }
  594. function transformMatches(result, data) {
  595. var matches = result.matches;
  596. data.matches = [];
  597. if (!isDefined(matches)) {
  598. return;
  599. }
  600. matches.forEach(function (match) {
  601. if (!isDefined(match.indices) || !match.indices.length) {
  602. return;
  603. }
  604. var indices = match.indices,
  605. value = match.value;
  606. var obj = {
  607. indices: indices,
  608. value: value
  609. };
  610. if (match.key) {
  611. obj.key = match.key.src;
  612. }
  613. if (match.idx > -1) {
  614. obj.refIndex = match.idx;
  615. }
  616. data.matches.push(obj);
  617. });
  618. }
  619. function transformScore(result, data) {
  620. data.score = result.score;
  621. }
  622. function computeScore(pattern) {
  623. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  624. _ref$errors = _ref.errors,
  625. errors = _ref$errors === void 0 ? 0 : _ref$errors,
  626. _ref$currentLocation = _ref.currentLocation,
  627. currentLocation = _ref$currentLocation === void 0 ? 0 : _ref$currentLocation,
  628. _ref$expectedLocation = _ref.expectedLocation,
  629. expectedLocation = _ref$expectedLocation === void 0 ? 0 : _ref$expectedLocation,
  630. _ref$distance = _ref.distance,
  631. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  632. _ref$ignoreLocation = _ref.ignoreLocation,
  633. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  634. var accuracy = errors / pattern.length;
  635. if (ignoreLocation) {
  636. return accuracy;
  637. }
  638. var proximity = Math.abs(expectedLocation - currentLocation);
  639. if (!distance) {
  640. // Dodge divide by zero error.
  641. return proximity ? 1.0 : accuracy;
  642. }
  643. return accuracy + proximity / distance;
  644. }
  645. function convertMaskToIndices() {
  646. var matchmask = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  647. var minMatchCharLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Config.minMatchCharLength;
  648. var indices = [];
  649. var start = -1;
  650. var end = -1;
  651. var i = 0;
  652. for (var len = matchmask.length; i < len; i += 1) {
  653. var match = matchmask[i];
  654. if (match && start === -1) {
  655. start = i;
  656. } else if (!match && start !== -1) {
  657. end = i - 1;
  658. if (end - start + 1 >= minMatchCharLength) {
  659. indices.push([start, end]);
  660. }
  661. start = -1;
  662. }
  663. } // (i-1 - start) + 1 => i - start
  664. if (matchmask[i - 1] && i - start >= minMatchCharLength) {
  665. indices.push([start, i - 1]);
  666. }
  667. return indices;
  668. }
  669. // Machine word size
  670. var MAX_BITS = 32;
  671. function search(text, pattern, patternAlphabet) {
  672. var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
  673. _ref$location = _ref.location,
  674. location = _ref$location === void 0 ? Config.location : _ref$location,
  675. _ref$distance = _ref.distance,
  676. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  677. _ref$threshold = _ref.threshold,
  678. threshold = _ref$threshold === void 0 ? Config.threshold : _ref$threshold,
  679. _ref$findAllMatches = _ref.findAllMatches,
  680. findAllMatches = _ref$findAllMatches === void 0 ? Config.findAllMatches : _ref$findAllMatches,
  681. _ref$minMatchCharLeng = _ref.minMatchCharLength,
  682. minMatchCharLength = _ref$minMatchCharLeng === void 0 ? Config.minMatchCharLength : _ref$minMatchCharLeng,
  683. _ref$includeMatches = _ref.includeMatches,
  684. includeMatches = _ref$includeMatches === void 0 ? Config.includeMatches : _ref$includeMatches,
  685. _ref$ignoreLocation = _ref.ignoreLocation,
  686. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  687. if (pattern.length > MAX_BITS) {
  688. throw new Error(PATTERN_LENGTH_TOO_LARGE(MAX_BITS));
  689. }
  690. var patternLen = pattern.length; // Set starting location at beginning text and initialize the alphabet.
  691. var textLen = text.length; // Handle the case when location > text.length
  692. var expectedLocation = Math.max(0, Math.min(location, textLen)); // Highest score beyond which we give up.
  693. var currentThreshold = threshold; // Is there a nearby exact match? (speedup)
  694. var bestLocation = expectedLocation; // Performance: only computer matches when the minMatchCharLength > 1
  695. // OR if `includeMatches` is true.
  696. var computeMatches = minMatchCharLength > 1 || includeMatches; // A mask of the matches, used for building the indices
  697. var matchMask = computeMatches ? Array(textLen) : [];
  698. var index; // Get all exact matches, here for speed up
  699. while ((index = text.indexOf(pattern, bestLocation)) > -1) {
  700. var score = computeScore(pattern, {
  701. currentLocation: index,
  702. expectedLocation: expectedLocation,
  703. distance: distance,
  704. ignoreLocation: ignoreLocation
  705. });
  706. currentThreshold = Math.min(score, currentThreshold);
  707. bestLocation = index + patternLen;
  708. if (computeMatches) {
  709. var i = 0;
  710. while (i < patternLen) {
  711. matchMask[index + i] = 1;
  712. i += 1;
  713. }
  714. }
  715. } // Reset the best location
  716. bestLocation = -1;
  717. var lastBitArr = [];
  718. var finalScore = 1;
  719. var binMax = patternLen + textLen;
  720. var mask = 1 << patternLen - 1;
  721. for (var _i = 0; _i < patternLen; _i += 1) {
  722. // Scan for the best match; each iteration allows for one more error.
  723. // Run a binary search to determine how far from the match location we can stray
  724. // at this error level.
  725. var binMin = 0;
  726. var binMid = binMax;
  727. while (binMin < binMid) {
  728. var _score2 = computeScore(pattern, {
  729. errors: _i,
  730. currentLocation: expectedLocation + binMid,
  731. expectedLocation: expectedLocation,
  732. distance: distance,
  733. ignoreLocation: ignoreLocation
  734. });
  735. if (_score2 <= currentThreshold) {
  736. binMin = binMid;
  737. } else {
  738. binMax = binMid;
  739. }
  740. binMid = Math.floor((binMax - binMin) / 2 + binMin);
  741. } // Use the result from this iteration as the maximum for the next.
  742. binMax = binMid;
  743. var start = Math.max(1, expectedLocation - binMid + 1);
  744. var finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen; // Initialize the bit array
  745. var bitArr = Array(finish + 2);
  746. bitArr[finish + 1] = (1 << _i) - 1;
  747. for (var j = finish; j >= start; j -= 1) {
  748. var currentLocation = j - 1;
  749. var charMatch = patternAlphabet[text.charAt(currentLocation)];
  750. if (computeMatches) {
  751. // Speed up: quick bool to int conversion (i.e, `charMatch ? 1 : 0`)
  752. matchMask[currentLocation] = +!!charMatch;
  753. } // First pass: exact match
  754. bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch; // Subsequent passes: fuzzy match
  755. if (_i) {
  756. bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
  757. }
  758. if (bitArr[j] & mask) {
  759. finalScore = computeScore(pattern, {
  760. errors: _i,
  761. currentLocation: currentLocation,
  762. expectedLocation: expectedLocation,
  763. distance: distance,
  764. ignoreLocation: ignoreLocation
  765. }); // This match will almost certainly be better than any existing match.
  766. // But check anyway.
  767. if (finalScore <= currentThreshold) {
  768. // Indeed it is
  769. currentThreshold = finalScore;
  770. bestLocation = currentLocation; // Already passed `loc`, downhill from here on in.
  771. if (bestLocation <= expectedLocation) {
  772. break;
  773. } // When passing `bestLocation`, don't exceed our current distance from `expectedLocation`.
  774. start = Math.max(1, 2 * expectedLocation - bestLocation);
  775. }
  776. }
  777. } // No hope for a (better) match at greater error levels.
  778. var _score = computeScore(pattern, {
  779. errors: _i + 1,
  780. currentLocation: expectedLocation,
  781. expectedLocation: expectedLocation,
  782. distance: distance,
  783. ignoreLocation: ignoreLocation
  784. });
  785. if (_score > currentThreshold) {
  786. break;
  787. }
  788. lastBitArr = bitArr;
  789. }
  790. var result = {
  791. isMatch: bestLocation >= 0,
  792. // Count exact matches (those with a score of 0) to be "almost" exact
  793. score: Math.max(0.001, finalScore)
  794. };
  795. if (computeMatches) {
  796. var indices = convertMaskToIndices(matchMask, minMatchCharLength);
  797. if (!indices.length) {
  798. result.isMatch = false;
  799. } else if (includeMatches) {
  800. result.indices = indices;
  801. }
  802. }
  803. return result;
  804. }
  805. function createPatternAlphabet(pattern) {
  806. var mask = {};
  807. for (var i = 0, len = pattern.length; i < len; i += 1) {
  808. var char = pattern.charAt(i);
  809. mask[char] = (mask[char] || 0) | 1 << len - i - 1;
  810. }
  811. return mask;
  812. }
  813. var BitapSearch = /*#__PURE__*/function () {
  814. function BitapSearch(pattern) {
  815. var _this = this;
  816. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  817. _ref$location = _ref.location,
  818. location = _ref$location === void 0 ? Config.location : _ref$location,
  819. _ref$threshold = _ref.threshold,
  820. threshold = _ref$threshold === void 0 ? Config.threshold : _ref$threshold,
  821. _ref$distance = _ref.distance,
  822. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  823. _ref$includeMatches = _ref.includeMatches,
  824. includeMatches = _ref$includeMatches === void 0 ? Config.includeMatches : _ref$includeMatches,
  825. _ref$findAllMatches = _ref.findAllMatches,
  826. findAllMatches = _ref$findAllMatches === void 0 ? Config.findAllMatches : _ref$findAllMatches,
  827. _ref$minMatchCharLeng = _ref.minMatchCharLength,
  828. minMatchCharLength = _ref$minMatchCharLeng === void 0 ? Config.minMatchCharLength : _ref$minMatchCharLeng,
  829. _ref$isCaseSensitive = _ref.isCaseSensitive,
  830. isCaseSensitive = _ref$isCaseSensitive === void 0 ? Config.isCaseSensitive : _ref$isCaseSensitive,
  831. _ref$ignoreLocation = _ref.ignoreLocation,
  832. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  833. _classCallCheck(this, BitapSearch);
  834. this.options = {
  835. location: location,
  836. threshold: threshold,
  837. distance: distance,
  838. includeMatches: includeMatches,
  839. findAllMatches: findAllMatches,
  840. minMatchCharLength: minMatchCharLength,
  841. isCaseSensitive: isCaseSensitive,
  842. ignoreLocation: ignoreLocation
  843. };
  844. this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
  845. this.chunks = [];
  846. if (!this.pattern.length) {
  847. return;
  848. }
  849. var addChunk = function addChunk(pattern, startIndex) {
  850. _this.chunks.push({
  851. pattern: pattern,
  852. alphabet: createPatternAlphabet(pattern),
  853. startIndex: startIndex
  854. });
  855. };
  856. var len = this.pattern.length;
  857. if (len > MAX_BITS) {
  858. var i = 0;
  859. var remainder = len % MAX_BITS;
  860. var end = len - remainder;
  861. while (i < end) {
  862. addChunk(this.pattern.substr(i, MAX_BITS), i);
  863. i += MAX_BITS;
  864. }
  865. if (remainder) {
  866. var startIndex = len - MAX_BITS;
  867. addChunk(this.pattern.substr(startIndex), startIndex);
  868. }
  869. } else {
  870. addChunk(this.pattern, 0);
  871. }
  872. }
  873. _createClass(BitapSearch, [{
  874. key: "searchIn",
  875. value: function searchIn(text) {
  876. var _this$options = this.options,
  877. isCaseSensitive = _this$options.isCaseSensitive,
  878. includeMatches = _this$options.includeMatches;
  879. if (!isCaseSensitive) {
  880. text = text.toLowerCase();
  881. } // Exact match
  882. if (this.pattern === text) {
  883. var _result = {
  884. isMatch: true,
  885. score: 0
  886. };
  887. if (includeMatches) {
  888. _result.indices = [[0, text.length - 1]];
  889. }
  890. return _result;
  891. } // Otherwise, use Bitap algorithm
  892. var _this$options2 = this.options,
  893. location = _this$options2.location,
  894. distance = _this$options2.distance,
  895. threshold = _this$options2.threshold,
  896. findAllMatches = _this$options2.findAllMatches,
  897. minMatchCharLength = _this$options2.minMatchCharLength,
  898. ignoreLocation = _this$options2.ignoreLocation;
  899. var allIndices = [];
  900. var totalScore = 0;
  901. var hasMatches = false;
  902. this.chunks.forEach(function (_ref2) {
  903. var pattern = _ref2.pattern,
  904. alphabet = _ref2.alphabet,
  905. startIndex = _ref2.startIndex;
  906. var _search = search(text, pattern, alphabet, {
  907. location: location + startIndex,
  908. distance: distance,
  909. threshold: threshold,
  910. findAllMatches: findAllMatches,
  911. minMatchCharLength: minMatchCharLength,
  912. includeMatches: includeMatches,
  913. ignoreLocation: ignoreLocation
  914. }),
  915. isMatch = _search.isMatch,
  916. score = _search.score,
  917. indices = _search.indices;
  918. if (isMatch) {
  919. hasMatches = true;
  920. }
  921. totalScore += score;
  922. if (isMatch && indices) {
  923. allIndices = [].concat(_toConsumableArray(allIndices), _toConsumableArray(indices));
  924. }
  925. });
  926. var result = {
  927. isMatch: hasMatches,
  928. score: hasMatches ? totalScore / this.chunks.length : 1
  929. };
  930. if (hasMatches && includeMatches) {
  931. result.indices = allIndices;
  932. }
  933. return result;
  934. }
  935. }]);
  936. return BitapSearch;
  937. }();
  938. var BaseMatch = /*#__PURE__*/function () {
  939. function BaseMatch(pattern) {
  940. _classCallCheck(this, BaseMatch);
  941. this.pattern = pattern;
  942. }
  943. _createClass(BaseMatch, [{
  944. key: "search",
  945. value: function search()
  946. /*text*/
  947. {}
  948. }], [{
  949. key: "isMultiMatch",
  950. value: function isMultiMatch(pattern) {
  951. return getMatch(pattern, this.multiRegex);
  952. }
  953. }, {
  954. key: "isSingleMatch",
  955. value: function isSingleMatch(pattern) {
  956. return getMatch(pattern, this.singleRegex);
  957. }
  958. }]);
  959. return BaseMatch;
  960. }();
  961. function getMatch(pattern, exp) {
  962. var matches = pattern.match(exp);
  963. return matches ? matches[1] : null;
  964. }
  965. var ExactMatch = /*#__PURE__*/function (_BaseMatch) {
  966. _inherits(ExactMatch, _BaseMatch);
  967. var _super = _createSuper(ExactMatch);
  968. function ExactMatch(pattern) {
  969. _classCallCheck(this, ExactMatch);
  970. return _super.call(this, pattern);
  971. }
  972. _createClass(ExactMatch, [{
  973. key: "search",
  974. value: function search(text) {
  975. var isMatch = text === this.pattern;
  976. return {
  977. isMatch: isMatch,
  978. score: isMatch ? 0 : 1,
  979. indices: [0, this.pattern.length - 1]
  980. };
  981. }
  982. }], [{
  983. key: "type",
  984. get: function get() {
  985. return 'exact';
  986. }
  987. }, {
  988. key: "multiRegex",
  989. get: function get() {
  990. return /^="(.*)"$/;
  991. }
  992. }, {
  993. key: "singleRegex",
  994. get: function get() {
  995. return /^=(.*)$/;
  996. }
  997. }]);
  998. return ExactMatch;
  999. }(BaseMatch);
  1000. var InverseExactMatch = /*#__PURE__*/function (_BaseMatch) {
  1001. _inherits(InverseExactMatch, _BaseMatch);
  1002. var _super = _createSuper(InverseExactMatch);
  1003. function InverseExactMatch(pattern) {
  1004. _classCallCheck(this, InverseExactMatch);
  1005. return _super.call(this, pattern);
  1006. }
  1007. _createClass(InverseExactMatch, [{
  1008. key: "search",
  1009. value: function search(text) {
  1010. var index = text.indexOf(this.pattern);
  1011. var isMatch = index === -1;
  1012. return {
  1013. isMatch: isMatch,
  1014. score: isMatch ? 0 : 1,
  1015. indices: [0, text.length - 1]
  1016. };
  1017. }
  1018. }], [{
  1019. key: "type",
  1020. get: function get() {
  1021. return 'inverse-exact';
  1022. }
  1023. }, {
  1024. key: "multiRegex",
  1025. get: function get() {
  1026. return /^!"(.*)"$/;
  1027. }
  1028. }, {
  1029. key: "singleRegex",
  1030. get: function get() {
  1031. return /^!(.*)$/;
  1032. }
  1033. }]);
  1034. return InverseExactMatch;
  1035. }(BaseMatch);
  1036. var PrefixExactMatch = /*#__PURE__*/function (_BaseMatch) {
  1037. _inherits(PrefixExactMatch, _BaseMatch);
  1038. var _super = _createSuper(PrefixExactMatch);
  1039. function PrefixExactMatch(pattern) {
  1040. _classCallCheck(this, PrefixExactMatch);
  1041. return _super.call(this, pattern);
  1042. }
  1043. _createClass(PrefixExactMatch, [{
  1044. key: "search",
  1045. value: function search(text) {
  1046. var isMatch = text.startsWith(this.pattern);
  1047. return {
  1048. isMatch: isMatch,
  1049. score: isMatch ? 0 : 1,
  1050. indices: [0, this.pattern.length - 1]
  1051. };
  1052. }
  1053. }], [{
  1054. key: "type",
  1055. get: function get() {
  1056. return 'prefix-exact';
  1057. }
  1058. }, {
  1059. key: "multiRegex",
  1060. get: function get() {
  1061. return /^\^"(.*)"$/;
  1062. }
  1063. }, {
  1064. key: "singleRegex",
  1065. get: function get() {
  1066. return /^\^(.*)$/;
  1067. }
  1068. }]);
  1069. return PrefixExactMatch;
  1070. }(BaseMatch);
  1071. var InversePrefixExactMatch = /*#__PURE__*/function (_BaseMatch) {
  1072. _inherits(InversePrefixExactMatch, _BaseMatch);
  1073. var _super = _createSuper(InversePrefixExactMatch);
  1074. function InversePrefixExactMatch(pattern) {
  1075. _classCallCheck(this, InversePrefixExactMatch);
  1076. return _super.call(this, pattern);
  1077. }
  1078. _createClass(InversePrefixExactMatch, [{
  1079. key: "search",
  1080. value: function search(text) {
  1081. var isMatch = !text.startsWith(this.pattern);
  1082. return {
  1083. isMatch: isMatch,
  1084. score: isMatch ? 0 : 1,
  1085. indices: [0, text.length - 1]
  1086. };
  1087. }
  1088. }], [{
  1089. key: "type",
  1090. get: function get() {
  1091. return 'inverse-prefix-exact';
  1092. }
  1093. }, {
  1094. key: "multiRegex",
  1095. get: function get() {
  1096. return /^!\^"(.*)"$/;
  1097. }
  1098. }, {
  1099. key: "singleRegex",
  1100. get: function get() {
  1101. return /^!\^(.*)$/;
  1102. }
  1103. }]);
  1104. return InversePrefixExactMatch;
  1105. }(BaseMatch);
  1106. var SuffixExactMatch = /*#__PURE__*/function (_BaseMatch) {
  1107. _inherits(SuffixExactMatch, _BaseMatch);
  1108. var _super = _createSuper(SuffixExactMatch);
  1109. function SuffixExactMatch(pattern) {
  1110. _classCallCheck(this, SuffixExactMatch);
  1111. return _super.call(this, pattern);
  1112. }
  1113. _createClass(SuffixExactMatch, [{
  1114. key: "search",
  1115. value: function search(text) {
  1116. var isMatch = text.endsWith(this.pattern);
  1117. return {
  1118. isMatch: isMatch,
  1119. score: isMatch ? 0 : 1,
  1120. indices: [text.length - this.pattern.length, text.length - 1]
  1121. };
  1122. }
  1123. }], [{
  1124. key: "type",
  1125. get: function get() {
  1126. return 'suffix-exact';
  1127. }
  1128. }, {
  1129. key: "multiRegex",
  1130. get: function get() {
  1131. return /^"(.*)"\$$/;
  1132. }
  1133. }, {
  1134. key: "singleRegex",
  1135. get: function get() {
  1136. return /^(.*)\$$/;
  1137. }
  1138. }]);
  1139. return SuffixExactMatch;
  1140. }(BaseMatch);
  1141. var InverseSuffixExactMatch = /*#__PURE__*/function (_BaseMatch) {
  1142. _inherits(InverseSuffixExactMatch, _BaseMatch);
  1143. var _super = _createSuper(InverseSuffixExactMatch);
  1144. function InverseSuffixExactMatch(pattern) {
  1145. _classCallCheck(this, InverseSuffixExactMatch);
  1146. return _super.call(this, pattern);
  1147. }
  1148. _createClass(InverseSuffixExactMatch, [{
  1149. key: "search",
  1150. value: function search(text) {
  1151. var isMatch = !text.endsWith(this.pattern);
  1152. return {
  1153. isMatch: isMatch,
  1154. score: isMatch ? 0 : 1,
  1155. indices: [0, text.length - 1]
  1156. };
  1157. }
  1158. }], [{
  1159. key: "type",
  1160. get: function get() {
  1161. return 'inverse-suffix-exact';
  1162. }
  1163. }, {
  1164. key: "multiRegex",
  1165. get: function get() {
  1166. return /^!"(.*)"\$$/;
  1167. }
  1168. }, {
  1169. key: "singleRegex",
  1170. get: function get() {
  1171. return /^!(.*)\$$/;
  1172. }
  1173. }]);
  1174. return InverseSuffixExactMatch;
  1175. }(BaseMatch);
  1176. var FuzzyMatch = /*#__PURE__*/function (_BaseMatch) {
  1177. _inherits(FuzzyMatch, _BaseMatch);
  1178. var _super = _createSuper(FuzzyMatch);
  1179. function FuzzyMatch(pattern) {
  1180. var _this;
  1181. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  1182. _ref$location = _ref.location,
  1183. location = _ref$location === void 0 ? Config.location : _ref$location,
  1184. _ref$threshold = _ref.threshold,
  1185. threshold = _ref$threshold === void 0 ? Config.threshold : _ref$threshold,
  1186. _ref$distance = _ref.distance,
  1187. distance = _ref$distance === void 0 ? Config.distance : _ref$distance,
  1188. _ref$includeMatches = _ref.includeMatches,
  1189. includeMatches = _ref$includeMatches === void 0 ? Config.includeMatches : _ref$includeMatches,
  1190. _ref$findAllMatches = _ref.findAllMatches,
  1191. findAllMatches = _ref$findAllMatches === void 0 ? Config.findAllMatches : _ref$findAllMatches,
  1192. _ref$minMatchCharLeng = _ref.minMatchCharLength,
  1193. minMatchCharLength = _ref$minMatchCharLeng === void 0 ? Config.minMatchCharLength : _ref$minMatchCharLeng,
  1194. _ref$isCaseSensitive = _ref.isCaseSensitive,
  1195. isCaseSensitive = _ref$isCaseSensitive === void 0 ? Config.isCaseSensitive : _ref$isCaseSensitive,
  1196. _ref$ignoreLocation = _ref.ignoreLocation,
  1197. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation;
  1198. _classCallCheck(this, FuzzyMatch);
  1199. _this = _super.call(this, pattern);
  1200. _this._bitapSearch = new BitapSearch(pattern, {
  1201. location: location,
  1202. threshold: threshold,
  1203. distance: distance,
  1204. includeMatches: includeMatches,
  1205. findAllMatches: findAllMatches,
  1206. minMatchCharLength: minMatchCharLength,
  1207. isCaseSensitive: isCaseSensitive,
  1208. ignoreLocation: ignoreLocation
  1209. });
  1210. return _this;
  1211. }
  1212. _createClass(FuzzyMatch, [{
  1213. key: "search",
  1214. value: function search(text) {
  1215. return this._bitapSearch.searchIn(text);
  1216. }
  1217. }], [{
  1218. key: "type",
  1219. get: function get() {
  1220. return 'fuzzy';
  1221. }
  1222. }, {
  1223. key: "multiRegex",
  1224. get: function get() {
  1225. return /^"(.*)"$/;
  1226. }
  1227. }, {
  1228. key: "singleRegex",
  1229. get: function get() {
  1230. return /^(.*)$/;
  1231. }
  1232. }]);
  1233. return FuzzyMatch;
  1234. }(BaseMatch);
  1235. var IncludeMatch = /*#__PURE__*/function (_BaseMatch) {
  1236. _inherits(IncludeMatch, _BaseMatch);
  1237. var _super = _createSuper(IncludeMatch);
  1238. function IncludeMatch(pattern) {
  1239. _classCallCheck(this, IncludeMatch);
  1240. return _super.call(this, pattern);
  1241. }
  1242. _createClass(IncludeMatch, [{
  1243. key: "search",
  1244. value: function search(text) {
  1245. var location = 0;
  1246. var index;
  1247. var indices = [];
  1248. var patternLen = this.pattern.length; // Get all exact matches
  1249. while ((index = text.indexOf(this.pattern, location)) > -1) {
  1250. location = index + patternLen;
  1251. indices.push([index, location - 1]);
  1252. }
  1253. var isMatch = !!indices.length;
  1254. return {
  1255. isMatch: isMatch,
  1256. score: isMatch ? 1 : 0,
  1257. indices: indices
  1258. };
  1259. }
  1260. }], [{
  1261. key: "type",
  1262. get: function get() {
  1263. return 'include';
  1264. }
  1265. }, {
  1266. key: "multiRegex",
  1267. get: function get() {
  1268. return /^'"(.*)"$/;
  1269. }
  1270. }, {
  1271. key: "singleRegex",
  1272. get: function get() {
  1273. return /^'(.*)$/;
  1274. }
  1275. }]);
  1276. return IncludeMatch;
  1277. }(BaseMatch);
  1278. var searchers = [ExactMatch, IncludeMatch, PrefixExactMatch, InversePrefixExactMatch, InverseSuffixExactMatch, SuffixExactMatch, InverseExactMatch, FuzzyMatch];
  1279. var searchersLen = searchers.length; // Regex to split by spaces, but keep anything in quotes together
  1280. var SPACE_RE = / +(?=([^\"]*\"[^\"]*\")*[^\"]*$)/;
  1281. var OR_TOKEN = '|'; // Return a 2D array representation of the query, for simpler parsing.
  1282. // Example:
  1283. // "^core go$ | rb$ | py$ xy$" => [["^core", "go$"], ["rb$"], ["py$", "xy$"]]
  1284. function parseQuery(pattern) {
  1285. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1286. return pattern.split(OR_TOKEN).map(function (item) {
  1287. var query = item.trim().split(SPACE_RE).filter(function (item) {
  1288. return item && !!item.trim();
  1289. });
  1290. var results = [];
  1291. for (var i = 0, len = query.length; i < len; i += 1) {
  1292. var queryItem = query[i]; // 1. Handle multiple query match (i.e, once that are quoted, like `"hello world"`)
  1293. var found = false;
  1294. var idx = -1;
  1295. while (!found && ++idx < searchersLen) {
  1296. var searcher = searchers[idx];
  1297. var token = searcher.isMultiMatch(queryItem);
  1298. if (token) {
  1299. results.push(new searcher(token, options));
  1300. found = true;
  1301. }
  1302. }
  1303. if (found) {
  1304. continue;
  1305. } // 2. Handle single query matches (i.e, once that are *not* quoted)
  1306. idx = -1;
  1307. while (++idx < searchersLen) {
  1308. var _searcher = searchers[idx];
  1309. var _token = _searcher.isSingleMatch(queryItem);
  1310. if (_token) {
  1311. results.push(new _searcher(_token, options));
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. return results;
  1317. });
  1318. }
  1319. // to a singl match
  1320. var MultiMatchSet = new Set([FuzzyMatch.type, IncludeMatch.type]);
  1321. /**
  1322. * Command-like searching
  1323. * ======================
  1324. *
  1325. * Given multiple search terms delimited by spaces.e.g. `^jscript .python$ ruby !java`,
  1326. * search in a given text.
  1327. *
  1328. * Search syntax:
  1329. *
  1330. * | Token | Match type | Description |
  1331. * | ----------- | -------------------------- | -------------------------------------- |
  1332. * | `jscript` | fuzzy-match | Items that fuzzy match `jscript` |
  1333. * | `=scheme` | exact-match | Items that are `scheme` |
  1334. * | `'python` | include-match | Items that include `python` |
  1335. * | `!ruby` | inverse-exact-match | Items that do not include `ruby` |
  1336. * | `^java` | prefix-exact-match | Items that start with `java` |
  1337. * | `!^earlang` | inverse-prefix-exact-match | Items that do not start with `earlang` |
  1338. * | `.js$` | suffix-exact-match | Items that end with `.js` |
  1339. * | `!.go$` | inverse-suffix-exact-match | Items that do not end with `.go` |
  1340. *
  1341. * A single pipe character acts as an OR operator. For example, the following
  1342. * query matches entries that start with `core` and end with either`go`, `rb`,
  1343. * or`py`.
  1344. *
  1345. * ```
  1346. * ^core go$ | rb$ | py$
  1347. * ```
  1348. */
  1349. var ExtendedSearch = /*#__PURE__*/function () {
  1350. function ExtendedSearch(pattern) {
  1351. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  1352. _ref$isCaseSensitive = _ref.isCaseSensitive,
  1353. isCaseSensitive = _ref$isCaseSensitive === void 0 ? Config.isCaseSensitive : _ref$isCaseSensitive,
  1354. _ref$includeMatches = _ref.includeMatches,
  1355. includeMatches = _ref$includeMatches === void 0 ? Config.includeMatches : _ref$includeMatches,
  1356. _ref$minMatchCharLeng = _ref.minMatchCharLength,
  1357. minMatchCharLength = _ref$minMatchCharLeng === void 0 ? Config.minMatchCharLength : _ref$minMatchCharLeng,
  1358. _ref$ignoreLocation = _ref.ignoreLocation,
  1359. ignoreLocation = _ref$ignoreLocation === void 0 ? Config.ignoreLocation : _ref$ignoreLocation,
  1360. _ref$findAllMatches = _ref.findAllMatches,
  1361. findAllMatches = _ref$findAllMatches === void 0 ? Config.findAllMatches : _ref$findAllMatches,
  1362. _ref$location = _ref.location,
  1363. location = _ref$location === void 0 ? Config.location : _ref$location,
  1364. _ref$threshold = _ref.threshold,
  1365. threshold = _ref$threshold === void 0 ? Config.threshold : _ref$threshold,
  1366. _ref$distance = _ref.distance,
  1367. distance = _ref$distance === void 0 ? Config.distance : _ref$distance;
  1368. _classCallCheck(this, ExtendedSearch);
  1369. this.query = null;
  1370. this.options = {
  1371. isCaseSensitive: isCaseSensitive,
  1372. includeMatches: includeMatches,
  1373. minMatchCharLength: minMatchCharLength,
  1374. findAllMatches: findAllMatches,
  1375. ignoreLocation: ignoreLocation,
  1376. location: location,
  1377. threshold: threshold,
  1378. distance: distance
  1379. };
  1380. this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
  1381. this.query = parseQuery(this.pattern, this.options);
  1382. }
  1383. _createClass(ExtendedSearch, [{
  1384. key: "searchIn",
  1385. value: function searchIn(text) {
  1386. var query = this.query;
  1387. if (!query) {
  1388. return {
  1389. isMatch: false,
  1390. score: 1
  1391. };
  1392. }
  1393. var _this$options = this.options,
  1394. includeMatches = _this$options.includeMatches,
  1395. isCaseSensitive = _this$options.isCaseSensitive;
  1396. text = isCaseSensitive ? text : text.toLowerCase();
  1397. var numMatches = 0;
  1398. var allIndices = [];
  1399. var totalScore = 0; // ORs
  1400. for (var i = 0, qLen = query.length; i < qLen; i += 1) {
  1401. var searchers = query[i]; // Reset indices
  1402. allIndices.length = 0;
  1403. numMatches = 0; // ANDs
  1404. for (var j = 0, pLen = searchers.length; j < pLen; j += 1) {
  1405. var searcher = searchers[j];
  1406. var _searcher$search = searcher.search(text),
  1407. isMatch = _searcher$search.isMatch,
  1408. indices = _searcher$search.indices,
  1409. score = _searcher$search.score;
  1410. if (isMatch) {
  1411. numMatches += 1;
  1412. totalScore += score;
  1413. if (includeMatches) {
  1414. var type = searcher.constructor.type;
  1415. if (MultiMatchSet.has(type)) {
  1416. allIndices = [].concat(_toConsumableArray(allIndices), _toConsumableArray(indices));
  1417. } else {
  1418. allIndices.push(indices);
  1419. }
  1420. }
  1421. } else {
  1422. totalScore = 0;
  1423. numMatches = 0;
  1424. allIndices.length = 0;
  1425. break;
  1426. }
  1427. } // OR condition, so if TRUE, return
  1428. if (numMatches) {
  1429. var result = {
  1430. isMatch: true,
  1431. score: totalScore / numMatches
  1432. };
  1433. if (includeMatches) {
  1434. result.indices = allIndices;
  1435. }
  1436. return result;
  1437. }
  1438. } // Nothing was matched
  1439. return {
  1440. isMatch: false,
  1441. score: 1
  1442. };
  1443. }
  1444. }], [{
  1445. key: "condition",
  1446. value: function condition(_, options) {
  1447. return options.useExtendedSearch;
  1448. }
  1449. }]);
  1450. return ExtendedSearch;
  1451. }();
  1452. var registeredSearchers = [];
  1453. function register() {
  1454. registeredSearchers.push.apply(registeredSearchers, arguments);
  1455. }
  1456. function createSearcher(pattern, options) {
  1457. for (var i = 0, len = registeredSearchers.length; i < len; i += 1) {
  1458. var searcherClass = registeredSearchers[i];
  1459. if (searcherClass.condition(pattern, options)) {
  1460. return new searcherClass(pattern, options);
  1461. }
  1462. }
  1463. return new BitapSearch(pattern, options);
  1464. }
  1465. var LogicalOperator = {
  1466. AND: '$and',
  1467. OR: '$or'
  1468. };
  1469. var KeyType = {
  1470. PATH: '$path',
  1471. PATTERN: '$val'
  1472. };
  1473. var isExpression = function isExpression(query) {
  1474. return !!(query[LogicalOperator.AND] || query[LogicalOperator.OR]);
  1475. };
  1476. var isPath = function isPath(query) {
  1477. return !!query[KeyType.PATH];
  1478. };
  1479. var isLeaf = function isLeaf(query) {
  1480. return !isArray(query) && isObject(query) && !isExpression(query);
  1481. };
  1482. var convertToExplicit = function convertToExplicit(query) {
  1483. return _defineProperty({}, LogicalOperator.AND, Object.keys(query).map(function (key) {
  1484. return _defineProperty({}, key, query[key]);
  1485. }));
  1486. }; // When `auto` is `true`, the parse function will infer and initialize and add
  1487. // the appropriate `Searcher` instance
  1488. function parse(query, options) {
  1489. var _ref3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  1490. _ref3$auto = _ref3.auto,
  1491. auto = _ref3$auto === void 0 ? true : _ref3$auto;
  1492. var next = function next(query) {
  1493. var keys = Object.keys(query);
  1494. var isQueryPath = isPath(query);
  1495. if (!isQueryPath && keys.length > 1 && !isExpression(query)) {
  1496. return next(convertToExplicit(query));
  1497. }
  1498. if (isLeaf(query)) {
  1499. var key = isQueryPath ? query[KeyType.PATH] : keys[0];
  1500. var pattern = isQueryPath ? query[KeyType.PATTERN] : query[key];
  1501. if (!isString(pattern)) {
  1502. throw new Error(LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key));
  1503. }
  1504. var obj = {
  1505. keyId: createKeyId(key),
  1506. pattern: pattern
  1507. };
  1508. if (auto) {
  1509. obj.searcher = createSearcher(pattern, options);
  1510. }
  1511. return obj;
  1512. }
  1513. var node = {
  1514. children: [],
  1515. operator: keys[0]
  1516. };
  1517. keys.forEach(function (key) {
  1518. var value = query[key];
  1519. if (isArray(value)) {
  1520. value.forEach(function (item) {
  1521. node.children.push(next(item));
  1522. });
  1523. }
  1524. });
  1525. return node;
  1526. };
  1527. if (!isExpression(query)) {
  1528. query = convertToExplicit(query);
  1529. }
  1530. return next(query);
  1531. }
  1532. var Fuse = /*#__PURE__*/function () {
  1533. function Fuse(docs) {
  1534. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1535. var index = arguments.length > 2 ? arguments[2] : undefined;
  1536. _classCallCheck(this, Fuse);
  1537. this.options = _objectSpread2({}, Config, {}, options);
  1538. if (this.options.useExtendedSearch && !true) {
  1539. throw new Error(EXTENDED_SEARCH_UNAVAILABLE);
  1540. }
  1541. this._keyStore = new KeyStore(this.options.keys);
  1542. this.setCollection(docs, index);
  1543. }
  1544. _createClass(Fuse, [{
  1545. key: "setCollection",
  1546. value: function setCollection(docs, index) {
  1547. this._docs = docs;
  1548. if (index && !(index instanceof FuseIndex)) {
  1549. throw new Error(INCORRECT_INDEX_TYPE);
  1550. }
  1551. this._myIndex = index || createIndex(this.options.keys, this._docs, {
  1552. getFn: this.options.getFn
  1553. });
  1554. }
  1555. }, {
  1556. key: "add",
  1557. value: function add(doc) {
  1558. if (!isDefined(doc)) {
  1559. return;
  1560. }
  1561. this._docs.push(doc);
  1562. this._myIndex.add(doc);
  1563. }
  1564. }, {
  1565. key: "remove",
  1566. value: function remove() {
  1567. var predicate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
  1568. return (
  1569. /* doc, idx */
  1570. false
  1571. );
  1572. };
  1573. var results = [];
  1574. for (var i = 0, len = this._docs.length; i < len; i += 1) {
  1575. var doc = this._docs[i];
  1576. if (predicate(doc, i)) {
  1577. this.removeAt(i);
  1578. i -= 1;
  1579. len -= 1;
  1580. results.push(doc);
  1581. }
  1582. }
  1583. return results;
  1584. }
  1585. }, {
  1586. key: "removeAt",
  1587. value: function removeAt(idx) {
  1588. this._docs.splice(idx, 1);
  1589. this._myIndex.removeAt(idx);
  1590. }
  1591. }, {
  1592. key: "getIndex",
  1593. value: function getIndex() {
  1594. return this._myIndex;
  1595. }
  1596. }, {
  1597. key: "search",
  1598. value: function search(query) {
  1599. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  1600. _ref$limit = _ref.limit,
  1601. limit = _ref$limit === void 0 ? -1 : _ref$limit;
  1602. var _this$options = this.options,
  1603. includeMatches = _this$options.includeMatches,
  1604. includeScore = _this$options.includeScore,
  1605. shouldSort = _this$options.shouldSort,
  1606. sortFn = _this$options.sortFn,
  1607. ignoreFieldNorm = _this$options.ignoreFieldNorm;
  1608. var results = isString(query) ? isString(this._docs[0]) ? this._searchStringList(query) : this._searchObjectList(query) : this._searchLogical(query);
  1609. computeScore$1(results, {
  1610. ignoreFieldNorm: ignoreFieldNorm
  1611. });
  1612. if (shouldSort) {
  1613. results.sort(sortFn);
  1614. }
  1615. if (isNumber(limit) && limit > -1) {
  1616. results = results.slice(0, limit);
  1617. }
  1618. return format(results, this._docs, {
  1619. includeMatches: includeMatches,
  1620. includeScore: includeScore
  1621. });
  1622. }
  1623. }, {
  1624. key: "_searchStringList",
  1625. value: function _searchStringList(query) {
  1626. var searcher = createSearcher(query, this.options);
  1627. var records = this._myIndex.records;
  1628. var results = []; // Iterate over every string in the index
  1629. records.forEach(function (_ref2) {
  1630. var text = _ref2.v,
  1631. idx = _ref2.i,
  1632. norm = _ref2.n;
  1633. if (!isDefined(text)) {
  1634. return;
  1635. }
  1636. var _searcher$searchIn = searcher.searchIn(text),
  1637. isMatch = _searcher$searchIn.isMatch,
  1638. score = _searcher$searchIn.score,
  1639. indices = _searcher$searchIn.indices;
  1640. if (isMatch) {
  1641. results.push({
  1642. item: text,
  1643. idx: idx,
  1644. matches: [{
  1645. score: score,
  1646. value: text,
  1647. norm: norm,
  1648. indices: indices
  1649. }]
  1650. });
  1651. }
  1652. });
  1653. return results;
  1654. }
  1655. }, {
  1656. key: "_searchLogical",
  1657. value: function _searchLogical(query) {
  1658. var _this = this;
  1659. var expression = parse(query, this.options);
  1660. var evaluate = function evaluate(node, item, idx) {
  1661. if (!node.children) {
  1662. var keyId = node.keyId,
  1663. searcher = node.searcher;
  1664. var matches = _this._findMatches({
  1665. key: _this._keyStore.get(keyId),
  1666. value: _this._myIndex.getValueForItemAtKeyId(item, keyId),
  1667. searcher: searcher
  1668. });
  1669. if (matches && matches.length) {
  1670. return [{
  1671. idx: idx,
  1672. item: item,
  1673. matches: matches
  1674. }];
  1675. }
  1676. return [];
  1677. }
  1678. /*eslint indent: [2, 2, {"SwitchCase": 1}]*/
  1679. switch (node.operator) {
  1680. case LogicalOperator.AND:
  1681. {
  1682. var res = [];
  1683. for (var i = 0, len = node.children.length; i < len; i += 1) {
  1684. var child = node.children[i];
  1685. var result = evaluate(child, item, idx);
  1686. if (result.length) {
  1687. res.push.apply(res, _toConsumableArray(result));
  1688. } else {
  1689. return [];
  1690. }
  1691. }
  1692. return res;
  1693. }
  1694. case LogicalOperator.OR:
  1695. {
  1696. var _res = [];
  1697. for (var _i = 0, _len = node.children.length; _i < _len; _i += 1) {
  1698. var _child = node.children[_i];
  1699. var _result = evaluate(_child, item, idx);
  1700. if (_result.length) {
  1701. _res.push.apply(_res, _toConsumableArray(_result));
  1702. break;
  1703. }
  1704. }
  1705. return _res;
  1706. }
  1707. }
  1708. };
  1709. var records = this._myIndex.records;
  1710. var resultMap = {};
  1711. var results = [];
  1712. records.forEach(function (_ref3) {
  1713. var item = _ref3.$,
  1714. idx = _ref3.i;
  1715. if (isDefined(item)) {
  1716. var expResults = evaluate(expression, item, idx);
  1717. if (expResults.length) {
  1718. // Dedupe when adding
  1719. if (!resultMap[idx]) {
  1720. resultMap[idx] = {
  1721. idx: idx,
  1722. item: item,
  1723. matches: []
  1724. };
  1725. results.push(resultMap[idx]);
  1726. }
  1727. expResults.forEach(function (_ref4) {
  1728. var _resultMap$idx$matche;
  1729. var matches = _ref4.matches;
  1730. (_resultMap$idx$matche = resultMap[idx].matches).push.apply(_resultMap$idx$matche, _toConsumableArray(matches));
  1731. });
  1732. }
  1733. }
  1734. });
  1735. return results;
  1736. }
  1737. }, {
  1738. key: "_searchObjectList",
  1739. value: function _searchObjectList(query) {
  1740. var _this2 = this;
  1741. var searcher = createSearcher(query, this.options);
  1742. var _this$_myIndex = this._myIndex,
  1743. keys = _this$_myIndex.keys,
  1744. records = _this$_myIndex.records;
  1745. var results = []; // List is Array<Object>
  1746. records.forEach(function (_ref5) {
  1747. var item = _ref5.$,
  1748. idx = _ref5.i;
  1749. if (!isDefined(item)) {
  1750. return;
  1751. }
  1752. var matches = []; // Iterate over every key (i.e, path), and fetch the value at that key
  1753. keys.forEach(function (key, keyIndex) {
  1754. matches.push.apply(matches, _toConsumableArray(_this2._findMatches({
  1755. key: key,
  1756. value: item[keyIndex],
  1757. searcher: searcher
  1758. })));
  1759. });
  1760. if (matches.length) {
  1761. results.push({
  1762. idx: idx,
  1763. item: item,
  1764. matches: matches
  1765. });
  1766. }
  1767. });
  1768. return results;
  1769. }
  1770. }, {
  1771. key: "_findMatches",
  1772. value: function _findMatches(_ref6) {
  1773. var key = _ref6.key,
  1774. value = _ref6.value,
  1775. searcher = _ref6.searcher;
  1776. if (!isDefined(value)) {
  1777. return [];
  1778. }
  1779. var matches = [];
  1780. if (isArray(value)) {
  1781. value.forEach(function (_ref7) {
  1782. var text = _ref7.v,
  1783. idx = _ref7.i,
  1784. norm = _ref7.n;
  1785. if (!isDefined(text)) {
  1786. return;
  1787. }
  1788. var _searcher$searchIn2 = searcher.searchIn(text),
  1789. isMatch = _searcher$searchIn2.isMatch,
  1790. score = _searcher$searchIn2.score,
  1791. indices = _searcher$searchIn2.indices;
  1792. if (isMatch) {
  1793. matches.push({
  1794. score: score,
  1795. key: key,
  1796. value: text,
  1797. idx: idx,
  1798. norm: norm,
  1799. indices: indices
  1800. });
  1801. }
  1802. });
  1803. } else {
  1804. var text = value.v,
  1805. norm = value.n;
  1806. var _searcher$searchIn3 = searcher.searchIn(text),
  1807. isMatch = _searcher$searchIn3.isMatch,
  1808. score = _searcher$searchIn3.score,
  1809. indices = _searcher$searchIn3.indices;
  1810. if (isMatch) {
  1811. matches.push({
  1812. score: score,
  1813. key: key,
  1814. value: text,
  1815. norm: norm,
  1816. indices: indices
  1817. });
  1818. }
  1819. }
  1820. return matches;
  1821. }
  1822. }]);
  1823. return Fuse;
  1824. }(); // Practical scoring function
  1825. function computeScore$1(results, _ref8) {
  1826. var _ref8$ignoreFieldNorm = _ref8.ignoreFieldNorm,
  1827. ignoreFieldNorm = _ref8$ignoreFieldNorm === void 0 ? Config.ignoreFieldNorm : _ref8$ignoreFieldNorm;
  1828. results.forEach(function (result) {
  1829. var totalScore = 1;
  1830. result.matches.forEach(function (_ref9) {
  1831. var key = _ref9.key,
  1832. norm = _ref9.norm,
  1833. score = _ref9.score;
  1834. var weight = key ? key.weight : null;
  1835. totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
  1836. });
  1837. result.score = totalScore;
  1838. });
  1839. }
  1840. function format(results, docs) {
  1841. var _ref10 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  1842. _ref10$includeMatches = _ref10.includeMatches,
  1843. includeMatches = _ref10$includeMatches === void 0 ? Config.includeMatches : _ref10$includeMatches,
  1844. _ref10$includeScore = _ref10.includeScore,
  1845. includeScore = _ref10$includeScore === void 0 ? Config.includeScore : _ref10$includeScore;
  1846. var transformers = [];
  1847. if (includeMatches) transformers.push(transformMatches);
  1848. if (includeScore) transformers.push(transformScore);
  1849. return results.map(function (result) {
  1850. var idx = result.idx;
  1851. var data = {
  1852. item: docs[idx],
  1853. refIndex: idx
  1854. };
  1855. if (transformers.length) {
  1856. transformers.forEach(function (transformer) {
  1857. transformer(result, data);
  1858. });
  1859. }
  1860. return data;
  1861. });
  1862. }
  1863. Fuse.version = '6.4.3';
  1864. Fuse.createIndex = createIndex;
  1865. Fuse.parseIndex = parseIndex;
  1866. Fuse.config = Config;
  1867. {
  1868. Fuse.parseQuery = parse;
  1869. }
  1870. {
  1871. register(ExtendedSearch);
  1872. }
  1873. module.exports = Fuse;