@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.
Files changed (74) hide show
  1. package/lib/cn/DivisionCode.js +311 -0
  2. package/lib/cn/DivisionCode.js.map +1 -0
  3. package/lib/cn/Mod11Checksum.js +42 -0
  4. package/lib/cn/Mod11Checksum.js.map +1 -0
  5. package/lib/cn/Mod31Checksum.js +48 -0
  6. package/lib/cn/Mod31Checksum.js.map +1 -0
  7. package/lib/cn/ResidentIdentityCardNumber.js +50 -0
  8. package/lib/cn/ResidentIdentityCardNumber.js.map +1 -0
  9. package/lib/cn/UnifiedSocialCreditCode.js +118 -0
  10. package/lib/cn/UnifiedSocialCreditCode.js.map +1 -0
  11. package/lib/cn/formatDate.js +15 -0
  12. package/lib/cn/formatDate.js.map +1 -0
  13. package/lib/cn/index.js +4 -0
  14. package/lib/cn/index.js.map +1 -0
  15. package/lib/cn/parseSex.js +22 -0
  16. package/lib/cn/parseSex.js.map +1 -0
  17. package/lib/cn/types.d.js +8 -0
  18. package/lib/cn/types.d.js.map +1 -0
  19. package/lib/meta/defineFileType.js +44 -0
  20. package/lib/meta/defineFileType.js.map +1 -0
  21. package/lib/meta/defineMetadata.js +14 -3
  22. package/lib/meta/defineMetadata.js.map +1 -1
  23. package/lib/meta/index.js +1 -0
  24. package/lib/meta/index.js.map +1 -1
  25. package/lib/parseSort.js +15 -0
  26. package/lib/parseSort.js.map +1 -1
  27. package/lib/search/AdvanceSearch.js +10 -0
  28. package/lib/search/AdvanceSearch.js.map +1 -0
  29. package/lib/search/formatAdvanceSearch.js +64 -0
  30. package/lib/search/formatAdvanceSearch.js.map +1 -0
  31. package/lib/search/index.js +2 -0
  32. package/lib/search/index.js.map +1 -0
  33. package/lib/search/optimizeAdvanceSearch.js +89 -0
  34. package/lib/search/optimizeAdvanceSearch.js.map +1 -0
  35. package/lib/search/parseAdvanceSearch.js +20 -0
  36. package/lib/search/parseAdvanceSearch.js.map +1 -0
  37. package/lib/search/parser.d.js +3 -0
  38. package/lib/search/parser.d.js.map +1 -0
  39. package/lib/search/parser.js +3065 -0
  40. package/lib/search/parser.js.map +1 -0
  41. package/lib/search/types.d.js +3 -0
  42. package/lib/search/types.d.js.map +1 -0
  43. package/package.json +12 -4
  44. package/src/cn/DivisionCode.test.ts +50 -0
  45. package/src/cn/DivisionCode.ts +253 -0
  46. package/src/cn/Mod11Checksum.ts +24 -0
  47. package/src/cn/Mod31Checksum.ts +36 -0
  48. package/src/cn/ResidentIdentityCardNumber.test.ts +21 -0
  49. package/src/cn/ResidentIdentityCardNumber.ts +96 -0
  50. package/src/cn/UnifiedSocialCreditCode.test.ts +16 -0
  51. package/src/cn/UnifiedSocialCreditCode.ts +148 -0
  52. package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +15 -0
  53. package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +41 -0
  54. package/src/cn/formatDate.ts +12 -0
  55. package/src/cn/index.ts +3 -0
  56. package/src/cn/parseSex.ts +27 -0
  57. package/src/cn/types.d.ts +51 -0
  58. package/src/meta/defineFileType.tsx +68 -0
  59. package/src/meta/defineMetadata.ts +16 -3
  60. package/src/meta/index.ts +2 -0
  61. package/src/parseSort.test.ts +1 -0
  62. package/src/parseSort.ts +18 -0
  63. package/src/search/AdvanceSearch.test.ts +156 -0
  64. package/src/search/AdvanceSearch.ts +14 -0
  65. package/src/search/Makefile +2 -0
  66. package/src/search/__snapshots__/AdvanceSearch.test.ts.snap +675 -0
  67. package/src/search/formatAdvanceSearch.ts +61 -0
  68. package/src/search/index.ts +1 -0
  69. package/src/search/optimizeAdvanceSearch.ts +90 -0
  70. package/src/search/parseAdvanceSearch.ts +26 -0
  71. package/src/search/parser.d.ts +8 -0
  72. package/src/search/parser.js +2794 -0
  73. package/src/search/parser.peggy +237 -0
  74. 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
+ }
@@ -0,0 +1,8 @@
1
+ export function parse(input: string, options?: ParseOptions): any;
2
+
3
+ export interface ParseOptions {
4
+ grammarSource?: string;
5
+ startRule?: string;
6
+
7
+ [k: string]: any;
8
+ }