mbeditor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +127 -0
  3. data/app/assets/javascripts/mbeditor/application.js +19 -0
  4. data/app/assets/javascripts/mbeditor/components/CodeReviewPanel.js +202 -0
  5. data/app/assets/javascripts/mbeditor/components/CollapsibleSection.js +71 -0
  6. data/app/assets/javascripts/mbeditor/components/CombinedDiffViewer.js +139 -0
  7. data/app/assets/javascripts/mbeditor/components/CommitGraph.js +65 -0
  8. data/app/assets/javascripts/mbeditor/components/DiffViewer.js +142 -0
  9. data/app/assets/javascripts/mbeditor/components/EditorPanel.js +363 -0
  10. data/app/assets/javascripts/mbeditor/components/FileHistoryPanel.js +112 -0
  11. data/app/assets/javascripts/mbeditor/components/FileTree.js +304 -0
  12. data/app/assets/javascripts/mbeditor/components/GitPanel.js +416 -0
  13. data/app/assets/javascripts/mbeditor/components/MbeditorApp.js +2335 -0
  14. data/app/assets/javascripts/mbeditor/components/QuickOpenDialog.js +118 -0
  15. data/app/assets/javascripts/mbeditor/components/ShortcutHelp.js +186 -0
  16. data/app/assets/javascripts/mbeditor/components/TabBar.js +123 -0
  17. data/app/assets/javascripts/mbeditor/editor_plugins.js +282 -0
  18. data/app/assets/javascripts/mbeditor/editor_store.js +53 -0
  19. data/app/assets/javascripts/mbeditor/file_service.js +77 -0
  20. data/app/assets/javascripts/mbeditor/git_service.js +104 -0
  21. data/app/assets/javascripts/mbeditor/search_service.js +53 -0
  22. data/app/assets/javascripts/mbeditor/tab_manager.js +461 -0
  23. data/app/assets/stylesheets/mbeditor/application.css +705 -0
  24. data/app/assets/stylesheets/mbeditor/editor.css +1264 -0
  25. data/app/controllers/mbeditor/application_controller.rb +10 -0
  26. data/app/controllers/mbeditor/editors_controller.rb +695 -0
  27. data/app/controllers/mbeditor/git_controller.rb +188 -0
  28. data/app/services/mbeditor/git_blame_service.rb +98 -0
  29. data/app/services/mbeditor/git_commit_graph_service.rb +60 -0
  30. data/app/services/mbeditor/git_diff_service.rb +71 -0
  31. data/app/services/mbeditor/git_file_history_service.rb +42 -0
  32. data/app/services/mbeditor/git_service.rb +82 -0
  33. data/app/services/mbeditor/redmine_service.rb +86 -0
  34. data/app/views/layouts/mbeditor/application.html.erb +71 -0
  35. data/app/views/mbeditor/editors/index.html.erb +1 -0
  36. data/config/environments/development.rb +53 -0
  37. data/config/initializers/assets.rb +9 -0
  38. data/config/routes.rb +37 -0
  39. data/lib/mbeditor/configuration.rb +16 -0
  40. data/lib/mbeditor/engine.rb +28 -0
  41. data/lib/mbeditor/version.rb +3 -0
  42. data/lib/mbeditor.rb +19 -0
  43. data/mbeditor.gemspec +30 -0
  44. data/public/min-maps/vs/base/worker/workerMain.js.map +1 -0
  45. data/public/monaco-editor/vs/base/browser/ui/codicons/codicon/codicon.ttf +0 -0
  46. data/public/monaco-editor/vs/base/worker/workerMain.js +31 -0
  47. data/public/monaco-editor/vs/basic-languages/cameligo/cameligo.js +10 -0
  48. data/public/monaco-editor/vs/basic-languages/css/css.js +12 -0
  49. data/public/monaco-editor/vs/basic-languages/dart/dart.js +10 -0
  50. data/public/monaco-editor/vs/basic-languages/flow9/flow9.js +10 -0
  51. data/public/monaco-editor/vs/basic-languages/go/go.js +10 -0
  52. data/public/monaco-editor/vs/basic-languages/handlebars/handlebars.js +440 -0
  53. data/public/monaco-editor/vs/basic-languages/javascript/javascript.js +10 -0
  54. data/public/monaco-editor/vs/basic-languages/markdown/markdown.js +10 -0
  55. data/public/monaco-editor/vs/basic-languages/msdax/msdax.js +10 -0
  56. data/public/monaco-editor/vs/basic-languages/postiats/postiats.js +10 -0
  57. data/public/monaco-editor/vs/basic-languages/pug/pug.js +412 -0
  58. data/public/monaco-editor/vs/basic-languages/restructuredtext/restructuredtext.js +10 -0
  59. data/public/monaco-editor/vs/basic-languages/ruby/ruby.js +10 -0
  60. data/public/monaco-editor/vs/basic-languages/sb/sb.js +10 -0
  61. data/public/monaco-editor/vs/basic-languages/typespec/typespec.js +10 -0
  62. data/public/monaco-editor/vs/basic-languages/yaml/yaml.js +10 -0
  63. data/public/monaco-editor/vs/editor/editor.main.css +8 -0
  64. data/public/monaco-editor/vs/editor/editor.main.js +797 -0
  65. data/public/monaco-editor/vs/language/typescript/tsMode.js +20 -0
  66. data/public/monaco-editor/vs/language/typescript/tsWorker.js +51328 -0
  67. data/public/monaco-editor/vs/loader.js +10 -0
  68. data/public/monaco-editor/vs/nls.messages.de.js +20 -0
  69. data/public/monaco-editor/vs/nls.messages.es.js +20 -0
  70. data/public/monaco-editor/vs/nls.messages.fr.js +18 -0
  71. data/public/monaco-editor/vs/nls.messages.it.js +18 -0
  72. data/public/monaco-editor/vs/nls.messages.ja.js +20 -0
  73. data/public/monaco-editor/vs/nls.messages.ko.js +18 -0
  74. data/public/monaco-editor/vs/nls.messages.ru.js +20 -0
  75. data/public/monaco-editor/vs/nls.messages.zh-cn.js +20 -0
  76. data/public/monaco-editor/vs/nls.messages.zh-tw.js +18 -0
  77. data/public/monaco_worker.js +5 -0
  78. data/vendor/assets/javascripts/axios.min.js +2 -0
  79. data/vendor/assets/javascripts/lodash.min.js +140 -0
  80. data/vendor/assets/javascripts/marked.min.js +6 -0
  81. data/vendor/assets/javascripts/minisearch.min.js +2044 -0
  82. data/vendor/assets/javascripts/prettier-plugin-babel.js +16 -0
  83. data/vendor/assets/javascripts/prettier-plugin-estree.js +35 -0
  84. data/vendor/assets/javascripts/prettier-plugin-html.js +19 -0
  85. data/vendor/assets/javascripts/prettier-plugin-markdown.js +59 -0
  86. data/vendor/assets/javascripts/prettier-plugin-postcss.js +52 -0
  87. data/vendor/assets/javascripts/prettier-standalone.js +37 -0
  88. data/vendor/assets/javascripts/react-dom.min.js +267 -0
  89. data/vendor/assets/javascripts/react.min.js +31 -0
  90. data/vendor/assets/stylesheets/fontawesome.min.css +9 -0
  91. data/vendor/assets/webfonts/fa-brands-400.woff2 +0 -0
  92. data/vendor/assets/webfonts/fa-regular-400.woff2 +0 -0
  93. data/vendor/assets/webfonts/fa-solid-900.woff2 +0 -0
  94. metadata +173 -0
