@wener/common 1.0.4 → 2.0.1

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 (151) hide show
  1. package/lib/cn/DivisionCode.js +55 -32
  2. package/lib/cn/DivisionCode.test.js +140 -0
  3. package/lib/cn/Mod11Checksum.js +80 -37
  4. package/lib/cn/Mod31Checksum.js +89 -40
  5. package/lib/cn/ResidentIdentityCardNumber.js +16 -16
  6. package/lib/cn/ResidentIdentityCardNumber.test.js +21 -0
  7. package/lib/cn/UnifiedSocialCreditCode.js +32 -26
  8. package/lib/cn/UnifiedSocialCreditCode.test.js +16 -0
  9. package/lib/cn/formatDate.js +5 -7
  10. package/lib/cn/index.js +0 -1
  11. package/lib/cn/parseSex.js +0 -2
  12. package/lib/cn/types.d.js +0 -2
  13. package/lib/consola/createStandardConsolaReporter.js +18 -0
  14. package/lib/consola/formatLogObject.js +226 -0
  15. package/lib/consola/index.js +2 -0
  16. package/lib/data/formatSort.js +5 -6
  17. package/lib/data/formatSort.test.js +34 -0
  18. package/lib/data/index.js +0 -1
  19. package/lib/data/maybeNumber.js +11 -5
  20. package/lib/data/parseSort.js +28 -22
  21. package/lib/data/parseSort.test.js +188 -0
  22. package/lib/data/resolvePagination.js +27 -16
  23. package/lib/data/resolvePagination.test.js +232 -0
  24. package/lib/data/types.d.js +0 -2
  25. package/lib/index.js +0 -1
  26. package/lib/jsonschema/JsonSchema.js +80 -54
  27. package/lib/jsonschema/JsonSchema.test.js +137 -0
  28. package/lib/jsonschema/index.js +0 -1
  29. package/lib/jsonschema/types.d.js +5 -3
  30. package/lib/meta/defineFileType.js +103 -20
  31. package/lib/meta/defineInit.js +250 -31
  32. package/lib/meta/defineMetadata.js +140 -24
  33. package/lib/meta/defineMetadata.test.js +13 -0
  34. package/lib/meta/index.js +0 -1
  35. package/lib/password/PHC.js +87 -63
  36. package/lib/password/PHC.test.js +539 -0
  37. package/lib/password/Password.js +291 -29
  38. package/lib/password/Password.test.js +362 -0
  39. package/lib/password/createArgon2PasswordAlgorithm.js +191 -35
  40. package/lib/password/createBase64PasswordAlgorithm.js +141 -8
  41. package/lib/password/createBcryptPasswordAlgorithm.js +168 -13
  42. package/lib/password/createPBKDF2PasswordAlgorithm.js +228 -46
  43. package/lib/password/createScryptPasswordAlgorithm.js +211 -55
  44. package/lib/password/index.js +0 -1
  45. package/lib/password/server/index.js +0 -1
  46. package/lib/resource/Identifiable.js +1 -0
  47. package/lib/resource/getTitleOfResource.js +10 -0
  48. package/lib/resource/index.js +1 -0
  49. package/lib/resource/schema/AnyResourceSchema.js +89 -0
  50. package/lib/resource/schema/BaseResourceSchema.js +29 -0
  51. package/lib/resource/schema/ResourceActionType.js +118 -0
  52. package/lib/resource/schema/ResourceStatus.js +93 -0
  53. package/lib/resource/schema/ResourceType.js +24 -0
  54. package/lib/resource/schema/SchemaRegistry.js +38 -0
  55. package/lib/resource/schema/SexType.js +10 -0
  56. package/lib/resource/schema/types.js +89 -0
  57. package/lib/resource/schema/types.test.js +14 -0
  58. package/lib/schema/TypeSchema.d.js +1 -0
  59. package/lib/schema/createSchemaData.js +173 -0
  60. package/lib/schema/findJsonSchemaByPath.js +49 -0
  61. package/lib/schema/getSchemaCache.js +11 -0
  62. package/lib/schema/getSchemaOptions.js +24 -0
  63. package/lib/schema/index.js +5 -0
  64. package/lib/schema/toJsonSchema.js +441 -0
  65. package/lib/schema/toJsonSchema.test.js +27 -0
  66. package/lib/schema/validate.js +124 -0
  67. package/lib/search/AdvanceSearch.js +0 -1
  68. package/lib/search/AdvanceSearch.test.js +435 -0
  69. package/lib/search/formatAdvanceSearch.js +41 -27
  70. package/lib/search/index.js +0 -1
  71. package/lib/search/optimizeAdvanceSearch.js +79 -25
  72. package/lib/search/parseAdvanceSearch.js +5 -5
  73. package/lib/search/parser.d.js +0 -2
  74. package/lib/search/parser.js +97 -74
  75. package/lib/search/types.d.js +0 -2
  76. package/lib/tools/generateSchema.js +201 -0
  77. package/lib/tools/renderJsonSchemaToMarkdownDoc.js +143 -55
  78. package/package.json +30 -9
  79. package/src/consola/createStandardConsolaReporter.ts +31 -0
  80. package/src/consola/formatLogObject.ts +171 -0
  81. package/src/consola/index.ts +2 -0
  82. package/src/data/maybeNumber.ts +5 -1
  83. package/src/data/resolvePagination.test.ts +1 -1
  84. package/src/data/resolvePagination.ts +18 -7
  85. package/src/data/types.d.ts +12 -0
  86. package/src/jsonschema/JsonSchema.test.ts +17 -0
  87. package/src/jsonschema/JsonSchema.ts +4 -4
  88. package/src/jsonschema/types.d.ts +63 -12
  89. package/src/resource/Identifiable.ts +3 -0
  90. package/src/resource/getTitleOfResource.tsx +6 -0
  91. package/src/resource/index.ts +3 -0
  92. package/src/resource/schema/AnyResourceSchema.ts +113 -0
  93. package/src/resource/schema/BaseResourceSchema.ts +32 -0
  94. package/src/resource/schema/ResourceActionType.ts +123 -0
  95. package/src/resource/schema/ResourceStatus.ts +94 -0
  96. package/src/resource/schema/ResourceType.ts +25 -0
  97. package/src/resource/schema/SchemaRegistry.ts +42 -0
  98. package/src/resource/schema/SexType.ts +13 -0
  99. package/src/resource/schema/types.test.ts +18 -0
  100. package/src/resource/schema/types.ts +105 -0
  101. package/src/schema/TypeSchema.d.ts +32 -0
  102. package/src/schema/createSchemaData.ts +81 -0
  103. package/src/schema/findJsonSchemaByPath.ts +37 -0
  104. package/src/schema/getSchemaCache.ts +21 -0
  105. package/src/schema/getSchemaOptions.ts +24 -0
  106. package/src/schema/index.ts +6 -0
  107. package/src/schema/toJsonSchema.test.ts +37 -0
  108. package/src/schema/toJsonSchema.ts +200 -0
  109. package/src/schema/validate.ts +135 -0
  110. package/src/tools/generateSchema.ts +39 -0
  111. package/lib/cn/DivisionCode.js.map +0 -1
  112. package/lib/cn/Mod11Checksum.js.map +0 -1
  113. package/lib/cn/Mod31Checksum.js.map +0 -1
  114. package/lib/cn/ResidentIdentityCardNumber.js.map +0 -1
  115. package/lib/cn/UnifiedSocialCreditCode.js.map +0 -1
  116. package/lib/cn/formatDate.js.map +0 -1
  117. package/lib/cn/index.js.map +0 -1
  118. package/lib/cn/parseSex.js.map +0 -1
  119. package/lib/cn/types.d.js.map +0 -1
  120. package/lib/data/formatSort.js.map +0 -1
  121. package/lib/data/index.js.map +0 -1
  122. package/lib/data/maybeNumber.js.map +0 -1
  123. package/lib/data/parseSort.js.map +0 -1
  124. package/lib/data/resolvePagination.js.map +0 -1
  125. package/lib/data/types.d.js.map +0 -1
  126. package/lib/index.js.map +0 -1
  127. package/lib/jsonschema/JsonSchema.js.map +0 -1
  128. package/lib/jsonschema/index.js.map +0 -1
  129. package/lib/jsonschema/types.d.js.map +0 -1
  130. package/lib/meta/defineFileType.js.map +0 -1
  131. package/lib/meta/defineInit.js.map +0 -1
  132. package/lib/meta/defineMetadata.js.map +0 -1
  133. package/lib/meta/index.js.map +0 -1
  134. package/lib/password/PHC.js.map +0 -1
  135. package/lib/password/Password.js.map +0 -1
  136. package/lib/password/createArgon2PasswordAlgorithm.js.map +0 -1
  137. package/lib/password/createBase64PasswordAlgorithm.js.map +0 -1
  138. package/lib/password/createBcryptPasswordAlgorithm.js.map +0 -1
  139. package/lib/password/createPBKDF2PasswordAlgorithm.js.map +0 -1
  140. package/lib/password/createScryptPasswordAlgorithm.js.map +0 -1
  141. package/lib/password/index.js.map +0 -1
  142. package/lib/password/server/index.js.map +0 -1
  143. package/lib/search/AdvanceSearch.js.map +0 -1
  144. package/lib/search/formatAdvanceSearch.js.map +0 -1
  145. package/lib/search/index.js.map +0 -1
  146. package/lib/search/optimizeAdvanceSearch.js.map +0 -1
  147. package/lib/search/parseAdvanceSearch.js.map +0 -1
  148. package/lib/search/parser.d.js.map +0 -1
  149. package/lib/search/parser.js.map +0 -1
  150. package/lib/search/types.d.js.map +0 -1
  151. package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +0 -1
