Package Exports
- wfilter-expr
- wfilter-expr/wfilterExpr.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (wfilter-expr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
wfilter
轻量级过滤表达式编译与执行工具,支持比较操作与逻辑组合,适合对数组数据进行动态筛选。
功能特性
- 支持比较操作:
===、!==、==、!=、>、<、>=、<= - 支持集合操作:
in、notin - 支持逻辑组合:
any(任一满足)、all(全部满足) - 支持将过滤表达式编译为可复用谓词函数
安装
npm install wfilter核心 API
compile(filter):编译过滤表达式,返回谓词函数(item) => booleanmatches(item, filter):判断单个对象是否匹配表达式filterBy(list, filter):按表达式过滤数组
表达式格式
过滤表达式使用元组数组形式:
- 比较表达式:
[op, field, value] - 逻辑表达式:
['any' | 'all', ...subFilters]
示例:
['>=', 'age', 18]
['in', 'status', ['active', 'trial']]
['all', ['>=', 'age', 18], ['===', 'country', 'CN']]使用示例
import { compile, matches, filterBy } from 'wfilter';
const users = [
{ id: 1, age: 16, role: 'guest', active: false },
{ id: 2, age: 20, role: 'member', active: true },
{ id: 3, age: 32, role: 'admin', active: true }
];
const expr = ['all', ['>=', 'age', 18], ['in', 'role', ['member', 'admin']]] as const;
const predicate = compile(expr);
const canPass = predicate(users[1]); // true
const oneMatched = matches(users[2], expr); // true
const result = filterBy(users, expr); // id: 2, 3项目信息
- 名称:
wfilter - 描述:过滤条件解析与执行
- 语言:TypeScript
- 许可证:MIT