@wener/common 1.0.1 → 1.0.3
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/lib/cn/DivisionCode.js +311 -0
- package/lib/cn/DivisionCode.js.map +1 -0
- package/lib/cn/Mod11Checksum.js +42 -0
- package/lib/cn/Mod11Checksum.js.map +1 -0
- package/lib/cn/Mod31Checksum.js +48 -0
- package/lib/cn/Mod31Checksum.js.map +1 -0
- package/lib/cn/ResidentIdentityCardNumber.js +50 -0
- package/lib/cn/ResidentIdentityCardNumber.js.map +1 -0
- package/lib/cn/UnifiedSocialCreditCode.js +118 -0
- package/lib/cn/UnifiedSocialCreditCode.js.map +1 -0
- package/lib/cn/formatDate.js +15 -0
- package/lib/cn/formatDate.js.map +1 -0
- package/lib/cn/index.js +4 -0
- package/lib/cn/index.js.map +1 -0
- package/lib/cn/parseSex.js +22 -0
- package/lib/cn/parseSex.js.map +1 -0
- package/lib/cn/types.d.js +8 -0
- package/lib/cn/types.d.js.map +1 -0
- package/lib/meta/defineFileType.js +44 -0
- package/lib/meta/defineFileType.js.map +1 -0
- package/lib/meta/defineMetadata.js +14 -3
- package/lib/meta/defineMetadata.js.map +1 -1
- package/lib/meta/index.js +1 -0
- package/lib/meta/index.js.map +1 -1
- package/lib/parseSort.js +15 -0
- package/lib/parseSort.js.map +1 -1
- package/lib/search/AdvanceSearch.js +10 -0
- package/lib/search/AdvanceSearch.js.map +1 -0
- package/lib/search/formatAdvanceSearch.js +64 -0
- package/lib/search/formatAdvanceSearch.js.map +1 -0
- package/lib/search/index.js +2 -0
- package/lib/search/index.js.map +1 -0
- package/lib/search/optimizeAdvanceSearch.js +89 -0
- package/lib/search/optimizeAdvanceSearch.js.map +1 -0
- package/lib/search/parseAdvanceSearch.js +20 -0
- package/lib/search/parseAdvanceSearch.js.map +1 -0
- package/lib/search/parser.d.js +3 -0
- package/lib/search/parser.d.js.map +1 -0
- package/lib/search/parser.js +3065 -0
- package/lib/search/parser.js.map +1 -0
- package/lib/search/types.d.js +3 -0
- package/lib/search/types.d.js.map +1 -0
- package/package.json +12 -4
- package/src/cn/DivisionCode.test.ts +50 -0
- package/src/cn/DivisionCode.ts +253 -0
- package/src/cn/Mod11Checksum.ts +24 -0
- package/src/cn/Mod31Checksum.ts +36 -0
- package/src/cn/ResidentIdentityCardNumber.test.ts +21 -0
- package/src/cn/ResidentIdentityCardNumber.ts +96 -0
- package/src/cn/UnifiedSocialCreditCode.test.ts +16 -0
- package/src/cn/UnifiedSocialCreditCode.ts +148 -0
- package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +15 -0
- package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +41 -0
- package/src/cn/formatDate.ts +12 -0
- package/src/cn/index.ts +3 -0
- package/src/cn/parseSex.ts +27 -0
- package/src/cn/types.d.ts +51 -0
- package/src/meta/defineFileType.tsx +68 -0
- package/src/meta/defineMetadata.ts +16 -3
- package/src/meta/index.ts +2 -0
- package/src/parseSort.test.ts +1 -0
- package/src/parseSort.ts +18 -0
- package/src/search/AdvanceSearch.test.ts +156 -0
- package/src/search/AdvanceSearch.ts +14 -0
- package/src/search/Makefile +2 -0
- package/src/search/__snapshots__/AdvanceSearch.test.ts.snap +675 -0
- package/src/search/formatAdvanceSearch.ts +61 -0
- package/src/search/index.ts +1 -0
- package/src/search/optimizeAdvanceSearch.ts +90 -0
- package/src/search/parseAdvanceSearch.ts +26 -0
- package/src/search/parser.d.ts +8 -0
- package/src/search/parser.js +2794 -0
- package/src/search/parser.peggy +237 -0
- package/src/search/types.d.ts +71 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { match } from 'ts-pattern';
|
|
2
|
+
import { AdvanceSearch } from './AdvanceSearch';
|
|
3
|
+
|
|
4
|
+
export function formatAdvanceSearch(input: AdvanceSearch.Expr[]) {
|
|
5
|
+
const OP = {
|
|
6
|
+
match: ':',
|
|
7
|
+
eq: ':=',
|
|
8
|
+
ne: ':!=',
|
|
9
|
+
gt: ':>',
|
|
10
|
+
lt: ':<',
|
|
11
|
+
gte: ':>=',
|
|
12
|
+
lte: ':<=',
|
|
13
|
+
range: ':',
|
|
14
|
+
} as const;
|
|
15
|
+
|
|
16
|
+
const _exprs = (s: AdvanceSearch.Expr[]): string => {
|
|
17
|
+
return s.map(_expr).join(' ');
|
|
18
|
+
};
|
|
19
|
+
const _expr = (s: AdvanceSearch.Expr): string => {
|
|
20
|
+
return match(s)
|
|
21
|
+
.with({ type: 'keyword' }, ({ value, exact, negative }) => {
|
|
22
|
+
return `${negative ? '-' : ''}${exact ? `"${value}"` : value}`;
|
|
23
|
+
})
|
|
24
|
+
.with({ type: 'logical' }, ({ operator, value }) => value.map(_expr).join(` ${operator.toLocaleUpperCase()} `))
|
|
25
|
+
.with({ type: 'not' }, ({ value }) => `NOT ${_expr(value)}`)
|
|
26
|
+
.with({ type: 'compare' }, ({ field, operator, value, negative, mention }) => {
|
|
27
|
+
return `${negative ? '-' : ''}${mention ? '@' : ''}${field}${OP[operator]}${_value(value)}`;
|
|
28
|
+
})
|
|
29
|
+
.with({ type: 'comment' }, ({ value }) => `/* ${value} */`)
|
|
30
|
+
.with({ type: 'parentheses' }, ({ value }) => `(${_exprs(value)})`)
|
|
31
|
+
.exhaustive();
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const _literal = (s: string | null | number) => {
|
|
35
|
+
if (typeof s === 'string') {
|
|
36
|
+
return s.includes(' ') ? `"${s}"` : s;
|
|
37
|
+
}
|
|
38
|
+
return JSON.stringify(s);
|
|
39
|
+
};
|
|
40
|
+
const _value = (v: AdvanceSearch.Value): string => {
|
|
41
|
+
return match(v)
|
|
42
|
+
.with({ type: 'range' }, ({ minimum, maximum, minimumExclusive, maximumExclusive }) => {
|
|
43
|
+
if (minimumExclusive === undefined && maximumExclusive === undefined) {
|
|
44
|
+
let min = minimum === undefined ? '*' : _value(minimum);
|
|
45
|
+
let max = maximum === undefined ? '*' : _value(maximum);
|
|
46
|
+
return `${min}..${max}`;
|
|
47
|
+
}
|
|
48
|
+
let min = minimum === undefined ? '' : _value(minimum);
|
|
49
|
+
let max = maximum === undefined ? '' : _value(maximum);
|
|
50
|
+
return `${minimumExclusive ? '(' : '['}${min},${max}${maximumExclusive ? ')' : ']'}`;
|
|
51
|
+
})
|
|
52
|
+
.with({ format: 'mention' }, ({ value }) => {
|
|
53
|
+
return `@${value}`;
|
|
54
|
+
})
|
|
55
|
+
.otherwise((value) => {
|
|
56
|
+
return _literal(value.value);
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return _exprs(input);
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AdvanceSearch } from './AdvanceSearch';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { arrayOfMaybeArray, deepEqual, type MaybeArray } from '@wener/utils';
|
|
2
|
+
import { match } from 'ts-pattern';
|
|
3
|
+
import { AdvanceSearch } from './AdvanceSearch';
|
|
4
|
+
|
|
5
|
+
export function optimizeAdvanceSearch(expr: AdvanceSearch.Exprs): AdvanceSearch.Exprs {
|
|
6
|
+
const NEG = {
|
|
7
|
+
eq: 'ne',
|
|
8
|
+
ne: 'eq',
|
|
9
|
+
gt: 'lte',
|
|
10
|
+
lt: 'gte',
|
|
11
|
+
gte: 'lt',
|
|
12
|
+
lte: 'gt',
|
|
13
|
+
} as const;
|
|
14
|
+
const _expr = (e: AdvanceSearch.Expr): MaybeArray<AdvanceSearch.Expr> => {
|
|
15
|
+
// merge Exprs to AND ?
|
|
16
|
+
return (
|
|
17
|
+
match(e)
|
|
18
|
+
// (EXPR) -> EXPR
|
|
19
|
+
// TODO (EXPR EXPR) -> EXPR AND EXPR
|
|
20
|
+
.with({ type: 'parentheses' }, (expr) => {
|
|
21
|
+
// unwrap
|
|
22
|
+
if (expr.value.length < 2) {
|
|
23
|
+
return expr.value[0];
|
|
24
|
+
}
|
|
25
|
+
expr.value = expr.value.flatMap(_expr);
|
|
26
|
+
return expr;
|
|
27
|
+
})
|
|
28
|
+
.with({ type: 'comment' }, (expr) => {
|
|
29
|
+
// remove empty comment
|
|
30
|
+
if (!expr.value.length) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
return expr;
|
|
34
|
+
})
|
|
35
|
+
// NOT
|
|
36
|
+
.with({ type: 'not' }, (expr) => {
|
|
37
|
+
let out = arrayOfMaybeArray(_expr(expr.value));
|
|
38
|
+
if (!out.length) {
|
|
39
|
+
return [];
|
|
40
|
+
} else if (out.length === 1) {
|
|
41
|
+
expr.value = out[0];
|
|
42
|
+
} else {
|
|
43
|
+
throw new Error('NOT should have only one value');
|
|
44
|
+
}
|
|
45
|
+
return (
|
|
46
|
+
match(expr.value)
|
|
47
|
+
// NOT NOT EXPR -> EXPR
|
|
48
|
+
.with({ type: 'not' }, (expr) => expr.value)
|
|
49
|
+
// NOT EXPR -> -EXPR
|
|
50
|
+
.with({ type: 'compare' }, (expr) => {
|
|
51
|
+
return {
|
|
52
|
+
...expr,
|
|
53
|
+
negative: !expr.negative,
|
|
54
|
+
};
|
|
55
|
+
})
|
|
56
|
+
.with({ type: 'keyword' }, (expr) => {
|
|
57
|
+
return {
|
|
58
|
+
...expr,
|
|
59
|
+
negative: !expr.negative,
|
|
60
|
+
};
|
|
61
|
+
})
|
|
62
|
+
.otherwise(() => expr)
|
|
63
|
+
);
|
|
64
|
+
})
|
|
65
|
+
.with({ type: 'compare' }, (expr) => {
|
|
66
|
+
// negative by swap operator
|
|
67
|
+
if (expr.negative) {
|
|
68
|
+
const ne = NEG[expr.operator as keyof typeof NEG];
|
|
69
|
+
if (ne) {
|
|
70
|
+
expr.operator = ne;
|
|
71
|
+
expr.negative = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (expr.operator === 'range') {
|
|
75
|
+
}
|
|
76
|
+
return expr;
|
|
77
|
+
})
|
|
78
|
+
.otherwise((e) => e)
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let last = expr;
|
|
83
|
+
while (true) {
|
|
84
|
+
let next = structuredClone(last).flatMap(_expr);
|
|
85
|
+
if (deepEqual(last, next)) {
|
|
86
|
+
return last;
|
|
87
|
+
}
|
|
88
|
+
last = next;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AdvanceSearch } from './AdvanceSearch';
|
|
2
|
+
import { parse } from './parser';
|
|
3
|
+
|
|
4
|
+
export function parseAdvanceSearch(s: string | undefined | null): AdvanceSearch.Expr[] {
|
|
5
|
+
s = s?.trim();
|
|
6
|
+
if (!s) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// no Logical, no Compare, no Quote, no Comment
|
|
11
|
+
if (!/AND|OR|NOT|[-"():]|\/\*/.test(s)) {
|
|
12
|
+
// fast path
|
|
13
|
+
return s
|
|
14
|
+
.split(/\s+/)
|
|
15
|
+
.map((v) => v.trim())
|
|
16
|
+
.filter(Boolean)
|
|
17
|
+
.map((v) => {
|
|
18
|
+
return {
|
|
19
|
+
type: 'keyword',
|
|
20
|
+
value: v,
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return parse(s);
|
|
26
|
+
}
|