feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 Kirollos Risk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+220
View File
@@ -0,0 +1,220 @@
# Fuse.js
![Node.js CI](https://github.com/krisk/Fuse/workflows/Node.js%20CI/badge.svg)
[![Version](https://img.shields.io/npm/v/fuse.js.svg)](https://www.npmjs.com/package/fuse.js)
[![Downloads](https://img.shields.io/npm/dm/fuse.js.svg)](https://npmcharts.com/compare/fuse.js?minimal=tru)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Contributors](https://img.shields.io/github/contributors/krisk/fuse.svg)](https://github.com/krisk/Fuse/graphs/contributors)
![License](https://img.shields.io/npm/l/fuse.js.svg)
Fuse.js is a lightweight, zero-dependency fuzzy-search library written in TypeScript. It works in the browser and on the server, and is designed for searching small-to-medium datasets on the client side where you can't rely on a dedicated search backend.
## ✨ What's New: Token Search
Multi-word fuzzy search with relevance ranking. Type `"javascrpt paterns"` and find `"JavaScript Patterns"` — typo tolerance, multiple words, and smart ranking all at once.
```js
const fuse = new Fuse(docs, {
useTokenSearch: true,
keys: ['title', 'author', 'description']
})
fuse.search('javascrpt paterns')
// → [{ item: { title: 'JavaScript Patterns', ... } }]
```
See [Token Search](#token-search) below for details.
## 🧪 Beta: Web Workers
Search large datasets without freezing the UI. `FuseWorker` splits your data across multiple Web Workers and searches in parallel — ~5x faster on 100K documents.
```js
import { FuseWorker } from 'fuse.js/worker'
const fuse = new FuseWorker(docs, {
keys: ['title', 'author', 'description']
})
const results = await fuse.search('query')
fuse.terminate()
```
Same options and results as `Fuse` — just async. Function-valued options (`sortFn`, `getFn`, `keys[].getFn`) aren't supported because functions can't be transferred to a worker; everything else carries over. See the [Web Workers docs](https://fusejs.io/web-workers.html) for the interactive demo and full API.
## Installation
```bash
npm install fuse.js
```
```bash
yarn add fuse.js
```
Or include directly via CDN:
```html
<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.min.mjs"></script>
```
## Quick Start
```js
import Fuse from 'fuse.js'
const books = [
{ title: "Old Man's War", author: 'John Scalzi' },
{ title: 'The Lock Artist', author: 'Steve Hamilton' },
{ title: 'HTML5', author: 'Remy Sharp' },
{ title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' }
]
const fuse = new Fuse(books, {
keys: ['title', 'author']
})
fuse.search('javscript')
// → [{ item: { title: 'JavaScript: The Good Parts', ... }, ... }]
```
## Features
### Fuzzy Search
The core of Fuse.js. Uses the [Bitap algorithm](https://en.wikipedia.org/wiki/Bitap_algorithm) for approximate string matching — handles typos, misspellings, and partial matches out of the box.
```js
fuse.search('javscript')
// → [{ item: { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' } }]
```
### Weighted Keys
Search across multiple fields with different importance levels. Title matches can rank higher than description matches.
```js
const fuse = new Fuse(docs, {
keys: [
{ name: 'title', weight: 2 },
{ name: 'description', weight: 1 }
]
})
```
### Extended Search
Use operators for precise control: exact match (`=`), prefix (`^`), suffix (`!`), and more. Enable with `useExtendedSearch: true`.
```js
const fuse = new Fuse(list, {
useExtendedSearch: true,
keys: ['title']
})
fuse.search('=exact match') // exact match
fuse.search('^prefix') // starts with
fuse.search('!term') // does not include
```
### Token Search
Splits multi-word queries into individual terms, fuzzy-matches each independently, and ranks results using BM25-style IDF weighting. Enable with `useTokenSearch: true`.
```js
const fuse = new Fuse(docs, {
useTokenSearch: true,
keys: ['title', 'body']
})
fuse.search('express midleware rout')
// Finds "Express Middleware" and "Express Routing Guide" despite typos
```
- **Typo tolerance per word** — each term is fuzzy-matched independently
- **Relevance ranking** — rare terms are weighted higher than common ones
- **Word order independent** — `"patterns javascript"` and `"javascript patterns"` return identical results
- **No query length limit** — long multi-word queries work naturally since each term is searched separately
- **AND or OR** — `tokenMatch: 'all'` returns only records matching *every* word (filtering); the default `'any'` matches any word
Available in the full build. See [TOKEN_SEARCH.md](TOKEN_SEARCH.md) for details and performance benchmarks.
### Logical Search
Combine conditions with `$and` and `$or` for complex queries. Available in the full build.
```js
fuse.search({
$and: [
{ title: 'javascript' },
{ author: 'crockford' }
]
})
```
### Match Highlighting
Get character-level match indices for highlighting search results in your UI.
```js
const fuse = new Fuse(list, {
includeMatches: true,
keys: ['title']
})
const result = fuse.search('javscript')
// result[0].matches[0].indices → [[0, 9]]
```
### Single String Matching
Use `Fuse.match()` to fuzzy-match a pattern against a single string without creating an index. Useful for one-off comparisons or custom filtering.
```js
const result = Fuse.match('javscript', 'JavaScript: The Good Parts')
// → { isMatch: true, score: 0.04, indices: [[0, 9]] }
```
`Fuse.match()` does **not** support `useTokenSearch` — token search requires corpus-level statistics (`df`, `fieldCount`) that a one-off string comparison can't provide. Passing `useTokenSearch: true` throws an explicit error. Use `new Fuse(docs, { useTokenSearch: true }).search(query)` for token-search behavior.
### Dynamic Collections
Add and remove documents from a live index without rebuilding.
```js
fuse.add({ title: 'New Book', author: 'New Author' })
fuse.remove((doc) => doc.title === 'Old Book')
```
## Builds
Fuse.js ships in two variants:
| Build | Includes | Min + gzip |
|---|---|---|
| **Full** | Fuzzy + Extended + Logical + Token search | ~8.6 kB |
| **Basic** | Fuzzy search only | ~6.8 kB |
Use the basic build if you only need fuzzy search and want the smallest bundle size.
## Documentation
For the full API reference, configuration options, scoring theory, and interactive demos, visit **[fusejs.io](https://fusejs.io)**.
## Official ports
- **[fuse-swift](https://github.com/krisk/fuse-swift)**: Swift port for iOS, macOS, tvOS, watchOS, visionOS, and Linux. Byte-equivalent results, idiomatic Swift API, syncs with each upstream release. Currently in 2.0.0-rc.1.
## Supporting Fuse.js
- [Become a backer or sponsor on **GitHub**](https://github.com/sponsors/krisk)
- [Become a backer or sponsor on **Patreon**](https://patreon.com/fusejs)
- [One-time donation via **PayPal**](https://www.paypal.me/kirorisk)
## Develop
See [DEVELOPERS.md](DEVELOPERS.md) for setup, scripts, and project structure.
## Contribute
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on issues and pull requests.
+257
View File
@@ -0,0 +1,257 @@
/**
* Fuse.js v7.4.0 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2026 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
'use strict';
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
const FUSE_WORKER_UNSUPPORTED_FN_OPTION = option => `FuseWorker does not support function-valued option '${option}': ` + `functions cannot be transferred to Web Workers via postMessage. ` + `Remove this option or fall back to Fuse.`;
const FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED = `FuseWorker does not support useTokenSearch: token search depends on ` + `corpus-level statistics (df, fieldCount) that are computed per shard, ` + `so per-shard scores would diverge from single-thread Fuse. Use Fuse on ` + `the main thread for token search.`;
/// <reference lib="dom" />
const DEFAULT_MAX_WORKERS = 8;
function getDefaultWorkerCount() {
const hw = typeof navigator !== 'undefined' ? navigator.hardwareConcurrency || 4 : 4;
return Math.min(hw, DEFAULT_MAX_WORKERS);
}
class FuseWorker {
_shards = null;
_addCursor = 0;
_initPromise = null;
_pending = new Map();
_nextId = 0;
constructor(docs, options, workerOptions) {
this._docs = docs.slice();
this._options = options || {};
this._workerOptions = workerOptions || {};
// Reject function-valued options eagerly. Without this check, postMessage
// throws DataCloneError on first search() rather than at construction.
FuseWorker._assertNoFunctionOptions(this._options);
// Token search needs global corpus statistics, but each shard would build
// its own — scores would diverge from single-thread Fuse. Refuse upfront
// rather than silently returning ordering that doesn't match.
if (this._options.useTokenSearch) {
throw new Error(FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore -- import.meta.url is resolved by Rollup at build time
this._workerUrl = this._workerOptions.workerUrl || new URL('./fuse.worker.mjs', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('fuse-worker.cjs', document.baseURI).href)));
}
static _assertNoFunctionOptions(options) {
if (typeof options.sortFn === 'function') {
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION('sortFn'));
}
if (typeof options.getFn === 'function') {
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION('getFn'));
}
if (typeof options.tokenize === 'function') {
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION('tokenize'));
}
const keys = options.keys;
if (Array.isArray(keys)) {
for (let i = 0, len = keys.length; i < len; i += 1) {
const key = keys[i];
if (key && typeof key === 'object' && !Array.isArray(key)) {
if (typeof key.getFn === 'function') {
const name = key.name;
const label = Array.isArray(name) ? name.join('.') : name ?? String(i);
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION(`keys[${label}].getFn`));
}
}
}
}
}
_getNumWorkers() {
return this._workerOptions.numWorkers || getDefaultWorkerCount();
}
_ensureInit() {
if (this._initPromise) return this._initPromise;
this._initPromise = this._init();
return this._initPromise;
}
_spawnWorker() {
const worker = new Worker(this._workerUrl, {
type: 'module'
});
worker.onmessage = e => {
const {
id,
result,
error
} = e.data;
const handler = this._pending.get(id);
if (!handler) return;
this._pending.delete(id);
if (error) {
handler.reject(new Error(error));
} else {
handler.resolve(result);
}
};
worker.onerror = e => {
for (const [, handler] of this._pending) {
handler.reject(new Error(e.message));
}
};
return worker;
}
_workerInitOptions() {
// Force includeScore so the main thread can do a global (score, idx)
// tie-break across shards. Score is stripped from the final result if the
// caller didn't ask for it.
return {
...this._options,
includeScore: true
};
}
async _init() {
const numWorkers = this._getNumWorkers();
const chunkSize = Math.ceil(this._docs.length / numWorkers);
this._shards = [];
this._addCursor = 0;
const workerInitOptions = this._workerInitOptions();
const initPromises = [];
for (let i = 0; i < numWorkers; i++) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, this._docs.length);
const chunk = this._docs.slice(start, end);
const globalIndices = [];
for (let j = start; j < end; j += 1) globalIndices.push(j);
const shard = {
worker: this._spawnWorker(),
globalIndices
};
this._shards.push(shard);
initPromises.push(this._call(shard.worker, 'init', [chunk, workerInitOptions]));
}
await Promise.all(initPromises);
}
_call(worker, method, args) {
const id = this._nextId++;
return new Promise((resolve, reject) => {
this._pending.set(id, {
resolve,
reject
});
worker.postMessage({
id,
method,
args
});
});
}
async search(query, options) {
await this._ensureInit();
const shards = this._shards;
const results = await Promise.all(shards.map(s => this._call(s.worker, 'search', [query, options])));
// Merge results from all shards, rewriting refIndex from shard-local to global
const merged = [];
for (let i = 0, len = results.length; i < len; i += 1) {
const {
globalIndices
} = shards[i];
for (const r of results[i]) {
merged.push({
...r,
refIndex: globalIndices[r.refIndex]
});
}
}
const shouldSort = this._options.shouldSort !== false;
if (shouldSort) {
// Mirror Fuse's default sortFn: (score, idx) tie-break, but on the
// GLOBAL refIndex so equal-score results across shards collapse into a
// deterministic order matching single-thread Fuse.
merged.sort((a, b) => {
const sa = a.score ?? 0;
const sb = b.score ?? 0;
if (sa === sb) {
return a.refIndex < b.refIndex ? -1 : 1;
}
return sa < sb ? -1 : 1;
});
} else {
// Restore global collection order. Round-robin add() and
// shard-concatenation order otherwise leak through.
merged.sort((a, b) => a.refIndex - b.refIndex);
}
// Workers always include score so the merge above can tie-break; strip it
// here if the caller didn't ask for it. Rebuild the object instead of
// `delete`-ing — `delete` deopts the shape and roughly doubles the
// post-sort cost on large result sets.
if (!this._options.includeScore) {
for (let i = 0, len = merged.length; i < len; i += 1) {
const r = merged[i];
merged[i] = r.matches !== undefined ? {
item: r.item,
refIndex: r.refIndex,
matches: r.matches
} : {
item: r.item,
refIndex: r.refIndex
};
}
}
const limit = options?.limit;
if (limit && limit > 0) {
return merged.slice(0, limit);
}
return merged;
}
async add(doc) {
await this._ensureInit();
const shards = this._shards;
const shard = shards[this._addCursor % shards.length];
this._addCursor += 1;
const globalIdx = this._docs.length;
this._docs.push(doc);
shard.globalIndices.push(globalIdx);
await this._call(shard.worker, 'add', [doc]);
}
async setCollection(docs) {
this._docs = docs.slice();
if (!this._shards) {
this._initPromise = null;
return;
}
const shards = this._shards;
const chunkSize = Math.ceil(this._docs.length / shards.length);
this._addCursor = 0;
const tasks = [];
for (let i = 0, len = shards.length; i < len; i += 1) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, this._docs.length);
const chunk = this._docs.slice(start, end);
const globalIndices = [];
for (let j = start; j < end; j += 1) globalIndices.push(j);
shards[i].globalIndices = globalIndices;
tasks.push(this._call(shards[i].worker, 'setCollection', [chunk]));
}
await Promise.all(tasks);
}
terminate() {
if (this._shards) {
for (const {
worker
} of this._shards) {
worker.terminate();
}
this._shards = null;
}
this._initPromise = null;
for (const [, handler] of this._pending) {
handler.reject(new Error('FuseWorker terminated'));
}
this._pending.clear();
}
}
exports.FuseWorker = FuseWorker;
+254
View File
@@ -0,0 +1,254 @@
/**
* Fuse.js v7.4.0 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2026 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
const FUSE_WORKER_UNSUPPORTED_FN_OPTION = option => `FuseWorker does not support function-valued option '${option}': ` + `functions cannot be transferred to Web Workers via postMessage. ` + `Remove this option or fall back to Fuse.`;
const FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED = `FuseWorker does not support useTokenSearch: token search depends on ` + `corpus-level statistics (df, fieldCount) that are computed per shard, ` + `so per-shard scores would diverge from single-thread Fuse. Use Fuse on ` + `the main thread for token search.`;
/// <reference lib="dom" />
const DEFAULT_MAX_WORKERS = 8;
function getDefaultWorkerCount() {
const hw = typeof navigator !== 'undefined' ? navigator.hardwareConcurrency || 4 : 4;
return Math.min(hw, DEFAULT_MAX_WORKERS);
}
class FuseWorker {
_shards = null;
_addCursor = 0;
_initPromise = null;
_pending = new Map();
_nextId = 0;
constructor(docs, options, workerOptions) {
this._docs = docs.slice();
this._options = options || {};
this._workerOptions = workerOptions || {};
// Reject function-valued options eagerly. Without this check, postMessage
// throws DataCloneError on first search() rather than at construction.
FuseWorker._assertNoFunctionOptions(this._options);
// Token search needs global corpus statistics, but each shard would build
// its own — scores would diverge from single-thread Fuse. Refuse upfront
// rather than silently returning ordering that doesn't match.
if (this._options.useTokenSearch) {
throw new Error(FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore -- import.meta.url is resolved by Rollup at build time
this._workerUrl = this._workerOptions.workerUrl || new URL('./fuse.worker.mjs', import.meta.url);
}
static _assertNoFunctionOptions(options) {
if (typeof options.sortFn === 'function') {
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION('sortFn'));
}
if (typeof options.getFn === 'function') {
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION('getFn'));
}
if (typeof options.tokenize === 'function') {
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION('tokenize'));
}
const keys = options.keys;
if (Array.isArray(keys)) {
for (let i = 0, len = keys.length; i < len; i += 1) {
const key = keys[i];
if (key && typeof key === 'object' && !Array.isArray(key)) {
if (typeof key.getFn === 'function') {
const name = key.name;
const label = Array.isArray(name) ? name.join('.') : name ?? String(i);
throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION(`keys[${label}].getFn`));
}
}
}
}
}
_getNumWorkers() {
return this._workerOptions.numWorkers || getDefaultWorkerCount();
}
_ensureInit() {
if (this._initPromise) return this._initPromise;
this._initPromise = this._init();
return this._initPromise;
}
_spawnWorker() {
const worker = new Worker(this._workerUrl, {
type: 'module'
});
worker.onmessage = e => {
const {
id,
result,
error
} = e.data;
const handler = this._pending.get(id);
if (!handler) return;
this._pending.delete(id);
if (error) {
handler.reject(new Error(error));
} else {
handler.resolve(result);
}
};
worker.onerror = e => {
for (const [, handler] of this._pending) {
handler.reject(new Error(e.message));
}
};
return worker;
}
_workerInitOptions() {
// Force includeScore so the main thread can do a global (score, idx)
// tie-break across shards. Score is stripped from the final result if the
// caller didn't ask for it.
return {
...this._options,
includeScore: true
};
}
async _init() {
const numWorkers = this._getNumWorkers();
const chunkSize = Math.ceil(this._docs.length / numWorkers);
this._shards = [];
this._addCursor = 0;
const workerInitOptions = this._workerInitOptions();
const initPromises = [];
for (let i = 0; i < numWorkers; i++) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, this._docs.length);
const chunk = this._docs.slice(start, end);
const globalIndices = [];
for (let j = start; j < end; j += 1) globalIndices.push(j);
const shard = {
worker: this._spawnWorker(),
globalIndices
};
this._shards.push(shard);
initPromises.push(this._call(shard.worker, 'init', [chunk, workerInitOptions]));
}
await Promise.all(initPromises);
}
_call(worker, method, args) {
const id = this._nextId++;
return new Promise((resolve, reject) => {
this._pending.set(id, {
resolve,
reject
});
worker.postMessage({
id,
method,
args
});
});
}
async search(query, options) {
await this._ensureInit();
const shards = this._shards;
const results = await Promise.all(shards.map(s => this._call(s.worker, 'search', [query, options])));
// Merge results from all shards, rewriting refIndex from shard-local to global
const merged = [];
for (let i = 0, len = results.length; i < len; i += 1) {
const {
globalIndices
} = shards[i];
for (const r of results[i]) {
merged.push({
...r,
refIndex: globalIndices[r.refIndex]
});
}
}
const shouldSort = this._options.shouldSort !== false;
if (shouldSort) {
// Mirror Fuse's default sortFn: (score, idx) tie-break, but on the
// GLOBAL refIndex so equal-score results across shards collapse into a
// deterministic order matching single-thread Fuse.
merged.sort((a, b) => {
const sa = a.score ?? 0;
const sb = b.score ?? 0;
if (sa === sb) {
return a.refIndex < b.refIndex ? -1 : 1;
}
return sa < sb ? -1 : 1;
});
} else {
// Restore global collection order. Round-robin add() and
// shard-concatenation order otherwise leak through.
merged.sort((a, b) => a.refIndex - b.refIndex);
}
// Workers always include score so the merge above can tie-break; strip it
// here if the caller didn't ask for it. Rebuild the object instead of
// `delete`-ing — `delete` deopts the shape and roughly doubles the
// post-sort cost on large result sets.
if (!this._options.includeScore) {
for (let i = 0, len = merged.length; i < len; i += 1) {
const r = merged[i];
merged[i] = r.matches !== undefined ? {
item: r.item,
refIndex: r.refIndex,
matches: r.matches
} : {
item: r.item,
refIndex: r.refIndex
};
}
}
const limit = options?.limit;
if (limit && limit > 0) {
return merged.slice(0, limit);
}
return merged;
}
async add(doc) {
await this._ensureInit();
const shards = this._shards;
const shard = shards[this._addCursor % shards.length];
this._addCursor += 1;
const globalIdx = this._docs.length;
this._docs.push(doc);
shard.globalIndices.push(globalIdx);
await this._call(shard.worker, 'add', [doc]);
}
async setCollection(docs) {
this._docs = docs.slice();
if (!this._shards) {
this._initPromise = null;
return;
}
const shards = this._shards;
const chunkSize = Math.ceil(this._docs.length / shards.length);
this._addCursor = 0;
const tasks = [];
for (let i = 0, len = shards.length; i < len; i += 1) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, this._docs.length);
const chunk = this._docs.slice(start, end);
const globalIndices = [];
for (let j = start; j < end; j += 1) globalIndices.push(j);
shards[i].globalIndices = globalIndices;
tasks.push(this._call(shards[i].worker, 'setCollection', [chunk]));
}
await Promise.all(tasks);
}
terminate() {
if (this._shards) {
for (const {
worker
} of this._shards) {
worker.terminate();
}
this._shards = null;
}
this._initPromise = null;
for (const [, handler] of this._pending) {
handler.reject(new Error('FuseWorker terminated'));
}
this._pending.clear();
}
}
export { FuseWorker };
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
+2261
View File
File diff suppressed because it is too large Load Diff
+333
View File
@@ -0,0 +1,333 @@
// Type definitions for Fuse.js v7.4.0
// TypeScript v6.0.3
type RangeTuple = [number, number];
interface SearchResult {
isMatch: boolean;
score: number;
indices?: ReadonlyArray<RangeTuple>;
/** @internal Aggregation flag for extended-search inverse terms. */
hasInverse?: boolean;
/**
* @internal Token-search `tokenMatch: 'all'` coverage for this text.
* `matchedMask` bit `i` ⇒ query term `i` matched here (≤31-term fast path);
* `matchedTerms` is the equivalent set for the ≥32-term fallback.
*/
matchedMask?: number;
/** @internal */
matchedTerms?: Set<number>;
/** @internal Query token count; descriptor for the record-level AND gate. */
termCount?: number;
}
interface Searcher {
searchIn(text: string): SearchResult;
}
interface FuseOptionKeyObject<T> {
name: string | string[];
weight?: number;
getFn?: (obj: T) => ReadonlyArray<string> | string | null | undefined;
}
type FuseOptionKey<T> = FuseOptionKeyObject<T> | string | string[];
interface KeyObject {
path: string[];
id: string;
weight: number;
src: string | string[];
getFn?: ((obj: any) => ReadonlyArray<string> | string | null | undefined) | null;
}
type FuseGetFunction<T> = (obj: T, path: string | string[]) => ReadonlyArray<string> | string;
type GetFunction = (obj: any, path: string | string[]) => any;
/**
* Custom tokenizer for `useTokenSearch`. Receives the field/query text after
* case-folding and diacritic-stripping (per `isCaseSensitive` /
* `ignoreDiacritics`) and must return the term list. Functions must be
* deterministic — non-deterministic output silently breaks `df` accounting.
*/
type FuseTokenizeFunction = (text: string) => string[];
interface NormInterface {
get(value: string): number;
clear(): void;
}
interface RecordEntryObject {
/** The text value */
v: string;
/** The field-length norm */
n: number;
}
type RecordEntryArrayItem = ReadonlyArray<RecordEntryObject & {
i: number;
}>;
type RecordEntry = {
[key: string]: RecordEntryObject | RecordEntryArrayItem;
};
interface FuseIndexObjectRecord {
/** The index of the record in the source list */
i: number;
$: RecordEntry;
}
interface FuseIndexStringRecord {
/** The index of the record in the source list */
i: number;
/** The text value */
v: string;
/** The field-length norm */
n: number;
}
type FuseIndexRecords = ReadonlyArray<FuseIndexObjectRecord> | ReadonlyArray<FuseIndexStringRecord>;
interface IndexRecord {
i: number;
v?: string;
n?: number;
$?: Record<number, SubRecord | SubRecord[]>;
}
interface SubRecord {
v: string;
i?: number;
n: number;
}
type FuseSortFunctionItem = {
[key: string]: {
$: string;
} | {
$: string;
idx: number;
}[];
};
type FuseSortFunctionMatch = {
score: number;
key: KeyObject;
value: string;
indices: ReadonlyArray<number>[];
};
type FuseSortFunctionMatchList = FuseSortFunctionMatch & {
idx: number;
};
type FuseSortFunctionArg = {
idx: number;
item: FuseSortFunctionItem;
score: number;
matches?: (FuseSortFunctionMatch | FuseSortFunctionMatchList)[];
};
type FuseSortFunction = (a: FuseSortFunctionArg, b: FuseSortFunctionArg) => number;
interface MatchScore {
score: number;
key?: KeyObject | null;
value: string;
idx?: number;
hasInverse?: boolean;
norm: number;
indices?: ReadonlyArray<RangeTuple>;
/** @internal Token-search `tokenMatch: 'all'` coverage carried up for record-level gating. */
matchedMask?: number;
/** @internal */
matchedTerms?: Set<number>;
/** @internal */
termCount?: number;
}
interface InternalResult {
idx: number;
item: any;
score?: number;
matches: MatchScore[];
}
interface IFuseOptions<T> {
/** Indicates whether comparisons should be case sensitive. */
isCaseSensitive?: boolean;
/** Indicates whether comparisons should ignore diacritics (accents). */
ignoreDiacritics?: boolean;
/** Determines how close the match must be to the fuzzy location. */
distance?: number;
/** When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string. */
findAllMatches?: boolean;
/** The function to use to retrieve an object's value at the provided path. */
getFn?: FuseGetFunction<T>;
/** When `true`, search will ignore `location` and `distance`. */
ignoreLocation?: boolean;
/** When `true`, the calculation for the relevance score will ignore the field-length norm. */
ignoreFieldNorm?: boolean;
/** Determines how much the field-length norm affects scoring. */
fieldNormWeight?: number;
/** Whether the matches should be included in the result set. */
includeMatches?: boolean;
/** Whether the score should be included in the result set. */
includeScore?: boolean;
/** List of keys that will be searched. */
keys?: Array<FuseOptionKey<T>>;
/** Determines approximately where in the text is the pattern expected to be found. */
location?: number;
/** Only the matches whose length exceeds this value will be returned. */
minMatchCharLength?: number;
/** Whether to sort the result list, by score. */
shouldSort?: boolean;
/** The function to use to sort all the results. */
sortFn?: FuseSortFunction;
/** At what point does the match algorithm give up. */
threshold?: number;
/** When `true`, it enables the use of unix-like search commands. */
useExtendedSearch?: boolean;
/** When `true`, enables token search with TF-IDF scoring. */
useTokenSearch?: boolean;
/**
* Tokenizer used by `useTokenSearch`, applied identically at index-build
* and query time. Accepts either a global `RegExp` or a function returning
* `string[]`. Defaults to `/[\p{L}\p{M}\p{N}_]+/gu`, which handles CJK,
* Cyrillic, Greek, Arabic, Hebrew, Devanagari, etc. out of the box. Use a
* function form (e.g. wrapping `Intl.Segmenter`) for word-segmentation in
* scripts without whitespace boundaries.
*/
tokenize?: RegExp | FuseTokenizeFunction;
/**
* How the words of a multi-word query combine, for `useTokenSearch` only.
* `'any'` (default) returns a record if it matches any query word (OR);
* `'all'` returns it only when every query word matches somewhere in the
* record — any field or array element (AND). Use `'all'` for filtering,
* where adding a word should narrow the results. Has no effect unless
* `useTokenSearch` is `true`.
*/
tokenMatch?: 'all' | 'any';
}
interface FuseIndexOptions<T> {
getFn?: FuseGetFunction<T>;
fieldNormWeight?: number;
}
interface FuseSearchOptions {
limit?: number;
}
interface FuseResultMatch {
indices: ReadonlyArray<RangeTuple>;
key?: string;
refIndex?: number;
value?: string;
}
interface FuseResult<T> {
item: T;
refIndex: number;
score?: number;
matches?: ReadonlyArray<FuseResultMatch>;
}
type Expression = string | {
[key: string]: string;
} | {
$path: ReadonlyArray<string>;
$val: string;
} | {
$and?: Expression[];
} | {
$or?: Expression[];
};
declare class FuseIndex<T = any> {
norm: NormInterface;
getFn: GetFunction;
isCreated: boolean;
docs: ReadonlyArray<T>;
records: IndexRecord[];
keys: KeyObject[];
_keysMap: Record<string, number>;
constructor({ getFn, fieldNormWeight }?: FuseIndexOptions<T>);
setSources(docs?: ReadonlyArray<T>): void;
setIndexRecords(records?: IndexRecord[]): void;
setKeys(keys?: KeyObject[]): void;
create(): void;
add(doc: T, docIndex: number): IndexRecord | null;
removeAt(idx: number): void;
removeAll(indices: number[]): void;
getValueForItemAtKeyId(item: any, keyId: string): any;
size(): number;
_createStringRecord(doc: string, docIndex: number): IndexRecord | null;
_createObjectRecord(doc: any, docIndex: number): IndexRecord;
toJSON(): {
keys: ReadonlyArray<Omit<KeyObject, 'getFn'>>;
records: IndexRecord[];
};
}
declare function createIndex<T>(keys: FuseOptionKey<T>[], docs: ReadonlyArray<T>, { getFn, fieldNormWeight }?: FuseIndexOptions<T>): FuseIndex<T>;
declare function parseIndex<T>(data: {
keys: ReadonlyArray<KeyObject>;
records: IndexRecord[];
}, { getFn, fieldNormWeight }?: FuseIndexOptions<T>): FuseIndex<T>;
declare class KeyStore {
_keys: KeyObject[];
_keyMap: Record<string, KeyObject>;
constructor(keys: FuseOptionKey<any>[]);
get(keyId: string): KeyObject;
keys(): KeyObject[];
toJSON(): string;
}
interface ParsedLeaf {
keyId: string | null;
pattern: string;
searcher?: Searcher;
}
interface ParsedOperator {
children: ParsedNode[];
operator: string;
}
type ParsedNode = ParsedLeaf | ParsedOperator;
declare function parse(query: Expression, options: any, { auto }?: {
auto?: boolean | undefined;
}): ParsedNode;
declare const Config: Required<IFuseOptions<any>>;
declare class MaxHeap {
limit: number;
heap: InternalResult[];
constructor(limit: number);
get size(): number;
shouldInsert(score: number): boolean;
insert(item: InternalResult): void;
extractSorted(sortFn: (a: InternalResult, b: InternalResult) => number): InternalResult[];
_bubbleUp(i: number): void;
_sinkDown(i: number): void;
}
interface InvertedIndexData {
fieldCount: number;
df: Map<string, number>;
docFieldCount: Map<number, number>;
docTermFieldHits: Map<number, Map<string, number>>;
}
interface HeapSearchOptions {
heap?: MaxHeap;
ignoreFieldNorm?: boolean;
}
declare class Fuse<T> {
options: Required<IFuseOptions<T>>;
_keyStore: KeyStore;
_docs: T[];
_myIndex: FuseIndex<T>;
_invertedIndex: InvertedIndexData | null;
_lastQuery: string | null;
_lastSearcher: Searcher | null;
static version: string;
static createIndex: typeof createIndex;
static parseIndex: typeof parseIndex;
static config: typeof Config;
static parseQuery: typeof parse;
static use: (...plugins: any[]) => void;
static match: (pattern: string, text: string, options?: IFuseOptions<any>) => SearchResult;
constructor(docs: ReadonlyArray<T>, options?: IFuseOptions<T>, index?: FuseIndex<T>);
_getSearcher(query: string): Searcher;
setCollection(docs: ReadonlyArray<T>, index?: FuseIndex<T>): void;
add(doc: T): void;
remove(predicate?: (doc: T, idx: number) => boolean): T[];
removeAt(idx: number): T;
_invalidateSearcherCache(): void;
getIndex(): FuseIndex<T>;
search(query: string | Expression, options?: FuseSearchOptions): FuseResult<T>[];
_searchStringList(query: string, { heap, ignoreFieldNorm }?: HeapSearchOptions): InternalResult[] | null;
_searchLogical(query: Expression): InternalResult[];
_searchObjectList(query: string, { heap, ignoreFieldNorm }?: HeapSearchOptions): InternalResult[] | null;
_findMatches({ key, value, searcher }: {
key: KeyObject | null;
value: SubRecord | SubRecord[] | undefined;
searcher: Searcher;
}): MatchScore[];
_coversAllTokens(matches: MatchScore[]): boolean;
}
export { FuseIndex, Fuse as default };
export type { Expression, FuseGetFunction, FuseIndexObjectRecord, FuseIndexOptions, FuseIndexRecords, FuseIndexStringRecord, FuseOptionKey, FuseOptionKeyObject, FuseResult, FuseResultMatch, FuseSearchOptions, FuseSortFunction, FuseSortFunctionArg, FuseSortFunctionItem, FuseSortFunctionMatch, FuseSortFunctionMatchList, FuseTokenizeFunction, IFuseOptions, RangeTuple, RecordEntry, RecordEntryArrayItem, RecordEntryObject, SearchResult };
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2259
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
+131
View File
@@ -0,0 +1,131 @@
{
"name": "fuse.js",
"author": {
"name": "Kiro Risk",
"email": "kirollos@gmail.com",
"url": "http://kiro.me"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/krisk"
},
"type": "module",
"main": "./dist/fuse.cjs",
"module": "./dist/fuse.mjs",
"unpkg": "./dist/fuse.mjs",
"jsdelivr": "./dist/fuse.mjs",
"types": "./dist/fuse.d.ts",
"exports": {
".": {
"types": "./dist/fuse.d.ts",
"import": "./dist/fuse.mjs",
"require": "./dist/fuse.cjs"
},
"./min": {
"types": "./dist/fuse.d.ts",
"import": "./dist/fuse.min.mjs",
"require": "./dist/fuse.min.cjs"
},
"./basic": {
"types": "./dist/fuse.d.ts",
"import": "./dist/fuse.basic.mjs",
"require": "./dist/fuse.basic.cjs"
},
"./min-basic": {
"types": "./dist/fuse.d.ts",
"import": "./dist/fuse.basic.min.mjs",
"require": "./dist/fuse.basic.min.cjs"
},
"./worker": {
"import": "./dist/fuse-worker.mjs",
"require": "./dist/fuse-worker.cjs"
},
"./worker-script": {
"import": "./dist/fuse.worker.mjs"
}
},
"sideEffects": false,
"files": [
"dist"
],
"version": "7.4.0",
"description": "Lightweight fuzzy-search",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/krisk/Fuse.git"
},
"homepage": "http://fusejs.io",
"keywords": [
"fuzzy",
"search",
"bitap"
],
"scripts": {
"dev": "rollup -w -c scripts/configs.cjs --environment TARGET:esm-dev-full",
"dev:cjs": "rollup -w -c scripts/configs.cjs --environment TARGET:commonjs-dev-full",
"build": "rm -r dist && mkdir dist && node ./scripts/build.main.cjs",
"test": "vitest run",
"typecheck": "tsc -p src/tsconfig.json --noEmit",
"lint": "eslint src test",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.{js,ts}\"",
"format:check": "prettier --check \"src/**/*.ts\" \"test/**/*.{js,ts}\"",
"bench": "npm run build && npm run bench:search && npm run bench:index",
"bench:search": "node bench/search.mjs",
"bench:index": "node bench/index-creation.mjs",
"bench:extended": "node bench/extended.mjs",
"bench:tokens": "node bench/token-search.mjs",
"bench:workers": "node bench/parallel-workers.mjs",
"release": "./scripts/release.sh",
"docs:bump": "node ./scripts/bump-docs.cjs",
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:release": "./scripts/deploy-docs.sh",
"prepare": "husky install"
},
"standard-version": {
"scripts": {
"postbump": "npm run build && npm run lint && npm run test 2>/dev/null",
"precommit": "git add dist/*.cjs dist/*.mjs dist/*.d.ts"
}
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"overrides": {
"serialize-javascript": "^7.0.5",
"esbuild": "^0.25.0"
},
"devDependencies": {
"@babel/core": "^7.20.12",
"@babel/preset-typescript": "7.18.6",
"@commitlint/cli": "^17.4.2",
"@commitlint/config-conventional": "^17.4.2",
"@monaco-editor/loader": "^1.3.2",
"@rollup/plugin-babel": "^7.0.0",
"@rollup/plugin-node-resolve": "^16.0.3",
"@rollup/plugin-replace": "^6.0.3",
"@sapphire/stopwatch": "^1.5.0",
"@sapphire/utilities": "^3.11.0",
"eslint": "^9.39.4",
"eslint-config-prettier": "^10.1.8",
"husky": "^8.0.3",
"monaco-editor": "^0.34.1",
"prettier": "^3.8.3",
"replace-in-file": "^6.3.5",
"rollup": "^4.60.1",
"rollup-plugin-dts": "^6.4.1",
"standard-version": "^9.5.0",
"terser": "^5.16.1",
"typescript": "^6.0.2",
"typescript-eslint": "^8.58.0",
"vitepress": "^1.6.4",
"vitest": "^2.1.9",
"vue": "^3.5.33"
},
"engines": {
"node": ">=10"
}
}