76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import { filter } from '../src/index.ts';
|
||
|
||
const users = [
|
||
{
|
||
metadata: {
|
||
tags: ['premium', 'active'],
|
||
type: 'user',
|
||
region: 'beijing',
|
||
created_at: '2024-03-15T10:00:00Z'
|
||
}
|
||
},
|
||
{
|
||
metadata: {
|
||
tags: ['free'],
|
||
type: 'admin',
|
||
region: 'shanghai',
|
||
created_at: '2024-01-10T09:00:00Z'
|
||
}
|
||
},
|
||
{
|
||
metadata: {
|
||
tags: ['enterprise', 'premium'],
|
||
type: 'user',
|
||
region: 'guangzhou',
|
||
created_at: '2024-04-05T12:00:00Z'
|
||
}
|
||
}
|
||
];
|
||
|
||
console.log('=== 方式1: users.filter(createFilter("...")) ===');
|
||
|
||
console.log('\n=== premium 用户 ===');
|
||
console.log(filter(users, "WHERE metadata.tags CONTAINS 'premium'"));
|
||
|
||
console.log('\n=== 北京用户 ===');
|
||
console.log(filter(users, "WHERE metadata.region = 'beijing'"));
|
||
|
||
console.log('\n=== user 类型用户 ===');
|
||
console.log(filter(users, "WHERE metadata.type = 'user'"));
|
||
|
||
console.log('\n=== premium 且 user 类型 ===');
|
||
console.log(filter(users, "WHERE metadata.tags CONTAINS 'premium' AND metadata.type = 'user'"));
|
||
|
||
console.log('\n=== 北京或上海用户 ===');
|
||
console.log(filter(users, "WHERE metadata.region IN ['beijing', 'shanghai']"));
|
||
|
||
console.log('\n=== 方式2: filter(users, "...") (支持 ORDER BY 和 LIMIT) ===');
|
||
|
||
console.log('\n=== 北京用户,按时间倒序 ===');
|
||
console.log(filter(users, "WHERE metadata.region = 'beijing' ORDER BY metadata.created_at DESC"));
|
||
|
||
console.log('\n=== 前2个用户,按时间倒序 ===');
|
||
const limitResult = filter(users, "ORDER BY metadata.created_at DESC LIMIT 2");
|
||
console.log('Limit result length:', limitResult.length);
|
||
console.log(limitResult);
|
||
|
||
const products = [
|
||
{ name: 'Apple iPhone 15', category: 'Phone' },
|
||
{ name: 'Samsung Galaxy S24', category: 'Phone' },
|
||
{ name: 'Apple MacBook Pro', category: 'Laptop' },
|
||
{ name: 'Apple Watch', category: 'Watch' }
|
||
];
|
||
|
||
console.log('\n=== LIKE 测试 ===');
|
||
console.log('\n=== 包含 Apple 的产品 (%Apple%) ===');
|
||
console.log(filter(products, "WHERE name LIKE '%Apple%'"));
|
||
|
||
console.log('\n=== 以 Apple 开头的产品 (Apple%) ===');
|
||
console.log(filter(products, "WHERE name LIKE 'Apple%'"));
|
||
|
||
console.log('\n=== 以 Phone 结尾的产品 (%Phone) ===');
|
||
console.log(filter(products, "WHERE name LIKE '%Phone%'"));
|
||
|
||
console.log('\n=== 第二个字符任意的产品 (A_pple%) ===');
|
||
console.log(filter(products, "WHERE name LIKE 'A_pple%'"));
|