All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2261 lines
56 KiB

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