@@ -1,4 +1,4 @@
1
- const DivisionCodeLevels = [
1
+ var DivisionCodeLevels = [
2
2
  {
3
3
  level: 1,
4
4
  code: 'Province',
@@ -36,7 +36,7 @@ const DivisionCodeLevels = [
36
36
  }
37
37
  ];
38
38
  (function(DivisionCode) {
39
- let DivisionCodeLevel = /*#__PURE__*/ function(DivisionCodeLevel) {
39
+ var DivisionCodeLevel = /*#__PURE__*/ function(DivisionCodeLevel) {
40
40
  DivisionCodeLevel[DivisionCodeLevel["Province"] = 1] = "Province";
41
41
  DivisionCodeLevel[DivisionCodeLevel["City"] = 2] = "City";
42
42
  DivisionCodeLevel[DivisionCodeLevel["County"] = 3] = "County";
@@ -45,8 +45,8 @@ const DivisionCodeLevels = [
45
45
  return DivisionCodeLevel;
46
46
  }({});
47
47
  DivisionCode.levels = DivisionCodeLevels;
48
- DivisionCode.regex = /^(?<province>\d{2})(?<city>\d{2})?(?<county>\d{2})?(?<town>\d{3})?(?<village>\d{3})?$/;
49
- const root = [
48
+ DivisionCode.regex = RegExp("^(?<province>\\d{2})(?<city>\\d{2})?(?<county>\\d{2})?(?<town>\\d{3})?(?<village>\\d{3})?$");
49
+ var root = [
50
50
  {
51
51
  value: '11',
52
52
  label: '北京市'
@@ -187,11 +187,12 @@ const DivisionCodeLevels = [
187
187
  function parse(code) {
188
188
  if (!code) return;
189
189
  code = String(code);
190
- const match = DivisionCode.regex.exec(code);
190
+ var match = DivisionCode.regex.exec(code);
191
191
  if (!match) return;
192
- const { province, city, county, town, village } = match.groups ?? {};
192
+ var _match_groups;
193
+ var _ref = (_match_groups = match.groups) !== null && _match_groups !== void 0 ? _match_groups : {}, province = _ref.province, city = _ref.city, county = _ref.county, town = _ref.town, village = _ref.village;
193
194
  if (!province) return;
194
- let codes = [
195
+ var codes = [
195
196
  province,
196
197
  city,
197
198
  county,
@@ -199,26 +200,27 @@ const DivisionCodeLevels = [
199
200
  village
200
201
  ].filter(Boolean);
201
202
  return {
202
- province,
203
- city,
204
- county,
205
- town,
206
- village,
203
+ province: province,
204
+ city: city,
205
+ county: county,
206
+ town: town,
207
+ village: village,
207
208
  codes: codes,
208
209
  level: codes.length
209
210
  };
210
211
  }
211
212
  DivisionCode.parse = parse;
212
- function format({ province, city, county, town, village }) {
213
- const codes = [];
214
- for(let i = 0; i < [
213
+ function format(param) {
214
+ var province = param.province, city = param.city, county = param.county, town = param.town, village = param.village;
215
+ var codes = [];
216
+ for(var i = 0; i < [
215
217
  province,
216
218
  city,
217
219
  county,
218
220
  town,
219
221
  village
220
222
  ].length; i++){
221
- let x = [
223
+ var x = [
222
224
  province,
223
225
  city,
224
226
  county,
@@ -228,7 +230,7 @@ const DivisionCodeLevels = [
228
230
  if (x === undefined || x === null || x === '') {
229
231
  break;
230
232
  }
231
- let len = DivisionCode.levels[i].size;
233
+ var len = DivisionCode.levels[i].size;
232
234
  codes.push(String(x).padStart(len, '0').slice(0, len));
233
235
  }
234
236
  return codes.join('');
@@ -287,25 +289,46 @@ export function randomPick(s) {
287
289
  return s[Math.floor(Math.random() * s.length)];
288
290
  }
289
291
  function lookup(opts) {
290
- const { values, root } = opts;
291
- const found = [];
292
- let currentLevel = root;
293
- for (const v of values){
294
- const node = currentLevel.find((n)=>n.value === v);
295
- if (!node) {
296
- break;
292
+ var values = opts.values, root = opts.root;
293
+ var found = [];
294
+ var currentLevel = root;
295
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
296
+ try {
297
+ var _loop = function() {
298
+ var v = _step.value;
299
+ var node = currentLevel.find(function(n) {
300
+ return n.value === v;
301
+ });
302
+ if (!node) {
303
+ return "break";
304
+ }
305
+ found.push(node);
306
+ if (node.children) {
307
+ currentLevel = node.children;
308
+ } else {
309
+ return "break";
310
+ }
311
+ };
312
+ for(var _iterator = values[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
313
+ var _ret = _loop();
314
+ if (_ret === "break") break;
297
315
  }
298
- found.push(node);
299
- if (node.children) {
300
- currentLevel = node.children;
301
- } else {
302
- break;
316
+ } catch (err) {
317
+ _didIteratorError = true;
318
+ _iteratorError = err;
319
+ } finally{
320
+ try {
321
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
322
+ _iterator.return();
323
+ }
324
+ } finally{
325
+ if (_didIteratorError) {
326
+ throw _iteratorError;
327
+ }
303
328
  }
304
329
  }
305
330
  return {
306
- found
331
+ found: found
307
332
  };
308
333
  }
309
334
  export var DivisionCode;
310
-
311
- //# sourceMappingURL=DivisionCode.js.map
@@ -0,0 +1,140 @@
1
+ function _array_like_to_array(arr, len) {
2
+ if (len == null || len > arr.length)
3
+ len = arr.length;
4
+ for (var i = 0, arr2 = new Array(len); i < len; i++)
5
+ arr2[i] = arr[i];
6
+ return arr2;
7
+ }
8
+ function _array_with_holes(arr) {
9
+ if (Array.isArray(arr))
10
+ return arr;
11
+ }
12
+ function _iterable_to_array_limit(arr, i) {
13
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
14
+ if (_i == null)
15
+ return;
16
+ var _arr = [];
17
+ var _n = true;
18
+ var _d = false;
19
+ var _s, _e;
20
+ try {
21
+ for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
22
+ _arr.push(_s.value);
23
+ if (i && _arr.length === i)
24
+ break;
25
+ }
26
+ }
27
+ catch (err) {
28
+ _d = true;
29
+ _e = err;
30
+ }
31
+ finally {
32
+ try {
33
+ if (!_n && _i["return"] != null)
34
+ _i["return"]();
35
+ }
36
+ finally {
37
+ if (_d)
38
+ throw _e;
39
+ }
40
+ }
41
+ return _arr;
42
+ }
43
+ function _non_iterable_rest() {
44
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
45
+ }
46
+ function _sliced_to_array(arr, i) {
47
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
48
+ }
49
+ function _unsupported_iterable_to_array(o, minLen) {
50
+ if (!o)
51
+ return;
52
+ if (typeof o === "string")
53
+ return _array_like_to_array(o, minLen);
54
+ var n = Object.prototype.toString.call(o).slice(8, -1);
55
+ if (n === "Object" && o.constructor)
56
+ n = o.constructor.name;
57
+ if (n === "Map" || n === "Set")
58
+ return Array.from(n);
59
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
60
+ return _array_like_to_array(o, minLen);
61
+ }
62
+ import { describe, expect, it } from "vitest";
63
+ import { DivisionCode } from "./DivisionCode.js";
64
+ describe("DivisionCode", function () {
65
+ it("should parse", function () {
66
+ for (var _i = 0, _iter = [
67
+ [
68
+ "441900003001",
69
+ {
70
+ province: "44",
71
+ city: "19",
72
+ county: "00",
73
+ town: "003",
74
+ village: "001",
75
+ codes: [
76
+ "44",
77
+ "19",
78
+ "00",
79
+ "003",
80
+ "001"
81
+ ]
82
+ }
83
+ ],
84
+ [
85
+ 441900003001,
86
+ {
87
+ province: "44",
88
+ city: "19",
89
+ county: "00",
90
+ town: "003",
91
+ village: "001",
92
+ codes: [
93
+ "44",
94
+ "19",
95
+ "00",
96
+ "003",
97
+ "001"
98
+ ]
99
+ }
100
+ ],
101
+ [
102
+ 441900,
103
+ {
104
+ province: "44",
105
+ city: "19",
106
+ county: "00"
107
+ }
108
+ ],
109
+ [
110
+ "31",
111
+ {
112
+ province: "31"
113
+ }
114
+ ],
115
+ [
116
+ "4",
117
+ undefined
118
+ ],
119
+ [
120
+ "",
121
+ undefined
122
+ ],
123
+ [
124
+ null,
125
+ undefined
126
+ ],
127
+ [
128
+ undefined,
129
+ undefined
130
+ ]
131
+ ]; _i < _iter.length; _i++) {
132
+ var _iter__i = _sliced_to_array(_iter[_i], 2), a = _iter__i[0], b = _iter__i[1];
133
+ var out = DivisionCode.parse(a);
134
+ expect(out).toMatchObject(b);
135
+ if (out) {
136
+ expect(DivisionCode.format(out)).toBe(String(a));
137
+ }
138
+ }
139
+ });
140
+ });
@@ -1,42 +1,85 @@
1
1
  /**
2
2
  * ISO 7064:1983, MOD 11-2.
3
- */ export class Mod11Checksum {
4
- weights = [
5
- 7,
6
- 9,
7
- 10,
8
- 5,
9
- 8,
10
- 4,
11
- 2,
12
- 1,
13
- 6,
14
- 3,
15
- 7,
16
- 9,
17
- 10,
18
- 5,
19
- 8,
20
- 4,
21
- 2,
22
- 1
23
- ];
24
- validate(s) {
25
- return s.at(-1) === this.compute(s.slice(0, s.length - 1));
3
+ */ function _class_call_check(instance, Constructor) {
4
+ if (!(instance instanceof Constructor)) {
5
+ throw new TypeError("Cannot call a class as a function");
26
6
  }
27
- compute(s) {
28
- const { weights } = this;
29
- let sum = 0;
30
- for(let i = 0; i < s.length; i++){
31
- sum += parseInt(s[i]) * weights[i];
32
- }
33
- const num = (12 - sum % 11) % 11;
34
- if (num < 10) {
35
- return num.toString();
36
- } else {
37
- return 'X';
38
- }
7
+ }
8
+ function _defineProperties(target, props) {
9
+ for(var i = 0; i < props.length; i++){
10
+ var descriptor = props[i];
11
+ descriptor.enumerable = descriptor.enumerable || false;
12
+ descriptor.configurable = true;
13
+ if ("value" in descriptor) descriptor.writable = true;
14
+ Object.defineProperty(target, descriptor.key, descriptor);
15
+ }
16
+ }
17
+ function _create_class(Constructor, protoProps, staticProps) {
18
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
19
+ if (staticProps) _defineProperties(Constructor, staticProps);
20
+ return Constructor;
21
+ }
22
+ function _define_property(obj, key, value) {
23
+ if (key in obj) {
24
+ Object.defineProperty(obj, key, {
25
+ value: value,
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true
29
+ });
30
+ } else {
31
+ obj[key] = value;
39
32
  }
33
+ return obj;
40
34
  }
41
-
42
- //# sourceMappingURL=Mod11Checksum.js.map
35
+ export var Mod11Checksum = /*#__PURE__*/ function() {
36
+ "use strict";
37
+ function Mod11Checksum() {
38
+ _class_call_check(this, Mod11Checksum);
39
+ _define_property(this, "weights", [
40
+ 7,
41
+ 9,
42
+ 10,
43
+ 5,
44
+ 8,
45
+ 4,
46
+ 2,
47
+ 1,
48
+ 6,
49
+ 3,
50
+ 7,
51
+ 9,
52
+ 10,
53
+ 5,
54
+ 8,
55
+ 4,
56
+ 2,
57
+ 1
58
+ ]);
59
+ }
60
+ _create_class(Mod11Checksum, [
61
+ {
62
+ key: "validate",
63
+ value: function validate(s) {
64
+ return s.at(-1) === this.compute(s.slice(0, s.length - 1));
65
+ }
66
+ },
67
+ {
68
+ key: "compute",
69
+ value: function compute(s) {
70
+ var weights = this.weights;
71
+ var sum = 0;
72
+ for(var i = 0; i < s.length; i++){
73
+ sum += parseInt(s[i]) * weights[i];
74
+ }
75
+ var num = (12 - sum % 11) % 11;
76
+ if (num < 10) {
77
+ return num.toString();
78
+ } else {
79
+ return 'X';
80
+ }
81
+ }
82
+ }
83
+ ]);
84
+ return Mod11Checksum;
85
+ }();
@@ -2,47 +2,96 @@
2
2
  * GB/T 17710(采ISO 7064)的模31校验码
3
3
  *
4
4
  * Mod31-3
5
- */ export class Mod31Checksum {
6
- weights = [
7
- 1,
8
- 3,
9
- 9,
10
- 27,
11
- 19,
12
- 26,
13
- 16,
14
- 17,
15
- 20,
16
- 29,
17
- 25,
18
- 13,
19
- 8,
20
- 24,
21
- 10,
22
- 30,
23
- 28
24
- ];
25
- chars = '0123456789ABCDEFGHJKLMNPQRTUWXY';
26
- numbers = this.chars.split('').reduce((acc, cur, i)=>{
27
- acc[cur] = i;
28
- return acc;
29
- }, {});
30
- toChar(n) {
31
- return this.chars[n];
5
+ */ function _class_call_check(instance, Constructor) {
6
+ if (!(instance instanceof Constructor)) {
7
+ throw new TypeError("Cannot call a class as a function");
32
8
  }
33
- toNumber(c) {
34
- return this.numbers[c];
35
- }
36
- validate(s) {
37
- return s.at(-1) === this.compute(s.slice(0, s.length - 1));
9
+ }
10
+ function _defineProperties(target, props) {
11
+ for(var i = 0; i < props.length; i++){
12
+ var descriptor = props[i];
13
+ descriptor.enumerable = descriptor.enumerable || false;
14
+ descriptor.configurable = true;
15
+ if ("value" in descriptor) descriptor.writable = true;
16
+ Object.defineProperty(target, descriptor.key, descriptor);
38
17
  }
39
- compute(s) {
40
- let sum = 0;
41
- for(let i = 0; i < s.length; i++){
42
- sum += this.numbers[s[i]] * this.weights[i];
43
- }
44
- return this.toChar(31 - sum % 31);
18
+ }
19
+ function _create_class(Constructor, protoProps, staticProps) {
20
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
21
+ if (staticProps) _defineProperties(Constructor, staticProps);
22
+ return Constructor;
23
+ }
24
+ function _define_property(obj, key, value) {
25
+ if (key in obj) {
26
+ Object.defineProperty(obj, key, {
27
+ value: value,
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true
31
+ });
32
+ } else {
33
+ obj[key] = value;
45
34
  }
35
+ return obj;
46
36
  }
47
-
48
- //# sourceMappingURL=Mod31Checksum.js.map
37
+ export var Mod31Checksum = /*#__PURE__*/ function() {
38
+ "use strict";
39
+ function Mod31Checksum() {
40
+ _class_call_check(this, Mod31Checksum);
41
+ _define_property(this, "weights", [
42
+ 1,
43
+ 3,
44
+ 9,
45
+ 27,
46
+ 19,
47
+ 26,
48
+ 16,
49
+ 17,
50
+ 20,
51
+ 29,
52
+ 25,
53
+ 13,
54
+ 8,
55
+ 24,
56
+ 10,
57
+ 30,
58
+ 28
59
+ ]);
60
+ _define_property(this, "chars", '0123456789ABCDEFGHJKLMNPQRTUWXY');
61
+ _define_property(this, "numbers", this.chars.split('').reduce(function(acc, cur, i) {
62
+ acc[cur] = i;
63
+ return acc;
64
+ }, {}));
65
+ }
66
+ _create_class(Mod31Checksum, [
67
+ {
68
+ key: "toChar",
69
+ value: function toChar(n) {
70
+ return this.chars[n];
71
+ }
72
+ },
73
+ {
74
+ key: "toNumber",
75
+ value: function toNumber(c) {
76
+ return this.numbers[c];
77
+ }
78
+ },
79
+ {
80
+ key: "validate",
81
+ value: function validate(s) {
82
+ return s.at(-1) === this.compute(s.slice(0, s.length - 1));
83
+ }
84
+ },
85
+ {
86
+ key: "compute",
87
+ value: function compute(s) {
88
+ var sum = 0;
89
+ for(var i = 0; i < s.length; i++){
90
+ sum += this.numbers[s[i]] * this.weights[i];
91
+ }
92
+ return this.toChar(31 - sum % 31);
93
+ }
94
+ }
95
+ ]);
96
+ return Mod31Checksum;
97
+ }();
@@ -3,19 +3,19 @@ import { Mod11Checksum } from "./Mod11Checksum.js";
3
3
  (function (ResidentIdentityCardNumber) {
4
4
  ResidentIdentityCardNumber.Checksum = new Mod11Checksum();
5
5
  function parse(s) {
6
- const division = s.slice(0, 6);
7
- const birthDate = s.slice(6, 14);
8
- const sequence = parseInt(s.slice(14, 17), 10);
9
- const checksum = s.slice(17, 18);
10
- const valid = ResidentIdentityCardNumber.Checksum.validate(s);
11
- const sex = sequence % 2 === 1 ? "Male" : "Female";
6
+ var division = s.slice(0, 6);
7
+ var birthDate = s.slice(6, 14);
8
+ var sequence = parseInt(s.slice(14, 17), 10);
9
+ var checksum = s.slice(17, 18);
10
+ var valid = ResidentIdentityCardNumber.Checksum.validate(s);
11
+ var sex = sequence % 2 === 1 ? "Male" : "Female";
12
12
  return {
13
- division,
14
- birthDate,
15
- sequence,
16
- checksum,
17
- valid,
18
- sex,
13
+ division: division,
14
+ birthDate: birthDate,
15
+ sequence: sequence,
16
+ checksum: checksum,
17
+ valid: valid,
18
+ sex: sex,
19
19
  male: sex === "Male",
20
20
  female: sex === "Female",
21
21
  age: new Date().getFullYear() - parseInt(birthDate.slice(0, 4), 10)
@@ -25,7 +25,8 @@ import { Mod11Checksum } from "./Mod11Checksum.js";
25
25
  export const length = 18;
26
26
  export const pattern = /^\d{6}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/;
27
27
  */ ResidentIdentityCardNumber.parse = parse;
28
- function format({ division, birthDate, sequence, checksum }) {
28
+ function format(param) {
29
+ var division = param.division, birthDate = param.birthDate, sequence = param.sequence, checksum = param.checksum;
29
30
  if (typeof birthDate !== "string") {
30
31
  birthDate = formatDate(birthDate, "yyyyMMDD");
31
32
  }
@@ -34,17 +35,16 @@ import { Mod11Checksum } from "./Mod11Checksum.js";
34
35
  }
35
36
  typeof sequence === "number" && (sequence = sequence.toString());
36
37
  sequence = sequence.padStart(3, "0");
37
- const base = [
38
+ var base = [
38
39
  division,
39
40
  birthDate,
40
41
  sequence
41
42
  ].join("");
42
43
  if (base.length !== 17)
43
44
  throw new Error("Invalid params");
44
- checksum ||= ResidentIdentityCardNumber.Checksum.compute(base);
45
+ checksum || (checksum = ResidentIdentityCardNumber.Checksum.compute(base));
45
46
  return base + checksum;
46
47
  }
47
48
  ResidentIdentityCardNumber.format = format;
48
49
  })(ResidentIdentityCardNumber || (ResidentIdentityCardNumber = {}));
49
50
  export var ResidentIdentityCardNumber;
50
- //# sourceMappingURL=ResidentIdentityCardNumber.js.map
@@ -0,0 +1,21 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { ResidentIdentityCardNumber } from "./ResidentIdentityCardNumber.js";
3
+ describe("ResidentIdentityCardNumber", function () {
4
+ it("should parse", function () {
5
+ for (var _i = 0, _iter = [
6
+ "11010519491231002X"
7
+ ]; _i < _iter.length; _i++) {
8
+ var a = _iter[_i];
9
+ var out = ResidentIdentityCardNumber.parse(a);
10
+ expect(ResidentIdentityCardNumber.format(out)).toBe(a);
11
+ expect(out).toMatchSnapshot();
12
+ }
13
+ });
14
+ it("should format with partial", function () {
15
+ expect(ResidentIdentityCardNumber.format({
16
+ division: "110105",
17
+ birthDate: new Date("1949-12-31"),
18
+ sequence: 2
19
+ })).toBe("11010519491231002X");
20
+ });
21
+ });