From 1fc06856fcdbab58f5f0273b7fcde647c9930bfa Mon Sep 17 00:00:00 2001 From: abearxiong Date: Wed, 11 Mar 2026 00:34:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E8=87=B3=200.0.6=EF=BC=8C=E6=B7=BB=E5=8A=A0=20queryFilter=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=BB=A5=E6=94=AF=E6=8C=81=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=20Fuse.js=20=E7=9A=84=E6=9F=A5=E8=AF=A2=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 13 ++++++------- src/query-filter.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/query-filter.ts diff --git a/package.json b/package.json index b337a74..0b832c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/js-filter", - "version": "0.0.5", + "version": "0.0.6", "description": "用于 JavaScript 数组的 SQL LIKE 过滤器函数", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -10,20 +10,19 @@ "keywords": [], "author": "abearxiong (https://www.xiongxiao.me)", "license": "MIT", - "packageManager": "pnpm@10.26.0", + "packageManager": "pnpm@10.32.0", "type": "module", "exports": { - ".": { - "import": "./dist/index.js", - "types": "./dist/index.d.ts" - } + ".": "./dist/index.js", + "./query-filter": "./dist/query-filter.js" }, "files": [ "dist", "src" ], "devDependencies": { - "typescript": "^5.0.0" + "fuse.js": "^7.1.0", + "typescript": "^5.9.3" }, "publishConfig": { "access": "public" diff --git a/src/query-filter.ts b/src/query-filter.ts new file mode 100644 index 0000000..f95f0db --- /dev/null +++ b/src/query-filter.ts @@ -0,0 +1,16 @@ +import Fuse from 'fuse.js'; +import { filter } from './index'; +export const queryFilter = (opts?: { query?: string, routes?: any[] }) => { + const query = opts?.query || ''; + const routes = opts?.routes || []; + if (query.toLocaleUpperCase().startsWith('WHERE')) { + return filter(routes, query); + } + const fuse = new Fuse(routes, { + keys: ['path', 'key', 'description'], + threshold: 0.4, + }); + let findsRoutes = fuse.search(query); + const resultRoutes = findsRoutes.map(r => r.item); + return resultRoutes; +}