@@ -0,0 +1,2044 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.MiniSearch = factory());
5
+ })(this, (function () { 'use strict';
6
+
7
+ /******************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+ /* global Reflect, Promise, SuppressedError, Symbol */
22
+
23
+
24
+ function __awaiter(thisArg, _arguments, P, generator) {
25
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
+ return new (P || (P = Promise))(function (resolve, reject) {
27
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
30
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
31
+ });
32
+ }
33
+
34
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
35
+ var e = new Error(message);
36
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
37
+ };
38
+
39
+ /** @ignore */
40
+ const ENTRIES = 'ENTRIES';
41
+ /** @ignore */
42
+ const KEYS = 'KEYS';
43
+ /** @ignore */
44
+ const VALUES = 'VALUES';
45
+ /** @ignore */
46
+ const LEAF = '';
47
+ /**
48
+ * @private
49
+ */
50
+ class TreeIterator {
51
+ constructor(set, type) {
52
+ const node = set._tree;
53
+ const keys = Array.from(node.keys());
54
+ this.set = set;
55
+ this._type = type;
56
+ this._path = keys.length > 0 ? [{ node, keys }] : [];
57
+ }
58
+ next() {
59
+ const value = this.dive();
60
+ this.backtrack();
61
+ return value;
62
+ }
63
+ dive() {
64
+ if (this._path.length === 0) {
65
+ return { done: true, value: undefined };
66
+ }
67
+ const { node, keys } = last$1(this._path);
68
+ if (last$1(keys) === LEAF) {
69
+ return { done: false, value: this.result() };
70
+ }
71
+ const child = node.get(last$1(keys));
72
+ this._path.push({ node: child, keys: Array.from(child.keys()) });
73
+ return this.dive();
74
+ }
75
+ backtrack() {
76
+ if (this._path.length === 0) {
77
+ return;
78
+ }
79
+ const keys = last$1(this._path).keys;
80
+ keys.pop();
81
+ if (keys.length > 0) {
82
+ return;
83
+ }
84
+ this._path.pop();
85
+ this.backtrack();
86
+ }
87
+ key() {
88
+ return this.set._prefix + this._path
89
+ .map(({ keys }) => last$1(keys))
90
+ .filter(key => key !== LEAF)
91
+ .join('');
92
+ }
93
+ value() {
94
+ return last$1(this._path).node.get(LEAF);
95
+ }
96
+ result() {
97
+ switch (this._type) {
98
+ case VALUES: return this.value();
99
+ case KEYS: return this.key();
100
+ default: return [this.key(), this.value()];
101
+ }
102
+ }
103
+ [Symbol.iterator]() {
104
+ return this;
105
+ }
106
+ }
107
+ const last$1 = (array) => {
108
+ return array[array.length - 1];
109
+ };
110
+
111
+ /* eslint-disable no-labels */
112
+ /**
113
+ * @ignore
114
+ */
115
+ const fuzzySearch = (node, query, maxDistance) => {
116
+ const results = new Map();
117
+ if (query === undefined)
118
+ return results;
119
+ // Number of columns in the Levenshtein matrix.
120
+ const n = query.length + 1;
121
+ // Matching terms can never be longer than N + maxDistance.
122
+ const m = n + maxDistance;
123
+ // Fill first matrix row and column with numbers: 0 1 2 3 ...
124
+ const matrix = new Uint8Array(m * n).fill(maxDistance + 1);
125
+ for (let j = 0; j < n; ++j)
126
+ matrix[j] = j;
127
+ for (let i = 1; i < m; ++i)
128
+ matrix[i * n] = i;
129
+ recurse(node, query, maxDistance, results, matrix, 1, n, '');
130
+ return results;
131
+ };
132
+ // Modified version of http://stevehanov.ca/blog/?id=114
133
+ // This builds a Levenshtein matrix for a given query and continuously updates
134
+ // it for nodes in the radix tree that fall within the given maximum edit
135
+ // distance. Keeping the same matrix around is beneficial especially for larger
136
+ // edit distances.
137
+ //
138
+ // k a t e <-- query
139
+ // 0 1 2 3 4
140
+ // c 1 1 2 3 4
141
+ // a 2 2 1 2 3
142
+ // t 3 3 2 1 [2] <-- edit distance
143
+ // ^
144
+ // ^ term in radix tree, rows are added and removed as needed
145
+ const recurse = (node, query, maxDistance, results, matrix, m, n, prefix) => {
146
+ const offset = m * n;
147
+ key: for (const key of node.keys()) {
148
+ if (key === LEAF) {
149
+ // We've reached a leaf node. Check if the edit distance acceptable and
150
+ // store the result if it is.
151
+ const distance = matrix[offset - 1];
152
+ if (distance <= maxDistance) {
153
+ results.set(prefix, [node.get(key), distance]);
154
+ }
155
+ }
156
+ else {
157
+ // Iterate over all characters in the key. Update the Levenshtein matrix
158
+ // and check if the minimum distance in the last row is still within the
159
+ // maximum edit distance. If it is, we can recurse over all child nodes.
160
+ let i = m;
161
+ for (let pos = 0; pos < key.length; ++pos, ++i) {
162
+ const char = key[pos];
163
+ const thisRowOffset = n * i;
164
+ const prevRowOffset = thisRowOffset - n;
165
+ // Set the first column based on the previous row, and initialize the
166
+ // minimum distance in the current row.
167
+ let minDistance = matrix[thisRowOffset];
168
+ const jmin = Math.max(0, i - maxDistance - 1);
169
+ const jmax = Math.min(n - 1, i + maxDistance);
170
+ // Iterate over remaining columns (characters in the query).
171
+ for (let j = jmin; j < jmax; ++j) {
172
+ const different = char !== query[j];
173
+ // It might make sense to only read the matrix positions used for
174
+ // deletion/insertion if the characters are different. But we want to
175
+ // avoid conditional reads for performance reasons.
176
+ const rpl = matrix[prevRowOffset + j] + +different;
177
+ const del = matrix[prevRowOffset + j + 1] + 1;
178
+ const ins = matrix[thisRowOffset + j] + 1;
179
+ const dist = matrix[thisRowOffset + j + 1] = Math.min(rpl, del, ins);
180
+ if (dist < minDistance)
181
+ minDistance = dist;
182
+ }
183
+ // Because distance will never decrease, we can stop. There will be no
184
+ // matching child nodes.
185
+ if (minDistance > maxDistance) {
186
+ continue key;
187
+ }
188
+ }
189
+ recurse(node.get(key), query, maxDistance, results, matrix, i, n, prefix + key);
190
+ }
191
+ }
192
+ };
193
+
194
+ /* eslint-disable no-labels */
195
+ /**
196
+ * A class implementing the same interface as a standard JavaScript
197
+ * [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
198
+ * with string keys, but adding support for efficiently searching entries with
199
+ * prefix or fuzzy search. This class is used internally by {@link MiniSearch}
200
+ * as the inverted index data structure. The implementation is a radix tree
201
+ * (compressed prefix tree).
202
+ *
203
+ * Since this class can be of general utility beyond _MiniSearch_, it is
204
+ * exported by the `minisearch` package and can be imported (or required) as
205
+ * `minisearch/SearchableMap`.
206
+ *
207
+ * @typeParam T The type of the values stored in the map.
208
+ */
209
+ class SearchableMap {
210
+ /**
211
+ * The constructor is normally called without arguments, creating an empty
212
+ * map. In order to create a {@link SearchableMap} from an iterable or from an
213
+ * object, check {@link SearchableMap.from} and {@link
214
+ * SearchableMap.fromObject}.
215
+ *
216
+ * The constructor arguments are for internal use, when creating derived
217
+ * mutable views of a map at a prefix.
218
+ */
219
+ constructor(tree = new Map(), prefix = '') {
220
+ this._size = undefined;
221
+ this._tree = tree;
222
+ this._prefix = prefix;
223
+ }
224
+ /**
225
+ * Creates and returns a mutable view of this {@link SearchableMap},
226
+ * containing only entries that share the given prefix.
227
+ *
228
+ * ### Usage:
229
+ *
230
+ * ```javascript
231
+ * let map = new SearchableMap()
232
+ * map.set("unicorn", 1)
233
+ * map.set("universe", 2)
234
+ * map.set("university", 3)
235
+ * map.set("unique", 4)
236
+ * map.set("hello", 5)
237
+ *
238
+ * let uni = map.atPrefix("uni")
239
+ * uni.get("unique") // => 4
240
+ * uni.get("unicorn") // => 1
241
+ * uni.get("hello") // => undefined
242
+ *
243
+ * let univer = map.atPrefix("univer")
244
+ * univer.get("unique") // => undefined
245
+ * univer.get("universe") // => 2
246
+ * univer.get("university") // => 3
247
+ * ```
248
+ *
249
+ * @param prefix The prefix
250
+ * @return A {@link SearchableMap} representing a mutable view of the original
251
+ * Map at the given prefix
252
+ */
253
+ atPrefix(prefix) {
254
+ if (!prefix.startsWith(this._prefix)) {
255
+ throw new Error('Mismatched prefix');
256
+ }
257
+ const [node, path] = trackDown(this._tree, prefix.slice(this._prefix.length));
258
+ if (node === undefined) {
259
+ const [parentNode, key] = last(path);
260
+ for (const k of parentNode.keys()) {
261
+ if (k !== LEAF && k.startsWith(key)) {
262
+ const node = new Map();
263
+ node.set(k.slice(key.length), parentNode.get(k));
264
+ return new SearchableMap(node, prefix);
265
+ }
266
+ }
267
+ }
268
+ return new SearchableMap(node, prefix);
269
+ }
270
+ /**
271
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
272
+ */
273
+ clear() {
274
+ this._size = undefined;
275
+ this._tree.clear();
276
+ }
277
+ /**
278
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete
279
+ * @param key Key to delete
280
+ */
281
+ delete(key) {
282
+ this._size = undefined;
283
+ return remove(this._tree, key);
284
+ }
285
+ /**
286
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries
287
+ * @return An iterator iterating through `[key, value]` entries.
288
+ */
289
+ entries() {
290
+ return new TreeIterator(this, ENTRIES);
291
+ }
292
+ /**
293
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach
294
+ * @param fn Iteration function
295
+ */
296
+ forEach(fn) {
297
+ for (const [key, value] of this) {
298
+ fn(key, value, this);
299
+ }
300
+ }
301
+ /**
302
+ * Returns a Map of all the entries that have a key within the given edit
303
+ * distance from the search key. The keys of the returned Map are the matching
304
+ * keys, while the values are two-element arrays where the first element is
305
+ * the value associated to the key, and the second is the edit distance of the
306
+ * key to the search key.
307
+ *
308
+ * ### Usage:
309
+ *
310
+ * ```javascript
311
+ * let map = new SearchableMap()
312
+ * map.set('hello', 'world')
313
+ * map.set('hell', 'yeah')
314
+ * map.set('ciao', 'mondo')
315
+ *
316
+ * // Get all entries that match the key 'hallo' with a maximum edit distance of 2
317
+ * map.fuzzyGet('hallo', 2)
318
+ * // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] }
319
+ *
320
+ * // In the example, the "hello" key has value "world" and edit distance of 1
321
+ * // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2
322
+ * // (change "e" to "a", delete "o")
323
+ * ```
324
+ *
325
+ * @param key The search key
326
+ * @param maxEditDistance The maximum edit distance (Levenshtein)
327
+ * @return A Map of the matching keys to their value and edit distance
328
+ */
329
+ fuzzyGet(key, maxEditDistance) {
330
+ return fuzzySearch(this._tree, key, maxEditDistance);
331
+ }
332
+ /**
333
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
334
+ * @param key Key to get
335
+ * @return Value associated to the key, or `undefined` if the key is not
336
+ * found.
337
+ */
338
+ get(key) {
339
+ const node = lookup(this._tree, key);
340
+ return node !== undefined ? node.get(LEAF) : undefined;
341
+ }
342
+ /**
343
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
344
+ * @param key Key
345
+ * @return True if the key is in the map, false otherwise
346
+ */
347
+ has(key) {
348
+ const node = lookup(this._tree, key);
349
+ return node !== undefined && node.has(LEAF);
350
+ }
351
+ /**
352
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
353
+ * @return An `Iterable` iterating through keys
354
+ */
355
+ keys() {
356
+ return new TreeIterator(this, KEYS);
357
+ }
358
+ /**
359
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set
360
+ * @param key Key to set
361
+ * @param value Value to associate to the key
362
+ * @return The {@link SearchableMap} itself, to allow chaining
363
+ */
364
+ set(key, value) {
365
+ if (typeof key !== 'string') {
366
+ throw new Error('key must be a string');
367
+ }
368
+ this._size = undefined;
369
+ const node = createPath(this._tree, key);
370
+ node.set(LEAF, value);
371
+ return this;
372
+ }
373
+ /**
374
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size
375
+ */
376
+ get size() {
377
+ if (this._size) {
378
+ return this._size;
379
+ }
380
+ /** @ignore */
381
+ this._size = 0;
382
+ const iter = this.entries();
383
+ while (!iter.next().done)
384
+ this._size += 1;
385
+ return this._size;
386
+ }
387
+ /**
388
+ * Updates the value at the given key using the provided function. The function
389
+ * is called with the current value at the key, and its return value is used as
390
+ * the new value to be set.
391
+ *
392
+ * ### Example:
393
+ *
394
+ * ```javascript
395
+ * // Increment the current value by one
396
+ * searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1)
397
+ * ```
398
+ *
399
+ * If the value at the given key is or will be an object, it might not require
400
+ * re-assignment. In that case it is better to use `fetch()`, because it is
401
+ * faster.
402
+ *
403
+ * @param key The key to update
404
+ * @param fn The function used to compute the new value from the current one
405
+ * @return The {@link SearchableMap} itself, to allow chaining
406
+ */
407
+ update(key, fn) {
408
+ if (typeof key !== 'string') {
409
+ throw new Error('key must be a string');
410
+ }
411
+ this._size = undefined;
412
+ const node = createPath(this._tree, key);
413
+ node.set(LEAF, fn(node.get(LEAF)));
414
+ return this;
415
+ }
416
+ /**
417
+ * Fetches the value of the given key. If the value does not exist, calls the
418
+ * given function to create a new value, which is inserted at the given key
419
+ * and subsequently returned.
420
+ *
421
+ * ### Example:
422
+ *
423
+ * ```javascript
424
+ * const map = searchableMap.fetch('somekey', () => new Map())
425
+ * map.set('foo', 'bar')
426
+ * ```
427
+ *
428
+ * @param key The key to update
429
+ * @param initial A function that creates a new value if the key does not exist
430
+ * @return The existing or new value at the given key
431
+ */
432
+ fetch(key, initial) {
433
+ if (typeof key !== 'string') {
434
+ throw new Error('key must be a string');
435
+ }
436
+ this._size = undefined;
437
+ const node = createPath(this._tree, key);
438
+ let value = node.get(LEAF);
439
+ if (value === undefined) {
440
+ node.set(LEAF, value = initial());
441
+ }
442
+ return value;
443
+ }
444
+ /**
445
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
446
+ * @return An `Iterable` iterating through values.
447
+ */
448
+ values() {
449
+ return new TreeIterator(this, VALUES);
450
+ }
451
+ /**
452
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator
453
+ */
454
+ [Symbol.iterator]() {
455
+ return this.entries();
456
+ }
457
+ /**
458
+ * Creates a {@link SearchableMap} from an `Iterable` of entries
459
+ *
460
+ * @param entries Entries to be inserted in the {@link SearchableMap}
461
+ * @return A new {@link SearchableMap} with the given entries
462
+ */
463
+ static from(entries) {
464
+ const tree = new SearchableMap();
465
+ for (const [key, value] of entries) {
466
+ tree.set(key, value);
467
+ }
468
+ return tree;
469
+ }
470
+ /**
471
+ * Creates a {@link SearchableMap} from the iterable properties of a JavaScript object
472
+ *
473
+ * @param object Object of entries for the {@link SearchableMap}
474
+ * @return A new {@link SearchableMap} with the given entries
475
+ */
476
+ static fromObject(object) {
477
+ return SearchableMap.from(Object.entries(object));
478
+ }
479
+ }
480
+ const trackDown = (tree, key, path = []) => {
481
+ if (key.length === 0 || tree == null) {
482
+ return [tree, path];
483
+ }
484
+ for (const k of tree.keys()) {
485
+ if (k !== LEAF && key.startsWith(k)) {
486
+ path.push([tree, k]); // performance: update in place
487
+ return trackDown(tree.get(k), key.slice(k.length), path);
488
+ }
489
+ }
490
+ path.push([tree, key]); // performance: update in place
491
+ return trackDown(undefined, '', path);
492
+ };
493
+ const lookup = (tree, key) => {
494
+ if (key.length === 0 || tree == null) {
495
+ return tree;
496
+ }
497
+ for (const k of tree.keys()) {
498
+ if (k !== LEAF && key.startsWith(k)) {
499
+ return lookup(tree.get(k), key.slice(k.length));
500
+ }
501
+ }
502
+ };
503
+ // Create a path in the radix tree for the given key, and returns the deepest
504
+ // node. This function is in the hot path for indexing. It avoids unnecessary
505
+ // string operations and recursion for performance.
506
+ const createPath = (node, key) => {
507
+ const keyLength = key.length;
508
+ outer: for (let pos = 0; node && pos < keyLength;) {
509
+ for (const k of node.keys()) {
510
+ // Check whether this key is a candidate: the first characters must match.
511
+ if (k !== LEAF && key[pos] === k[0]) {
512
+ const len = Math.min(keyLength - pos, k.length);
513
+ // Advance offset to the point where key and k no longer match.
514
+ let offset = 1;
515
+ while (offset < len && key[pos + offset] === k[offset])
516
+ ++offset;
517
+ const child = node.get(k);
518
+ if (offset === k.length) {
519
+ // The existing key is shorter than the key we need to create.
520
+ node = child;
521
+ }
522
+ else {
523
+ // Partial match: we need to insert an intermediate node to contain
524
+ // both the existing subtree and the new node.
525
+ const intermediate = new Map();
526
+ intermediate.set(k.slice(offset), child);
527
+ node.set(key.slice(pos, pos + offset), intermediate);
528
+ node.delete(k);
529
+ node = intermediate;
530
+ }
531
+ pos += offset;
532
+ continue outer;
533
+ }
534
+ }
535
+ // Create a final child node to contain the final suffix of the key.
536
+ const child = new Map();
537
+ node.set(key.slice(pos), child);
538
+ return child;
539
+ }
540
+ return node;
541
+ };
542
+ const remove = (tree, key) => {
543
+ const [node, path] = trackDown(tree, key);
544
+ if (node === undefined) {
545
+ return;
546
+ }
547
+ node.delete(LEAF);
548
+ if (node.size === 0) {
549
+ cleanup(path);
550
+ }
551
+ else if (node.size === 1) {
552
+ const [key, value] = node.entries().next().value;
553
+ merge(path, key, value);
554
+ }
555
+ };
556
+ const cleanup = (path) => {
557
+ if (path.length === 0) {
558
+ return;
559
+ }
560
+ const [node, key] = last(path);
561
+ node.delete(key);
562
+ if (node.size === 0) {
563
+ cleanup(path.slice(0, -1));
564
+ }
565
+ else if (node.size === 1) {
566
+ const [key, value] = node.entries().next().value;
567
+ if (key !== LEAF) {
568
+ merge(path.slice(0, -1), key, value);
569
+ }
570
+ }
571
+ };
572
+ const merge = (path, key, value) => {
573
+ if (path.length === 0) {
574
+ return;
575
+ }
576
+ const [node, nodeKey] = last(path);
577
+ node.set(nodeKey + key, value);
578
+ node.delete(nodeKey);
579
+ };
580
+ const last = (array) => {
581
+ return array[array.length - 1];
582
+ };
583
+
584
+ const OR = 'or';
585
+ const AND = 'and';
586
+ const AND_NOT = 'and_not';
587
+ /**
588
+ * {@link MiniSearch} is the main entrypoint class, implementing a full-text
589
+ * search engine in memory.
590
+ *
591
+ * @typeParam T The type of the documents being indexed.
592
+ *
593
+ * ### Basic example:
594
+ *
595
+ * ```javascript
596
+ * const documents = [
597
+ * {
598
+ * id: 1,
599
+ * title: 'Moby Dick',
600
+ * text: 'Call me Ishmael. Some years ago...',
601
+ * category: 'fiction'
602
+ * },
603
+ * {
604
+ * id: 2,
605
+ * title: 'Zen and the Art of Motorcycle Maintenance',
606
+ * text: 'I can see by my watch...',
607
+ * category: 'fiction'
608
+ * },
609
+ * {
610
+ * id: 3,
611
+ * title: 'Neuromancer',
612
+ * text: 'The sky above the port was...',
613
+ * category: 'fiction'
614
+ * },
615
+ * {
616
+ * id: 4,
617
+ * title: 'Zen and the Art of Archery',
618
+ * text: 'At first sight it must seem...',
619
+ * category: 'non-fiction'
620
+ * },
621
+ * // ...and more
622
+ * ]
623
+ *
624
+ * // Create a search engine that indexes the 'title' and 'text' fields for
625
+ * // full-text search. Search results will include 'title' and 'category' (plus the
626
+ * // id field, that is always stored and returned)
627
+ * const miniSearch = new MiniSearch({
628
+ * fields: ['title', 'text'],
629
+ * storeFields: ['title', 'category']
630
+ * })
631
+ *
632
+ * // Add documents to the index
633
+ * miniSearch.addAll(documents)
634
+ *
635
+ * // Search for documents:
636
+ * let results = miniSearch.search('zen art motorcycle')
637
+ * // => [
638
+ * // { id: 2, title: 'Zen and the Art of Motorcycle Maintenance', category: 'fiction', score: 2.77258 },
639
+ * // { id: 4, title: 'Zen and the Art of Archery', category: 'non-fiction', score: 1.38629 }
640
+ * // ]
641
+ * ```
642
+ */
643
+ class MiniSearch {
644
+ /**
645
+ * @param options Configuration options
646
+ *
647
+ * ### Examples:
648
+ *
649
+ * ```javascript
650
+ * // Create a search engine that indexes the 'title' and 'text' fields of your
651
+ * // documents:
652
+ * const miniSearch = new MiniSearch({ fields: ['title', 'text'] })
653
+ * ```
654
+ *
655
+ * ### ID Field:
656
+ *
657
+ * ```javascript
658
+ * // Your documents are assumed to include a unique 'id' field, but if you want
659
+ * // to use a different field for document identification, you can set the
660
+ * // 'idField' option:
661
+ * const miniSearch = new MiniSearch({ idField: 'key', fields: ['title', 'text'] })
662
+ * ```
663
+ *
664
+ * ### Options and defaults:
665
+ *
666
+ * ```javascript
667
+ * // The full set of options (here with their default value) is:
668
+ * const miniSearch = new MiniSearch({
669
+ * // idField: field that uniquely identifies a document
670
+ * idField: 'id',
671
+ *
672
+ * // extractField: function used to get the value of a field in a document.
673
+ * // By default, it assumes the document is a flat object with field names as
674
+ * // property keys and field values as string property values, but custom logic
675
+ * // can be implemented by setting this option to a custom extractor function.
676
+ * extractField: (document, fieldName) => document[fieldName],
677
+ *
678
+ * // tokenize: function used to split fields into individual terms. By
679
+ * // default, it is also used to tokenize search queries, unless a specific
680
+ * // `tokenize` search option is supplied. When tokenizing an indexed field,
681
+ * // the field name is passed as the second argument.
682
+ * tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),
683
+ *
684
+ * // processTerm: function used to process each tokenized term before
685
+ * // indexing. It can be used for stemming and normalization. Return a falsy
686
+ * // value in order to discard a term. By default, it is also used to process
687
+ * // search queries, unless a specific `processTerm` option is supplied as a
688
+ * // search option. When processing a term from a indexed field, the field
689
+ * // name is passed as the second argument.
690
+ * processTerm: (term, _fieldName) => term.toLowerCase(),
691
+ *
692
+ * // searchOptions: default search options, see the `search` method for
693
+ * // details
694
+ * searchOptions: undefined,
695
+ *
696
+ * // fields: document fields to be indexed. Mandatory, but not set by default
697
+ * fields: undefined
698
+ *
699
+ * // storeFields: document fields to be stored and returned as part of the
700
+ * // search results.
701
+ * storeFields: []
702
+ * })
703
+ * ```
704
+ */
705
+ constructor(options) {
706
+ if ((options === null || options === void 0 ? void 0 : options.fields) == null) {
707
+ throw new Error('MiniSearch: option "fields" must be provided');
708
+ }
709
+ const autoVacuum = (options.autoVacuum == null || options.autoVacuum === true) ? defaultAutoVacuumOptions : options.autoVacuum;
710
+ this._options = Object.assign(Object.assign(Object.assign({}, defaultOptions), options), { autoVacuum, searchOptions: Object.assign(Object.assign({}, defaultSearchOptions), (options.searchOptions || {})), autoSuggestOptions: Object.assign(Object.assign({}, defaultAutoSuggestOptions), (options.autoSuggestOptions || {})) });
711
+ this._index = new SearchableMap();
712
+ this._documentCount = 0;
713
+ this._documentIds = new Map();
714
+ this._idToShortId = new Map();
715
+ // Fields are defined during initialization, don't change, are few in
716
+ // number, rarely need iterating over, and have string keys. Therefore in
717
+ // this case an object is a better candidate than a Map to store the mapping
718
+ // from field key to ID.
719
+ this._fieldIds = {};
720
+ this._fieldLength = new Map();
721
+ this._avgFieldLength = [];
722
+ this._nextId = 0;
723
+ this._storedFields = new Map();
724
+ this._dirtCount = 0;
725
+ this._currentVacuum = null;
726
+ this._enqueuedVacuum = null;
727
+ this._enqueuedVacuumConditions = defaultVacuumConditions;
728
+ this.addFields(this._options.fields);
729
+ }
730
+ /**
731
+ * Adds a document to the index
732
+ *
733
+ * @param document The document to be indexed
734
+ */
735
+ add(document) {
736
+ const { extractField, tokenize, processTerm, fields, idField } = this._options;
737
+ const id = extractField(document, idField);
738
+ if (id == null) {
739
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
740
+ }
741
+ if (this._idToShortId.has(id)) {
742
+ throw new Error(`MiniSearch: duplicate ID ${id}`);
743
+ }
744
+ const shortDocumentId = this.addDocumentId(id);
745
+ this.saveStoredFields(shortDocumentId, document);
746
+ for (const field of fields) {
747
+ const fieldValue = extractField(document, field);
748
+ if (fieldValue == null)
749
+ continue;
750
+ const tokens = tokenize(fieldValue.toString(), field);
751
+ const fieldId = this._fieldIds[field];
752
+ const uniqueTerms = new Set(tokens).size;
753
+ this.addFieldLength(shortDocumentId, fieldId, this._documentCount - 1, uniqueTerms);
754
+ for (const term of tokens) {
755
+ const processedTerm = processTerm(term, field);
756
+ if (Array.isArray(processedTerm)) {
757
+ for (const t of processedTerm) {
758
+ this.addTerm(fieldId, shortDocumentId, t);
759
+ }
760
+ }
761
+ else if (processedTerm) {
762
+ this.addTerm(fieldId, shortDocumentId, processedTerm);
763
+ }
764
+ }
765
+ }
766
+ }
767
+ /**
768
+ * Adds all the given documents to the index
769
+ *
770
+ * @param documents An array of documents to be indexed
771
+ */
772
+ addAll(documents) {
773
+ for (const document of documents)
774
+ this.add(document);
775
+ }
776
+ /**
777
+ * Adds all the given documents to the index asynchronously.
778
+ *
779
+ * Returns a promise that resolves (to `undefined`) when the indexing is done.
780
+ * This method is useful when index many documents, to avoid blocking the main
781
+ * thread. The indexing is performed asynchronously and in chunks.
782
+ *
783
+ * @param documents An array of documents to be indexed
784
+ * @param options Configuration options
785
+ * @return A promise resolving to `undefined` when the indexing is done
786
+ */
787
+ addAllAsync(documents, options = {}) {
788
+ const { chunkSize = 10 } = options;
789
+ const acc = { chunk: [], promise: Promise.resolve() };
790
+ const { chunk, promise } = documents.reduce(({ chunk, promise }, document, i) => {
791
+ chunk.push(document);
792
+ if ((i + 1) % chunkSize === 0) {
793
+ return {
794
+ chunk: [],
795
+ promise: promise
796
+ .then(() => new Promise(resolve => setTimeout(resolve, 0)))
797
+ .then(() => this.addAll(chunk))
798
+ };
799
+ }
800
+ else {
801
+ return { chunk, promise };
802
+ }
803
+ }, acc);
804
+ return promise.then(() => this.addAll(chunk));
805
+ }
806
+ /**
807
+ * Removes the given document from the index.
808
+ *
809
+ * The document to remove must NOT have changed between indexing and removal,
810
+ * otherwise the index will be corrupted.
811
+ *
812
+ * This method requires passing the full document to be removed (not just the
813
+ * ID), and immediately removes the document from the inverted index, allowing
814
+ * memory to be released. A convenient alternative is {@link
815
+ * MiniSearch#discard}, which needs only the document ID, and has the same
816
+ * visible effect, but delays cleaning up the index until the next vacuuming.
817
+ *
818
+ * @param document The document to be removed
819
+ */
820
+ remove(document) {
821
+ const { tokenize, processTerm, extractField, fields, idField } = this._options;
822
+ const id = extractField(document, idField);
823
+ if (id == null) {
824
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
825
+ }
826
+ const shortId = this._idToShortId.get(id);
827
+ if (shortId == null) {
828
+ throw new Error(`MiniSearch: cannot remove document with ID ${id}: it is not in the index`);
829
+ }
830
+ for (const field of fields) {
831
+ const fieldValue = extractField(document, field);
832
+ if (fieldValue == null)
833
+ continue;
834
+ const tokens = tokenize(fieldValue.toString(), field);
835
+ const fieldId = this._fieldIds[field];
836
+ const uniqueTerms = new Set(tokens).size;
837
+ this.removeFieldLength(shortId, fieldId, this._documentCount, uniqueTerms);
838
+ for (const term of tokens) {
839
+ const processedTerm = processTerm(term, field);
840
+ if (Array.isArray(processedTerm)) {
841
+ for (const t of processedTerm) {
842
+ this.removeTerm(fieldId, shortId, t);
843
+ }
844
+ }
845
+ else if (processedTerm) {
846
+ this.removeTerm(fieldId, shortId, processedTerm);
847
+ }
848
+ }
849
+ }
850
+ this._storedFields.delete(shortId);
851
+ this._documentIds.delete(shortId);
852
+ this._idToShortId.delete(id);
853
+ this._fieldLength.delete(shortId);
854
+ this._documentCount -= 1;
855
+ }
856
+ /**
857
+ * Removes all the given documents from the index. If called with no arguments,
858
+ * it removes _all_ documents from the index.
859
+ *
860
+ * @param documents The documents to be removed. If this argument is omitted,
861
+ * all documents are removed. Note that, for removing all documents, it is
862
+ * more efficient to call this method with no arguments than to pass all
863
+ * documents.
864
+ */
865
+ removeAll(documents) {
866
+ if (documents) {
867
+ for (const document of documents)
868
+ this.remove(document);
869
+ }
870
+ else if (arguments.length > 0) {
871
+ throw new Error('Expected documents to be present. Omit the argument to remove all documents.');
872
+ }
873
+ else {
874
+ this._index = new SearchableMap();
875
+ this._documentCount = 0;
876
+ this._documentIds = new Map();
877
+ this._idToShortId = new Map();
878
+ this._fieldLength = new Map();
879
+ this._avgFieldLength = [];
880
+ this._storedFields = new Map();
881
+ this._nextId = 0;
882
+ }
883
+ }
884
+ /**
885
+ * Discards the document with the given ID, so it won't appear in search results
886
+ *
887
+ * It has the same visible effect of {@link MiniSearch.remove} (both cause the
888
+ * document to stop appearing in searches), but a different effect on the
889
+ * internal data structures:
890
+ *
891
+ * - {@link MiniSearch#remove} requires passing the full document to be
892
+ * removed as argument, and removes it from the inverted index immediately.
893
+ *
894
+ * - {@link MiniSearch#discard} instead only needs the document ID, and
895
+ * works by marking the current version of the document as discarded, so it
896
+ * is immediately ignored by searches. This is faster and more convenient
897
+ * than {@link MiniSearch#remove}, but the index is not immediately
898
+ * modified. To take care of that, vacuuming is performed after a certain
899
+ * number of documents are discarded, cleaning up the index and allowing
900
+ * memory to be released.
901
+ *
902
+ * After discarding a document, it is possible to re-add a new version, and
903
+ * only the new version will appear in searches. In other words, discarding
904
+ * and re-adding a document works exactly like removing and re-adding it. The
905
+ * {@link MiniSearch.replace} method can also be used to replace a document
906
+ * with a new version.
907
+ *
908
+ * #### Details about vacuuming
909
+ *
910
+ * Repetite calls to this method would leave obsolete document references in
911
+ * the index, invisible to searches. Two mechanisms take care of cleaning up:
912
+ * clean up during search, and vacuuming.
913
+ *
914
+ * - Upon search, whenever a discarded ID is found (and ignored for the
915
+ * results), references to the discarded document are removed from the
916
+ * inverted index entries for the search terms. This ensures that subsequent
917
+ * searches for the same terms do not need to skip these obsolete references
918
+ * again.
919
+ *
920
+ * - In addition, vacuuming is performed automatically by default (see the
921
+ * `autoVacuum` field in {@link Options}) after a certain number of
922
+ * documents are discarded. Vacuuming traverses all terms in the index,
923
+ * cleaning up all references to discarded documents. Vacuuming can also be
924
+ * triggered manually by calling {@link MiniSearch#vacuum}.
925
+ *
926
+ * @param id The ID of the document to be discarded
927
+ */
928
+ discard(id) {
929
+ const shortId = this._idToShortId.get(id);
930
+ if (shortId == null) {
931
+ throw new Error(`MiniSearch: cannot discard document with ID ${id}: it is not in the index`);
932
+ }
933
+ this._idToShortId.delete(id);
934
+ this._documentIds.delete(shortId);
935
+ this._storedFields.delete(shortId);
936
+ (this._fieldLength.get(shortId) || []).forEach((fieldLength, fieldId) => {
937
+ this.removeFieldLength(shortId, fieldId, this._documentCount, fieldLength);
938
+ });
939
+ this._fieldLength.delete(shortId);
940
+ this._documentCount -= 1;
941
+ this._dirtCount += 1;
942
+ this.maybeAutoVacuum();
943
+ }
944
+ maybeAutoVacuum() {
945
+ if (this._options.autoVacuum === false) {
946
+ return;
947
+ }
948
+ const { minDirtFactor, minDirtCount, batchSize, batchWait } = this._options.autoVacuum;
949
+ this.conditionalVacuum({ batchSize, batchWait }, { minDirtCount, minDirtFactor });
950
+ }
951
+ /**
952
+ * Discards the documents with the given IDs, so they won't appear in search
953
+ * results
954
+ *
955
+ * It is equivalent to calling {@link MiniSearch#discard} for all the given
956
+ * IDs, but with the optimization of triggering at most one automatic
957
+ * vacuuming at the end.
958
+ *
959
+ * Note: to remove all documents from the index, it is faster and more
960
+ * convenient to call {@link MiniSearch.removeAll} with no argument, instead
961
+ * of passing all IDs to this method.
962
+ */
963
+ discardAll(ids) {
964
+ const autoVacuum = this._options.autoVacuum;
965
+ try {
966
+ this._options.autoVacuum = false;
967
+ for (const id of ids) {
968
+ this.discard(id);
969
+ }
970
+ }
971
+ finally {
972
+ this._options.autoVacuum = autoVacuum;
973
+ }
974
+ this.maybeAutoVacuum();
975
+ }
976
+ /**
977
+ * It replaces an existing document with the given updated version
978
+ *
979
+ * It works by discarding the current version and adding the updated one, so
980
+ * it is functionally equivalent to calling {@link MiniSearch#discard}
981
+ * followed by {@link MiniSearch#add}. The ID of the updated document should
982
+ * be the same as the original one.
983
+ *
984
+ * Since it uses {@link MiniSearch#discard} internally, this method relies on
985
+ * vacuuming to clean up obsolete document references from the index, allowing
986
+ * memory to be released (see {@link MiniSearch#discard}).
987
+ *
988
+ * @param updatedDocument The updated document to replace the old version
989
+ * with
990
+ */
991
+ replace(updatedDocument) {
992
+ const { idField, extractField } = this._options;
993
+ const id = extractField(updatedDocument, idField);
994
+ this.discard(id);
995
+ this.add(updatedDocument);
996
+ }
997
+ /**
998
+ * Triggers a manual vacuuming, cleaning up references to discarded documents
999
+ * from the inverted index
1000
+ *
1001
+ * Vacuuming is only useful for applications that use the {@link
1002
+ * MiniSearch#discard} or {@link MiniSearch#replace} methods.
1003
+ *
1004
+ * By default, vacuuming is performed automatically when needed (controlled by
1005
+ * the `autoVacuum` field in {@link Options}), so there is usually no need to
1006
+ * call this method, unless one wants to make sure to perform vacuuming at a
1007
+ * specific moment.
1008
+ *
1009
+ * Vacuuming traverses all terms in the inverted index in batches, and cleans
1010
+ * up references to discarded documents from the posting list, allowing memory
1011
+ * to be released.
1012
+ *
1013
+ * The method takes an optional object as argument with the following keys:
1014
+ *
1015
+ * - `batchSize`: the size of each batch (1000 by default)
1016
+ *
1017
+ * - `batchWait`: the number of milliseconds to wait between batches (10 by
1018
+ * default)
1019
+ *
1020
+ * On large indexes, vacuuming could have a non-negligible cost: batching
1021
+ * avoids blocking the thread for long, diluting this cost so that it is not
1022
+ * negatively affecting the application. Nonetheless, this method should only
1023
+ * be called when necessary, and relying on automatic vacuuming is usually
1024
+ * better.
1025
+ *
1026
+ * It returns a promise that resolves (to undefined) when the clean up is
1027
+ * completed. If vacuuming is already ongoing at the time this method is
1028
+ * called, a new one is enqueued immediately after the ongoing one, and a
1029
+ * corresponding promise is returned. However, no more than one vacuuming is
1030
+ * enqueued on top of the ongoing one, even if this method is called more
1031
+ * times (enqueuing multiple ones would be useless).
1032
+ *
1033
+ * @param options Configuration options for the batch size and delay. See
1034
+ * {@link VacuumOptions}.
1035
+ */
1036
+ vacuum(options = {}) {
1037
+ return this.conditionalVacuum(options);
1038
+ }
1039
+ conditionalVacuum(options, conditions) {
1040
+ // If a vacuum is already ongoing, schedule another as soon as it finishes,
1041
+ // unless there's already one enqueued. If one was already enqueued, do not
1042
+ // enqueue another on top, but make sure that the conditions are the
1043
+ // broadest.
1044
+ if (this._currentVacuum) {
1045
+ this._enqueuedVacuumConditions = this._enqueuedVacuumConditions && conditions;
1046
+ if (this._enqueuedVacuum != null) {
1047
+ return this._enqueuedVacuum;
1048
+ }
1049
+ this._enqueuedVacuum = this._currentVacuum.then(() => {
1050
+ const conditions = this._enqueuedVacuumConditions;
1051
+ this._enqueuedVacuumConditions = defaultVacuumConditions;
1052
+ return this.performVacuuming(options, conditions);
1053
+ });
1054
+ return this._enqueuedVacuum;
1055
+ }
1056
+ if (this.vacuumConditionsMet(conditions) === false) {
1057
+ return Promise.resolve();
1058
+ }
1059
+ this._currentVacuum = this.performVacuuming(options);
1060
+ return this._currentVacuum;
1061
+ }
1062
+ performVacuuming(options, conditions) {
1063
+ return __awaiter(this, void 0, void 0, function* () {
1064
+ const initialDirtCount = this._dirtCount;
1065
+ if (this.vacuumConditionsMet(conditions)) {
1066
+ const batchSize = options.batchSize || defaultVacuumOptions.batchSize;
1067
+ const batchWait = options.batchWait || defaultVacuumOptions.batchWait;
1068
+ let i = 1;
1069
+ for (const [term, fieldsData] of this._index) {
1070
+ for (const [fieldId, fieldIndex] of fieldsData) {
1071
+ for (const [shortId] of fieldIndex) {
1072
+ if (this._documentIds.has(shortId)) {
1073
+ continue;
1074
+ }
1075
+ if (fieldIndex.size <= 1) {
1076
+ fieldsData.delete(fieldId);
1077
+ }
1078
+ else {
1079
+ fieldIndex.delete(shortId);
1080
+ }
1081
+ }
1082
+ }
1083
+ if (this._index.get(term).size === 0) {
1084
+ this._index.delete(term);
1085
+ }
1086
+ if (i % batchSize === 0) {
1087
+ yield new Promise((resolve) => setTimeout(resolve, batchWait));
1088
+ }
1089
+ i += 1;
1090
+ }
1091
+ this._dirtCount -= initialDirtCount;
1092
+ }
1093
+ // Make the next lines always async, so they execute after this function returns
1094
+ yield null;
1095
+ this._currentVacuum = this._enqueuedVacuum;
1096
+ this._enqueuedVacuum = null;
1097
+ });
1098
+ }
1099
+ vacuumConditionsMet(conditions) {
1100
+ if (conditions == null) {
1101
+ return true;
1102
+ }
1103
+ let { minDirtCount, minDirtFactor } = conditions;
1104
+ minDirtCount = minDirtCount || defaultAutoVacuumOptions.minDirtCount;
1105
+ minDirtFactor = minDirtFactor || defaultAutoVacuumOptions.minDirtFactor;
1106
+ return this.dirtCount >= minDirtCount && this.dirtFactor >= minDirtFactor;
1107
+ }
1108
+ /**
1109
+ * Is `true` if a vacuuming operation is ongoing, `false` otherwise
1110
+ */
1111
+ get isVacuuming() {
1112
+ return this._currentVacuum != null;
1113
+ }
1114
+ /**
1115
+ * The number of documents discarded since the most recent vacuuming
1116
+ */
1117
+ get dirtCount() {
1118
+ return this._dirtCount;
1119
+ }
1120
+ /**
1121
+ * A number between 0 and 1 giving an indication about the proportion of
1122
+ * documents that are discarded, and can therefore be cleaned up by vacuuming.
1123
+ * A value close to 0 means that the index is relatively clean, while a higher
1124
+ * value means that the index is relatively dirty, and vacuuming could release
1125
+ * memory.
1126
+ */
1127
+ get dirtFactor() {
1128
+ return this._dirtCount / (1 + this._documentCount + this._dirtCount);
1129
+ }
1130
+ /**
1131
+ * Returns `true` if a document with the given ID is present in the index and
1132
+ * available for search, `false` otherwise
1133
+ *
1134
+ * @param id The document ID
1135
+ */
1136
+ has(id) {
1137
+ return this._idToShortId.has(id);
1138
+ }
1139
+ /**
1140
+ * Returns the stored fields (as configured in the `storeFields` constructor
1141
+ * option) for the given document ID. Returns `undefined` if the document is
1142
+ * not present in the index.
1143
+ *
1144
+ * @param id The document ID
1145
+ */
1146
+ getStoredFields(id) {
1147
+ const shortId = this._idToShortId.get(id);
1148
+ if (shortId == null) {
1149
+ return undefined;
1150
+ }
1151
+ return this._storedFields.get(shortId);
1152
+ }
1153
+ /**
1154
+ * Search for documents matching the given search query.
1155
+ *
1156
+ * The result is a list of scored document IDs matching the query, sorted by
1157
+ * descending score, and each including data about which terms were matched and
1158
+ * in which fields.
1159
+ *
1160
+ * ### Basic usage:
1161
+ *
1162
+ * ```javascript
1163
+ * // Search for "zen art motorcycle" with default options: terms have to match
1164
+ * // exactly, and individual terms are joined with OR
1165
+ * miniSearch.search('zen art motorcycle')
1166
+ * // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
1167
+ * ```
1168
+ *
1169
+ * ### Restrict search to specific fields:
1170
+ *
1171
+ * ```javascript
1172
+ * // Search only in the 'title' field
1173
+ * miniSearch.search('zen', { fields: ['title'] })
1174
+ * ```
1175
+ *
1176
+ * ### Field boosting:
1177
+ *
1178
+ * ```javascript
1179
+ * // Boost a field
1180
+ * miniSearch.search('zen', { boost: { title: 2 } })
1181
+ * ```
1182
+ *
1183
+ * ### Prefix search:
1184
+ *
1185
+ * ```javascript
1186
+ * // Search for "moto" with prefix search (it will match documents
1187
+ * // containing terms that start with "moto" or "neuro")
1188
+ * miniSearch.search('moto neuro', { prefix: true })
1189
+ * ```
1190
+ *
1191
+ * ### Fuzzy search:
1192
+ *
1193
+ * ```javascript
1194
+ * // Search for "ismael" with fuzzy search (it will match documents containing
1195
+ * // terms similar to "ismael", with a maximum edit distance of 0.2 term.length
1196
+ * // (rounded to nearest integer)
1197
+ * miniSearch.search('ismael', { fuzzy: 0.2 })
1198
+ * ```
1199
+ *
1200
+ * ### Combining strategies:
1201
+ *
1202
+ * ```javascript
1203
+ * // Mix of exact match, prefix search, and fuzzy search
1204
+ * miniSearch.search('ismael mob', {
1205
+ * prefix: true,
1206
+ * fuzzy: 0.2
1207
+ * })
1208
+ * ```
1209
+ *
1210
+ * ### Advanced prefix and fuzzy search:
1211
+ *
1212
+ * ```javascript
1213
+ * // Perform fuzzy and prefix search depending on the search term. Here
1214
+ * // performing prefix and fuzzy search only on terms longer than 3 characters
1215
+ * miniSearch.search('ismael mob', {
1216
+ * prefix: term => term.length > 3
1217
+ * fuzzy: term => term.length > 3 ? 0.2 : null
1218
+ * })
1219
+ * ```
1220
+ *
1221
+ * ### Combine with AND:
1222
+ *
1223
+ * ```javascript
1224
+ * // Combine search terms with AND (to match only documents that contain both
1225
+ * // "motorcycle" and "art")
1226
+ * miniSearch.search('motorcycle art', { combineWith: 'AND' })
1227
+ * ```
1228
+ *
1229
+ * ### Combine with AND_NOT:
1230
+ *
1231
+ * There is also an AND_NOT combinator, that finds documents that match the
1232
+ * first term, but do not match any of the other terms. This combinator is
1233
+ * rarely useful with simple queries, and is meant to be used with advanced
1234
+ * query combinations (see later for more details).
1235
+ *
1236
+ * ### Filtering results:
1237
+ *
1238
+ * ```javascript
1239
+ * // Filter only results in the 'fiction' category (assuming that 'category'
1240
+ * // is a stored field)
1241
+ * miniSearch.search('motorcycle art', {
1242
+ * filter: (result) => result.category === 'fiction'
1243
+ * })
1244
+ * ```
1245
+ *
1246
+ * ### Wildcard query
1247
+ *
1248
+ * Searching for an empty string (assuming the default tokenizer) returns no
1249
+ * results. Sometimes though, one needs to match all documents, like in a
1250
+ * "wildcard" search. This is possible by passing the special value
1251
+ * {@link MiniSearch.wildcard} as the query:
1252
+ *
1253
+ * ```javascript
1254
+ * // Return search results for all documents
1255
+ * miniSearch.search(MiniSearch.wildcard)
1256
+ * ```
1257
+ *
1258
+ * Note that search options such as `filter` and `boostDocument` are still
1259
+ * applied, influencing which results are returned, and their order:
1260
+ *
1261
+ * ```javascript
1262
+ * // Return search results for all documents in the 'fiction' category
1263
+ * miniSearch.search(MiniSearch.wildcard, {
1264
+ * filter: (result) => result.category === 'fiction'
1265
+ * })
1266
+ * ```
1267
+ *
1268
+ * ### Advanced combination of queries:
1269
+ *
1270
+ * It is possible to combine different subqueries with OR, AND, and AND_NOT,
1271
+ * and even with different search options, by passing a query expression
1272
+ * tree object as the first argument, instead of a string.
1273
+ *
1274
+ * ```javascript
1275
+ * // Search for documents that contain "zen" and ("motorcycle" or "archery")
1276
+ * miniSearch.search({
1277
+ * combineWith: 'AND',
1278
+ * queries: [
1279
+ * 'zen',
1280
+ * {
1281
+ * combineWith: 'OR',
1282
+ * queries: ['motorcycle', 'archery']
1283
+ * }
1284
+ * ]
1285
+ * })
1286
+ *
1287
+ * // Search for documents that contain ("apple" or "pear") but not "juice" and
1288
+ * // not "tree"
1289
+ * miniSearch.search({
1290
+ * combineWith: 'AND_NOT',
1291
+ * queries: [
1292
+ * {
1293
+ * combineWith: 'OR',
1294
+ * queries: ['apple', 'pear']
1295
+ * },
1296
+ * 'juice',
1297
+ * 'tree'
1298
+ * ]
1299
+ * })
1300
+ * ```
1301
+ *
1302
+ * Each node in the expression tree can be either a string, or an object that
1303
+ * supports all {@link SearchOptions} fields, plus a `queries` array field for
1304
+ * subqueries.
1305
+ *
1306
+ * Note that, while this can become complicated to do by hand for complex or
1307
+ * deeply nested queries, it provides a formalized expression tree API for
1308
+ * external libraries that implement a parser for custom query languages.
1309
+ *
1310
+ * @param query Search query
1311
+ * @param searchOptions Search options. Each option, if not given, defaults to the corresponding value of `searchOptions` given to the constructor, or to the library default.
1312
+ */
1313
+ search(query, searchOptions = {}) {
1314
+ const { searchOptions: globalSearchOptions } = this._options;
1315
+ const searchOptionsWithDefaults = Object.assign(Object.assign({}, globalSearchOptions), searchOptions);
1316
+ const rawResults = this.executeQuery(query, searchOptions);
1317
+ const results = [];
1318
+ for (const [docId, { score, terms, match }] of rawResults) {
1319
+ // terms are the matched query terms, which will be returned to the user
1320
+ // as queryTerms. The quality is calculated based on them, as opposed to
1321
+ // the matched terms in the document (which can be different due to
1322
+ // prefix and fuzzy match)
1323
+ const quality = terms.length || 1;
1324
+ const result = {
1325
+ id: this._documentIds.get(docId),
1326
+ score: score * quality,
1327
+ terms: Object.keys(match),
1328
+ queryTerms: terms,
1329
+ match
1330
+ };
1331
+ Object.assign(result, this._storedFields.get(docId));
1332
+ if (searchOptionsWithDefaults.filter == null || searchOptionsWithDefaults.filter(result)) {
1333
+ results.push(result);
1334
+ }
1335
+ }
1336
+ // If it's a wildcard query, and no document boost is applied, skip sorting
1337
+ // the results, as all results have the same score of 1
1338
+ if (query === MiniSearch.wildcard && searchOptionsWithDefaults.boostDocument == null) {
1339
+ return results;
1340
+ }
1341
+ results.sort(byScore);
1342
+ return results;
1343
+ }
1344
+ /**
1345
+ * Provide suggestions for the given search query
1346
+ *
1347
+ * The result is a list of suggested modified search queries, derived from the
1348
+ * given search query, each with a relevance score, sorted by descending score.
1349
+ *
1350
+ * By default, it uses the same options used for search, except that by
1351
+ * default it performs prefix search on the last term of the query, and
1352
+ * combine terms with `'AND'` (requiring all query terms to match). Custom
1353
+ * options can be passed as a second argument. Defaults can be changed upon
1354
+ * calling the {@link MiniSearch} constructor, by passing a
1355
+ * `autoSuggestOptions` option.
1356
+ *
1357
+ * ### Basic usage:
1358
+ *
1359
+ * ```javascript
1360
+ * // Get suggestions for 'neuro':
1361
+ * miniSearch.autoSuggest('neuro')
1362
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
1363
+ * ```
1364
+ *
1365
+ * ### Multiple words:
1366
+ *
1367
+ * ```javascript
1368
+ * // Get suggestions for 'zen ar':
1369
+ * miniSearch.autoSuggest('zen ar')
1370
+ * // => [
1371
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
1372
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
1373
+ * // ]
1374
+ * ```
1375
+ *
1376
+ * ### Fuzzy suggestions:
1377
+ *
1378
+ * ```javascript
1379
+ * // Correct spelling mistakes using fuzzy search:
1380
+ * miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 })
1381
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
1382
+ * ```
1383
+ *
1384
+ * ### Filtering:
1385
+ *
1386
+ * ```javascript
1387
+ * // Get suggestions for 'zen ar', but only within the 'fiction' category
1388
+ * // (assuming that 'category' is a stored field):
1389
+ * miniSearch.autoSuggest('zen ar', {
1390
+ * filter: (result) => result.category === 'fiction'
1391
+ * })
1392
+ * // => [
1393
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
1394
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
1395
+ * // ]
1396
+ * ```
1397
+ *
1398
+ * @param queryString Query string to be expanded into suggestions
1399
+ * @param options Search options. The supported options and default values
1400
+ * are the same as for the {@link MiniSearch#search} method, except that by
1401
+ * default prefix search is performed on the last term in the query, and terms
1402
+ * are combined with `'AND'`.
1403
+ * @return A sorted array of suggestions sorted by relevance score.
1404
+ */
1405
+ autoSuggest(queryString, options = {}) {
1406
+ options = Object.assign(Object.assign({}, this._options.autoSuggestOptions), options);
1407
+ const suggestions = new Map();
1408
+ for (const { score, terms } of this.search(queryString, options)) {
1409
+ const phrase = terms.join(' ');
1410
+ const suggestion = suggestions.get(phrase);
1411
+ if (suggestion != null) {
1412
+ suggestion.score += score;
1413
+ suggestion.count += 1;
1414
+ }
1415
+ else {
1416
+ suggestions.set(phrase, { score, terms, count: 1 });
1417
+ }
1418
+ }
1419
+ const results = [];
1420
+ for (const [suggestion, { score, terms, count }] of suggestions) {
1421
+ results.push({ suggestion, terms, score: score / count });
1422
+ }
1423
+ results.sort(byScore);
1424
+ return results;
1425
+ }
1426
+ /**
1427
+ * Total number of documents available to search
1428
+ */
1429
+ get documentCount() {
1430
+ return this._documentCount;
1431
+ }
1432
+ /**
1433
+ * Number of terms in the index
1434
+ */
1435
+ get termCount() {
1436
+ return this._index.size;
1437
+ }
1438
+ /**
1439
+ * Deserializes a JSON index (serialized with `JSON.stringify(miniSearch)`)
1440
+ * and instantiates a MiniSearch instance. It should be given the same options
1441
+ * originally used when serializing the index.
1442
+ *
1443
+ * ### Usage:
1444
+ *
1445
+ * ```javascript
1446
+ * // If the index was serialized with:
1447
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1448
+ * miniSearch.addAll(documents)
1449
+ *
1450
+ * const json = JSON.stringify(miniSearch)
1451
+ * // It can later be deserialized like this:
1452
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
1453
+ * ```
1454
+ *
1455
+ * @param json JSON-serialized index
1456
+ * @param options configuration options, same as the constructor
1457
+ * @return An instance of MiniSearch deserialized from the given JSON.
1458
+ */
1459
+ static loadJSON(json, options) {
1460
+ if (options == null) {
1461
+ throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index');
1462
+ }
1463
+ return this.loadJS(JSON.parse(json), options);
1464
+ }
1465
+ /**
1466
+ * Async equivalent of {@link MiniSearch.loadJSON}
1467
+ *
1468
+ * This function is an alternative to {@link MiniSearch.loadJSON} that returns
1469
+ * a promise, and loads the index in batches, leaving pauses between them to avoid
1470
+ * blocking the main thread. It tends to be slower than the synchronous
1471
+ * version, but does not block the main thread, so it can be a better choice
1472
+ * when deserializing very large indexes.
1473
+ *
1474
+ * @param json JSON-serialized index
1475
+ * @param options configuration options, same as the constructor
1476
+ * @return A Promise that will resolve to an instance of MiniSearch deserialized from the given JSON.
1477
+ */
1478
+ static loadJSONAsync(json, options) {
1479
+ return __awaiter(this, void 0, void 0, function* () {
1480
+ if (options == null) {
1481
+ throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index');
1482
+ }
1483
+ return this.loadJSAsync(JSON.parse(json), options);
1484
+ });
1485
+ }
1486
+ /**
1487
+ * Returns the default value of an option. It will throw an error if no option
1488
+ * with the given name exists.
1489
+ *
1490
+ * @param optionName Name of the option
1491
+ * @return The default value of the given option
1492
+ *
1493
+ * ### Usage:
1494
+ *
1495
+ * ```javascript
1496
+ * // Get default tokenizer
1497
+ * MiniSearch.getDefault('tokenize')
1498
+ *
1499
+ * // Get default term processor
1500
+ * MiniSearch.getDefault('processTerm')
1501
+ *
1502
+ * // Unknown options will throw an error
1503
+ * MiniSearch.getDefault('notExisting')
1504
+ * // => throws 'MiniSearch: unknown option "notExisting"'
1505
+ * ```
1506
+ */
1507
+ static getDefault(optionName) {
1508
+ if (defaultOptions.hasOwnProperty(optionName)) {
1509
+ return getOwnProperty(defaultOptions, optionName);
1510
+ }
1511
+ else {
1512
+ throw new Error(`MiniSearch: unknown option "${optionName}"`);
1513
+ }
1514
+ }
1515
+ /**
1516
+ * @ignore
1517
+ */
1518
+ static loadJS(js, options) {
1519
+ const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
1520
+ const miniSearch = this.instantiateMiniSearch(js, options);
1521
+ miniSearch._documentIds = objectToNumericMap(documentIds);
1522
+ miniSearch._fieldLength = objectToNumericMap(fieldLength);
1523
+ miniSearch._storedFields = objectToNumericMap(storedFields);
1524
+ for (const [shortId, id] of miniSearch._documentIds) {
1525
+ miniSearch._idToShortId.set(id, shortId);
1526
+ }
1527
+ for (const [term, data] of index) {
1528
+ const dataMap = new Map();
1529
+ for (const fieldId of Object.keys(data)) {
1530
+ let indexEntry = data[fieldId];
1531
+ // Version 1 used to nest the index entry inside a field called ds
1532
+ if (serializationVersion === 1) {
1533
+ indexEntry = indexEntry.ds;
1534
+ }
1535
+ dataMap.set(parseInt(fieldId, 10), objectToNumericMap(indexEntry));
1536
+ }
1537
+ miniSearch._index.set(term, dataMap);
1538
+ }
1539
+ return miniSearch;
1540
+ }
1541
+ /**
1542
+ * @ignore
1543
+ */
1544
+ static loadJSAsync(js, options) {
1545
+ return __awaiter(this, void 0, void 0, function* () {
1546
+ const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
1547
+ const miniSearch = this.instantiateMiniSearch(js, options);
1548
+ miniSearch._documentIds = yield objectToNumericMapAsync(documentIds);
1549
+ miniSearch._fieldLength = yield objectToNumericMapAsync(fieldLength);
1550
+ miniSearch._storedFields = yield objectToNumericMapAsync(storedFields);
1551
+ for (const [shortId, id] of miniSearch._documentIds) {
1552
+ miniSearch._idToShortId.set(id, shortId);
1553
+ }
1554
+ let count = 0;
1555
+ for (const [term, data] of index) {
1556
+ const dataMap = new Map();
1557
+ for (const fieldId of Object.keys(data)) {
1558
+ let indexEntry = data[fieldId];
1559
+ // Version 1 used to nest the index entry inside a field called ds
1560
+ if (serializationVersion === 1) {
1561
+ indexEntry = indexEntry.ds;
1562
+ }
1563
+ dataMap.set(parseInt(fieldId, 10), yield objectToNumericMapAsync(indexEntry));
1564
+ }
1565
+ if (++count % 1000 === 0)
1566
+ yield wait(0);
1567
+ miniSearch._index.set(term, dataMap);
1568
+ }
1569
+ return miniSearch;
1570
+ });
1571
+ }
1572
+ /**
1573
+ * @ignore
1574
+ */
1575
+ static instantiateMiniSearch(js, options) {
1576
+ const { documentCount, nextId, fieldIds, averageFieldLength, dirtCount, serializationVersion } = js;
1577
+ if (serializationVersion !== 1 && serializationVersion !== 2) {
1578
+ throw new Error('MiniSearch: cannot deserialize an index created with an incompatible version');
1579
+ }
1580
+ const miniSearch = new MiniSearch(options);
1581
+ miniSearch._documentCount = documentCount;
1582
+ miniSearch._nextId = nextId;
1583
+ miniSearch._idToShortId = new Map();
1584
+ miniSearch._fieldIds = fieldIds;
1585
+ miniSearch._avgFieldLength = averageFieldLength;
1586
+ miniSearch._dirtCount = dirtCount || 0;
1587
+ miniSearch._index = new SearchableMap();
1588
+ return miniSearch;
1589
+ }
1590
+ /**
1591
+ * @ignore
1592
+ */
1593
+ executeQuery(query, searchOptions = {}) {
1594
+ if (query === MiniSearch.wildcard) {
1595
+ return this.executeWildcardQuery(searchOptions);
1596
+ }
1597
+ if (typeof query !== 'string') {
1598
+ const options = Object.assign(Object.assign(Object.assign({}, searchOptions), query), { queries: undefined });
1599
+ const results = query.queries.map((subquery) => this.executeQuery(subquery, options));
1600
+ return this.combineResults(results, options.combineWith);
1601
+ }
1602
+ const { tokenize, processTerm, searchOptions: globalSearchOptions } = this._options;
1603
+ const options = Object.assign(Object.assign({ tokenize, processTerm }, globalSearchOptions), searchOptions);
1604
+ const { tokenize: searchTokenize, processTerm: searchProcessTerm } = options;
1605
+ const terms = searchTokenize(query)
1606
+ .flatMap((term) => searchProcessTerm(term))
1607
+ .filter((term) => !!term);
1608
+ const queries = terms.map(termToQuerySpec(options));
1609
+ const results = queries.map(query => this.executeQuerySpec(query, options));
1610
+ return this.combineResults(results, options.combineWith);
1611
+ }
1612
+ /**
1613
+ * @ignore
1614
+ */
1615
+ executeQuerySpec(query, searchOptions) {
1616
+ const options = Object.assign(Object.assign({}, this._options.searchOptions), searchOptions);
1617
+ const boosts = (options.fields || this._options.fields).reduce((boosts, field) => (Object.assign(Object.assign({}, boosts), { [field]: getOwnProperty(options.boost, field) || 1 })), {});
1618
+ const { boostDocument, weights, maxFuzzy, bm25: bm25params } = options;
1619
+ const { fuzzy: fuzzyWeight, prefix: prefixWeight } = Object.assign(Object.assign({}, defaultSearchOptions.weights), weights);
1620
+ const data = this._index.get(query.term);
1621
+ const results = this.termResults(query.term, query.term, 1, query.termBoost, data, boosts, boostDocument, bm25params);
1622
+ let prefixMatches;
1623
+ let fuzzyMatches;
1624
+ if (query.prefix) {
1625
+ prefixMatches = this._index.atPrefix(query.term);
1626
+ }
1627
+ if (query.fuzzy) {
1628
+ const fuzzy = (query.fuzzy === true) ? 0.2 : query.fuzzy;
1629
+ const maxDistance = fuzzy < 1 ? Math.min(maxFuzzy, Math.round(query.term.length * fuzzy)) : fuzzy;
1630
+ if (maxDistance)
1631
+ fuzzyMatches = this._index.fuzzyGet(query.term, maxDistance);
1632
+ }
1633
+ if (prefixMatches) {
1634
+ for (const [term, data] of prefixMatches) {
1635
+ const distance = term.length - query.term.length;
1636
+ if (!distance) {
1637
+ continue;
1638
+ } // Skip exact match.
1639
+ // Delete the term from fuzzy results (if present) if it is also a
1640
+ // prefix result. This entry will always be scored as a prefix result.
1641
+ fuzzyMatches === null || fuzzyMatches === void 0 ? void 0 : fuzzyMatches.delete(term);
1642
+ // Weight gradually approaches 0 as distance goes to infinity, with the
1643
+ // weight for the hypothetical distance 0 being equal to prefixWeight.
1644
+ // The rate of change is much lower than that of fuzzy matches to
1645
+ // account for the fact that prefix matches stay more relevant than
1646
+ // fuzzy matches for longer distances.
1647
+ const weight = prefixWeight * term.length / (term.length + 0.3 * distance);
1648
+ this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results);
1649
+ }
1650
+ }
1651
+ if (fuzzyMatches) {
1652
+ for (const term of fuzzyMatches.keys()) {
1653
+ const [data, distance] = fuzzyMatches.get(term);
1654
+ if (!distance) {
1655
+ continue;
1656
+ } // Skip exact match.
1657
+ // Weight gradually approaches 0 as distance goes to infinity, with the
1658
+ // weight for the hypothetical distance 0 being equal to fuzzyWeight.
1659
+ const weight = fuzzyWeight * term.length / (term.length + distance);
1660
+ this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results);
1661
+ }
1662
+ }
1663
+ return results;
1664
+ }
1665
+ /**
1666
+ * @ignore
1667
+ */
1668
+ executeWildcardQuery(searchOptions) {
1669
+ const results = new Map();
1670
+ const options = Object.assign(Object.assign({}, this._options.searchOptions), searchOptions);
1671
+ for (const [shortId, id] of this._documentIds) {
1672
+ const score = options.boostDocument ? options.boostDocument(id, '', this._storedFields.get(shortId)) : 1;
1673
+ results.set(shortId, {
1674
+ score,
1675
+ terms: [],
1676
+ match: {}
1677
+ });
1678
+ }
1679
+ return results;
1680
+ }
1681
+ /**
1682
+ * @ignore
1683
+ */
1684
+ combineResults(results, combineWith = OR) {
1685
+ if (results.length === 0) {
1686
+ return new Map();
1687
+ }
1688
+ const operator = combineWith.toLowerCase();
1689
+ const combinator = combinators[operator];
1690
+ if (!combinator) {
1691
+ throw new Error(`Invalid combination operator: ${combineWith}`);
1692
+ }
1693
+ return results.reduce(combinator) || new Map();
1694
+ }
1695
+ /**
1696
+ * Allows serialization of the index to JSON, to possibly store it and later
1697
+ * deserialize it with {@link MiniSearch.loadJSON}.
1698
+ *
1699
+ * Normally one does not directly call this method, but rather call the
1700
+ * standard JavaScript `JSON.stringify()` passing the {@link MiniSearch}
1701
+ * instance, and JavaScript will internally call this method. Upon
1702
+ * deserialization, one must pass to {@link MiniSearch.loadJSON} the same
1703
+ * options used to create the original instance that was serialized.
1704
+ *
1705
+ * ### Usage:
1706
+ *
1707
+ * ```javascript
1708
+ * // Serialize the index:
1709
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1710
+ * miniSearch.addAll(documents)
1711
+ * const json = JSON.stringify(miniSearch)
1712
+ *
1713
+ * // Later, to deserialize it:
1714
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
1715
+ * ```
1716
+ *
1717
+ * @return A plain-object serializable representation of the search index.
1718
+ */
1719
+ toJSON() {
1720
+ const index = [];
1721
+ for (const [term, fieldIndex] of this._index) {
1722
+ const data = {};
1723
+ for (const [fieldId, freqs] of fieldIndex) {
1724
+ data[fieldId] = Object.fromEntries(freqs);
1725
+ }
1726
+ index.push([term, data]);
1727
+ }
1728
+ return {
1729
+ documentCount: this._documentCount,
1730
+ nextId: this._nextId,
1731
+ documentIds: Object.fromEntries(this._documentIds),
1732
+ fieldIds: this._fieldIds,
1733
+ fieldLength: Object.fromEntries(this._fieldLength),
1734
+ averageFieldLength: this._avgFieldLength,
1735
+ storedFields: Object.fromEntries(this._storedFields),
1736
+ dirtCount: this._dirtCount,
1737
+ index,
1738
+ serializationVersion: 2
1739
+ };
1740
+ }
1741
+ /**
1742
+ * @ignore
1743
+ */
1744
+ termResults(sourceTerm, derivedTerm, termWeight, termBoost, fieldTermData, fieldBoosts, boostDocumentFn, bm25params, results = new Map()) {
1745
+ if (fieldTermData == null)
1746
+ return results;
1747
+ for (const field of Object.keys(fieldBoosts)) {
1748
+ const fieldBoost = fieldBoosts[field];
1749
+ const fieldId = this._fieldIds[field];
1750
+ const fieldTermFreqs = fieldTermData.get(fieldId);
1751
+ if (fieldTermFreqs == null)
1752
+ continue;
1753
+ let matchingFields = fieldTermFreqs.size;
1754
+ const avgFieldLength = this._avgFieldLength[fieldId];
1755
+ for (const docId of fieldTermFreqs.keys()) {
1756
+ if (!this._documentIds.has(docId)) {
1757
+ this.removeTerm(fieldId, docId, derivedTerm);
1758
+ matchingFields -= 1;
1759
+ continue;
1760
+ }
1761
+ const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1;
1762
+ if (!docBoost)
1763
+ continue;
1764
+ const termFreq = fieldTermFreqs.get(docId);
1765
+ const fieldLength = this._fieldLength.get(docId)[fieldId];
1766
+ // NOTE: The total number of fields is set to the number of documents
1767
+ // `this._documentCount`. It could also make sense to use the number of
1768
+ // documents where the current field is non-blank as a normalization
1769
+ // factor. This will make a difference in scoring if the field is rarely
1770
+ // present. This is currently not supported, and may require further
1771
+ // analysis to see if it is a valid use case.
1772
+ const rawScore = calcBM25Score(termFreq, matchingFields, this._documentCount, fieldLength, avgFieldLength, bm25params);
1773
+ const weightedScore = termWeight * termBoost * fieldBoost * docBoost * rawScore;
1774
+ const result = results.get(docId);
1775
+ if (result) {
1776
+ result.score += weightedScore;
1777
+ assignUniqueTerm(result.terms, sourceTerm);
1778
+ const match = getOwnProperty(result.match, derivedTerm);
1779
+ if (match) {
1780
+ match.push(field);
1781
+ }
1782
+ else {
1783
+ result.match[derivedTerm] = [field];
1784
+ }
1785
+ }
1786
+ else {
1787
+ results.set(docId, {
1788
+ score: weightedScore,
1789
+ terms: [sourceTerm],
1790
+ match: { [derivedTerm]: [field] }
1791
+ });
1792
+ }
1793
+ }
1794
+ }
1795
+ return results;
1796
+ }
1797
+ /**
1798
+ * @ignore
1799
+ */
1800
+ addTerm(fieldId, documentId, term) {
1801
+ const indexData = this._index.fetch(term, createMap);
1802
+ let fieldIndex = indexData.get(fieldId);
1803
+ if (fieldIndex == null) {
1804
+ fieldIndex = new Map();
1805
+ fieldIndex.set(documentId, 1);
1806
+ indexData.set(fieldId, fieldIndex);
1807
+ }
1808
+ else {
1809
+ const docs = fieldIndex.get(documentId);
1810
+ fieldIndex.set(documentId, (docs || 0) + 1);
1811
+ }
1812
+ }
1813
+ /**
1814
+ * @ignore
1815
+ */
1816
+ removeTerm(fieldId, documentId, term) {
1817
+ if (!this._index.has(term)) {
1818
+ this.warnDocumentChanged(documentId, fieldId, term);
1819
+ return;
1820
+ }
1821
+ const indexData = this._index.fetch(term, createMap);
1822
+ const fieldIndex = indexData.get(fieldId);
1823
+ if (fieldIndex == null || fieldIndex.get(documentId) == null) {
1824
+ this.warnDocumentChanged(documentId, fieldId, term);
1825
+ }
1826
+ else if (fieldIndex.get(documentId) <= 1) {
1827
+ if (fieldIndex.size <= 1) {
1828
+ indexData.delete(fieldId);
1829
+ }
1830
+ else {
1831
+ fieldIndex.delete(documentId);
1832
+ }
1833
+ }
1834
+ else {
1835
+ fieldIndex.set(documentId, fieldIndex.get(documentId) - 1);
1836
+ }
1837
+ if (this._index.get(term).size === 0) {
1838
+ this._index.delete(term);
1839
+ }
1840
+ }
1841
+ /**
1842
+ * @ignore
1843
+ */
1844
+ warnDocumentChanged(shortDocumentId, fieldId, term) {
1845
+ for (const fieldName of Object.keys(this._fieldIds)) {
1846
+ if (this._fieldIds[fieldName] === fieldId) {
1847
+ this._options.logger('warn', `MiniSearch: document with ID ${this._documentIds.get(shortDocumentId)} has changed before removal: term "${term}" was not present in field "${fieldName}". Removing a document after it has changed can corrupt the index!`, 'version_conflict');
1848
+ return;
1849
+ }
1850
+ }
1851
+ }
1852
+ /**
1853
+ * @ignore
1854
+ */
1855
+ addDocumentId(documentId) {
1856
+ const shortDocumentId = this._nextId;
1857
+ this._idToShortId.set(documentId, shortDocumentId);
1858
+ this._documentIds.set(shortDocumentId, documentId);
1859
+ this._documentCount += 1;
1860
+ this._nextId += 1;
1861
+ return shortDocumentId;
1862
+ }
1863
+ /**
1864
+ * @ignore
1865
+ */
1866
+ addFields(fields) {
1867
+ for (let i = 0; i < fields.length; i++) {
1868
+ this._fieldIds[fields[i]] = i;
1869
+ }
1870
+ }
1871
+ /**
1872
+ * @ignore
1873
+ */
1874
+ addFieldLength(documentId, fieldId, count, length) {
1875
+ let fieldLengths = this._fieldLength.get(documentId);
1876
+ if (fieldLengths == null)
1877
+ this._fieldLength.set(documentId, fieldLengths = []);
1878
+ fieldLengths[fieldId] = length;
1879
+ const averageFieldLength = this._avgFieldLength[fieldId] || 0;
1880
+ const totalFieldLength = (averageFieldLength * count) + length;
1881
+ this._avgFieldLength[fieldId] = totalFieldLength / (count + 1);
1882
+ }
1883
+ /**
1884
+ * @ignore
1885
+ */
1886
+ removeFieldLength(documentId, fieldId, count, length) {
1887
+ if (count === 1) {
1888
+ this._avgFieldLength[fieldId] = 0;
1889
+ return;
1890
+ }
1891
+ const totalFieldLength = (this._avgFieldLength[fieldId] * count) - length;
1892
+ this._avgFieldLength[fieldId] = totalFieldLength / (count - 1);
1893
+ }
1894
+ /**
1895
+ * @ignore
1896
+ */
1897
+ saveStoredFields(documentId, doc) {
1898
+ const { storeFields, extractField } = this._options;
1899
+ if (storeFields == null || storeFields.length === 0) {
1900
+ return;
1901
+ }
1902
+ let documentFields = this._storedFields.get(documentId);
1903
+ if (documentFields == null)
1904
+ this._storedFields.set(documentId, documentFields = {});
1905
+ for (const fieldName of storeFields) {
1906
+ const fieldValue = extractField(doc, fieldName);
1907
+ if (fieldValue !== undefined)
1908
+ documentFields[fieldName] = fieldValue;
1909
+ }
1910
+ }
1911
+ }
1912
+ /**
1913
+ * The special wildcard symbol that can be passed to {@link MiniSearch#search}
1914
+ * to match all documents
1915
+ */
1916
+ MiniSearch.wildcard = Symbol('*');
1917
+ const getOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) ? object[property] : undefined;
1918
+ const combinators = {
1919
+ [OR]: (a, b) => {
1920
+ for (const docId of b.keys()) {
1921
+ const existing = a.get(docId);
1922
+ if (existing == null) {
1923
+ a.set(docId, b.get(docId));
1924
+ }
1925
+ else {
1926
+ const { score, terms, match } = b.get(docId);
1927
+ existing.score = existing.score + score;
1928
+ existing.match = Object.assign(existing.match, match);
1929
+ assignUniqueTerms(existing.terms, terms);
1930
+ }
1931
+ }
1932
+ return a;
1933
+ },
1934
+ [AND]: (a, b) => {
1935
+ const combined = new Map();
1936
+ for (const docId of b.keys()) {
1937
+ const existing = a.get(docId);
1938
+ if (existing == null)
1939
+ continue;
1940
+ const { score, terms, match } = b.get(docId);
1941
+ assignUniqueTerms(existing.terms, terms);
1942
+ combined.set(docId, {
1943
+ score: existing.score + score,
1944
+ terms: existing.terms,
1945
+ match: Object.assign(existing.match, match)
1946
+ });
1947
+ }
1948
+ return combined;
1949
+ },
1950
+ [AND_NOT]: (a, b) => {
1951
+ for (const docId of b.keys())
1952
+ a.delete(docId);
1953
+ return a;
1954
+ }
1955
+ };
1956
+ const defaultBM25params = { k: 1.2, b: 0.7, d: 0.5 };
1957
+ const calcBM25Score = (termFreq, matchingCount, totalCount, fieldLength, avgFieldLength, bm25params) => {
1958
+ const { k, b, d } = bm25params;
1959
+ const invDocFreq = Math.log(1 + (totalCount - matchingCount + 0.5) / (matchingCount + 0.5));
1960
+ return invDocFreq * (d + termFreq * (k + 1) / (termFreq + k * (1 - b + b * fieldLength / avgFieldLength)));
1961
+ };
1962
+ const termToQuerySpec = (options) => (term, i, terms) => {
1963
+ const fuzzy = (typeof options.fuzzy === 'function')
1964
+ ? options.fuzzy(term, i, terms)
1965
+ : (options.fuzzy || false);
1966
+ const prefix = (typeof options.prefix === 'function')
1967
+ ? options.prefix(term, i, terms)
1968
+ : (options.prefix === true);
1969
+ const termBoost = (typeof options.boostTerm === 'function')
1970
+ ? options.boostTerm(term, i, terms)
1971
+ : 1;
1972
+ return { term, fuzzy, prefix, termBoost };
1973
+ };
1974
+ const defaultOptions = {
1975
+ idField: 'id',
1976
+ extractField: (document, fieldName) => document[fieldName],
1977
+ tokenize: (text) => text.split(SPACE_OR_PUNCTUATION),
1978
+ processTerm: (term) => term.toLowerCase(),
1979
+ fields: undefined,
1980
+ searchOptions: undefined,
1981
+ storeFields: [],
1982
+ logger: (level, message) => {
1983
+ if (typeof (console === null || console === void 0 ? void 0 : console[level]) === 'function')
1984
+ console[level](message);
1985
+ },
1986
+ autoVacuum: true
1987
+ };
1988
+ const defaultSearchOptions = {
1989
+ combineWith: OR,
1990
+ prefix: false,
1991
+ fuzzy: false,
1992
+ maxFuzzy: 6,
1993
+ boost: {},
1994
+ weights: { fuzzy: 0.45, prefix: 0.375 },
1995
+ bm25: defaultBM25params
1996
+ };
1997
+ const defaultAutoSuggestOptions = {
1998
+ combineWith: AND,
1999
+ prefix: (term, i, terms) => i === terms.length - 1
2000
+ };
2001
+ const defaultVacuumOptions = { batchSize: 1000, batchWait: 10 };
2002
+ const defaultVacuumConditions = { minDirtFactor: 0.1, minDirtCount: 20 };
2003
+ const defaultAutoVacuumOptions = Object.assign(Object.assign({}, defaultVacuumOptions), defaultVacuumConditions);
2004
+ const assignUniqueTerm = (target, term) => {
2005
+ // Avoid adding duplicate terms.
2006
+ if (!target.includes(term))
2007
+ target.push(term);
2008
+ };
2009
+ const assignUniqueTerms = (target, source) => {
2010
+ for (const term of source) {
2011
+ // Avoid adding duplicate terms.
2012
+ if (!target.includes(term))
2013
+ target.push(term);
2014
+ }
2015
+ };
2016
+ const byScore = ({ score: a }, { score: b }) => b - a;
2017
+ const createMap = () => new Map();
2018
+ const objectToNumericMap = (object) => {
2019
+ const map = new Map();
2020
+ for (const key of Object.keys(object)) {
2021
+ map.set(parseInt(key, 10), object[key]);
2022
+ }
2023
+ return map;
2024
+ };
2025
+ const objectToNumericMapAsync = (object) => __awaiter(void 0, void 0, void 0, function* () {
2026
+ const map = new Map();
2027
+ let count = 0;
2028
+ for (const key of Object.keys(object)) {
2029
+ map.set(parseInt(key, 10), object[key]);
2030
+ if (++count % 1000 === 0) {
2031
+ yield wait(0);
2032
+ }
2033
+ }
2034
+ return map;
2035
+ });
2036
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2037
+ // This regular expression matches any Unicode space, newline, or punctuation
2038
+ // character
2039
+ const SPACE_OR_PUNCTUATION = /[\n\r\p{Z}\p{P}]+/u;
2040
+
2041
+ return MiniSearch;
2042
+
2043
+ }));
2044
+ //# sourceMappingURL=index.js.map