@mx-space/cli 0.6.8 → 0.7.1
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.
- package/dist/bin/mxs.mjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{mxs-CCNd5SIa.mjs → mxs-ClNF0UNu.mjs} +1900 -87
- package/package.json +2 -1
- package/skills/authoring.md +61 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ as drain, $a as identity, $i as reduce$1, $n as try_, $r as get$11, $t as either, A as compose, Aa as pipeArguments, Ai as empty$11, An as orElse$3, Ar as Scope, At as encodeSync, B as ensuring$1, Ba as cached, Bi as head$2, Bn as scope, Br as set$3, Bt as async, C as Number$, Ca as some, Ci as isConfigError, Cn as match$2, Ct as currentContext, D as String$, Da as left, Di as appendAll, Dn as onError, Dr as taggedEnum, Dt as unsafeMake, Ea as string$1, Ei as append$2, En as negate, Er as tagged, Et as get$15, Fa as redact$1, Fi as findFirstIndex, Fn as retry, Fr as isFailure, Ft as addFinalizer, G as mapError$1, Gi as join$1, Gn as suspend, Gr as updateAs, Gt as catchIf, H as fromChannel, Ha as hash, Hi as isArray$1, Hn as serviceFunctions, Hr as updateAndGet, Ht as catchAll, I as make$21, Ia as symbolRedactable, Ii as findLast, In as runFork$1, Ir as void_$1, It as all$5, J as splitLines, Ja as globalValue, Ji as match$1, Jn as tapError, Jr as cons$1, Jt as catchTags, K as run$5, Ka as isObject, Ki as make$22, Kn as sync, Kr as CommitPrototype, Kt as catchSome, L as isConfig, La as toStringUnknown, Li as flatMap$4, Ln as runPromise, Lr as ParentSpan, Lt as annotateLogs, M as decodeUnknown, Ma as Class, Mi as filter$2, Mn as provide$1, Mr as isInterrupted, Mt as _void, N as int, Na as NodeInspectSymbol, Ni as filterMap, Nn as provideService, Nr as isInterruptedOnly, Nt as acquireRelease, O as TaggedError, Oa as merge$
|
|
1
|
+
import { $ as drain, $a as identity, $i as reduce$1, $n as try_, $r as get$11, $t as either, A as compose, Aa as pipeArguments, Ai as empty$11, An as orElse$3, Ar as Scope, At as encodeSync, B as ensuring$1, Ba as cached, Bi as head$2, Bn as scope, Br as set$3, Bt as async, C as Number$, Ca as some, Ci as isConfigError, Cn as match$2, Ct as currentContext, D as String$, Da as left, Di as appendAll, Dn as onError, Dr as taggedEnum, Dt as unsafeMake, Ea as string$1, Ei as append$2, En as negate, Er as tagged, Et as get$15, Fa as redact$1, Fi as findFirstIndex, Fn as retry, Fr as isFailure, Ft as addFinalizer, G as mapError$1, Gi as join$1, Gn as suspend, Gr as updateAs, Gt as catchIf, H as fromChannel, Ha as hash, Hi as isArray$1, Hn as serviceFunctions, Hr as updateAndGet, Ht as catchAll, I as make$21, Ia as symbolRedactable, Ii as findLast, In as runFork$1, Ir as void_$1, It as all$5, J as splitLines, Ja as globalValue, Ji as match$1, Jn as tapError, Jr as cons$1, Jt as catchTags, K as run$5, Ka as isObject, Ki as make$22, Kn as sync, Kr as CommitPrototype, Kt as catchSome, L as isConfig, La as toStringUnknown, Li as flatMap$4, Ln as runPromise, Lr as ParentSpan, Lt as annotateLogs, M as decodeUnknown, Ma as Class, Mi as filter$2, Mn as provide$1, Mr as isInterrupted, Mt as _void, N as int, Na as NodeInspectSymbol, Ni as filterMap, Nn as provideService, Nr as isInterruptedOnly, Nt as acquireRelease, O as TaggedError, Oa as merge$2, Oi as contains, On as onInterrupt, Or as error$1, Ot as nominal, P as optional$7, Pa as format$3, Pi as findFirst, Pn as repeatN, Pr as fail$5, Pt as acquireUseRelease$1, Q as unwrapScoped, Qa as dual, Qi as prepend$1, Qn as tryPromise, Qr as fromIterable$2, Qt as dieMessage, R as asyncScoped, Ra as equals, Ri as fromIterable$1, Rn as runSync, Rr as get$10, Rt as as, S as Literal, Sa as orElse$2, Si as omit, Sn as mapInputContext, Sr as unsafeDone, St as succeed$3, Ta as mapInput, Ti as allocate, Tn as matchEffect, Tr as TaggedError$1, Tt as currentTracerEnabled, Ua as string, Ui as isEmptyReadonlyArray, Un as serviceOption, Ur as allLevels, Ut as catchAllCause, Va as combine$2, Vi as headNonEmpty, Vn as scoped, Vr as update$6, Vt as cached$1, W as mapChunks, Wa as symbol, Wi as isNonEmptyReadonlyArray, Wn as succeed$1, Wr as getOrDefault, Wt as catchAllDefect, X as transduce, Xa as constUndefined, Xi as matchRight, Xn as timeout, Xr as of$3, Xt as contextWithEffect, Y as suspend$1, Ya as constFalse, Yi as matchLeft, Yn as tapErrorCause, Yr as isNil, Yt as context, Z as unwrap$5, Za as constVoid, Zi as of$2, Zn as tryMap, Zr as empty$12, Zt as die, _ as fromString, _a as isOption, _i as Reference, _n as logError, _r as failCause$1, _t as effect, a as make$27, aa as tailNonEmpty, ai as add$1, an as filterOrDieMessage, ar as useSpan, at as embedInput, b as Defect, ba as match, bi as getOption, bn as mapBoth, br as make$26, bt as provideMerge, c as make$25, ci as remove$4, cn as fnUntraced, cr as withParentSpan, ct as fromEffect, d as defaultLogger, da as flatMap$3, di as map$10, dn as forkScoped, dr as zipRight, dt as write, ea as reduceRight, ei as has, en as ensuring, eo as pipe, er as uninterruptible, et as foldLeftChunks, f as prettyLoggerDefault, fa as fromNullable, fi as of$1, fn as gen, fr as zipWith, ft as spaced, g as when, ga as isNone, gi as GenericTag, gn as locally, gr as fail$4, gt as make$30, h as value$1, ha as getOrUndefined, hi as unsafeFromArray, hn as isEffect, hr as complete, ht as get$14, i as isMailbox, ia as splitAt, ii as make$29, in as filter$1, io as __toESM, ir as unsafeMakeSemaphore, it as acquireUseRelease, ja as BaseProto, ji as every, jn as orElseFail, jr as addFinalizer$1, jt as is$2, k as Union, ka as right, ki as drop, kn as orDie, kr as log, kt as decodeSync, l as updateEffect, la as map$9, li as seconds, ln as forEach$1, lr as zip, lt as suspend$2, m as orElse$1, ma as getOrThrow, mi as toArray, mn as interruptible, mr as _await, mt as updateContext, n as make$28, na as sort$2, ni as make$24, nn as failCause, no as __exportAll, nr as unlessEffect, nt as fromChannel$1, o as toChannel, oa as take, oi as fromIterable$3, on as flatMap$1, or as when$1, ot as ensuring$2, p as withMinimumLogLevel, pa as getOrElse, pi as reduce$2, pn as ignore, pr as DeferredTypeId, pt as runFork, q as runCollect, qa as isTagged, qi as map$7, qn as tap, qr as round, qt as catchTag, r as unsafeAdd, ra as span, ri as set$2, rn as failSync, ro as __require, rr as unsafeMakeLatch, rt as unwrapScoped$1, s as get$12, sa as unsafeGet, si as has$1, sn as flatten$3, sr as withFiberRuntime, st as flatMap$2, t as join$2, ta as some$1, ti as isEmpty$5, tn as fail$2, to as __commonJSMin, tr as uninterruptibleMask, tt as forEach, u as unify, ua as filter$3, ui as appendAll$1, un as forkDaemon, ur as zipLeft, ut as void_, v as Cause, va as isSome, vi as Tag$2, vn as logWarning, vr as interrupt, vt as mergeAll, w as NumberFromString, wa as boolean$3, wi as isMissingDataOnly, wn as matchCauseEffect, wr as Error$1, wt as currentLoggers, x as Int, xa as none$3, xi as merge$3, xn as mapError, xr as succeed$2, xt as scoped$1, y as Date$, ya as map$8, yi as get$13, yn as map$6, yr as isDone, yt as provide, z as bufferChunks, za as symbol$1, zi as getSomes, zn as runtime, zr as make$23, zt as asVoid } from "./esm-CK9m5JKX.mjs";
|
|
2
2
|
import { _ as LinkNode, a as HeadingNode, b as CodeHighlightNode, d as ListItemNode, f as ListNode, g as AutoLinkNode, o as QuoteNode, x as CodeNode } from "./LexicalRichText.node-DDpwfE-P.mjs";
|
|
3
3
|
import { t as HorizontalRuleNode } from "./LexicalExtension.node-CF3EAPN7.mjs";
|
|
4
4
|
import { Ht as ElementNode, Vt as DecoratorNode } from "./Lexical.node-BUsI9gWr.mjs";
|
|
@@ -24,7 +24,7 @@ import * as ChildProcess from "node:child_process";
|
|
|
24
24
|
import childProcess, { execFile, spawn } from "node:child_process";
|
|
25
25
|
import * as readline from "node:readline";
|
|
26
26
|
import E from "node:readline";
|
|
27
|
-
import
|
|
27
|
+
import process$1, { stdin, stdout } from "node:process";
|
|
28
28
|
import "node:tty";
|
|
29
29
|
//#region ../../node_modules/.pnpm/@effect+platform@0.96.1_effect@3.21.2/node_modules/@effect/platform/dist/esm/Error.js
|
|
30
30
|
/**
|
|
@@ -4719,7 +4719,7 @@ const map$1 = /* @__PURE__ */ dual(2, (self, f) => makeMap(self, (a) => right(f(
|
|
|
4719
4719
|
/** @internal */
|
|
4720
4720
|
const optional$3 = (self) => withDefault(map$1(self, some), none$3());
|
|
4721
4721
|
/** @internal */
|
|
4722
|
-
const orElse = /* @__PURE__ */ dual(2, (self, that) => orElseEither(self, that).pipe(map$1(merge$
|
|
4722
|
+
const orElse = /* @__PURE__ */ dual(2, (self, that) => orElseEither(self, that).pipe(map$1(merge$2)));
|
|
4723
4723
|
/** @internal */
|
|
4724
4724
|
const orElseEither = /* @__PURE__ */ dual(2, (self, that) => makeOrElse(self, that));
|
|
4725
4725
|
/** @internal */
|
|
@@ -5144,7 +5144,7 @@ const matchOptions = (input, options, config) => {
|
|
|
5144
5144
|
return matchOptions(otherArgs, otherOptions, config).pipe(map$6(([error, otherArgs, map2]) => [
|
|
5145
5145
|
error,
|
|
5146
5146
|
otherArgs,
|
|
5147
|
-
merge(map1, fromIterable$1(map2))
|
|
5147
|
+
merge$1(map1, fromIterable$1(map2))
|
|
5148
5148
|
]));
|
|
5149
5149
|
}), catchAll((e) => succeed$1([
|
|
5150
5150
|
some(e),
|
|
@@ -5483,7 +5483,7 @@ const matchUnclustered = (input, tail, options, config) => {
|
|
|
5483
5483
|
return matchUnclustered(otherFlags, tail, opts1, config).pipe(map$6(([_, opts2, map2]) => [
|
|
5484
5484
|
tail,
|
|
5485
5485
|
opts2,
|
|
5486
|
-
merge(map1, fromIterable$1(map2))
|
|
5486
|
+
merge$1(map1, fromIterable$1(map2))
|
|
5487
5487
|
]));
|
|
5488
5488
|
}));
|
|
5489
5489
|
}
|
|
@@ -5496,11 +5496,11 @@ const matchUnclustered = (input, tail, options, config) => {
|
|
|
5496
5496
|
/**
|
|
5497
5497
|
* Sums the list associated with the same key.
|
|
5498
5498
|
*/
|
|
5499
|
-
const merge = (map1, map2) => {
|
|
5499
|
+
const merge$1 = (map1, map2) => {
|
|
5500
5500
|
if (isNonEmptyReadonlyArray(map2)) {
|
|
5501
5501
|
const head = headNonEmpty(map2);
|
|
5502
5502
|
const tail = tailNonEmpty(map2);
|
|
5503
|
-
return merge(match(get$11(map1, head[0]), {
|
|
5503
|
+
return merge$1(match(get$11(map1, head[0]), {
|
|
5504
5504
|
onNone: () => set$2(map1, head[0], head[1]),
|
|
5505
5505
|
onSome: (elems) => set$2(map1, head[0], appendAll(elems, head[1]))
|
|
5506
5506
|
}), tail);
|
|
@@ -6866,7 +6866,7 @@ const setAll$1 = /* @__PURE__ */ dual(2, (self, headers) => make$12({
|
|
|
6866
6866
|
* @since 1.0.0
|
|
6867
6867
|
* @category combinators
|
|
6868
6868
|
*/
|
|
6869
|
-
const remove$
|
|
6869
|
+
const remove$3 = /* @__PURE__ */ dual(2, (self, key) => {
|
|
6870
6870
|
const out = make$12(self);
|
|
6871
6871
|
const modify = (key) => {
|
|
6872
6872
|
if (typeof key === "string") {
|
|
@@ -7232,7 +7232,7 @@ const setHash = /* @__PURE__ */ dual(2, (self, hash) => makeInternal(self.method
|
|
|
7232
7232
|
/** @internal */
|
|
7233
7233
|
const setBody = /* @__PURE__ */ dual(2, (self, body) => {
|
|
7234
7234
|
let headers = self.headers;
|
|
7235
|
-
if (body._tag === "Empty" || body._tag === "FormData") headers = remove$
|
|
7235
|
+
if (body._tag === "Empty" || body._tag === "FormData") headers = remove$3(headers, ["Content-type", "Content-length"]);
|
|
7236
7236
|
else {
|
|
7237
7237
|
const contentType = body.contentType;
|
|
7238
7238
|
if (contentType) headers = set$1(headers, "content-type", contentType);
|
|
@@ -7466,7 +7466,7 @@ const transformResponse = /* @__PURE__ */ dual(2, (self, f) => {
|
|
|
7466
7466
|
return makeWith((request) => f(client.postprocess(request)), client.preprocess);
|
|
7467
7467
|
});
|
|
7468
7468
|
/** @internal */
|
|
7469
|
-
const layerMergedContext$1 = (effect$1) => effect(tag, flatMap$1(context(), (context) => map$6(effect$1, (client) => transformResponse(client, mapInputContext((input) => merge$
|
|
7469
|
+
const layerMergedContext$1 = (effect$1) => effect(tag, flatMap$1(context(), (context) => map$6(effect$1, (client) => transformResponse(client, mapInputContext((input) => merge$3(context, input))))));
|
|
7470
7470
|
//#endregion
|
|
7471
7471
|
//#region ../../node_modules/.pnpm/@effect+platform@0.96.1_effect@3.21.2/node_modules/@effect/platform/dist/esm/HttpClient.js
|
|
7472
7472
|
/**
|
|
@@ -7867,7 +7867,7 @@ const removeFactory = (method) => {
|
|
|
7867
7867
|
force: options?.force ?? false
|
|
7868
7868
|
});
|
|
7869
7869
|
};
|
|
7870
|
-
const remove$
|
|
7870
|
+
const remove$2 = /* @__PURE__ */ removeFactory("remove");
|
|
7871
7871
|
const makeTempDirectoryScoped = /* @__PURE__ */ (() => {
|
|
7872
7872
|
const makeDirectory = /* @__PURE__ */ makeTempDirectoryFactory("makeTempDirectoryScoped");
|
|
7873
7873
|
const removeDirectory = /* @__PURE__ */ removeFactory("makeTempDirectoryScoped");
|
|
@@ -8034,7 +8034,7 @@ const symlink = /* @__PURE__ */ (() => {
|
|
|
8034
8034
|
const nodeSymlink = /* @__PURE__ */ effectify(NFS.symlink, /* @__PURE__ */ handleErrnoException("FileSystem", "symlink"), /* @__PURE__ */ handleBadArgument("symlink"));
|
|
8035
8035
|
return (target, path) => nodeSymlink(target, path);
|
|
8036
8036
|
})();
|
|
8037
|
-
const truncate
|
|
8037
|
+
const truncate = /* @__PURE__ */ (() => {
|
|
8038
8038
|
const nodeTruncate = /* @__PURE__ */ effectify(NFS.truncate, /* @__PURE__ */ handleErrnoException("FileSystem", "truncate"), /* @__PURE__ */ handleBadArgument("truncate"));
|
|
8039
8039
|
return (path, length) => nodeTruncate(path, length !== void 0 ? Number(length) : void 0);
|
|
8040
8040
|
})();
|
|
@@ -8112,11 +8112,11 @@ const layer$9 = /* @__PURE__ */ effect(FileSystem, /* @__PURE__ */ map$6(/* @__P
|
|
|
8112
8112
|
readFile,
|
|
8113
8113
|
readLink,
|
|
8114
8114
|
realPath,
|
|
8115
|
-
remove: remove$
|
|
8115
|
+
remove: remove$2,
|
|
8116
8116
|
rename,
|
|
8117
8117
|
stat,
|
|
8118
8118
|
symlink,
|
|
8119
|
-
truncate
|
|
8119
|
+
truncate,
|
|
8120
8120
|
utimes,
|
|
8121
8121
|
watch(path, options) {
|
|
8122
8122
|
return watch(backend, path, options);
|
|
@@ -8942,7 +8942,7 @@ const addPrettyLogger = (refs, fiberId) => {
|
|
|
8942
8942
|
return updateAs(refs, {
|
|
8943
8943
|
fiberId,
|
|
8944
8944
|
fiberRef: currentLoggers,
|
|
8945
|
-
value: loggers.pipe(remove$
|
|
8945
|
+
value: loggers.pipe(remove$4(defaultLogger), add$1(prettyLoggerDefault))
|
|
8946
8946
|
});
|
|
8947
8947
|
};
|
|
8948
8948
|
/**
|
|
@@ -9042,7 +9042,7 @@ function isInsideContainer() {
|
|
|
9042
9042
|
//#endregion
|
|
9043
9043
|
//#region ../../node_modules/.pnpm/is-wsl@3.1.1/node_modules/is-wsl/index.js
|
|
9044
9044
|
const isWsl = () => {
|
|
9045
|
-
if (
|
|
9045
|
+
if (process$1.platform !== "linux") return false;
|
|
9046
9046
|
if (os.release().toLowerCase().includes("microsoft")) {
|
|
9047
9047
|
if (isInsideContainer()) return false;
|
|
9048
9048
|
return true;
|
|
@@ -9053,11 +9053,11 @@ const isWsl = () => {
|
|
|
9053
9053
|
if (fs.existsSync("/proc/sys/fs/binfmt_misc/WSLInterop") || fs.existsSync("/run/WSL")) return !isInsideContainer();
|
|
9054
9054
|
return false;
|
|
9055
9055
|
};
|
|
9056
|
-
var is_wsl_default =
|
|
9056
|
+
var is_wsl_default = process$1.env.__IS_WSL_TEST__ ? isWsl : isWsl();
|
|
9057
9057
|
//#endregion
|
|
9058
9058
|
//#region ../../node_modules/.pnpm/powershell-utils@0.1.0/node_modules/powershell-utils/index.js
|
|
9059
9059
|
const execFile$2 = promisify(childProcess.execFile);
|
|
9060
|
-
const powerShellPath$1 = () => `${
|
|
9060
|
+
const powerShellPath$1 = () => `${process$1.env.SYSTEMROOT || process$1.env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
|
|
9061
9061
|
const executePowerShell = async (command, options = {}) => {
|
|
9062
9062
|
const { powerShellPath: psPath, ...execFileOptions } = options;
|
|
9063
9063
|
const encodedCommand = executePowerShell.encodeCommand(command);
|
|
@@ -9164,7 +9164,7 @@ function defineLazyProperty(object, propertyName, valueGetter) {
|
|
|
9164
9164
|
//#region ../../node_modules/.pnpm/default-browser-id@5.0.1/node_modules/default-browser-id/index.js
|
|
9165
9165
|
const execFileAsync$3 = promisify(execFile);
|
|
9166
9166
|
async function defaultBrowserId() {
|
|
9167
|
-
if (
|
|
9167
|
+
if (process$1.platform !== "darwin") throw new Error("macOS only");
|
|
9168
9168
|
const { stdout } = await execFileAsync$3("defaults", [
|
|
9169
9169
|
"read",
|
|
9170
9170
|
"com.apple.LaunchServices/com.apple.launchservices.secure",
|
|
@@ -9178,7 +9178,7 @@ async function defaultBrowserId() {
|
|
|
9178
9178
|
//#region ../../node_modules/.pnpm/run-applescript@7.1.0/node_modules/run-applescript/index.js
|
|
9179
9179
|
const execFileAsync$2 = promisify(execFile);
|
|
9180
9180
|
async function runAppleScript(script, { humanReadableOutput = true, signal } = {}) {
|
|
9181
|
-
if (
|
|
9181
|
+
if (process$1.platform !== "darwin") throw new Error("macOS only");
|
|
9182
9182
|
const outputArguments = humanReadableOutput ? [] : ["-ss"];
|
|
9183
9183
|
const execOptions = {};
|
|
9184
9184
|
if (signal) execOptions.signal = signal;
|
|
@@ -9289,14 +9289,14 @@ async function defaultBrowser$1(_execFileAsync = execFileAsync$1) {
|
|
|
9289
9289
|
const execFileAsync = promisify(execFile);
|
|
9290
9290
|
const titleize = (string) => string.toLowerCase().replaceAll(/(?:^|\s|-)\S/g, (x) => x.toUpperCase());
|
|
9291
9291
|
async function defaultBrowser() {
|
|
9292
|
-
if (
|
|
9292
|
+
if (process$1.platform === "darwin") {
|
|
9293
9293
|
const id = await defaultBrowserId();
|
|
9294
9294
|
return {
|
|
9295
9295
|
name: await bundleName(id),
|
|
9296
9296
|
id
|
|
9297
9297
|
};
|
|
9298
9298
|
}
|
|
9299
|
-
if (
|
|
9299
|
+
if (process$1.platform === "linux") {
|
|
9300
9300
|
const { stdout } = await execFileAsync("xdg-mime", [
|
|
9301
9301
|
"query",
|
|
9302
9302
|
"default",
|
|
@@ -9308,18 +9308,18 @@ async function defaultBrowser() {
|
|
|
9308
9308
|
id
|
|
9309
9309
|
};
|
|
9310
9310
|
}
|
|
9311
|
-
if (
|
|
9311
|
+
if (process$1.platform === "win32") return defaultBrowser$1();
|
|
9312
9312
|
throw new Error("Only macOS, Linux, and Windows are supported");
|
|
9313
9313
|
}
|
|
9314
9314
|
//#endregion
|
|
9315
9315
|
//#region ../../node_modules/.pnpm/is-in-ssh@1.0.0/node_modules/is-in-ssh/index.js
|
|
9316
|
-
const isInSsh = Boolean(
|
|
9316
|
+
const isInSsh = Boolean(process$1.env.SSH_CONNECTION || process$1.env.SSH_CLIENT || process$1.env.SSH_TTY);
|
|
9317
9317
|
//#endregion
|
|
9318
9318
|
//#region ../../node_modules/.pnpm/open@11.0.0/node_modules/open/index.js
|
|
9319
9319
|
const fallbackAttemptSymbol = Symbol("fallbackAttempt");
|
|
9320
9320
|
const __dirname = import.meta.url ? path.dirname(fileURLToPath(import.meta.url)) : "";
|
|
9321
9321
|
const localXdgOpenPath = path.join(__dirname, "xdg-open");
|
|
9322
|
-
const { platform: platform$1, arch: arch$1 } =
|
|
9322
|
+
const { platform: platform$1, arch: arch$1 } = process$1;
|
|
9323
9323
|
const tryEachApp = async (apps, opener) => {
|
|
9324
9324
|
if (apps.length === 0) return;
|
|
9325
9325
|
const errors = [];
|
|
@@ -9432,7 +9432,7 @@ const baseOpen = async (options) => {
|
|
|
9432
9432
|
await fs$1.access(localXdgOpenPath, constants.X_OK);
|
|
9433
9433
|
exeLocalXdgOpen = true;
|
|
9434
9434
|
} catch {}
|
|
9435
|
-
command =
|
|
9435
|
+
command = process$1.versions.electron ?? (platform$1 === "android" || isBundled || !exeLocalXdgOpen) ? "xdg-open" : localXdgOpenPath;
|
|
9436
9436
|
}
|
|
9437
9437
|
if (appArguments.length > 0) cliArguments.push(...appArguments);
|
|
9438
9438
|
if (!options.wait) {
|
|
@@ -11024,7 +11024,7 @@ var ht$1 = class extends m {
|
|
|
11024
11024
|
//#endregion
|
|
11025
11025
|
//#region ../../node_modules/.pnpm/@clack+prompts@1.4.0/node_modules/@clack/prompts/dist/index.mjs
|
|
11026
11026
|
function ee() {
|
|
11027
|
-
return
|
|
11027
|
+
return process$1.platform !== "win32" ? process$1.env.TERM !== "linux" : !!process$1.env.CI || !!process$1.env.WT_SESSION || !!process$1.env.TERMINUS_SUBLIME || process$1.env.ConEmuTask === "{cmd::Cmder}" || process$1.env.TERM_PROGRAM === "Terminus-Sublime" || process$1.env.TERM_PROGRAM === "vscode" || process$1.env.TERM === "xterm-256color" || process$1.env.TERM === "alacritty" || process$1.env.TERMINAL_EMULATOR === "JetBrains-JediTerm";
|
|
11028
11028
|
}
|
|
11029
11029
|
const tt = ee(), w = (t, i) => tt ? t : i, Tt = w("◆", "*"), at = w("■", "x"), ut = w("▲", "x"), H = w("◇", "o");
|
|
11030
11030
|
const $ = w("│", "|"), x = w("└", "—");
|
|
@@ -18214,7 +18214,7 @@ const replaceWith = (node, nodes) => {
|
|
|
18214
18214
|
node.remove();
|
|
18215
18215
|
}
|
|
18216
18216
|
};
|
|
18217
|
-
const remove = (prev, current, next) => {
|
|
18217
|
+
const remove$1 = (prev, current, next) => {
|
|
18218
18218
|
const { parentNode, nodeType } = current;
|
|
18219
18219
|
if (prev || next) {
|
|
18220
18220
|
setAdjacent(prev, next);
|
|
@@ -18265,7 +18265,7 @@ var CharacterData$1 = class extends Node$1 {
|
|
|
18265
18265
|
replaceWith(this, nodes);
|
|
18266
18266
|
}
|
|
18267
18267
|
remove() {
|
|
18268
|
-
remove(this[PREV], this, this[NEXT]);
|
|
18268
|
+
remove$1(this[PREV], this, this[NEXT]);
|
|
18269
18269
|
}
|
|
18270
18270
|
/* c8 ignore start */
|
|
18271
18271
|
get data() {
|
|
@@ -19656,13 +19656,13 @@ function reduceRules(a, b) {
|
|
|
19656
19656
|
//#endregion
|
|
19657
19657
|
//#region ../../node_modules/.pnpm/css-select@5.2.2/node_modules/css-select/lib/esm/index.js
|
|
19658
19658
|
const defaultEquals = (a, b) => a === b;
|
|
19659
|
-
const defaultOptions = {
|
|
19659
|
+
const defaultOptions$1 = {
|
|
19660
19660
|
adapter: esm_exports$1,
|
|
19661
19661
|
equals: defaultEquals
|
|
19662
19662
|
};
|
|
19663
19663
|
function convertOptionFormats(options) {
|
|
19664
19664
|
var _a, _b, _c, _d;
|
|
19665
|
-
const opts = options !== null && options !== void 0 ? options : defaultOptions;
|
|
19665
|
+
const opts = options !== null && options !== void 0 ? options : defaultOptions$1;
|
|
19666
19666
|
(_a = opts.adapter) !== null && _a !== void 0 || (opts.adapter = esm_exports$1);
|
|
19667
19667
|
(_b = opts.equals) !== null && _b !== void 0 || (opts.equals = (_d = (_c = opts.adapter) === null || _c === void 0 ? void 0 : _c.equals) !== null && _d !== void 0 ? _d : defaultEquals);
|
|
19668
19668
|
return opts;
|
|
@@ -20515,7 +20515,7 @@ var Element$1 = class extends ParentNode {
|
|
|
20515
20515
|
replaceWith(this, nodes);
|
|
20516
20516
|
}
|
|
20517
20517
|
remove() {
|
|
20518
|
-
remove(this[PREV], this, this[END][NEXT]);
|
|
20518
|
+
remove$1(this[PREV], this, this[END][NEXT]);
|
|
20519
20519
|
}
|
|
20520
20520
|
get id() {
|
|
20521
20521
|
return stringAttribute.get(this, "id");
|
|
@@ -28251,7 +28251,8 @@ const make$3 = (api, deps = {}) => {
|
|
|
28251
28251
|
const resolveCategoryRefs = (refs) => forEach$1(refs, (ref) => resolveCategory(ref), { concurrency: 1 });
|
|
28252
28252
|
const resolvePostReadPath = (slugOrId) => gen(function* () {
|
|
28253
28253
|
if (isSnowflakeId(slugOrId)) return `/posts/${slugOrId}`;
|
|
28254
|
-
const
|
|
28254
|
+
const env = yield* api.raw(`/posts/get-url/${encodeURIComponent(slugOrId)}`).pipe(mapError(mapApiError));
|
|
28255
|
+
const path = env?.data?.path ?? env?.path;
|
|
28255
28256
|
if (!path) return yield* fail$2(new ResourceNotFound({
|
|
28256
28257
|
message: `post not found: ${slugOrId}`,
|
|
28257
28258
|
kind: "post",
|
|
@@ -28262,7 +28263,8 @@ const make$3 = (api, deps = {}) => {
|
|
|
28262
28263
|
const resolvePostId = (slugOrId) => gen(function* () {
|
|
28263
28264
|
if (isSnowflakeId(slugOrId)) return slugOrId;
|
|
28264
28265
|
const apiPath = yield* resolvePostReadPath(slugOrId);
|
|
28265
|
-
const
|
|
28266
|
+
const env = yield* api.raw(apiPath).pipe(mapError(mapApiError));
|
|
28267
|
+
const id = env?.data?.id ?? env?.id;
|
|
28266
28268
|
if (!id) return yield* fail$2(new ResourceNotFound({
|
|
28267
28269
|
message: `post not found: ${slugOrId}`,
|
|
28268
28270
|
kind: "post",
|
|
@@ -28615,8 +28617,7 @@ const colorForCommentState = (raw, color) => {
|
|
|
28615
28617
|
if (label === "junk") return wrap(ANSI.red, label, color);
|
|
28616
28618
|
return formatStateBadge(label, color) ?? label;
|
|
28617
28619
|
};
|
|
28618
|
-
const
|
|
28619
|
-
const oneLine = (s) => s.replaceAll(/\s+/g, " ").trim();
|
|
28620
|
+
const indentBody = (s, indent = " ") => s.replace(/\s+$/, "").split("\n").map((line) => `${indent}${line}`).join("\n");
|
|
28620
28621
|
const authorOf = (doc) => {
|
|
28621
28622
|
const reader = asRecord(first(doc, "reader"));
|
|
28622
28623
|
if (Object.keys(reader).length > 0) {
|
|
@@ -28707,7 +28708,7 @@ const renderListItem = (doc, index, color) => {
|
|
|
28707
28708
|
if (created !== void 0) metaParts.push(tryFormatTimestamp(created, { style: "rel" }) ?? formatScalar(created));
|
|
28708
28709
|
if (metaParts.length > 0) lines.push(wrap(ANSI.dim, ` ${metaParts.join(" · ")}`, color));
|
|
28709
28710
|
const text = firstString(doc, "text");
|
|
28710
|
-
if (text) lines.push(
|
|
28711
|
+
if (text) lines.push(indentBody(text));
|
|
28711
28712
|
return lines.join("\n");
|
|
28712
28713
|
};
|
|
28713
28714
|
const renderCommentListReadable = (data, color) => {
|
|
@@ -32224,6 +32225,1777 @@ const profileCmd = make$13("profile").pipe(withDescription(help$2.description),
|
|
|
32224
32225
|
rm
|
|
32225
32226
|
]));
|
|
32226
32227
|
//#endregion
|
|
32228
|
+
//#region ../../node_modules/.pnpm/minisearch@7.2.0/node_modules/minisearch/dist/es/index.js
|
|
32229
|
+
/** @ignore */
|
|
32230
|
+
const ENTRIES = "ENTRIES";
|
|
32231
|
+
/** @ignore */
|
|
32232
|
+
const KEYS = "KEYS";
|
|
32233
|
+
/** @ignore */
|
|
32234
|
+
const VALUES = "VALUES";
|
|
32235
|
+
/** @ignore */
|
|
32236
|
+
const LEAF = "";
|
|
32237
|
+
/**
|
|
32238
|
+
* @private
|
|
32239
|
+
*/
|
|
32240
|
+
var TreeIterator = class {
|
|
32241
|
+
constructor(set, type) {
|
|
32242
|
+
const node = set._tree;
|
|
32243
|
+
const keys = Array.from(node.keys());
|
|
32244
|
+
this.set = set;
|
|
32245
|
+
this._type = type;
|
|
32246
|
+
this._path = keys.length > 0 ? [{
|
|
32247
|
+
node,
|
|
32248
|
+
keys
|
|
32249
|
+
}] : [];
|
|
32250
|
+
}
|
|
32251
|
+
next() {
|
|
32252
|
+
const value = this.dive();
|
|
32253
|
+
this.backtrack();
|
|
32254
|
+
return value;
|
|
32255
|
+
}
|
|
32256
|
+
dive() {
|
|
32257
|
+
if (this._path.length === 0) return {
|
|
32258
|
+
done: true,
|
|
32259
|
+
value: void 0
|
|
32260
|
+
};
|
|
32261
|
+
const { node, keys } = last$1(this._path);
|
|
32262
|
+
if (last$1(keys) === LEAF) return {
|
|
32263
|
+
done: false,
|
|
32264
|
+
value: this.result()
|
|
32265
|
+
};
|
|
32266
|
+
const child = node.get(last$1(keys));
|
|
32267
|
+
this._path.push({
|
|
32268
|
+
node: child,
|
|
32269
|
+
keys: Array.from(child.keys())
|
|
32270
|
+
});
|
|
32271
|
+
return this.dive();
|
|
32272
|
+
}
|
|
32273
|
+
backtrack() {
|
|
32274
|
+
if (this._path.length === 0) return;
|
|
32275
|
+
const keys = last$1(this._path).keys;
|
|
32276
|
+
keys.pop();
|
|
32277
|
+
if (keys.length > 0) return;
|
|
32278
|
+
this._path.pop();
|
|
32279
|
+
this.backtrack();
|
|
32280
|
+
}
|
|
32281
|
+
key() {
|
|
32282
|
+
return this.set._prefix + this._path.map(({ keys }) => last$1(keys)).filter((key) => key !== LEAF).join("");
|
|
32283
|
+
}
|
|
32284
|
+
value() {
|
|
32285
|
+
return last$1(this._path).node.get(LEAF);
|
|
32286
|
+
}
|
|
32287
|
+
result() {
|
|
32288
|
+
switch (this._type) {
|
|
32289
|
+
case VALUES: return this.value();
|
|
32290
|
+
case KEYS: return this.key();
|
|
32291
|
+
default: return [this.key(), this.value()];
|
|
32292
|
+
}
|
|
32293
|
+
}
|
|
32294
|
+
[Symbol.iterator]() {
|
|
32295
|
+
return this;
|
|
32296
|
+
}
|
|
32297
|
+
};
|
|
32298
|
+
const last$1 = (array) => {
|
|
32299
|
+
return array[array.length - 1];
|
|
32300
|
+
};
|
|
32301
|
+
/**
|
|
32302
|
+
* @ignore
|
|
32303
|
+
*/
|
|
32304
|
+
const fuzzySearch = (node, query, maxDistance) => {
|
|
32305
|
+
const results = /* @__PURE__ */ new Map();
|
|
32306
|
+
if (query === void 0) return results;
|
|
32307
|
+
const n = query.length + 1;
|
|
32308
|
+
const m = n + maxDistance;
|
|
32309
|
+
const matrix = new Uint8Array(m * n).fill(maxDistance + 1);
|
|
32310
|
+
for (let j = 0; j < n; ++j) matrix[j] = j;
|
|
32311
|
+
for (let i = 1; i < m; ++i) matrix[i * n] = i;
|
|
32312
|
+
recurse(node, query, maxDistance, results, matrix, 1, n, "");
|
|
32313
|
+
return results;
|
|
32314
|
+
};
|
|
32315
|
+
const recurse = (node, query, maxDistance, results, matrix, m, n, prefix) => {
|
|
32316
|
+
const offset = m * n;
|
|
32317
|
+
key: for (const key of node.keys()) if (key === LEAF) {
|
|
32318
|
+
const distance = matrix[offset - 1];
|
|
32319
|
+
if (distance <= maxDistance) results.set(prefix, [node.get(key), distance]);
|
|
32320
|
+
} else {
|
|
32321
|
+
let i = m;
|
|
32322
|
+
for (let pos = 0; pos < key.length; ++pos, ++i) {
|
|
32323
|
+
const char = key[pos];
|
|
32324
|
+
const thisRowOffset = n * i;
|
|
32325
|
+
const prevRowOffset = thisRowOffset - n;
|
|
32326
|
+
let minDistance = matrix[thisRowOffset];
|
|
32327
|
+
const jmin = Math.max(0, i - maxDistance - 1);
|
|
32328
|
+
const jmax = Math.min(n - 1, i + maxDistance);
|
|
32329
|
+
for (let j = jmin; j < jmax; ++j) {
|
|
32330
|
+
const different = char !== query[j];
|
|
32331
|
+
const rpl = matrix[prevRowOffset + j] + +different;
|
|
32332
|
+
const del = matrix[prevRowOffset + j + 1] + 1;
|
|
32333
|
+
const ins = matrix[thisRowOffset + j] + 1;
|
|
32334
|
+
const dist = matrix[thisRowOffset + j + 1] = Math.min(rpl, del, ins);
|
|
32335
|
+
if (dist < minDistance) minDistance = dist;
|
|
32336
|
+
}
|
|
32337
|
+
if (minDistance > maxDistance) continue key;
|
|
32338
|
+
}
|
|
32339
|
+
recurse(node.get(key), query, maxDistance, results, matrix, i, n, prefix + key);
|
|
32340
|
+
}
|
|
32341
|
+
};
|
|
32342
|
+
/**
|
|
32343
|
+
* A class implementing the same interface as a standard JavaScript
|
|
32344
|
+
* [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
|
|
32345
|
+
* with string keys, but adding support for efficiently searching entries with
|
|
32346
|
+
* prefix or fuzzy search. This class is used internally by {@link MiniSearch}
|
|
32347
|
+
* as the inverted index data structure. The implementation is a radix tree
|
|
32348
|
+
* (compressed prefix tree).
|
|
32349
|
+
*
|
|
32350
|
+
* Since this class can be of general utility beyond _MiniSearch_, it is
|
|
32351
|
+
* exported by the `minisearch` package and can be imported (or required) as
|
|
32352
|
+
* `minisearch/SearchableMap`.
|
|
32353
|
+
*
|
|
32354
|
+
* @typeParam T The type of the values stored in the map.
|
|
32355
|
+
*/
|
|
32356
|
+
var SearchableMap = class SearchableMap {
|
|
32357
|
+
/**
|
|
32358
|
+
* The constructor is normally called without arguments, creating an empty
|
|
32359
|
+
* map. In order to create a {@link SearchableMap} from an iterable or from an
|
|
32360
|
+
* object, check {@link SearchableMap.from} and {@link
|
|
32361
|
+
* SearchableMap.fromObject}.
|
|
32362
|
+
*
|
|
32363
|
+
* The constructor arguments are for internal use, when creating derived
|
|
32364
|
+
* mutable views of a map at a prefix.
|
|
32365
|
+
*/
|
|
32366
|
+
constructor(tree = /* @__PURE__ */ new Map(), prefix = "") {
|
|
32367
|
+
this._size = void 0;
|
|
32368
|
+
this._tree = tree;
|
|
32369
|
+
this._prefix = prefix;
|
|
32370
|
+
}
|
|
32371
|
+
/**
|
|
32372
|
+
* Creates and returns a mutable view of this {@link SearchableMap},
|
|
32373
|
+
* containing only entries that share the given prefix.
|
|
32374
|
+
*
|
|
32375
|
+
* ### Usage:
|
|
32376
|
+
*
|
|
32377
|
+
* ```javascript
|
|
32378
|
+
* let map = new SearchableMap()
|
|
32379
|
+
* map.set("unicorn", 1)
|
|
32380
|
+
* map.set("universe", 2)
|
|
32381
|
+
* map.set("university", 3)
|
|
32382
|
+
* map.set("unique", 4)
|
|
32383
|
+
* map.set("hello", 5)
|
|
32384
|
+
*
|
|
32385
|
+
* let uni = map.atPrefix("uni")
|
|
32386
|
+
* uni.get("unique") // => 4
|
|
32387
|
+
* uni.get("unicorn") // => 1
|
|
32388
|
+
* uni.get("hello") // => undefined
|
|
32389
|
+
*
|
|
32390
|
+
* let univer = map.atPrefix("univer")
|
|
32391
|
+
* univer.get("unique") // => undefined
|
|
32392
|
+
* univer.get("universe") // => 2
|
|
32393
|
+
* univer.get("university") // => 3
|
|
32394
|
+
* ```
|
|
32395
|
+
*
|
|
32396
|
+
* @param prefix The prefix
|
|
32397
|
+
* @return A {@link SearchableMap} representing a mutable view of the original
|
|
32398
|
+
* Map at the given prefix
|
|
32399
|
+
*/
|
|
32400
|
+
atPrefix(prefix) {
|
|
32401
|
+
if (!prefix.startsWith(this._prefix)) throw new Error("Mismatched prefix");
|
|
32402
|
+
const [node, path] = trackDown(this._tree, prefix.slice(this._prefix.length));
|
|
32403
|
+
if (node === void 0) {
|
|
32404
|
+
const [parentNode, key] = last(path);
|
|
32405
|
+
for (const k of parentNode.keys()) if (k !== LEAF && k.startsWith(key)) {
|
|
32406
|
+
const node = /* @__PURE__ */ new Map();
|
|
32407
|
+
node.set(k.slice(key.length), parentNode.get(k));
|
|
32408
|
+
return new SearchableMap(node, prefix);
|
|
32409
|
+
}
|
|
32410
|
+
}
|
|
32411
|
+
return new SearchableMap(node, prefix);
|
|
32412
|
+
}
|
|
32413
|
+
/**
|
|
32414
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
|
|
32415
|
+
*/
|
|
32416
|
+
clear() {
|
|
32417
|
+
this._size = void 0;
|
|
32418
|
+
this._tree.clear();
|
|
32419
|
+
}
|
|
32420
|
+
/**
|
|
32421
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete
|
|
32422
|
+
* @param key Key to delete
|
|
32423
|
+
*/
|
|
32424
|
+
delete(key) {
|
|
32425
|
+
this._size = void 0;
|
|
32426
|
+
return remove(this._tree, key);
|
|
32427
|
+
}
|
|
32428
|
+
/**
|
|
32429
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries
|
|
32430
|
+
* @return An iterator iterating through `[key, value]` entries.
|
|
32431
|
+
*/
|
|
32432
|
+
entries() {
|
|
32433
|
+
return new TreeIterator(this, ENTRIES);
|
|
32434
|
+
}
|
|
32435
|
+
/**
|
|
32436
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach
|
|
32437
|
+
* @param fn Iteration function
|
|
32438
|
+
*/
|
|
32439
|
+
forEach(fn) {
|
|
32440
|
+
for (const [key, value] of this) fn(key, value, this);
|
|
32441
|
+
}
|
|
32442
|
+
/**
|
|
32443
|
+
* Returns a Map of all the entries that have a key within the given edit
|
|
32444
|
+
* distance from the search key. The keys of the returned Map are the matching
|
|
32445
|
+
* keys, while the values are two-element arrays where the first element is
|
|
32446
|
+
* the value associated to the key, and the second is the edit distance of the
|
|
32447
|
+
* key to the search key.
|
|
32448
|
+
*
|
|
32449
|
+
* ### Usage:
|
|
32450
|
+
*
|
|
32451
|
+
* ```javascript
|
|
32452
|
+
* let map = new SearchableMap()
|
|
32453
|
+
* map.set('hello', 'world')
|
|
32454
|
+
* map.set('hell', 'yeah')
|
|
32455
|
+
* map.set('ciao', 'mondo')
|
|
32456
|
+
*
|
|
32457
|
+
* // Get all entries that match the key 'hallo' with a maximum edit distance of 2
|
|
32458
|
+
* map.fuzzyGet('hallo', 2)
|
|
32459
|
+
* // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] }
|
|
32460
|
+
*
|
|
32461
|
+
* // In the example, the "hello" key has value "world" and edit distance of 1
|
|
32462
|
+
* // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2
|
|
32463
|
+
* // (change "e" to "a", delete "o")
|
|
32464
|
+
* ```
|
|
32465
|
+
*
|
|
32466
|
+
* @param key The search key
|
|
32467
|
+
* @param maxEditDistance The maximum edit distance (Levenshtein)
|
|
32468
|
+
* @return A Map of the matching keys to their value and edit distance
|
|
32469
|
+
*/
|
|
32470
|
+
fuzzyGet(key, maxEditDistance) {
|
|
32471
|
+
return fuzzySearch(this._tree, key, maxEditDistance);
|
|
32472
|
+
}
|
|
32473
|
+
/**
|
|
32474
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
|
|
32475
|
+
* @param key Key to get
|
|
32476
|
+
* @return Value associated to the key, or `undefined` if the key is not
|
|
32477
|
+
* found.
|
|
32478
|
+
*/
|
|
32479
|
+
get(key) {
|
|
32480
|
+
const node = lookup(this._tree, key);
|
|
32481
|
+
return node !== void 0 ? node.get(LEAF) : void 0;
|
|
32482
|
+
}
|
|
32483
|
+
/**
|
|
32484
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
|
|
32485
|
+
* @param key Key
|
|
32486
|
+
* @return True if the key is in the map, false otherwise
|
|
32487
|
+
*/
|
|
32488
|
+
has(key) {
|
|
32489
|
+
const node = lookup(this._tree, key);
|
|
32490
|
+
return node !== void 0 && node.has(LEAF);
|
|
32491
|
+
}
|
|
32492
|
+
/**
|
|
32493
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
|
|
32494
|
+
* @return An `Iterable` iterating through keys
|
|
32495
|
+
*/
|
|
32496
|
+
keys() {
|
|
32497
|
+
return new TreeIterator(this, KEYS);
|
|
32498
|
+
}
|
|
32499
|
+
/**
|
|
32500
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set
|
|
32501
|
+
* @param key Key to set
|
|
32502
|
+
* @param value Value to associate to the key
|
|
32503
|
+
* @return The {@link SearchableMap} itself, to allow chaining
|
|
32504
|
+
*/
|
|
32505
|
+
set(key, value) {
|
|
32506
|
+
if (typeof key !== "string") throw new Error("key must be a string");
|
|
32507
|
+
this._size = void 0;
|
|
32508
|
+
createPath(this._tree, key).set(LEAF, value);
|
|
32509
|
+
return this;
|
|
32510
|
+
}
|
|
32511
|
+
/**
|
|
32512
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size
|
|
32513
|
+
*/
|
|
32514
|
+
get size() {
|
|
32515
|
+
if (this._size) return this._size;
|
|
32516
|
+
/** @ignore */
|
|
32517
|
+
this._size = 0;
|
|
32518
|
+
const iter = this.entries();
|
|
32519
|
+
while (!iter.next().done) this._size += 1;
|
|
32520
|
+
return this._size;
|
|
32521
|
+
}
|
|
32522
|
+
/**
|
|
32523
|
+
* Updates the value at the given key using the provided function. The function
|
|
32524
|
+
* is called with the current value at the key, and its return value is used as
|
|
32525
|
+
* the new value to be set.
|
|
32526
|
+
*
|
|
32527
|
+
* ### Example:
|
|
32528
|
+
*
|
|
32529
|
+
* ```javascript
|
|
32530
|
+
* // Increment the current value by one
|
|
32531
|
+
* searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1)
|
|
32532
|
+
* ```
|
|
32533
|
+
*
|
|
32534
|
+
* If the value at the given key is or will be an object, it might not require
|
|
32535
|
+
* re-assignment. In that case it is better to use `fetch()`, because it is
|
|
32536
|
+
* faster.
|
|
32537
|
+
*
|
|
32538
|
+
* @param key The key to update
|
|
32539
|
+
* @param fn The function used to compute the new value from the current one
|
|
32540
|
+
* @return The {@link SearchableMap} itself, to allow chaining
|
|
32541
|
+
*/
|
|
32542
|
+
update(key, fn) {
|
|
32543
|
+
if (typeof key !== "string") throw new Error("key must be a string");
|
|
32544
|
+
this._size = void 0;
|
|
32545
|
+
const node = createPath(this._tree, key);
|
|
32546
|
+
node.set(LEAF, fn(node.get(LEAF)));
|
|
32547
|
+
return this;
|
|
32548
|
+
}
|
|
32549
|
+
/**
|
|
32550
|
+
* Fetches the value of the given key. If the value does not exist, calls the
|
|
32551
|
+
* given function to create a new value, which is inserted at the given key
|
|
32552
|
+
* and subsequently returned.
|
|
32553
|
+
*
|
|
32554
|
+
* ### Example:
|
|
32555
|
+
*
|
|
32556
|
+
* ```javascript
|
|
32557
|
+
* const map = searchableMap.fetch('somekey', () => new Map())
|
|
32558
|
+
* map.set('foo', 'bar')
|
|
32559
|
+
* ```
|
|
32560
|
+
*
|
|
32561
|
+
* @param key The key to update
|
|
32562
|
+
* @param initial A function that creates a new value if the key does not exist
|
|
32563
|
+
* @return The existing or new value at the given key
|
|
32564
|
+
*/
|
|
32565
|
+
fetch(key, initial) {
|
|
32566
|
+
if (typeof key !== "string") throw new Error("key must be a string");
|
|
32567
|
+
this._size = void 0;
|
|
32568
|
+
const node = createPath(this._tree, key);
|
|
32569
|
+
let value = node.get(LEAF);
|
|
32570
|
+
if (value === void 0) node.set(LEAF, value = initial());
|
|
32571
|
+
return value;
|
|
32572
|
+
}
|
|
32573
|
+
/**
|
|
32574
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
|
|
32575
|
+
* @return An `Iterable` iterating through values.
|
|
32576
|
+
*/
|
|
32577
|
+
values() {
|
|
32578
|
+
return new TreeIterator(this, VALUES);
|
|
32579
|
+
}
|
|
32580
|
+
/**
|
|
32581
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator
|
|
32582
|
+
*/
|
|
32583
|
+
[Symbol.iterator]() {
|
|
32584
|
+
return this.entries();
|
|
32585
|
+
}
|
|
32586
|
+
/**
|
|
32587
|
+
* Creates a {@link SearchableMap} from an `Iterable` of entries
|
|
32588
|
+
*
|
|
32589
|
+
* @param entries Entries to be inserted in the {@link SearchableMap}
|
|
32590
|
+
* @return A new {@link SearchableMap} with the given entries
|
|
32591
|
+
*/
|
|
32592
|
+
static from(entries) {
|
|
32593
|
+
const tree = new SearchableMap();
|
|
32594
|
+
for (const [key, value] of entries) tree.set(key, value);
|
|
32595
|
+
return tree;
|
|
32596
|
+
}
|
|
32597
|
+
/**
|
|
32598
|
+
* Creates a {@link SearchableMap} from the iterable properties of a JavaScript object
|
|
32599
|
+
*
|
|
32600
|
+
* @param object Object of entries for the {@link SearchableMap}
|
|
32601
|
+
* @return A new {@link SearchableMap} with the given entries
|
|
32602
|
+
*/
|
|
32603
|
+
static fromObject(object) {
|
|
32604
|
+
return SearchableMap.from(Object.entries(object));
|
|
32605
|
+
}
|
|
32606
|
+
};
|
|
32607
|
+
const trackDown = (tree, key, path = []) => {
|
|
32608
|
+
if (key.length === 0 || tree == null) return [tree, path];
|
|
32609
|
+
for (const k of tree.keys()) if (k !== LEAF && key.startsWith(k)) {
|
|
32610
|
+
path.push([tree, k]);
|
|
32611
|
+
return trackDown(tree.get(k), key.slice(k.length), path);
|
|
32612
|
+
}
|
|
32613
|
+
path.push([tree, key]);
|
|
32614
|
+
return trackDown(void 0, "", path);
|
|
32615
|
+
};
|
|
32616
|
+
const lookup = (tree, key) => {
|
|
32617
|
+
if (key.length === 0 || tree == null) return tree;
|
|
32618
|
+
for (const k of tree.keys()) if (k !== LEAF && key.startsWith(k)) return lookup(tree.get(k), key.slice(k.length));
|
|
32619
|
+
};
|
|
32620
|
+
const createPath = (node, key) => {
|
|
32621
|
+
const keyLength = key.length;
|
|
32622
|
+
outer: for (let pos = 0; node && pos < keyLength;) {
|
|
32623
|
+
for (const k of node.keys()) if (k !== LEAF && key[pos] === k[0]) {
|
|
32624
|
+
const len = Math.min(keyLength - pos, k.length);
|
|
32625
|
+
let offset = 1;
|
|
32626
|
+
while (offset < len && key[pos + offset] === k[offset]) ++offset;
|
|
32627
|
+
const child = node.get(k);
|
|
32628
|
+
if (offset === k.length) node = child;
|
|
32629
|
+
else {
|
|
32630
|
+
const intermediate = /* @__PURE__ */ new Map();
|
|
32631
|
+
intermediate.set(k.slice(offset), child);
|
|
32632
|
+
node.set(key.slice(pos, pos + offset), intermediate);
|
|
32633
|
+
node.delete(k);
|
|
32634
|
+
node = intermediate;
|
|
32635
|
+
}
|
|
32636
|
+
pos += offset;
|
|
32637
|
+
continue outer;
|
|
32638
|
+
}
|
|
32639
|
+
const child = /* @__PURE__ */ new Map();
|
|
32640
|
+
node.set(key.slice(pos), child);
|
|
32641
|
+
return child;
|
|
32642
|
+
}
|
|
32643
|
+
return node;
|
|
32644
|
+
};
|
|
32645
|
+
const remove = (tree, key) => {
|
|
32646
|
+
const [node, path] = trackDown(tree, key);
|
|
32647
|
+
if (node === void 0) return;
|
|
32648
|
+
node.delete(LEAF);
|
|
32649
|
+
if (node.size === 0) cleanup(path);
|
|
32650
|
+
else if (node.size === 1) {
|
|
32651
|
+
const [key, value] = node.entries().next().value;
|
|
32652
|
+
merge(path, key, value);
|
|
32653
|
+
}
|
|
32654
|
+
};
|
|
32655
|
+
const cleanup = (path) => {
|
|
32656
|
+
if (path.length === 0) return;
|
|
32657
|
+
const [node, key] = last(path);
|
|
32658
|
+
node.delete(key);
|
|
32659
|
+
if (node.size === 0) cleanup(path.slice(0, -1));
|
|
32660
|
+
else if (node.size === 1) {
|
|
32661
|
+
const [key, value] = node.entries().next().value;
|
|
32662
|
+
if (key !== LEAF) merge(path.slice(0, -1), key, value);
|
|
32663
|
+
}
|
|
32664
|
+
};
|
|
32665
|
+
const merge = (path, key, value) => {
|
|
32666
|
+
if (path.length === 0) return;
|
|
32667
|
+
const [node, nodeKey] = last(path);
|
|
32668
|
+
node.set(nodeKey + key, value);
|
|
32669
|
+
node.delete(nodeKey);
|
|
32670
|
+
};
|
|
32671
|
+
const last = (array) => {
|
|
32672
|
+
return array[array.length - 1];
|
|
32673
|
+
};
|
|
32674
|
+
const OR = "or";
|
|
32675
|
+
const AND = "and";
|
|
32676
|
+
const AND_NOT = "and_not";
|
|
32677
|
+
/**
|
|
32678
|
+
* {@link MiniSearch} is the main entrypoint class, implementing a full-text
|
|
32679
|
+
* search engine in memory.
|
|
32680
|
+
*
|
|
32681
|
+
* @typeParam T The type of the documents being indexed.
|
|
32682
|
+
*
|
|
32683
|
+
* ### Basic example:
|
|
32684
|
+
*
|
|
32685
|
+
* ```javascript
|
|
32686
|
+
* const documents = [
|
|
32687
|
+
* {
|
|
32688
|
+
* id: 1,
|
|
32689
|
+
* title: 'Moby Dick',
|
|
32690
|
+
* text: 'Call me Ishmael. Some years ago...',
|
|
32691
|
+
* category: 'fiction'
|
|
32692
|
+
* },
|
|
32693
|
+
* {
|
|
32694
|
+
* id: 2,
|
|
32695
|
+
* title: 'Zen and the Art of Motorcycle Maintenance',
|
|
32696
|
+
* text: 'I can see by my watch...',
|
|
32697
|
+
* category: 'fiction'
|
|
32698
|
+
* },
|
|
32699
|
+
* {
|
|
32700
|
+
* id: 3,
|
|
32701
|
+
* title: 'Neuromancer',
|
|
32702
|
+
* text: 'The sky above the port was...',
|
|
32703
|
+
* category: 'fiction'
|
|
32704
|
+
* },
|
|
32705
|
+
* {
|
|
32706
|
+
* id: 4,
|
|
32707
|
+
* title: 'Zen and the Art of Archery',
|
|
32708
|
+
* text: 'At first sight it must seem...',
|
|
32709
|
+
* category: 'non-fiction'
|
|
32710
|
+
* },
|
|
32711
|
+
* // ...and more
|
|
32712
|
+
* ]
|
|
32713
|
+
*
|
|
32714
|
+
* // Create a search engine that indexes the 'title' and 'text' fields for
|
|
32715
|
+
* // full-text search. Search results will include 'title' and 'category' (plus the
|
|
32716
|
+
* // id field, that is always stored and returned)
|
|
32717
|
+
* const miniSearch = new MiniSearch({
|
|
32718
|
+
* fields: ['title', 'text'],
|
|
32719
|
+
* storeFields: ['title', 'category']
|
|
32720
|
+
* })
|
|
32721
|
+
*
|
|
32722
|
+
* // Add documents to the index
|
|
32723
|
+
* miniSearch.addAll(documents)
|
|
32724
|
+
*
|
|
32725
|
+
* // Search for documents:
|
|
32726
|
+
* let results = miniSearch.search('zen art motorcycle')
|
|
32727
|
+
* // => [
|
|
32728
|
+
* // { id: 2, title: 'Zen and the Art of Motorcycle Maintenance', category: 'fiction', score: 2.77258 },
|
|
32729
|
+
* // { id: 4, title: 'Zen and the Art of Archery', category: 'non-fiction', score: 1.38629 }
|
|
32730
|
+
* // ]
|
|
32731
|
+
* ```
|
|
32732
|
+
*/
|
|
32733
|
+
var MiniSearch = class MiniSearch {
|
|
32734
|
+
/**
|
|
32735
|
+
* @param options Configuration options
|
|
32736
|
+
*
|
|
32737
|
+
* ### Examples:
|
|
32738
|
+
*
|
|
32739
|
+
* ```javascript
|
|
32740
|
+
* // Create a search engine that indexes the 'title' and 'text' fields of your
|
|
32741
|
+
* // documents:
|
|
32742
|
+
* const miniSearch = new MiniSearch({ fields: ['title', 'text'] })
|
|
32743
|
+
* ```
|
|
32744
|
+
*
|
|
32745
|
+
* ### ID Field:
|
|
32746
|
+
*
|
|
32747
|
+
* ```javascript
|
|
32748
|
+
* // Your documents are assumed to include a unique 'id' field, but if you want
|
|
32749
|
+
* // to use a different field for document identification, you can set the
|
|
32750
|
+
* // 'idField' option:
|
|
32751
|
+
* const miniSearch = new MiniSearch({ idField: 'key', fields: ['title', 'text'] })
|
|
32752
|
+
* ```
|
|
32753
|
+
*
|
|
32754
|
+
* ### Options and defaults:
|
|
32755
|
+
*
|
|
32756
|
+
* ```javascript
|
|
32757
|
+
* // The full set of options (here with their default value) is:
|
|
32758
|
+
* const miniSearch = new MiniSearch({
|
|
32759
|
+
* // idField: field that uniquely identifies a document
|
|
32760
|
+
* idField: 'id',
|
|
32761
|
+
*
|
|
32762
|
+
* // extractField: function used to get the value of a field in a document.
|
|
32763
|
+
* // By default, it assumes the document is a flat object with field names as
|
|
32764
|
+
* // property keys and field values as string property values, but custom logic
|
|
32765
|
+
* // can be implemented by setting this option to a custom extractor function.
|
|
32766
|
+
* extractField: (document, fieldName) => document[fieldName],
|
|
32767
|
+
*
|
|
32768
|
+
* // tokenize: function used to split fields into individual terms. By
|
|
32769
|
+
* // default, it is also used to tokenize search queries, unless a specific
|
|
32770
|
+
* // `tokenize` search option is supplied. When tokenizing an indexed field,
|
|
32771
|
+
* // the field name is passed as the second argument.
|
|
32772
|
+
* tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),
|
|
32773
|
+
*
|
|
32774
|
+
* // processTerm: function used to process each tokenized term before
|
|
32775
|
+
* // indexing. It can be used for stemming and normalization. Return a falsy
|
|
32776
|
+
* // value in order to discard a term. By default, it is also used to process
|
|
32777
|
+
* // search queries, unless a specific `processTerm` option is supplied as a
|
|
32778
|
+
* // search option. When processing a term from a indexed field, the field
|
|
32779
|
+
* // name is passed as the second argument.
|
|
32780
|
+
* processTerm: (term, _fieldName) => term.toLowerCase(),
|
|
32781
|
+
*
|
|
32782
|
+
* // searchOptions: default search options, see the `search` method for
|
|
32783
|
+
* // details
|
|
32784
|
+
* searchOptions: undefined,
|
|
32785
|
+
*
|
|
32786
|
+
* // fields: document fields to be indexed. Mandatory, but not set by default
|
|
32787
|
+
* fields: undefined
|
|
32788
|
+
*
|
|
32789
|
+
* // storeFields: document fields to be stored and returned as part of the
|
|
32790
|
+
* // search results.
|
|
32791
|
+
* storeFields: []
|
|
32792
|
+
* })
|
|
32793
|
+
* ```
|
|
32794
|
+
*/
|
|
32795
|
+
constructor(options) {
|
|
32796
|
+
if ((options === null || options === void 0 ? void 0 : options.fields) == null) throw new Error("MiniSearch: option \"fields\" must be provided");
|
|
32797
|
+
const autoVacuum = options.autoVacuum == null || options.autoVacuum === true ? defaultAutoVacuumOptions : options.autoVacuum;
|
|
32798
|
+
this._options = {
|
|
32799
|
+
...defaultOptions,
|
|
32800
|
+
...options,
|
|
32801
|
+
autoVacuum,
|
|
32802
|
+
searchOptions: {
|
|
32803
|
+
...defaultSearchOptions,
|
|
32804
|
+
...options.searchOptions || {}
|
|
32805
|
+
},
|
|
32806
|
+
autoSuggestOptions: {
|
|
32807
|
+
...defaultAutoSuggestOptions,
|
|
32808
|
+
...options.autoSuggestOptions || {}
|
|
32809
|
+
}
|
|
32810
|
+
};
|
|
32811
|
+
this._index = new SearchableMap();
|
|
32812
|
+
this._documentCount = 0;
|
|
32813
|
+
this._documentIds = /* @__PURE__ */ new Map();
|
|
32814
|
+
this._idToShortId = /* @__PURE__ */ new Map();
|
|
32815
|
+
this._fieldIds = {};
|
|
32816
|
+
this._fieldLength = /* @__PURE__ */ new Map();
|
|
32817
|
+
this._avgFieldLength = [];
|
|
32818
|
+
this._nextId = 0;
|
|
32819
|
+
this._storedFields = /* @__PURE__ */ new Map();
|
|
32820
|
+
this._dirtCount = 0;
|
|
32821
|
+
this._currentVacuum = null;
|
|
32822
|
+
this._enqueuedVacuum = null;
|
|
32823
|
+
this._enqueuedVacuumConditions = defaultVacuumConditions;
|
|
32824
|
+
this.addFields(this._options.fields);
|
|
32825
|
+
}
|
|
32826
|
+
/**
|
|
32827
|
+
* Adds a document to the index
|
|
32828
|
+
*
|
|
32829
|
+
* @param document The document to be indexed
|
|
32830
|
+
*/
|
|
32831
|
+
add(document) {
|
|
32832
|
+
const { extractField, stringifyField, tokenize, processTerm, fields, idField } = this._options;
|
|
32833
|
+
const id = extractField(document, idField);
|
|
32834
|
+
if (id == null) throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
|
|
32835
|
+
if (this._idToShortId.has(id)) throw new Error(`MiniSearch: duplicate ID ${id}`);
|
|
32836
|
+
const shortDocumentId = this.addDocumentId(id);
|
|
32837
|
+
this.saveStoredFields(shortDocumentId, document);
|
|
32838
|
+
for (const field of fields) {
|
|
32839
|
+
const fieldValue = extractField(document, field);
|
|
32840
|
+
if (fieldValue == null) continue;
|
|
32841
|
+
const tokens = tokenize(stringifyField(fieldValue, field), field);
|
|
32842
|
+
const fieldId = this._fieldIds[field];
|
|
32843
|
+
const uniqueTerms = new Set(tokens).size;
|
|
32844
|
+
this.addFieldLength(shortDocumentId, fieldId, this._documentCount - 1, uniqueTerms);
|
|
32845
|
+
for (const term of tokens) {
|
|
32846
|
+
const processedTerm = processTerm(term, field);
|
|
32847
|
+
if (Array.isArray(processedTerm)) for (const t of processedTerm) this.addTerm(fieldId, shortDocumentId, t);
|
|
32848
|
+
else if (processedTerm) this.addTerm(fieldId, shortDocumentId, processedTerm);
|
|
32849
|
+
}
|
|
32850
|
+
}
|
|
32851
|
+
}
|
|
32852
|
+
/**
|
|
32853
|
+
* Adds all the given documents to the index
|
|
32854
|
+
*
|
|
32855
|
+
* @param documents An array of documents to be indexed
|
|
32856
|
+
*/
|
|
32857
|
+
addAll(documents) {
|
|
32858
|
+
for (const document of documents) this.add(document);
|
|
32859
|
+
}
|
|
32860
|
+
/**
|
|
32861
|
+
* Adds all the given documents to the index asynchronously.
|
|
32862
|
+
*
|
|
32863
|
+
* Returns a promise that resolves (to `undefined`) when the indexing is done.
|
|
32864
|
+
* This method is useful when index many documents, to avoid blocking the main
|
|
32865
|
+
* thread. The indexing is performed asynchronously and in chunks.
|
|
32866
|
+
*
|
|
32867
|
+
* @param documents An array of documents to be indexed
|
|
32868
|
+
* @param options Configuration options
|
|
32869
|
+
* @return A promise resolving to `undefined` when the indexing is done
|
|
32870
|
+
*/
|
|
32871
|
+
addAllAsync(documents, options = {}) {
|
|
32872
|
+
const { chunkSize = 10 } = options;
|
|
32873
|
+
const acc = {
|
|
32874
|
+
chunk: [],
|
|
32875
|
+
promise: Promise.resolve()
|
|
32876
|
+
};
|
|
32877
|
+
const { chunk, promise } = documents.reduce(({ chunk, promise }, document, i) => {
|
|
32878
|
+
chunk.push(document);
|
|
32879
|
+
if ((i + 1) % chunkSize === 0) return {
|
|
32880
|
+
chunk: [],
|
|
32881
|
+
promise: promise.then(() => new Promise((resolve) => setTimeout(resolve, 0))).then(() => this.addAll(chunk))
|
|
32882
|
+
};
|
|
32883
|
+
else return {
|
|
32884
|
+
chunk,
|
|
32885
|
+
promise
|
|
32886
|
+
};
|
|
32887
|
+
}, acc);
|
|
32888
|
+
return promise.then(() => this.addAll(chunk));
|
|
32889
|
+
}
|
|
32890
|
+
/**
|
|
32891
|
+
* Removes the given document from the index.
|
|
32892
|
+
*
|
|
32893
|
+
* The document to remove must NOT have changed between indexing and removal,
|
|
32894
|
+
* otherwise the index will be corrupted.
|
|
32895
|
+
*
|
|
32896
|
+
* This method requires passing the full document to be removed (not just the
|
|
32897
|
+
* ID), and immediately removes the document from the inverted index, allowing
|
|
32898
|
+
* memory to be released. A convenient alternative is {@link
|
|
32899
|
+
* MiniSearch#discard}, which needs only the document ID, and has the same
|
|
32900
|
+
* visible effect, but delays cleaning up the index until the next vacuuming.
|
|
32901
|
+
*
|
|
32902
|
+
* @param document The document to be removed
|
|
32903
|
+
*/
|
|
32904
|
+
remove(document) {
|
|
32905
|
+
const { tokenize, processTerm, extractField, stringifyField, fields, idField } = this._options;
|
|
32906
|
+
const id = extractField(document, idField);
|
|
32907
|
+
if (id == null) throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
|
|
32908
|
+
const shortId = this._idToShortId.get(id);
|
|
32909
|
+
if (shortId == null) throw new Error(`MiniSearch: cannot remove document with ID ${id}: it is not in the index`);
|
|
32910
|
+
for (const field of fields) {
|
|
32911
|
+
const fieldValue = extractField(document, field);
|
|
32912
|
+
if (fieldValue == null) continue;
|
|
32913
|
+
const tokens = tokenize(stringifyField(fieldValue, field), field);
|
|
32914
|
+
const fieldId = this._fieldIds[field];
|
|
32915
|
+
const uniqueTerms = new Set(tokens).size;
|
|
32916
|
+
this.removeFieldLength(shortId, fieldId, this._documentCount, uniqueTerms);
|
|
32917
|
+
for (const term of tokens) {
|
|
32918
|
+
const processedTerm = processTerm(term, field);
|
|
32919
|
+
if (Array.isArray(processedTerm)) for (const t of processedTerm) this.removeTerm(fieldId, shortId, t);
|
|
32920
|
+
else if (processedTerm) this.removeTerm(fieldId, shortId, processedTerm);
|
|
32921
|
+
}
|
|
32922
|
+
}
|
|
32923
|
+
this._storedFields.delete(shortId);
|
|
32924
|
+
this._documentIds.delete(shortId);
|
|
32925
|
+
this._idToShortId.delete(id);
|
|
32926
|
+
this._fieldLength.delete(shortId);
|
|
32927
|
+
this._documentCount -= 1;
|
|
32928
|
+
}
|
|
32929
|
+
/**
|
|
32930
|
+
* Removes all the given documents from the index. If called with no arguments,
|
|
32931
|
+
* it removes _all_ documents from the index.
|
|
32932
|
+
*
|
|
32933
|
+
* @param documents The documents to be removed. If this argument is omitted,
|
|
32934
|
+
* all documents are removed. Note that, for removing all documents, it is
|
|
32935
|
+
* more efficient to call this method with no arguments than to pass all
|
|
32936
|
+
* documents.
|
|
32937
|
+
*/
|
|
32938
|
+
removeAll(documents) {
|
|
32939
|
+
if (documents) for (const document of documents) this.remove(document);
|
|
32940
|
+
else if (arguments.length > 0) throw new Error("Expected documents to be present. Omit the argument to remove all documents.");
|
|
32941
|
+
else {
|
|
32942
|
+
this._index = new SearchableMap();
|
|
32943
|
+
this._documentCount = 0;
|
|
32944
|
+
this._documentIds = /* @__PURE__ */ new Map();
|
|
32945
|
+
this._idToShortId = /* @__PURE__ */ new Map();
|
|
32946
|
+
this._fieldLength = /* @__PURE__ */ new Map();
|
|
32947
|
+
this._avgFieldLength = [];
|
|
32948
|
+
this._storedFields = /* @__PURE__ */ new Map();
|
|
32949
|
+
this._nextId = 0;
|
|
32950
|
+
}
|
|
32951
|
+
}
|
|
32952
|
+
/**
|
|
32953
|
+
* Discards the document with the given ID, so it won't appear in search results
|
|
32954
|
+
*
|
|
32955
|
+
* It has the same visible effect of {@link MiniSearch.remove} (both cause the
|
|
32956
|
+
* document to stop appearing in searches), but a different effect on the
|
|
32957
|
+
* internal data structures:
|
|
32958
|
+
*
|
|
32959
|
+
* - {@link MiniSearch#remove} requires passing the full document to be
|
|
32960
|
+
* removed as argument, and removes it from the inverted index immediately.
|
|
32961
|
+
*
|
|
32962
|
+
* - {@link MiniSearch#discard} instead only needs the document ID, and
|
|
32963
|
+
* works by marking the current version of the document as discarded, so it
|
|
32964
|
+
* is immediately ignored by searches. This is faster and more convenient
|
|
32965
|
+
* than {@link MiniSearch#remove}, but the index is not immediately
|
|
32966
|
+
* modified. To take care of that, vacuuming is performed after a certain
|
|
32967
|
+
* number of documents are discarded, cleaning up the index and allowing
|
|
32968
|
+
* memory to be released.
|
|
32969
|
+
*
|
|
32970
|
+
* After discarding a document, it is possible to re-add a new version, and
|
|
32971
|
+
* only the new version will appear in searches. In other words, discarding
|
|
32972
|
+
* and re-adding a document works exactly like removing and re-adding it. The
|
|
32973
|
+
* {@link MiniSearch.replace} method can also be used to replace a document
|
|
32974
|
+
* with a new version.
|
|
32975
|
+
*
|
|
32976
|
+
* #### Details about vacuuming
|
|
32977
|
+
*
|
|
32978
|
+
* Repetite calls to this method would leave obsolete document references in
|
|
32979
|
+
* the index, invisible to searches. Two mechanisms take care of cleaning up:
|
|
32980
|
+
* clean up during search, and vacuuming.
|
|
32981
|
+
*
|
|
32982
|
+
* - Upon search, whenever a discarded ID is found (and ignored for the
|
|
32983
|
+
* results), references to the discarded document are removed from the
|
|
32984
|
+
* inverted index entries for the search terms. This ensures that subsequent
|
|
32985
|
+
* searches for the same terms do not need to skip these obsolete references
|
|
32986
|
+
* again.
|
|
32987
|
+
*
|
|
32988
|
+
* - In addition, vacuuming is performed automatically by default (see the
|
|
32989
|
+
* `autoVacuum` field in {@link Options}) after a certain number of
|
|
32990
|
+
* documents are discarded. Vacuuming traverses all terms in the index,
|
|
32991
|
+
* cleaning up all references to discarded documents. Vacuuming can also be
|
|
32992
|
+
* triggered manually by calling {@link MiniSearch#vacuum}.
|
|
32993
|
+
*
|
|
32994
|
+
* @param id The ID of the document to be discarded
|
|
32995
|
+
*/
|
|
32996
|
+
discard(id) {
|
|
32997
|
+
const shortId = this._idToShortId.get(id);
|
|
32998
|
+
if (shortId == null) throw new Error(`MiniSearch: cannot discard document with ID ${id}: it is not in the index`);
|
|
32999
|
+
this._idToShortId.delete(id);
|
|
33000
|
+
this._documentIds.delete(shortId);
|
|
33001
|
+
this._storedFields.delete(shortId);
|
|
33002
|
+
(this._fieldLength.get(shortId) || []).forEach((fieldLength, fieldId) => {
|
|
33003
|
+
this.removeFieldLength(shortId, fieldId, this._documentCount, fieldLength);
|
|
33004
|
+
});
|
|
33005
|
+
this._fieldLength.delete(shortId);
|
|
33006
|
+
this._documentCount -= 1;
|
|
33007
|
+
this._dirtCount += 1;
|
|
33008
|
+
this.maybeAutoVacuum();
|
|
33009
|
+
}
|
|
33010
|
+
maybeAutoVacuum() {
|
|
33011
|
+
if (this._options.autoVacuum === false) return;
|
|
33012
|
+
const { minDirtFactor, minDirtCount, batchSize, batchWait } = this._options.autoVacuum;
|
|
33013
|
+
this.conditionalVacuum({
|
|
33014
|
+
batchSize,
|
|
33015
|
+
batchWait
|
|
33016
|
+
}, {
|
|
33017
|
+
minDirtCount,
|
|
33018
|
+
minDirtFactor
|
|
33019
|
+
});
|
|
33020
|
+
}
|
|
33021
|
+
/**
|
|
33022
|
+
* Discards the documents with the given IDs, so they won't appear in search
|
|
33023
|
+
* results
|
|
33024
|
+
*
|
|
33025
|
+
* It is equivalent to calling {@link MiniSearch#discard} for all the given
|
|
33026
|
+
* IDs, but with the optimization of triggering at most one automatic
|
|
33027
|
+
* vacuuming at the end.
|
|
33028
|
+
*
|
|
33029
|
+
* Note: to remove all documents from the index, it is faster and more
|
|
33030
|
+
* convenient to call {@link MiniSearch.removeAll} with no argument, instead
|
|
33031
|
+
* of passing all IDs to this method.
|
|
33032
|
+
*/
|
|
33033
|
+
discardAll(ids) {
|
|
33034
|
+
const autoVacuum = this._options.autoVacuum;
|
|
33035
|
+
try {
|
|
33036
|
+
this._options.autoVacuum = false;
|
|
33037
|
+
for (const id of ids) this.discard(id);
|
|
33038
|
+
} finally {
|
|
33039
|
+
this._options.autoVacuum = autoVacuum;
|
|
33040
|
+
}
|
|
33041
|
+
this.maybeAutoVacuum();
|
|
33042
|
+
}
|
|
33043
|
+
/**
|
|
33044
|
+
* It replaces an existing document with the given updated version
|
|
33045
|
+
*
|
|
33046
|
+
* It works by discarding the current version and adding the updated one, so
|
|
33047
|
+
* it is functionally equivalent to calling {@link MiniSearch#discard}
|
|
33048
|
+
* followed by {@link MiniSearch#add}. The ID of the updated document should
|
|
33049
|
+
* be the same as the original one.
|
|
33050
|
+
*
|
|
33051
|
+
* Since it uses {@link MiniSearch#discard} internally, this method relies on
|
|
33052
|
+
* vacuuming to clean up obsolete document references from the index, allowing
|
|
33053
|
+
* memory to be released (see {@link MiniSearch#discard}).
|
|
33054
|
+
*
|
|
33055
|
+
* @param updatedDocument The updated document to replace the old version
|
|
33056
|
+
* with
|
|
33057
|
+
*/
|
|
33058
|
+
replace(updatedDocument) {
|
|
33059
|
+
const { idField, extractField } = this._options;
|
|
33060
|
+
const id = extractField(updatedDocument, idField);
|
|
33061
|
+
this.discard(id);
|
|
33062
|
+
this.add(updatedDocument);
|
|
33063
|
+
}
|
|
33064
|
+
/**
|
|
33065
|
+
* Triggers a manual vacuuming, cleaning up references to discarded documents
|
|
33066
|
+
* from the inverted index
|
|
33067
|
+
*
|
|
33068
|
+
* Vacuuming is only useful for applications that use the {@link
|
|
33069
|
+
* MiniSearch#discard} or {@link MiniSearch#replace} methods.
|
|
33070
|
+
*
|
|
33071
|
+
* By default, vacuuming is performed automatically when needed (controlled by
|
|
33072
|
+
* the `autoVacuum` field in {@link Options}), so there is usually no need to
|
|
33073
|
+
* call this method, unless one wants to make sure to perform vacuuming at a
|
|
33074
|
+
* specific moment.
|
|
33075
|
+
*
|
|
33076
|
+
* Vacuuming traverses all terms in the inverted index in batches, and cleans
|
|
33077
|
+
* up references to discarded documents from the posting list, allowing memory
|
|
33078
|
+
* to be released.
|
|
33079
|
+
*
|
|
33080
|
+
* The method takes an optional object as argument with the following keys:
|
|
33081
|
+
*
|
|
33082
|
+
* - `batchSize`: the size of each batch (1000 by default)
|
|
33083
|
+
*
|
|
33084
|
+
* - `batchWait`: the number of milliseconds to wait between batches (10 by
|
|
33085
|
+
* default)
|
|
33086
|
+
*
|
|
33087
|
+
* On large indexes, vacuuming could have a non-negligible cost: batching
|
|
33088
|
+
* avoids blocking the thread for long, diluting this cost so that it is not
|
|
33089
|
+
* negatively affecting the application. Nonetheless, this method should only
|
|
33090
|
+
* be called when necessary, and relying on automatic vacuuming is usually
|
|
33091
|
+
* better.
|
|
33092
|
+
*
|
|
33093
|
+
* It returns a promise that resolves (to undefined) when the clean up is
|
|
33094
|
+
* completed. If vacuuming is already ongoing at the time this method is
|
|
33095
|
+
* called, a new one is enqueued immediately after the ongoing one, and a
|
|
33096
|
+
* corresponding promise is returned. However, no more than one vacuuming is
|
|
33097
|
+
* enqueued on top of the ongoing one, even if this method is called more
|
|
33098
|
+
* times (enqueuing multiple ones would be useless).
|
|
33099
|
+
*
|
|
33100
|
+
* @param options Configuration options for the batch size and delay. See
|
|
33101
|
+
* {@link VacuumOptions}.
|
|
33102
|
+
*/
|
|
33103
|
+
vacuum(options = {}) {
|
|
33104
|
+
return this.conditionalVacuum(options);
|
|
33105
|
+
}
|
|
33106
|
+
conditionalVacuum(options, conditions) {
|
|
33107
|
+
if (this._currentVacuum) {
|
|
33108
|
+
this._enqueuedVacuumConditions = this._enqueuedVacuumConditions && conditions;
|
|
33109
|
+
if (this._enqueuedVacuum != null) return this._enqueuedVacuum;
|
|
33110
|
+
this._enqueuedVacuum = this._currentVacuum.then(() => {
|
|
33111
|
+
const conditions = this._enqueuedVacuumConditions;
|
|
33112
|
+
this._enqueuedVacuumConditions = defaultVacuumConditions;
|
|
33113
|
+
return this.performVacuuming(options, conditions);
|
|
33114
|
+
});
|
|
33115
|
+
return this._enqueuedVacuum;
|
|
33116
|
+
}
|
|
33117
|
+
if (this.vacuumConditionsMet(conditions) === false) return Promise.resolve();
|
|
33118
|
+
this._currentVacuum = this.performVacuuming(options);
|
|
33119
|
+
return this._currentVacuum;
|
|
33120
|
+
}
|
|
33121
|
+
async performVacuuming(options, conditions) {
|
|
33122
|
+
const initialDirtCount = this._dirtCount;
|
|
33123
|
+
if (this.vacuumConditionsMet(conditions)) {
|
|
33124
|
+
const batchSize = options.batchSize || defaultVacuumOptions.batchSize;
|
|
33125
|
+
const batchWait = options.batchWait || defaultVacuumOptions.batchWait;
|
|
33126
|
+
let i = 1;
|
|
33127
|
+
for (const [term, fieldsData] of this._index) {
|
|
33128
|
+
for (const [fieldId, fieldIndex] of fieldsData) for (const [shortId] of fieldIndex) {
|
|
33129
|
+
if (this._documentIds.has(shortId)) continue;
|
|
33130
|
+
if (fieldIndex.size <= 1) fieldsData.delete(fieldId);
|
|
33131
|
+
else fieldIndex.delete(shortId);
|
|
33132
|
+
}
|
|
33133
|
+
if (this._index.get(term).size === 0) this._index.delete(term);
|
|
33134
|
+
if (i % batchSize === 0) await new Promise((resolve) => setTimeout(resolve, batchWait));
|
|
33135
|
+
i += 1;
|
|
33136
|
+
}
|
|
33137
|
+
this._dirtCount -= initialDirtCount;
|
|
33138
|
+
}
|
|
33139
|
+
await null;
|
|
33140
|
+
this._currentVacuum = this._enqueuedVacuum;
|
|
33141
|
+
this._enqueuedVacuum = null;
|
|
33142
|
+
}
|
|
33143
|
+
vacuumConditionsMet(conditions) {
|
|
33144
|
+
if (conditions == null) return true;
|
|
33145
|
+
let { minDirtCount, minDirtFactor } = conditions;
|
|
33146
|
+
minDirtCount = minDirtCount || defaultAutoVacuumOptions.minDirtCount;
|
|
33147
|
+
minDirtFactor = minDirtFactor || defaultAutoVacuumOptions.minDirtFactor;
|
|
33148
|
+
return this.dirtCount >= minDirtCount && this.dirtFactor >= minDirtFactor;
|
|
33149
|
+
}
|
|
33150
|
+
/**
|
|
33151
|
+
* Is `true` if a vacuuming operation is ongoing, `false` otherwise
|
|
33152
|
+
*/
|
|
33153
|
+
get isVacuuming() {
|
|
33154
|
+
return this._currentVacuum != null;
|
|
33155
|
+
}
|
|
33156
|
+
/**
|
|
33157
|
+
* The number of documents discarded since the most recent vacuuming
|
|
33158
|
+
*/
|
|
33159
|
+
get dirtCount() {
|
|
33160
|
+
return this._dirtCount;
|
|
33161
|
+
}
|
|
33162
|
+
/**
|
|
33163
|
+
* A number between 0 and 1 giving an indication about the proportion of
|
|
33164
|
+
* documents that are discarded, and can therefore be cleaned up by vacuuming.
|
|
33165
|
+
* A value close to 0 means that the index is relatively clean, while a higher
|
|
33166
|
+
* value means that the index is relatively dirty, and vacuuming could release
|
|
33167
|
+
* memory.
|
|
33168
|
+
*/
|
|
33169
|
+
get dirtFactor() {
|
|
33170
|
+
return this._dirtCount / (1 + this._documentCount + this._dirtCount);
|
|
33171
|
+
}
|
|
33172
|
+
/**
|
|
33173
|
+
* Returns `true` if a document with the given ID is present in the index and
|
|
33174
|
+
* available for search, `false` otherwise
|
|
33175
|
+
*
|
|
33176
|
+
* @param id The document ID
|
|
33177
|
+
*/
|
|
33178
|
+
has(id) {
|
|
33179
|
+
return this._idToShortId.has(id);
|
|
33180
|
+
}
|
|
33181
|
+
/**
|
|
33182
|
+
* Returns the stored fields (as configured in the `storeFields` constructor
|
|
33183
|
+
* option) for the given document ID. Returns `undefined` if the document is
|
|
33184
|
+
* not present in the index.
|
|
33185
|
+
*
|
|
33186
|
+
* @param id The document ID
|
|
33187
|
+
*/
|
|
33188
|
+
getStoredFields(id) {
|
|
33189
|
+
const shortId = this._idToShortId.get(id);
|
|
33190
|
+
if (shortId == null) return;
|
|
33191
|
+
return this._storedFields.get(shortId);
|
|
33192
|
+
}
|
|
33193
|
+
/**
|
|
33194
|
+
* Search for documents matching the given search query.
|
|
33195
|
+
*
|
|
33196
|
+
* The result is a list of scored document IDs matching the query, sorted by
|
|
33197
|
+
* descending score, and each including data about which terms were matched and
|
|
33198
|
+
* in which fields.
|
|
33199
|
+
*
|
|
33200
|
+
* ### Basic usage:
|
|
33201
|
+
*
|
|
33202
|
+
* ```javascript
|
|
33203
|
+
* // Search for "zen art motorcycle" with default options: terms have to match
|
|
33204
|
+
* // exactly, and individual terms are joined with OR
|
|
33205
|
+
* miniSearch.search('zen art motorcycle')
|
|
33206
|
+
* // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
|
|
33207
|
+
* ```
|
|
33208
|
+
*
|
|
33209
|
+
* ### Restrict search to specific fields:
|
|
33210
|
+
*
|
|
33211
|
+
* ```javascript
|
|
33212
|
+
* // Search only in the 'title' field
|
|
33213
|
+
* miniSearch.search('zen', { fields: ['title'] })
|
|
33214
|
+
* ```
|
|
33215
|
+
*
|
|
33216
|
+
* ### Field boosting:
|
|
33217
|
+
*
|
|
33218
|
+
* ```javascript
|
|
33219
|
+
* // Boost a field
|
|
33220
|
+
* miniSearch.search('zen', { boost: { title: 2 } })
|
|
33221
|
+
* ```
|
|
33222
|
+
*
|
|
33223
|
+
* ### Prefix search:
|
|
33224
|
+
*
|
|
33225
|
+
* ```javascript
|
|
33226
|
+
* // Search for "moto" with prefix search (it will match documents
|
|
33227
|
+
* // containing terms that start with "moto" or "neuro")
|
|
33228
|
+
* miniSearch.search('moto neuro', { prefix: true })
|
|
33229
|
+
* ```
|
|
33230
|
+
*
|
|
33231
|
+
* ### Fuzzy search:
|
|
33232
|
+
*
|
|
33233
|
+
* ```javascript
|
|
33234
|
+
* // Search for "ismael" with fuzzy search (it will match documents containing
|
|
33235
|
+
* // terms similar to "ismael", with a maximum edit distance of 0.2 term.length
|
|
33236
|
+
* // (rounded to nearest integer)
|
|
33237
|
+
* miniSearch.search('ismael', { fuzzy: 0.2 })
|
|
33238
|
+
* ```
|
|
33239
|
+
*
|
|
33240
|
+
* ### Combining strategies:
|
|
33241
|
+
*
|
|
33242
|
+
* ```javascript
|
|
33243
|
+
* // Mix of exact match, prefix search, and fuzzy search
|
|
33244
|
+
* miniSearch.search('ismael mob', {
|
|
33245
|
+
* prefix: true,
|
|
33246
|
+
* fuzzy: 0.2
|
|
33247
|
+
* })
|
|
33248
|
+
* ```
|
|
33249
|
+
*
|
|
33250
|
+
* ### Advanced prefix and fuzzy search:
|
|
33251
|
+
*
|
|
33252
|
+
* ```javascript
|
|
33253
|
+
* // Perform fuzzy and prefix search depending on the search term. Here
|
|
33254
|
+
* // performing prefix and fuzzy search only on terms longer than 3 characters
|
|
33255
|
+
* miniSearch.search('ismael mob', {
|
|
33256
|
+
* prefix: term => term.length > 3
|
|
33257
|
+
* fuzzy: term => term.length > 3 ? 0.2 : null
|
|
33258
|
+
* })
|
|
33259
|
+
* ```
|
|
33260
|
+
*
|
|
33261
|
+
* ### Combine with AND:
|
|
33262
|
+
*
|
|
33263
|
+
* ```javascript
|
|
33264
|
+
* // Combine search terms with AND (to match only documents that contain both
|
|
33265
|
+
* // "motorcycle" and "art")
|
|
33266
|
+
* miniSearch.search('motorcycle art', { combineWith: 'AND' })
|
|
33267
|
+
* ```
|
|
33268
|
+
*
|
|
33269
|
+
* ### Combine with AND_NOT:
|
|
33270
|
+
*
|
|
33271
|
+
* There is also an AND_NOT combinator, that finds documents that match the
|
|
33272
|
+
* first term, but do not match any of the other terms. This combinator is
|
|
33273
|
+
* rarely useful with simple queries, and is meant to be used with advanced
|
|
33274
|
+
* query combinations (see later for more details).
|
|
33275
|
+
*
|
|
33276
|
+
* ### Filtering results:
|
|
33277
|
+
*
|
|
33278
|
+
* ```javascript
|
|
33279
|
+
* // Filter only results in the 'fiction' category (assuming that 'category'
|
|
33280
|
+
* // is a stored field)
|
|
33281
|
+
* miniSearch.search('motorcycle art', {
|
|
33282
|
+
* filter: (result) => result.category === 'fiction'
|
|
33283
|
+
* })
|
|
33284
|
+
* ```
|
|
33285
|
+
*
|
|
33286
|
+
* ### Wildcard query
|
|
33287
|
+
*
|
|
33288
|
+
* Searching for an empty string (assuming the default tokenizer) returns no
|
|
33289
|
+
* results. Sometimes though, one needs to match all documents, like in a
|
|
33290
|
+
* "wildcard" search. This is possible by passing the special value
|
|
33291
|
+
* {@link MiniSearch.wildcard} as the query:
|
|
33292
|
+
*
|
|
33293
|
+
* ```javascript
|
|
33294
|
+
* // Return search results for all documents
|
|
33295
|
+
* miniSearch.search(MiniSearch.wildcard)
|
|
33296
|
+
* ```
|
|
33297
|
+
*
|
|
33298
|
+
* Note that search options such as `filter` and `boostDocument` are still
|
|
33299
|
+
* applied, influencing which results are returned, and their order:
|
|
33300
|
+
*
|
|
33301
|
+
* ```javascript
|
|
33302
|
+
* // Return search results for all documents in the 'fiction' category
|
|
33303
|
+
* miniSearch.search(MiniSearch.wildcard, {
|
|
33304
|
+
* filter: (result) => result.category === 'fiction'
|
|
33305
|
+
* })
|
|
33306
|
+
* ```
|
|
33307
|
+
*
|
|
33308
|
+
* ### Advanced combination of queries:
|
|
33309
|
+
*
|
|
33310
|
+
* It is possible to combine different subqueries with OR, AND, and AND_NOT,
|
|
33311
|
+
* and even with different search options, by passing a query expression
|
|
33312
|
+
* tree object as the first argument, instead of a string.
|
|
33313
|
+
*
|
|
33314
|
+
* ```javascript
|
|
33315
|
+
* // Search for documents that contain "zen" and ("motorcycle" or "archery")
|
|
33316
|
+
* miniSearch.search({
|
|
33317
|
+
* combineWith: 'AND',
|
|
33318
|
+
* queries: [
|
|
33319
|
+
* 'zen',
|
|
33320
|
+
* {
|
|
33321
|
+
* combineWith: 'OR',
|
|
33322
|
+
* queries: ['motorcycle', 'archery']
|
|
33323
|
+
* }
|
|
33324
|
+
* ]
|
|
33325
|
+
* })
|
|
33326
|
+
*
|
|
33327
|
+
* // Search for documents that contain ("apple" or "pear") but not "juice" and
|
|
33328
|
+
* // not "tree"
|
|
33329
|
+
* miniSearch.search({
|
|
33330
|
+
* combineWith: 'AND_NOT',
|
|
33331
|
+
* queries: [
|
|
33332
|
+
* {
|
|
33333
|
+
* combineWith: 'OR',
|
|
33334
|
+
* queries: ['apple', 'pear']
|
|
33335
|
+
* },
|
|
33336
|
+
* 'juice',
|
|
33337
|
+
* 'tree'
|
|
33338
|
+
* ]
|
|
33339
|
+
* })
|
|
33340
|
+
* ```
|
|
33341
|
+
*
|
|
33342
|
+
* Each node in the expression tree can be either a string, or an object that
|
|
33343
|
+
* supports all {@link SearchOptions} fields, plus a `queries` array field for
|
|
33344
|
+
* subqueries.
|
|
33345
|
+
*
|
|
33346
|
+
* Note that, while this can become complicated to do by hand for complex or
|
|
33347
|
+
* deeply nested queries, it provides a formalized expression tree API for
|
|
33348
|
+
* external libraries that implement a parser for custom query languages.
|
|
33349
|
+
*
|
|
33350
|
+
* @param query Search query
|
|
33351
|
+
* @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.
|
|
33352
|
+
*/
|
|
33353
|
+
search(query, searchOptions = {}) {
|
|
33354
|
+
const { searchOptions: globalSearchOptions } = this._options;
|
|
33355
|
+
const searchOptionsWithDefaults = {
|
|
33356
|
+
...globalSearchOptions,
|
|
33357
|
+
...searchOptions
|
|
33358
|
+
};
|
|
33359
|
+
const rawResults = this.executeQuery(query, searchOptions);
|
|
33360
|
+
const results = [];
|
|
33361
|
+
for (const [docId, { score, terms, match }] of rawResults) {
|
|
33362
|
+
const quality = terms.length || 1;
|
|
33363
|
+
const result = {
|
|
33364
|
+
id: this._documentIds.get(docId),
|
|
33365
|
+
score: score * quality,
|
|
33366
|
+
terms: Object.keys(match),
|
|
33367
|
+
queryTerms: terms,
|
|
33368
|
+
match
|
|
33369
|
+
};
|
|
33370
|
+
Object.assign(result, this._storedFields.get(docId));
|
|
33371
|
+
if (searchOptionsWithDefaults.filter == null || searchOptionsWithDefaults.filter(result)) results.push(result);
|
|
33372
|
+
}
|
|
33373
|
+
if (query === MiniSearch.wildcard && searchOptionsWithDefaults.boostDocument == null) return results;
|
|
33374
|
+
results.sort(byScore);
|
|
33375
|
+
return results;
|
|
33376
|
+
}
|
|
33377
|
+
/**
|
|
33378
|
+
* Provide suggestions for the given search query
|
|
33379
|
+
*
|
|
33380
|
+
* The result is a list of suggested modified search queries, derived from the
|
|
33381
|
+
* given search query, each with a relevance score, sorted by descending score.
|
|
33382
|
+
*
|
|
33383
|
+
* By default, it uses the same options used for search, except that by
|
|
33384
|
+
* default it performs prefix search on the last term of the query, and
|
|
33385
|
+
* combine terms with `'AND'` (requiring all query terms to match). Custom
|
|
33386
|
+
* options can be passed as a second argument. Defaults can be changed upon
|
|
33387
|
+
* calling the {@link MiniSearch} constructor, by passing a
|
|
33388
|
+
* `autoSuggestOptions` option.
|
|
33389
|
+
*
|
|
33390
|
+
* ### Basic usage:
|
|
33391
|
+
*
|
|
33392
|
+
* ```javascript
|
|
33393
|
+
* // Get suggestions for 'neuro':
|
|
33394
|
+
* miniSearch.autoSuggest('neuro')
|
|
33395
|
+
* // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
|
|
33396
|
+
* ```
|
|
33397
|
+
*
|
|
33398
|
+
* ### Multiple words:
|
|
33399
|
+
*
|
|
33400
|
+
* ```javascript
|
|
33401
|
+
* // Get suggestions for 'zen ar':
|
|
33402
|
+
* miniSearch.autoSuggest('zen ar')
|
|
33403
|
+
* // => [
|
|
33404
|
+
* // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
|
|
33405
|
+
* // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
|
|
33406
|
+
* // ]
|
|
33407
|
+
* ```
|
|
33408
|
+
*
|
|
33409
|
+
* ### Fuzzy suggestions:
|
|
33410
|
+
*
|
|
33411
|
+
* ```javascript
|
|
33412
|
+
* // Correct spelling mistakes using fuzzy search:
|
|
33413
|
+
* miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 })
|
|
33414
|
+
* // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
|
|
33415
|
+
* ```
|
|
33416
|
+
*
|
|
33417
|
+
* ### Filtering:
|
|
33418
|
+
*
|
|
33419
|
+
* ```javascript
|
|
33420
|
+
* // Get suggestions for 'zen ar', but only within the 'fiction' category
|
|
33421
|
+
* // (assuming that 'category' is a stored field):
|
|
33422
|
+
* miniSearch.autoSuggest('zen ar', {
|
|
33423
|
+
* filter: (result) => result.category === 'fiction'
|
|
33424
|
+
* })
|
|
33425
|
+
* // => [
|
|
33426
|
+
* // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
|
|
33427
|
+
* // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
|
|
33428
|
+
* // ]
|
|
33429
|
+
* ```
|
|
33430
|
+
*
|
|
33431
|
+
* @param queryString Query string to be expanded into suggestions
|
|
33432
|
+
* @param options Search options. The supported options and default values
|
|
33433
|
+
* are the same as for the {@link MiniSearch#search} method, except that by
|
|
33434
|
+
* default prefix search is performed on the last term in the query, and terms
|
|
33435
|
+
* are combined with `'AND'`.
|
|
33436
|
+
* @return A sorted array of suggestions sorted by relevance score.
|
|
33437
|
+
*/
|
|
33438
|
+
autoSuggest(queryString, options = {}) {
|
|
33439
|
+
options = {
|
|
33440
|
+
...this._options.autoSuggestOptions,
|
|
33441
|
+
...options
|
|
33442
|
+
};
|
|
33443
|
+
const suggestions = /* @__PURE__ */ new Map();
|
|
33444
|
+
for (const { score, terms } of this.search(queryString, options)) {
|
|
33445
|
+
const phrase = terms.join(" ");
|
|
33446
|
+
const suggestion = suggestions.get(phrase);
|
|
33447
|
+
if (suggestion != null) {
|
|
33448
|
+
suggestion.score += score;
|
|
33449
|
+
suggestion.count += 1;
|
|
33450
|
+
} else suggestions.set(phrase, {
|
|
33451
|
+
score,
|
|
33452
|
+
terms,
|
|
33453
|
+
count: 1
|
|
33454
|
+
});
|
|
33455
|
+
}
|
|
33456
|
+
const results = [];
|
|
33457
|
+
for (const [suggestion, { score, terms, count }] of suggestions) results.push({
|
|
33458
|
+
suggestion,
|
|
33459
|
+
terms,
|
|
33460
|
+
score: score / count
|
|
33461
|
+
});
|
|
33462
|
+
results.sort(byScore);
|
|
33463
|
+
return results;
|
|
33464
|
+
}
|
|
33465
|
+
/**
|
|
33466
|
+
* Total number of documents available to search
|
|
33467
|
+
*/
|
|
33468
|
+
get documentCount() {
|
|
33469
|
+
return this._documentCount;
|
|
33470
|
+
}
|
|
33471
|
+
/**
|
|
33472
|
+
* Number of terms in the index
|
|
33473
|
+
*/
|
|
33474
|
+
get termCount() {
|
|
33475
|
+
return this._index.size;
|
|
33476
|
+
}
|
|
33477
|
+
/**
|
|
33478
|
+
* Deserializes a JSON index (serialized with `JSON.stringify(miniSearch)`)
|
|
33479
|
+
* and instantiates a MiniSearch instance. It should be given the same options
|
|
33480
|
+
* originally used when serializing the index.
|
|
33481
|
+
*
|
|
33482
|
+
* ### Usage:
|
|
33483
|
+
*
|
|
33484
|
+
* ```javascript
|
|
33485
|
+
* // If the index was serialized with:
|
|
33486
|
+
* let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
|
|
33487
|
+
* miniSearch.addAll(documents)
|
|
33488
|
+
*
|
|
33489
|
+
* const json = JSON.stringify(miniSearch)
|
|
33490
|
+
* // It can later be deserialized like this:
|
|
33491
|
+
* miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
|
|
33492
|
+
* ```
|
|
33493
|
+
*
|
|
33494
|
+
* @param json JSON-serialized index
|
|
33495
|
+
* @param options configuration options, same as the constructor
|
|
33496
|
+
* @return An instance of MiniSearch deserialized from the given JSON.
|
|
33497
|
+
*/
|
|
33498
|
+
static loadJSON(json, options) {
|
|
33499
|
+
if (options == null) throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");
|
|
33500
|
+
return this.loadJS(JSON.parse(json), options);
|
|
33501
|
+
}
|
|
33502
|
+
/**
|
|
33503
|
+
* Async equivalent of {@link MiniSearch.loadJSON}
|
|
33504
|
+
*
|
|
33505
|
+
* This function is an alternative to {@link MiniSearch.loadJSON} that returns
|
|
33506
|
+
* a promise, and loads the index in batches, leaving pauses between them to avoid
|
|
33507
|
+
* blocking the main thread. It tends to be slower than the synchronous
|
|
33508
|
+
* version, but does not block the main thread, so it can be a better choice
|
|
33509
|
+
* when deserializing very large indexes.
|
|
33510
|
+
*
|
|
33511
|
+
* @param json JSON-serialized index
|
|
33512
|
+
* @param options configuration options, same as the constructor
|
|
33513
|
+
* @return A Promise that will resolve to an instance of MiniSearch deserialized from the given JSON.
|
|
33514
|
+
*/
|
|
33515
|
+
static async loadJSONAsync(json, options) {
|
|
33516
|
+
if (options == null) throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");
|
|
33517
|
+
return this.loadJSAsync(JSON.parse(json), options);
|
|
33518
|
+
}
|
|
33519
|
+
/**
|
|
33520
|
+
* Returns the default value of an option. It will throw an error if no option
|
|
33521
|
+
* with the given name exists.
|
|
33522
|
+
*
|
|
33523
|
+
* @param optionName Name of the option
|
|
33524
|
+
* @return The default value of the given option
|
|
33525
|
+
*
|
|
33526
|
+
* ### Usage:
|
|
33527
|
+
*
|
|
33528
|
+
* ```javascript
|
|
33529
|
+
* // Get default tokenizer
|
|
33530
|
+
* MiniSearch.getDefault('tokenize')
|
|
33531
|
+
*
|
|
33532
|
+
* // Get default term processor
|
|
33533
|
+
* MiniSearch.getDefault('processTerm')
|
|
33534
|
+
*
|
|
33535
|
+
* // Unknown options will throw an error
|
|
33536
|
+
* MiniSearch.getDefault('notExisting')
|
|
33537
|
+
* // => throws 'MiniSearch: unknown option "notExisting"'
|
|
33538
|
+
* ```
|
|
33539
|
+
*/
|
|
33540
|
+
static getDefault(optionName) {
|
|
33541
|
+
if (defaultOptions.hasOwnProperty(optionName)) return getOwnProperty(defaultOptions, optionName);
|
|
33542
|
+
else throw new Error(`MiniSearch: unknown option "${optionName}"`);
|
|
33543
|
+
}
|
|
33544
|
+
/**
|
|
33545
|
+
* @ignore
|
|
33546
|
+
*/
|
|
33547
|
+
static loadJS(js, options) {
|
|
33548
|
+
const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
|
|
33549
|
+
const miniSearch = this.instantiateMiniSearch(js, options);
|
|
33550
|
+
miniSearch._documentIds = objectToNumericMap(documentIds);
|
|
33551
|
+
miniSearch._fieldLength = objectToNumericMap(fieldLength);
|
|
33552
|
+
miniSearch._storedFields = objectToNumericMap(storedFields);
|
|
33553
|
+
for (const [shortId, id] of miniSearch._documentIds) miniSearch._idToShortId.set(id, shortId);
|
|
33554
|
+
for (const [term, data] of index) {
|
|
33555
|
+
const dataMap = /* @__PURE__ */ new Map();
|
|
33556
|
+
for (const fieldId of Object.keys(data)) {
|
|
33557
|
+
let indexEntry = data[fieldId];
|
|
33558
|
+
if (serializationVersion === 1) indexEntry = indexEntry.ds;
|
|
33559
|
+
dataMap.set(parseInt(fieldId, 10), objectToNumericMap(indexEntry));
|
|
33560
|
+
}
|
|
33561
|
+
miniSearch._index.set(term, dataMap);
|
|
33562
|
+
}
|
|
33563
|
+
return miniSearch;
|
|
33564
|
+
}
|
|
33565
|
+
/**
|
|
33566
|
+
* @ignore
|
|
33567
|
+
*/
|
|
33568
|
+
static async loadJSAsync(js, options) {
|
|
33569
|
+
const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
|
|
33570
|
+
const miniSearch = this.instantiateMiniSearch(js, options);
|
|
33571
|
+
miniSearch._documentIds = await objectToNumericMapAsync(documentIds);
|
|
33572
|
+
miniSearch._fieldLength = await objectToNumericMapAsync(fieldLength);
|
|
33573
|
+
miniSearch._storedFields = await objectToNumericMapAsync(storedFields);
|
|
33574
|
+
for (const [shortId, id] of miniSearch._documentIds) miniSearch._idToShortId.set(id, shortId);
|
|
33575
|
+
let count = 0;
|
|
33576
|
+
for (const [term, data] of index) {
|
|
33577
|
+
const dataMap = /* @__PURE__ */ new Map();
|
|
33578
|
+
for (const fieldId of Object.keys(data)) {
|
|
33579
|
+
let indexEntry = data[fieldId];
|
|
33580
|
+
if (serializationVersion === 1) indexEntry = indexEntry.ds;
|
|
33581
|
+
dataMap.set(parseInt(fieldId, 10), await objectToNumericMapAsync(indexEntry));
|
|
33582
|
+
}
|
|
33583
|
+
if (++count % 1e3 === 0) await wait(0);
|
|
33584
|
+
miniSearch._index.set(term, dataMap);
|
|
33585
|
+
}
|
|
33586
|
+
return miniSearch;
|
|
33587
|
+
}
|
|
33588
|
+
/**
|
|
33589
|
+
* @ignore
|
|
33590
|
+
*/
|
|
33591
|
+
static instantiateMiniSearch(js, options) {
|
|
33592
|
+
const { documentCount, nextId, fieldIds, averageFieldLength, dirtCount, serializationVersion } = js;
|
|
33593
|
+
if (serializationVersion !== 1 && serializationVersion !== 2) throw new Error("MiniSearch: cannot deserialize an index created with an incompatible version");
|
|
33594
|
+
const miniSearch = new MiniSearch(options);
|
|
33595
|
+
miniSearch._documentCount = documentCount;
|
|
33596
|
+
miniSearch._nextId = nextId;
|
|
33597
|
+
miniSearch._idToShortId = /* @__PURE__ */ new Map();
|
|
33598
|
+
miniSearch._fieldIds = fieldIds;
|
|
33599
|
+
miniSearch._avgFieldLength = averageFieldLength;
|
|
33600
|
+
miniSearch._dirtCount = dirtCount || 0;
|
|
33601
|
+
miniSearch._index = new SearchableMap();
|
|
33602
|
+
return miniSearch;
|
|
33603
|
+
}
|
|
33604
|
+
/**
|
|
33605
|
+
* @ignore
|
|
33606
|
+
*/
|
|
33607
|
+
executeQuery(query, searchOptions = {}) {
|
|
33608
|
+
if (query === MiniSearch.wildcard) return this.executeWildcardQuery(searchOptions);
|
|
33609
|
+
if (typeof query !== "string") {
|
|
33610
|
+
const options = {
|
|
33611
|
+
...searchOptions,
|
|
33612
|
+
...query,
|
|
33613
|
+
queries: void 0
|
|
33614
|
+
};
|
|
33615
|
+
const results = query.queries.map((subquery) => this.executeQuery(subquery, options));
|
|
33616
|
+
return this.combineResults(results, options.combineWith);
|
|
33617
|
+
}
|
|
33618
|
+
const { tokenize, processTerm, searchOptions: globalSearchOptions } = this._options;
|
|
33619
|
+
const options = {
|
|
33620
|
+
tokenize,
|
|
33621
|
+
processTerm,
|
|
33622
|
+
...globalSearchOptions,
|
|
33623
|
+
...searchOptions
|
|
33624
|
+
};
|
|
33625
|
+
const { tokenize: searchTokenize, processTerm: searchProcessTerm } = options;
|
|
33626
|
+
const results = searchTokenize(query).flatMap((term) => searchProcessTerm(term)).filter((term) => !!term).map(termToQuerySpec(options)).map((query) => this.executeQuerySpec(query, options));
|
|
33627
|
+
return this.combineResults(results, options.combineWith);
|
|
33628
|
+
}
|
|
33629
|
+
/**
|
|
33630
|
+
* @ignore
|
|
33631
|
+
*/
|
|
33632
|
+
executeQuerySpec(query, searchOptions) {
|
|
33633
|
+
const options = {
|
|
33634
|
+
...this._options.searchOptions,
|
|
33635
|
+
...searchOptions
|
|
33636
|
+
};
|
|
33637
|
+
const boosts = (options.fields || this._options.fields).reduce((boosts, field) => ({
|
|
33638
|
+
...boosts,
|
|
33639
|
+
[field]: getOwnProperty(options.boost, field) || 1
|
|
33640
|
+
}), {});
|
|
33641
|
+
const { boostDocument, weights, maxFuzzy, bm25: bm25params } = options;
|
|
33642
|
+
const { fuzzy: fuzzyWeight, prefix: prefixWeight } = {
|
|
33643
|
+
...defaultSearchOptions.weights,
|
|
33644
|
+
...weights
|
|
33645
|
+
};
|
|
33646
|
+
const data = this._index.get(query.term);
|
|
33647
|
+
const results = this.termResults(query.term, query.term, 1, query.termBoost, data, boosts, boostDocument, bm25params);
|
|
33648
|
+
let prefixMatches;
|
|
33649
|
+
let fuzzyMatches;
|
|
33650
|
+
if (query.prefix) prefixMatches = this._index.atPrefix(query.term);
|
|
33651
|
+
if (query.fuzzy) {
|
|
33652
|
+
const fuzzy = query.fuzzy === true ? .2 : query.fuzzy;
|
|
33653
|
+
const maxDistance = fuzzy < 1 ? Math.min(maxFuzzy, Math.round(query.term.length * fuzzy)) : fuzzy;
|
|
33654
|
+
if (maxDistance) fuzzyMatches = this._index.fuzzyGet(query.term, maxDistance);
|
|
33655
|
+
}
|
|
33656
|
+
if (prefixMatches) for (const [term, data] of prefixMatches) {
|
|
33657
|
+
const distance = term.length - query.term.length;
|
|
33658
|
+
if (!distance) continue;
|
|
33659
|
+
fuzzyMatches === null || fuzzyMatches === void 0 || fuzzyMatches.delete(term);
|
|
33660
|
+
const weight = prefixWeight * term.length / (term.length + .3 * distance);
|
|
33661
|
+
this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results);
|
|
33662
|
+
}
|
|
33663
|
+
if (fuzzyMatches) for (const term of fuzzyMatches.keys()) {
|
|
33664
|
+
const [data, distance] = fuzzyMatches.get(term);
|
|
33665
|
+
if (!distance) continue;
|
|
33666
|
+
const weight = fuzzyWeight * term.length / (term.length + distance);
|
|
33667
|
+
this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results);
|
|
33668
|
+
}
|
|
33669
|
+
return results;
|
|
33670
|
+
}
|
|
33671
|
+
/**
|
|
33672
|
+
* @ignore
|
|
33673
|
+
*/
|
|
33674
|
+
executeWildcardQuery(searchOptions) {
|
|
33675
|
+
const results = /* @__PURE__ */ new Map();
|
|
33676
|
+
const options = {
|
|
33677
|
+
...this._options.searchOptions,
|
|
33678
|
+
...searchOptions
|
|
33679
|
+
};
|
|
33680
|
+
for (const [shortId, id] of this._documentIds) {
|
|
33681
|
+
const score = options.boostDocument ? options.boostDocument(id, "", this._storedFields.get(shortId)) : 1;
|
|
33682
|
+
results.set(shortId, {
|
|
33683
|
+
score,
|
|
33684
|
+
terms: [],
|
|
33685
|
+
match: {}
|
|
33686
|
+
});
|
|
33687
|
+
}
|
|
33688
|
+
return results;
|
|
33689
|
+
}
|
|
33690
|
+
/**
|
|
33691
|
+
* @ignore
|
|
33692
|
+
*/
|
|
33693
|
+
combineResults(results, combineWith = OR) {
|
|
33694
|
+
if (results.length === 0) return /* @__PURE__ */ new Map();
|
|
33695
|
+
const combinator = combinators[combineWith.toLowerCase()];
|
|
33696
|
+
if (!combinator) throw new Error(`Invalid combination operator: ${combineWith}`);
|
|
33697
|
+
return results.reduce(combinator) || /* @__PURE__ */ new Map();
|
|
33698
|
+
}
|
|
33699
|
+
/**
|
|
33700
|
+
* Allows serialization of the index to JSON, to possibly store it and later
|
|
33701
|
+
* deserialize it with {@link MiniSearch.loadJSON}.
|
|
33702
|
+
*
|
|
33703
|
+
* Normally one does not directly call this method, but rather call the
|
|
33704
|
+
* standard JavaScript `JSON.stringify()` passing the {@link MiniSearch}
|
|
33705
|
+
* instance, and JavaScript will internally call this method. Upon
|
|
33706
|
+
* deserialization, one must pass to {@link MiniSearch.loadJSON} the same
|
|
33707
|
+
* options used to create the original instance that was serialized.
|
|
33708
|
+
*
|
|
33709
|
+
* ### Usage:
|
|
33710
|
+
*
|
|
33711
|
+
* ```javascript
|
|
33712
|
+
* // Serialize the index:
|
|
33713
|
+
* let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
|
|
33714
|
+
* miniSearch.addAll(documents)
|
|
33715
|
+
* const json = JSON.stringify(miniSearch)
|
|
33716
|
+
*
|
|
33717
|
+
* // Later, to deserialize it:
|
|
33718
|
+
* miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
|
|
33719
|
+
* ```
|
|
33720
|
+
*
|
|
33721
|
+
* @return A plain-object serializable representation of the search index.
|
|
33722
|
+
*/
|
|
33723
|
+
toJSON() {
|
|
33724
|
+
const index = [];
|
|
33725
|
+
for (const [term, fieldIndex] of this._index) {
|
|
33726
|
+
const data = {};
|
|
33727
|
+
for (const [fieldId, freqs] of fieldIndex) data[fieldId] = Object.fromEntries(freqs);
|
|
33728
|
+
index.push([term, data]);
|
|
33729
|
+
}
|
|
33730
|
+
return {
|
|
33731
|
+
documentCount: this._documentCount,
|
|
33732
|
+
nextId: this._nextId,
|
|
33733
|
+
documentIds: Object.fromEntries(this._documentIds),
|
|
33734
|
+
fieldIds: this._fieldIds,
|
|
33735
|
+
fieldLength: Object.fromEntries(this._fieldLength),
|
|
33736
|
+
averageFieldLength: this._avgFieldLength,
|
|
33737
|
+
storedFields: Object.fromEntries(this._storedFields),
|
|
33738
|
+
dirtCount: this._dirtCount,
|
|
33739
|
+
index,
|
|
33740
|
+
serializationVersion: 2
|
|
33741
|
+
};
|
|
33742
|
+
}
|
|
33743
|
+
/**
|
|
33744
|
+
* @ignore
|
|
33745
|
+
*/
|
|
33746
|
+
termResults(sourceTerm, derivedTerm, termWeight, termBoost, fieldTermData, fieldBoosts, boostDocumentFn, bm25params, results = /* @__PURE__ */ new Map()) {
|
|
33747
|
+
if (fieldTermData == null) return results;
|
|
33748
|
+
for (const field of Object.keys(fieldBoosts)) {
|
|
33749
|
+
const fieldBoost = fieldBoosts[field];
|
|
33750
|
+
const fieldId = this._fieldIds[field];
|
|
33751
|
+
const fieldTermFreqs = fieldTermData.get(fieldId);
|
|
33752
|
+
if (fieldTermFreqs == null) continue;
|
|
33753
|
+
let matchingFields = fieldTermFreqs.size;
|
|
33754
|
+
const avgFieldLength = this._avgFieldLength[fieldId];
|
|
33755
|
+
for (const docId of fieldTermFreqs.keys()) {
|
|
33756
|
+
if (!this._documentIds.has(docId)) {
|
|
33757
|
+
this.removeTerm(fieldId, docId, derivedTerm);
|
|
33758
|
+
matchingFields -= 1;
|
|
33759
|
+
continue;
|
|
33760
|
+
}
|
|
33761
|
+
const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1;
|
|
33762
|
+
if (!docBoost) continue;
|
|
33763
|
+
const termFreq = fieldTermFreqs.get(docId);
|
|
33764
|
+
const fieldLength = this._fieldLength.get(docId)[fieldId];
|
|
33765
|
+
const rawScore = calcBM25Score(termFreq, matchingFields, this._documentCount, fieldLength, avgFieldLength, bm25params);
|
|
33766
|
+
const weightedScore = termWeight * termBoost * fieldBoost * docBoost * rawScore;
|
|
33767
|
+
const result = results.get(docId);
|
|
33768
|
+
if (result) {
|
|
33769
|
+
result.score += weightedScore;
|
|
33770
|
+
assignUniqueTerm(result.terms, sourceTerm);
|
|
33771
|
+
const match = getOwnProperty(result.match, derivedTerm);
|
|
33772
|
+
if (match) match.push(field);
|
|
33773
|
+
else result.match[derivedTerm] = [field];
|
|
33774
|
+
} else results.set(docId, {
|
|
33775
|
+
score: weightedScore,
|
|
33776
|
+
terms: [sourceTerm],
|
|
33777
|
+
match: { [derivedTerm]: [field] }
|
|
33778
|
+
});
|
|
33779
|
+
}
|
|
33780
|
+
}
|
|
33781
|
+
return results;
|
|
33782
|
+
}
|
|
33783
|
+
/**
|
|
33784
|
+
* @ignore
|
|
33785
|
+
*/
|
|
33786
|
+
addTerm(fieldId, documentId, term) {
|
|
33787
|
+
const indexData = this._index.fetch(term, createMap);
|
|
33788
|
+
let fieldIndex = indexData.get(fieldId);
|
|
33789
|
+
if (fieldIndex == null) {
|
|
33790
|
+
fieldIndex = /* @__PURE__ */ new Map();
|
|
33791
|
+
fieldIndex.set(documentId, 1);
|
|
33792
|
+
indexData.set(fieldId, fieldIndex);
|
|
33793
|
+
} else {
|
|
33794
|
+
const docs = fieldIndex.get(documentId);
|
|
33795
|
+
fieldIndex.set(documentId, (docs || 0) + 1);
|
|
33796
|
+
}
|
|
33797
|
+
}
|
|
33798
|
+
/**
|
|
33799
|
+
* @ignore
|
|
33800
|
+
*/
|
|
33801
|
+
removeTerm(fieldId, documentId, term) {
|
|
33802
|
+
if (!this._index.has(term)) {
|
|
33803
|
+
this.warnDocumentChanged(documentId, fieldId, term);
|
|
33804
|
+
return;
|
|
33805
|
+
}
|
|
33806
|
+
const indexData = this._index.fetch(term, createMap);
|
|
33807
|
+
const fieldIndex = indexData.get(fieldId);
|
|
33808
|
+
if (fieldIndex == null || fieldIndex.get(documentId) == null) this.warnDocumentChanged(documentId, fieldId, term);
|
|
33809
|
+
else if (fieldIndex.get(documentId) <= 1) if (fieldIndex.size <= 1) indexData.delete(fieldId);
|
|
33810
|
+
else fieldIndex.delete(documentId);
|
|
33811
|
+
else fieldIndex.set(documentId, fieldIndex.get(documentId) - 1);
|
|
33812
|
+
if (this._index.get(term).size === 0) this._index.delete(term);
|
|
33813
|
+
}
|
|
33814
|
+
/**
|
|
33815
|
+
* @ignore
|
|
33816
|
+
*/
|
|
33817
|
+
warnDocumentChanged(shortDocumentId, fieldId, term) {
|
|
33818
|
+
for (const fieldName of Object.keys(this._fieldIds)) if (this._fieldIds[fieldName] === fieldId) {
|
|
33819
|
+
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");
|
|
33820
|
+
return;
|
|
33821
|
+
}
|
|
33822
|
+
}
|
|
33823
|
+
/**
|
|
33824
|
+
* @ignore
|
|
33825
|
+
*/
|
|
33826
|
+
addDocumentId(documentId) {
|
|
33827
|
+
const shortDocumentId = this._nextId;
|
|
33828
|
+
this._idToShortId.set(documentId, shortDocumentId);
|
|
33829
|
+
this._documentIds.set(shortDocumentId, documentId);
|
|
33830
|
+
this._documentCount += 1;
|
|
33831
|
+
this._nextId += 1;
|
|
33832
|
+
return shortDocumentId;
|
|
33833
|
+
}
|
|
33834
|
+
/**
|
|
33835
|
+
* @ignore
|
|
33836
|
+
*/
|
|
33837
|
+
addFields(fields) {
|
|
33838
|
+
for (let i = 0; i < fields.length; i++) this._fieldIds[fields[i]] = i;
|
|
33839
|
+
}
|
|
33840
|
+
/**
|
|
33841
|
+
* @ignore
|
|
33842
|
+
*/
|
|
33843
|
+
addFieldLength(documentId, fieldId, count, length) {
|
|
33844
|
+
let fieldLengths = this._fieldLength.get(documentId);
|
|
33845
|
+
if (fieldLengths == null) this._fieldLength.set(documentId, fieldLengths = []);
|
|
33846
|
+
fieldLengths[fieldId] = length;
|
|
33847
|
+
const totalFieldLength = (this._avgFieldLength[fieldId] || 0) * count + length;
|
|
33848
|
+
this._avgFieldLength[fieldId] = totalFieldLength / (count + 1);
|
|
33849
|
+
}
|
|
33850
|
+
/**
|
|
33851
|
+
* @ignore
|
|
33852
|
+
*/
|
|
33853
|
+
removeFieldLength(documentId, fieldId, count, length) {
|
|
33854
|
+
if (count === 1) {
|
|
33855
|
+
this._avgFieldLength[fieldId] = 0;
|
|
33856
|
+
return;
|
|
33857
|
+
}
|
|
33858
|
+
const totalFieldLength = this._avgFieldLength[fieldId] * count - length;
|
|
33859
|
+
this._avgFieldLength[fieldId] = totalFieldLength / (count - 1);
|
|
33860
|
+
}
|
|
33861
|
+
/**
|
|
33862
|
+
* @ignore
|
|
33863
|
+
*/
|
|
33864
|
+
saveStoredFields(documentId, doc) {
|
|
33865
|
+
const { storeFields, extractField } = this._options;
|
|
33866
|
+
if (storeFields == null || storeFields.length === 0) return;
|
|
33867
|
+
let documentFields = this._storedFields.get(documentId);
|
|
33868
|
+
if (documentFields == null) this._storedFields.set(documentId, documentFields = {});
|
|
33869
|
+
for (const fieldName of storeFields) {
|
|
33870
|
+
const fieldValue = extractField(doc, fieldName);
|
|
33871
|
+
if (fieldValue !== void 0) documentFields[fieldName] = fieldValue;
|
|
33872
|
+
}
|
|
33873
|
+
}
|
|
33874
|
+
};
|
|
33875
|
+
/**
|
|
33876
|
+
* The special wildcard symbol that can be passed to {@link MiniSearch#search}
|
|
33877
|
+
* to match all documents
|
|
33878
|
+
*/
|
|
33879
|
+
MiniSearch.wildcard = Symbol("*");
|
|
33880
|
+
const getOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) ? object[property] : void 0;
|
|
33881
|
+
const combinators = {
|
|
33882
|
+
[OR]: (a, b) => {
|
|
33883
|
+
for (const docId of b.keys()) {
|
|
33884
|
+
const existing = a.get(docId);
|
|
33885
|
+
if (existing == null) a.set(docId, b.get(docId));
|
|
33886
|
+
else {
|
|
33887
|
+
const { score, terms, match } = b.get(docId);
|
|
33888
|
+
existing.score = existing.score + score;
|
|
33889
|
+
existing.match = Object.assign(existing.match, match);
|
|
33890
|
+
assignUniqueTerms(existing.terms, terms);
|
|
33891
|
+
}
|
|
33892
|
+
}
|
|
33893
|
+
return a;
|
|
33894
|
+
},
|
|
33895
|
+
[AND]: (a, b) => {
|
|
33896
|
+
const combined = /* @__PURE__ */ new Map();
|
|
33897
|
+
for (const docId of b.keys()) {
|
|
33898
|
+
const existing = a.get(docId);
|
|
33899
|
+
if (existing == null) continue;
|
|
33900
|
+
const { score, terms, match } = b.get(docId);
|
|
33901
|
+
assignUniqueTerms(existing.terms, terms);
|
|
33902
|
+
combined.set(docId, {
|
|
33903
|
+
score: existing.score + score,
|
|
33904
|
+
terms: existing.terms,
|
|
33905
|
+
match: Object.assign(existing.match, match)
|
|
33906
|
+
});
|
|
33907
|
+
}
|
|
33908
|
+
return combined;
|
|
33909
|
+
},
|
|
33910
|
+
[AND_NOT]: (a, b) => {
|
|
33911
|
+
for (const docId of b.keys()) a.delete(docId);
|
|
33912
|
+
return a;
|
|
33913
|
+
}
|
|
33914
|
+
};
|
|
33915
|
+
const defaultBM25params = {
|
|
33916
|
+
k: 1.2,
|
|
33917
|
+
b: .7,
|
|
33918
|
+
d: .5
|
|
33919
|
+
};
|
|
33920
|
+
const calcBM25Score = (termFreq, matchingCount, totalCount, fieldLength, avgFieldLength, bm25params) => {
|
|
33921
|
+
const { k, b, d } = bm25params;
|
|
33922
|
+
return Math.log(1 + (totalCount - matchingCount + .5) / (matchingCount + .5)) * (d + termFreq * (k + 1) / (termFreq + k * (1 - b + b * fieldLength / avgFieldLength)));
|
|
33923
|
+
};
|
|
33924
|
+
const termToQuerySpec = (options) => (term, i, terms) => {
|
|
33925
|
+
return {
|
|
33926
|
+
term,
|
|
33927
|
+
fuzzy: typeof options.fuzzy === "function" ? options.fuzzy(term, i, terms) : options.fuzzy || false,
|
|
33928
|
+
prefix: typeof options.prefix === "function" ? options.prefix(term, i, terms) : options.prefix === true,
|
|
33929
|
+
termBoost: typeof options.boostTerm === "function" ? options.boostTerm(term, i, terms) : 1
|
|
33930
|
+
};
|
|
33931
|
+
};
|
|
33932
|
+
const defaultOptions = {
|
|
33933
|
+
idField: "id",
|
|
33934
|
+
extractField: (document, fieldName) => document[fieldName],
|
|
33935
|
+
stringifyField: (fieldValue, fieldName) => fieldValue.toString(),
|
|
33936
|
+
tokenize: (text) => text.split(SPACE_OR_PUNCTUATION),
|
|
33937
|
+
processTerm: (term) => term.toLowerCase(),
|
|
33938
|
+
fields: void 0,
|
|
33939
|
+
searchOptions: void 0,
|
|
33940
|
+
storeFields: [],
|
|
33941
|
+
logger: (level, message) => {
|
|
33942
|
+
if (typeof (console === null || console === void 0 ? void 0 : console[level]) === "function") console[level](message);
|
|
33943
|
+
},
|
|
33944
|
+
autoVacuum: true
|
|
33945
|
+
};
|
|
33946
|
+
const defaultSearchOptions = {
|
|
33947
|
+
combineWith: OR,
|
|
33948
|
+
prefix: false,
|
|
33949
|
+
fuzzy: false,
|
|
33950
|
+
maxFuzzy: 6,
|
|
33951
|
+
boost: {},
|
|
33952
|
+
weights: {
|
|
33953
|
+
fuzzy: .45,
|
|
33954
|
+
prefix: .375
|
|
33955
|
+
},
|
|
33956
|
+
bm25: defaultBM25params
|
|
33957
|
+
};
|
|
33958
|
+
const defaultAutoSuggestOptions = {
|
|
33959
|
+
combineWith: AND,
|
|
33960
|
+
prefix: (term, i, terms) => i === terms.length - 1
|
|
33961
|
+
};
|
|
33962
|
+
const defaultVacuumOptions = {
|
|
33963
|
+
batchSize: 1e3,
|
|
33964
|
+
batchWait: 10
|
|
33965
|
+
};
|
|
33966
|
+
const defaultVacuumConditions = {
|
|
33967
|
+
minDirtFactor: .1,
|
|
33968
|
+
minDirtCount: 20
|
|
33969
|
+
};
|
|
33970
|
+
const defaultAutoVacuumOptions = {
|
|
33971
|
+
...defaultVacuumOptions,
|
|
33972
|
+
...defaultVacuumConditions
|
|
33973
|
+
};
|
|
33974
|
+
const assignUniqueTerm = (target, term) => {
|
|
33975
|
+
if (!target.includes(term)) target.push(term);
|
|
33976
|
+
};
|
|
33977
|
+
const assignUniqueTerms = (target, source) => {
|
|
33978
|
+
for (const term of source) if (!target.includes(term)) target.push(term);
|
|
33979
|
+
};
|
|
33980
|
+
const byScore = ({ score: a }, { score: b }) => b - a;
|
|
33981
|
+
const createMap = () => /* @__PURE__ */ new Map();
|
|
33982
|
+
const objectToNumericMap = (object) => {
|
|
33983
|
+
const map = /* @__PURE__ */ new Map();
|
|
33984
|
+
for (const key of Object.keys(object)) map.set(parseInt(key, 10), object[key]);
|
|
33985
|
+
return map;
|
|
33986
|
+
};
|
|
33987
|
+
const objectToNumericMapAsync = async (object) => {
|
|
33988
|
+
const map = /* @__PURE__ */ new Map();
|
|
33989
|
+
let count = 0;
|
|
33990
|
+
for (const key of Object.keys(object)) {
|
|
33991
|
+
map.set(parseInt(key, 10), object[key]);
|
|
33992
|
+
if (++count % 1e3 === 0) await wait(0);
|
|
33993
|
+
}
|
|
33994
|
+
return map;
|
|
33995
|
+
};
|
|
33996
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
33997
|
+
const SPACE_OR_PUNCTUATION = /[\n\r\p{Z}\p{P}]+/u;
|
|
33998
|
+
//#endregion
|
|
32227
33999
|
//#region src/services/Skill.ts
|
|
32228
34000
|
const requireFrom$2 = createRequire(import.meta.url);
|
|
32229
34001
|
const findCliPackageRoot = () => {
|
|
@@ -32417,50 +34189,85 @@ const buildRegistry = (fs, path, cliDir, haklexDir) => gen(function* () {
|
|
|
32417
34189
|
}
|
|
32418
34190
|
return [...all].sort((a, b) => a.order - b.order);
|
|
32419
34191
|
});
|
|
32420
|
-
const
|
|
32421
|
-
const
|
|
32422
|
-
|
|
34192
|
+
const buildIndex = (chapters) => {
|
|
34193
|
+
const mini = new MiniSearch({
|
|
34194
|
+
fields: [
|
|
34195
|
+
"title",
|
|
34196
|
+
"description",
|
|
34197
|
+
"body"
|
|
34198
|
+
],
|
|
34199
|
+
storeFields: ["slug"],
|
|
34200
|
+
idField: "slug",
|
|
34201
|
+
searchOptions: {
|
|
34202
|
+
boost: {
|
|
34203
|
+
title: 3,
|
|
34204
|
+
description: 2
|
|
34205
|
+
},
|
|
34206
|
+
combineWith: "AND",
|
|
34207
|
+
prefix: true,
|
|
34208
|
+
fuzzy: .2
|
|
34209
|
+
}
|
|
34210
|
+
});
|
|
34211
|
+
mini.addAll(chapters.map((c) => ({
|
|
34212
|
+
slug: c.slug,
|
|
34213
|
+
title: c.title,
|
|
34214
|
+
description: c.description,
|
|
34215
|
+
body: c.body
|
|
34216
|
+
})));
|
|
34217
|
+
return mini;
|
|
34218
|
+
};
|
|
34219
|
+
/**
|
|
34220
|
+
* Tokenize the user's query string the same way MiniSearch does for indexing,
|
|
34221
|
+
* so snippets can be located in the body. We use this lightweight tokenizer
|
|
34222
|
+
* rather than `r.terms` because fuzzy / prefix matches expose the indexed
|
|
34223
|
+
* term, not the substring that appears in the body verbatim.
|
|
34224
|
+
*/
|
|
34225
|
+
const tokenizeQuery = (s) => s.toLowerCase().split(/[\s\p{P}]+/u).filter((t) => t.length > 0);
|
|
34226
|
+
const buildSnippets = (body, terms) => {
|
|
34227
|
+
if (terms.length === 0) return [];
|
|
32423
34228
|
const lower = body.toLowerCase();
|
|
32424
|
-
const
|
|
32425
|
-
|
|
32426
|
-
|
|
32427
|
-
|
|
32428
|
-
|
|
32429
|
-
|
|
32430
|
-
|
|
32431
|
-
|
|
32432
|
-
|
|
32433
|
-
|
|
32434
|
-
|
|
32435
|
-
|
|
32436
|
-
|
|
34229
|
+
const occs = terms.map((term) => {
|
|
34230
|
+
const t = term.toLowerCase();
|
|
34231
|
+
if (!t) return [];
|
|
34232
|
+
const positions = [];
|
|
34233
|
+
let from = 0;
|
|
34234
|
+
while (positions.length < 5) {
|
|
34235
|
+
const idx = lower.indexOf(t, from);
|
|
34236
|
+
if (idx < 0) break;
|
|
34237
|
+
positions.push(idx);
|
|
34238
|
+
from = idx + t.length;
|
|
34239
|
+
}
|
|
34240
|
+
return positions;
|
|
34241
|
+
});
|
|
34242
|
+
const cursors = Array.from({ length: terms.length }).fill(0);
|
|
34243
|
+
const out = [];
|
|
34244
|
+
let progressed = true;
|
|
34245
|
+
while (out.length < 3 && progressed) {
|
|
34246
|
+
progressed = false;
|
|
34247
|
+
for (let i = 0; i < terms.length && out.length < 3; i++) {
|
|
34248
|
+
const c = cursors[i];
|
|
34249
|
+
if (c >= occs[i].length) continue;
|
|
34250
|
+
const idx = occs[i][c];
|
|
34251
|
+
const tLen = terms[i].length;
|
|
34252
|
+
cursors[i] = c + 1;
|
|
34253
|
+
progressed = true;
|
|
34254
|
+
const start = Math.max(0, idx - 60);
|
|
34255
|
+
const end = Math.min(body.length, idx + tLen + 60);
|
|
34256
|
+
let snippet = body.slice(start, end).replaceAll(/\s+/g, " ").trim();
|
|
34257
|
+
if (start > 0) snippet = `…${snippet}`;
|
|
34258
|
+
if (end < body.length) snippet = `${snippet}…`;
|
|
34259
|
+
out.push(snippet);
|
|
34260
|
+
}
|
|
32437
34261
|
}
|
|
32438
34262
|
return out;
|
|
32439
34263
|
};
|
|
32440
|
-
const rank = (chapter, keyword) => {
|
|
32441
|
-
if (!keyword) return 0;
|
|
32442
|
-
let score = 0;
|
|
32443
|
-
if (indexOfCaseInsensitive(chapter.title, keyword) >= 0) score += 3;
|
|
32444
|
-
if (indexOfCaseInsensitive(chapter.description, keyword) >= 0) score += 2;
|
|
32445
|
-
const bodyLower = chapter.body.toLowerCase();
|
|
32446
|
-
const k = keyword.toLowerCase();
|
|
32447
|
-
let from = 0;
|
|
32448
|
-
let count = 0;
|
|
32449
|
-
while (count < 10) {
|
|
32450
|
-
const idx = bodyLower.indexOf(k, from);
|
|
32451
|
-
if (idx < 0) break;
|
|
32452
|
-
count += 1;
|
|
32453
|
-
from = idx + k.length;
|
|
32454
|
-
}
|
|
32455
|
-
score += count;
|
|
32456
|
-
return score;
|
|
32457
|
-
};
|
|
32458
34264
|
const make$2 = gen(function* () {
|
|
32459
34265
|
const fs = yield* FileSystem;
|
|
32460
34266
|
const path = yield* Path$1;
|
|
32461
34267
|
const cliDir = resolveCliSkillsDir();
|
|
32462
34268
|
const haklexDir = resolveHaklexSkillsDir();
|
|
32463
34269
|
const cache = yield* make$23(null);
|
|
34270
|
+
const indexCache = yield* make$23(null);
|
|
32464
34271
|
const load = gen(function* () {
|
|
32465
34272
|
const cached = yield* get$10(cache);
|
|
32466
34273
|
if (cached) return cached;
|
|
@@ -32468,6 +34275,13 @@ const make$2 = gen(function* () {
|
|
|
32468
34275
|
yield* set$3(cache, chapters);
|
|
32469
34276
|
return chapters;
|
|
32470
34277
|
});
|
|
34278
|
+
const loadIndex = gen(function* () {
|
|
34279
|
+
const cached = yield* get$10(indexCache);
|
|
34280
|
+
if (cached) return cached;
|
|
34281
|
+
const mini = buildIndex(yield* load);
|
|
34282
|
+
yield* set$3(indexCache, mini);
|
|
34283
|
+
return mini;
|
|
34284
|
+
});
|
|
32471
34285
|
return {
|
|
32472
34286
|
list: load,
|
|
32473
34287
|
all: load,
|
|
@@ -32484,24 +34298,23 @@ const make$2 = gen(function* () {
|
|
|
32484
34298
|
return found;
|
|
32485
34299
|
}),
|
|
32486
34300
|
search: (keyword) => gen(function* () {
|
|
32487
|
-
const chapters = yield* load;
|
|
32488
34301
|
const kw = keyword.trim();
|
|
32489
34302
|
if (!kw) return [];
|
|
32490
|
-
const
|
|
32491
|
-
|
|
32492
|
-
|
|
32493
|
-
|
|
32494
|
-
|
|
32495
|
-
|
|
32496
|
-
|
|
32497
|
-
|
|
32498
|
-
|
|
32499
|
-
|
|
32500
|
-
|
|
32501
|
-
|
|
32502
|
-
|
|
32503
|
-
|
|
32504
|
-
})
|
|
34303
|
+
const chapters = yield* load;
|
|
34304
|
+
const mini = yield* loadIndex;
|
|
34305
|
+
const tokens = tokenizeQuery(kw);
|
|
34306
|
+
const results = mini.search(kw);
|
|
34307
|
+
const bySlug = new Map(chapters.map((c) => [c.slug, c]));
|
|
34308
|
+
return results.flatMap((r) => {
|
|
34309
|
+
const chapter = bySlug.get(r.id);
|
|
34310
|
+
if (!chapter) return [];
|
|
34311
|
+
return [{
|
|
34312
|
+
slug: chapter.slug,
|
|
34313
|
+
title: chapter.title,
|
|
34314
|
+
description: chapter.description,
|
|
34315
|
+
snippets: buildSnippets(chapter.body, tokens)
|
|
34316
|
+
}];
|
|
34317
|
+
});
|
|
32505
34318
|
})
|
|
32506
34319
|
};
|
|
32507
34320
|
});
|