@wener/common 2.0.5 → 2.0.6

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 (206) hide show
  1. package/lib/ai/qwen3vl/index.js +1 -1
  2. package/lib/ai/qwen3vl/utils.js +15 -15
  3. package/lib/ai/vision/DocLayoutElementTypeSchema.js +22 -22
  4. package/lib/ai/vision/ImageAnnotationSchema.js +63 -47
  5. package/lib/ai/vision/index.js +2 -2
  6. package/lib/ai/vision/resolveImageAnnotation.js +81 -95
  7. package/lib/cn/ChineseResidentIdNo.js +55 -41
  8. package/lib/cn/ChineseResidentIdNo.mod.js +6 -1
  9. package/lib/cn/ChineseResidentIdNo.test.js +22 -21
  10. package/lib/cn/DivisionCode.js +220 -235
  11. package/lib/cn/DivisionCode.mod.js +6 -1
  12. package/lib/cn/DivisionCode.test.js +92 -121
  13. package/lib/cn/Mod11.js +18 -37
  14. package/lib/cn/Mod31.js +23 -41
  15. package/lib/cn/UnifiedSocialCreditCode.js +143 -137
  16. package/lib/cn/UnifiedSocialCreditCode.mod.js +6 -1
  17. package/lib/cn/UnifiedSocialCreditCode.test.js +21 -15
  18. package/lib/cn/formatChineseAmount.js +46 -71
  19. package/lib/cn/index.js +6 -6
  20. package/lib/cn/mod.js +5 -3
  21. package/lib/cn/parseChineseNumber.js +81 -85
  22. package/lib/cn/parseChineseNumber.test.js +183 -261
  23. package/lib/cn/pinyin/cartesianProduct.js +19 -19
  24. package/lib/cn/pinyin/cartesianProduct.test.js +78 -178
  25. package/lib/cn/pinyin/loader.js +13 -11
  26. package/lib/cn/pinyin/preload.js +2 -1
  27. package/lib/cn/pinyin/toPinyin.test.js +149 -161
  28. package/lib/cn/pinyin/toPinyinPure.js +28 -23
  29. package/lib/cn/pinyin/transform.js +11 -11
  30. package/lib/cn/types.d.js +2 -2
  31. package/lib/consola/createStandardConsolaReporter.js +14 -15
  32. package/lib/consola/formatLogObject.js +149 -133
  33. package/lib/consola/formatLogObject.test.js +167 -178
  34. package/lib/consola/index.js +2 -2
  35. package/lib/data/formatSort.js +14 -12
  36. package/lib/data/formatSort.test.js +33 -33
  37. package/lib/data/index.js +3 -3
  38. package/lib/data/maybeNumber.js +23 -23
  39. package/lib/data/parseSort.js +75 -68
  40. package/lib/data/parseSort.test.js +196 -187
  41. package/lib/data/resolvePagination.js +38 -39
  42. package/lib/data/resolvePagination.test.js +228 -218
  43. package/lib/data/types.d.js +2 -2
  44. package/lib/dayjs/dayjs.js +20 -20
  45. package/lib/dayjs/formatDuration.js +56 -56
  46. package/lib/dayjs/formatDuration.test.js +63 -77
  47. package/lib/dayjs/index.js +4 -4
  48. package/lib/dayjs/parseDuration.js +21 -26
  49. package/lib/dayjs/parseRelativeTime.js +65 -66
  50. package/lib/dayjs/parseRelativeTime.test.js +227 -243
  51. package/lib/dayjs/resolveRelativeTime.js +73 -72
  52. package/lib/dayjs/resolveRelativeTime.test.js +296 -307
  53. package/lib/decimal/index.js +1 -1
  54. package/lib/decimal/parseDecimal.js +12 -12
  55. package/lib/drain3/Drain.js +303 -338
  56. package/lib/drain3/LogCluster.js +25 -25
  57. package/lib/drain3/Node.js +24 -24
  58. package/lib/drain3/TemplateMiner.js +197 -196
  59. package/lib/drain3/index.js +5 -5
  60. package/lib/drain3/persistence/FilePersistence.js +19 -19
  61. package/lib/drain3/persistence/MemoryPersistence.js +8 -8
  62. package/lib/drain3/persistence/PersistenceHandler.js +2 -2
  63. package/lib/drain3/types.js +2 -2
  64. package/lib/emittery/emitter.js +7 -7
  65. package/lib/emittery/index.js +1 -1
  66. package/lib/foundation/schema/SexType.js +15 -12
  67. package/lib/foundation/schema/index.js +1 -1
  68. package/lib/foundation/schema/parseSexType.js +15 -16
  69. package/lib/foundation/schema/types.js +8 -6
  70. package/lib/fs/FileSystemError.js +18 -18
  71. package/lib/fs/IFileSystem.d.js +2 -2
  72. package/lib/fs/MemoryFileSystem.test.js +172 -181
  73. package/lib/fs/createBrowserFileSystem.js +222 -235
  74. package/lib/fs/createMemoryFileSystem.js +472 -510
  75. package/lib/fs/createSandboxFileSystem.js +102 -101
  76. package/lib/fs/createWebDavFileSystem.js +162 -149
  77. package/lib/fs/createWebFileSystem.js +197 -220
  78. package/lib/fs/findMimeType.js +14 -14
  79. package/lib/fs/index.js +7 -7
  80. package/lib/fs/minio/createMinioFileSystem.js +959 -956
  81. package/lib/fs/minio/index.js +1 -1
  82. package/lib/fs/orpc/FileSystemContract.js +57 -57
  83. package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
  84. package/lib/fs/orpc/index.js +2 -2
  85. package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
  86. package/lib/fs/orpc/server/index.js +1 -1
  87. package/lib/fs/s3/createS3MiniFileSystem.js +756 -737
  88. package/lib/fs/s3/index.js +1 -1
  89. package/lib/fs/s3/s3mini.test.js +524 -553
  90. package/lib/fs/scandir.js +56 -56
  91. package/lib/fs/server/createDatabaseFileSystem.js +834 -741
  92. package/lib/fs/server/createNodeFileSystem.js +407 -405
  93. package/lib/fs/server/dbfs.test.js +201 -214
  94. package/lib/fs/server/index.js +1 -1
  95. package/lib/fs/server/loadTestDatabase.js +40 -43
  96. package/lib/fs/tests/runFileSystemTest.js +352 -316
  97. package/lib/fs/types.js +17 -20
  98. package/lib/fs/utils/getFileUrl.js +24 -30
  99. package/lib/fs/utils.js +17 -17
  100. package/lib/fs/webdav/index.js +1 -1
  101. package/lib/index.js +2 -2
  102. package/lib/jsonschema/JsonSchema.js +216 -155
  103. package/lib/jsonschema/JsonSchema.test.js +123 -124
  104. package/lib/jsonschema/forEachJsonSchema.js +41 -41
  105. package/lib/jsonschema/index.js +2 -2
  106. package/lib/jsonschema/types.d.js +2 -2
  107. package/lib/meta/defineFileType.js +32 -38
  108. package/lib/meta/defineInit.js +39 -35
  109. package/lib/meta/defineMetadata.js +37 -34
  110. package/lib/meta/defineMetadata.test.js +13 -12
  111. package/lib/meta/index.js +3 -3
  112. package/lib/orpc/createOpenApiContractClient.js +26 -24
  113. package/lib/orpc/createRpcContractClient.js +37 -31
  114. package/lib/orpc/index.js +2 -2
  115. package/lib/orpc/resolveLinkPlugins.js +25 -25
  116. package/lib/password/PHC.js +187 -189
  117. package/lib/password/PHC.test.js +517 -535
  118. package/lib/password/Password.js +85 -80
  119. package/lib/password/Password.test.js +330 -364
  120. package/lib/password/createArgon2PasswordAlgorithm.js +50 -51
  121. package/lib/password/createBase64PasswordAlgorithm.js +11 -11
  122. package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
  123. package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
  124. package/lib/password/createScryptPasswordAlgorithm.js +74 -63
  125. package/lib/password/index.js +5 -5
  126. package/lib/password/server/index.js +1 -1
  127. package/lib/resource/Identifiable.js +2 -2
  128. package/lib/resource/ListQuery.js +42 -42
  129. package/lib/resource/getTitleOfResource.js +5 -5
  130. package/lib/resource/index.js +2 -2
  131. package/lib/resource/schema/AnyResourceSchema.js +91 -89
  132. package/lib/resource/schema/BaseResourceSchema.js +26 -26
  133. package/lib/resource/schema/ResourceActionType.js +117 -115
  134. package/lib/resource/schema/ResourceStatus.js +94 -92
  135. package/lib/resource/schema/ResourceType.js +25 -23
  136. package/lib/resource/schema/index.js +5 -5
  137. package/lib/resource/schema/types.js +86 -55
  138. package/lib/resource/schema/types.test.js +16 -13
  139. package/lib/s3/formatS3Url.js +60 -60
  140. package/lib/s3/formatS3Url.test.js +238 -261
  141. package/lib/s3/index.js +2 -2
  142. package/lib/s3/parseS3Url.js +61 -60
  143. package/lib/s3/parseS3Url.test.js +270 -269
  144. package/lib/schema/SchemaRegistry.js +41 -42
  145. package/lib/schema/SchemaRegistry.mod.js +1 -1
  146. package/lib/schema/TypeSchema.d.js +2 -2
  147. package/lib/schema/createSchemaData.js +113 -67
  148. package/lib/schema/findJsonSchemaByPath.js +28 -23
  149. package/lib/schema/formatZodError.js +112 -131
  150. package/lib/schema/formatZodError.test.js +192 -195
  151. package/lib/schema/getSchemaCache.js +7 -7
  152. package/lib/schema/getSchemaOptions.js +17 -16
  153. package/lib/schema/index.js +6 -6
  154. package/lib/schema/toJsonSchema.js +195 -189
  155. package/lib/schema/toJsonSchema.test.js +34 -26
  156. package/lib/schema/validate.js +105 -96
  157. package/lib/tools/generateSchema.js +40 -40
  158. package/lib/tools/renderJsonSchemaToMarkdownDoc.js +74 -74
  159. package/lib/utils/buildBaseUrl.js +8 -8
  160. package/lib/utils/buildRedactorFormSchema.js +54 -53
  161. package/lib/utils/getEstimateProcessTime.js +24 -19
  162. package/lib/utils/index.js +3 -3
  163. package/lib/utils/resolveFeatureOptions.js +9 -9
  164. package/package.json +14 -14
  165. package/src/ai/vision/index.ts +2 -2
  166. package/src/cn/index.ts +1 -2
  167. package/src/consola/index.ts +1 -1
  168. package/src/data/index.ts +3 -4
  169. package/src/data/resolvePagination.ts +2 -2
  170. package/src/dayjs/formatDuration.ts +8 -9
  171. package/src/dayjs/index.ts +1 -1
  172. package/src/dayjs/parseRelativeTime.ts +1 -1
  173. package/src/dayjs/resolveRelativeTime.ts +1 -1
  174. package/src/drain3/Drain.test.ts +2 -2
  175. package/src/drain3/index.ts +2 -4
  176. package/src/fs/createWebDavFileSystem.ts +2 -7
  177. package/src/fs/createWebFileSystem.ts +1 -1
  178. package/src/fs/index.ts +4 -4
  179. package/src/fs/minio/createMinioFileSystem.ts +2 -2
  180. package/src/fs/minio/index.ts +1 -1
  181. package/src/fs/s3/createS3MiniFileSystem.ts +1 -1
  182. package/src/fs/server/createDatabaseFileSystem.ts +84 -120
  183. package/src/fs/server/dbfs.test.ts +14 -10
  184. package/src/fs/server/index.ts +1 -0
  185. package/src/fs/server/loadTestDatabase.ts +8 -119
  186. package/src/jsonschema/index.ts +1 -1
  187. package/src/meta/index.ts +2 -3
  188. package/src/orm/createSqliteDialect.ts +17 -0
  189. package/src/orm/index.ts +1 -0
  190. package/src/orpc/createOpenApiContractClient.ts +1 -1
  191. package/src/orpc/index.ts +1 -1
  192. package/src/password/createArgon2PasswordAlgorithm.ts +1 -1
  193. package/src/password/index.ts +2 -2
  194. package/src/resource/index.ts +3 -3
  195. package/src/resource/schema/index.ts +4 -4
  196. package/src/s3/index.ts +1 -1
  197. package/src/schema/SchemaRegistry.ts +1 -1
  198. package/src/schema/createSchemaData.ts +1 -1
  199. package/src/schema/findJsonSchemaByPath.ts +1 -1
  200. package/src/schema/index.ts +5 -5
  201. package/src/schema/validate.ts +1 -1
  202. package/src/utils/buildRedactorFormSchema.ts +1 -1
  203. package/src/utils/formatNumber.ts +18 -0
  204. package/src/utils/formatPercent.ts +17 -0
  205. package/src/utils/index.ts +3 -3
  206. package/src/utils/resolveFeatureOptions.ts +1 -1
@@ -1,247 +1,231 @@
1
1
  function _define_property(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- }
10
- else {
11
- obj[key] = value;
12
- }
13
- return obj;
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
14
13
  }
15
- import dayjs from "dayjs";
16
- import duration from "dayjs/plugin/duration";
17
- import { describe, expect, it } from "vitest";
18
- import { parseRelativeTime } from "./parseRelativeTime.js";
14
+
15
+ import dayjs from 'dayjs';
16
+ import duration from 'dayjs/plugin/duration';
17
+ import { describe, expect, it } from 'vitest';
18
+ import { parseRelativeTime } from './parseRelativeTime.js';
19
+
19
20
  dayjs.extend(duration);
20
- describe("parseRelativeTime", function () {
21
- it("should return empty array for \"now\"", function () {
22
- var result = parseRelativeTime("now");
23
- expect(result).toEqual([]);
24
- });
25
- it("should parse simple addition operations", function () {
26
- var result = parseRelativeTime("now+1h");
27
- expect(result).toHaveLength(1);
28
- expect(result[0]).toEqual({
29
- operator: "+",
30
- duration: dayjs.duration({
31
- hours: 1
32
- })
33
- });
34
- });
35
- it("should parse simple subtraction operations", function () {
36
- var result = parseRelativeTime("now-30m");
37
- expect(result).toHaveLength(1);
38
- expect(result[0]).toEqual({
39
- operator: "-",
40
- duration: dayjs.duration({
41
- minutes: 30
42
- })
43
- });
44
- });
45
- it("should parse milliseconds", function () {
46
- var result = parseRelativeTime("now+500ms");
47
- expect(result).toHaveLength(1);
48
- expect(result[0]).toEqual({
49
- operator: "+",
50
- duration: dayjs.duration({
51
- milliseconds: 500
52
- })
53
- });
54
- });
55
- it("should parse complex duration combinations", function () {
56
- var result = parseRelativeTime("now+1d6h30m");
57
- expect(result).toHaveLength(1);
58
- expect(result[0]).toEqual({
59
- operator: "+",
60
- duration: dayjs.duration({
61
- days: 1,
62
- hours: 6,
63
- minutes: 30
64
- })
65
- });
66
- });
67
- it("should parse truncation operations", function () {
68
- var result = parseRelativeTime("now/d");
69
- expect(result).toHaveLength(1);
70
- expect(result[0]).toEqual({
71
- operator: "/",
72
- unit: "d"
73
- });
74
- var result2 = parseRelativeTime("now\\M");
75
- expect(result2).toHaveLength(1);
76
- expect(result2[0]).toEqual({
77
- operator: "\\",
78
- unit: "M"
79
- });
80
- });
81
- it("should parse multiple operations", function () {
82
- var result = parseRelativeTime("now-1h+30m");
83
- expect(result).toHaveLength(2);
84
- expect(result[0]).toEqual({
85
- operator: "-",
86
- duration: dayjs.duration({
87
- hours: 1
88
- })
89
- });
90
- expect(result[1]).toEqual({
91
- operator: "+",
92
- duration: dayjs.duration({
93
- minutes: 30
94
- })
95
- });
96
- });
97
- it("should parse mixed arithmetic and truncation operations", function () {
98
- var result = parseRelativeTime("now-1M/M");
99
- expect(result).toHaveLength(2);
100
- expect(result[0]).toEqual({
101
- operator: "-",
102
- duration: dayjs.duration({
103
- months: 1
104
- })
105
- });
106
- expect(result[1]).toEqual({
107
- operator: "/",
108
- unit: "M"
109
- });
110
- });
111
- it("should handle all supported units", function () {
112
- var units = [
113
- "ms",
114
- "s",
115
- "m",
116
- "h",
117
- "d",
118
- "w",
119
- "M",
120
- "Q",
121
- "y"
122
- ];
123
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
124
- try {
125
- for (var _iterator = units[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
126
- var unit = _step.value;
127
- var result = parseRelativeTime("now+1".concat(unit));
128
- expect(result).toHaveLength(1);
129
- expect(result[0]).toEqual({
130
- operator: "+",
131
- duration: dayjs.duration(_define_property({}, unit, 1))
132
- });
133
- }
134
- }
135
- catch (err) {
136
- _didIteratorError = true;
137
- _iteratorError = err;
138
- }
139
- finally {
140
- try {
141
- if (!_iteratorNormalCompletion && _iterator.return != null) {
142
- _iterator.return();
143
- }
144
- }
145
- finally {
146
- if (_didIteratorError) {
147
- throw _iteratorError;
148
- }
149
- }
150
- }
151
- });
152
- it("should handle complex multi-unit expressions", function () {
153
- var result = parseRelativeTime("now+1y2M3w4d5h6m7s8ms");
154
- expect(result).toHaveLength(1);
155
- expect(result[0]).toEqual({
156
- operator: "+",
157
- duration: dayjs.duration({
158
- years: 1,
159
- months: 2,
160
- weeks: 3,
161
- days: 4,
162
- hours: 5,
163
- minutes: 6,
164
- seconds: 7,
165
- milliseconds: 8
166
- })
167
- });
168
- });
169
- it("should handle same unit multiple times (should sum)", function () {
170
- var result = parseRelativeTime("now+1h2h3h");
171
- expect(result).toHaveLength(1);
172
- expect(result[0]).toEqual({
173
- operator: "+",
174
- duration: dayjs.duration({
175
- hours: 6
176
- })
177
- });
178
- });
179
- it("should throw error for invalid format", function () {
180
- expect(function () {
181
- return parseRelativeTime("invalid");
182
- }).toThrow("Invalid relative time format");
183
- expect(function () {
184
- return parseRelativeTime("now+");
185
- }).toThrow("Invalid relative time format");
186
- expect(function () {
187
- return parseRelativeTime("now+abc");
188
- }).toThrow("Invalid duration specification");
189
- });
190
- it("should throw error for invalid units in truncation", function () {
191
- expect(function () {
192
- return parseRelativeTime("now/invalid");
193
- }).toThrow("Invalid unit");
194
- });
195
- it("should throw error for invalid units in duration", function () {
196
- expect(function () {
197
- return parseRelativeTime("now+1invalid");
198
- }).toThrow("Invalid unit");
199
- });
200
- it("should handle whitespace correctly", function () {
201
- // The regex should handle operands without extra whitespace issues
202
- var result = parseRelativeTime("now+1h30m");
203
- expect(result).toHaveLength(1);
204
- expect(result[0]).toEqual({
205
- operator: "+",
206
- duration: dayjs.duration({
207
- hours: 1,
208
- minutes: 30
209
- })
210
- });
211
- });
212
- it("should parse real-world examples", function () {
213
- // Last 24 hours
214
- var result1 = parseRelativeTime("now-24h");
215
- expect(result1).toHaveLength(1);
216
- expect(result1[0]).toEqual({
217
- operator: "-",
218
- duration: dayjs.duration({
219
- hours: 24
220
- })
221
- });
222
- // Start of previous month
223
- var result2 = parseRelativeTime("now-1M/M");
224
- expect(result2).toHaveLength(2);
225
- expect(result2[0]).toEqual({
226
- operator: "-",
227
- duration: dayjs.duration({
228
- months: 1
229
- })
230
- });
231
- expect(result2[1]).toEqual({
232
- operator: "/",
233
- unit: "M"
234
- });
235
- // Complex time ago
236
- var result3 = parseRelativeTime("now-1w2d3h");
237
- expect(result3).toHaveLength(1);
238
- expect(result3[0]).toEqual({
239
- operator: "-",
240
- duration: dayjs.duration({
241
- weeks: 1,
242
- days: 2,
243
- hours: 3
244
- })
245
- });
246
- });
21
+ describe('parseRelativeTime', () => {
22
+ it('should return empty array for "now"', () => {
23
+ var result = parseRelativeTime('now');
24
+ expect(result).toEqual([]);
25
+ });
26
+ it('should parse simple addition operations', () => {
27
+ var result = parseRelativeTime('now+1h');
28
+ expect(result).toHaveLength(1);
29
+ expect(result[0]).toEqual({
30
+ operator: '+',
31
+ duration: dayjs.duration({
32
+ hours: 1,
33
+ }),
34
+ });
35
+ });
36
+ it('should parse simple subtraction operations', () => {
37
+ var result = parseRelativeTime('now-30m');
38
+ expect(result).toHaveLength(1);
39
+ expect(result[0]).toEqual({
40
+ operator: '-',
41
+ duration: dayjs.duration({
42
+ minutes: 30,
43
+ }),
44
+ });
45
+ });
46
+ it('should parse milliseconds', () => {
47
+ var result = parseRelativeTime('now+500ms');
48
+ expect(result).toHaveLength(1);
49
+ expect(result[0]).toEqual({
50
+ operator: '+',
51
+ duration: dayjs.duration({
52
+ milliseconds: 500,
53
+ }),
54
+ });
55
+ });
56
+ it('should parse complex duration combinations', () => {
57
+ var result = parseRelativeTime('now+1d6h30m');
58
+ expect(result).toHaveLength(1);
59
+ expect(result[0]).toEqual({
60
+ operator: '+',
61
+ duration: dayjs.duration({
62
+ days: 1,
63
+ hours: 6,
64
+ minutes: 30,
65
+ }),
66
+ });
67
+ });
68
+ it('should parse truncation operations', () => {
69
+ var result = parseRelativeTime('now/d');
70
+ expect(result).toHaveLength(1);
71
+ expect(result[0]).toEqual({
72
+ operator: '/',
73
+ unit: 'd',
74
+ });
75
+ var result2 = parseRelativeTime('now\\M');
76
+ expect(result2).toHaveLength(1);
77
+ expect(result2[0]).toEqual({
78
+ operator: '\\',
79
+ unit: 'M',
80
+ });
81
+ });
82
+ it('should parse multiple operations', () => {
83
+ var result = parseRelativeTime('now-1h+30m');
84
+ expect(result).toHaveLength(2);
85
+ expect(result[0]).toEqual({
86
+ operator: '-',
87
+ duration: dayjs.duration({
88
+ hours: 1,
89
+ }),
90
+ });
91
+ expect(result[1]).toEqual({
92
+ operator: '+',
93
+ duration: dayjs.duration({
94
+ minutes: 30,
95
+ }),
96
+ });
97
+ });
98
+ it('should parse mixed arithmetic and truncation operations', () => {
99
+ var result = parseRelativeTime('now-1M/M');
100
+ expect(result).toHaveLength(2);
101
+ expect(result[0]).toEqual({
102
+ operator: '-',
103
+ duration: dayjs.duration({
104
+ months: 1,
105
+ }),
106
+ });
107
+ expect(result[1]).toEqual({
108
+ operator: '/',
109
+ unit: 'M',
110
+ });
111
+ });
112
+ it('should handle all supported units', () => {
113
+ var units = ['ms', 's', 'm', 'h', 'd', 'w', 'M', 'Q', 'y'];
114
+ var _iteratorNormalCompletion = true,
115
+ _didIteratorError = false,
116
+ _iteratorError;
117
+ try {
118
+ for (
119
+ var _iterator = units[Symbol.iterator](), _step;
120
+ !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
121
+ _iteratorNormalCompletion = true
122
+ ) {
123
+ var unit = _step.value;
124
+ var result = parseRelativeTime('now+1'.concat(unit));
125
+ expect(result).toHaveLength(1);
126
+ expect(result[0]).toEqual({
127
+ operator: '+',
128
+ duration: dayjs.duration(_define_property({}, unit, 1)),
129
+ });
130
+ }
131
+ } catch (err) {
132
+ _didIteratorError = true;
133
+ _iteratorError = err;
134
+ } finally {
135
+ try {
136
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
137
+ _iterator.return();
138
+ }
139
+ } finally {
140
+ if (_didIteratorError) {
141
+ throw _iteratorError;
142
+ }
143
+ }
144
+ }
145
+ });
146
+ it('should handle complex multi-unit expressions', () => {
147
+ var result = parseRelativeTime('now+1y2M3w4d5h6m7s8ms');
148
+ expect(result).toHaveLength(1);
149
+ expect(result[0]).toEqual({
150
+ operator: '+',
151
+ duration: dayjs.duration({
152
+ years: 1,
153
+ months: 2,
154
+ weeks: 3,
155
+ days: 4,
156
+ hours: 5,
157
+ minutes: 6,
158
+ seconds: 7,
159
+ milliseconds: 8,
160
+ }),
161
+ });
162
+ });
163
+ it('should handle same unit multiple times (should sum)', () => {
164
+ var result = parseRelativeTime('now+1h2h3h');
165
+ expect(result).toHaveLength(1);
166
+ expect(result[0]).toEqual({
167
+ operator: '+',
168
+ duration: dayjs.duration({
169
+ hours: 6,
170
+ }),
171
+ });
172
+ });
173
+ it('should throw error for invalid format', () => {
174
+ expect(() => parseRelativeTime('invalid')).toThrow('Invalid relative time format');
175
+ expect(() => parseRelativeTime('now+')).toThrow('Invalid relative time format');
176
+ expect(() => parseRelativeTime('now+abc')).toThrow('Invalid duration specification');
177
+ });
178
+ it('should throw error for invalid units in truncation', () => {
179
+ expect(() => parseRelativeTime('now/invalid')).toThrow('Invalid unit');
180
+ });
181
+ it('should throw error for invalid units in duration', () => {
182
+ expect(() => parseRelativeTime('now+1invalid')).toThrow('Invalid unit');
183
+ });
184
+ it('should handle whitespace correctly', () => {
185
+ // The regex should handle operands without extra whitespace issues
186
+ var result = parseRelativeTime('now+1h30m');
187
+ expect(result).toHaveLength(1);
188
+ expect(result[0]).toEqual({
189
+ operator: '+',
190
+ duration: dayjs.duration({
191
+ hours: 1,
192
+ minutes: 30,
193
+ }),
194
+ });
195
+ });
196
+ it('should parse real-world examples', () => {
197
+ // Last 24 hours
198
+ var result1 = parseRelativeTime('now-24h');
199
+ expect(result1).toHaveLength(1);
200
+ expect(result1[0]).toEqual({
201
+ operator: '-',
202
+ duration: dayjs.duration({
203
+ hours: 24,
204
+ }),
205
+ });
206
+ // Start of previous month
207
+ var result2 = parseRelativeTime('now-1M/M');
208
+ expect(result2).toHaveLength(2);
209
+ expect(result2[0]).toEqual({
210
+ operator: '-',
211
+ duration: dayjs.duration({
212
+ months: 1,
213
+ }),
214
+ });
215
+ expect(result2[1]).toEqual({
216
+ operator: '/',
217
+ unit: 'M',
218
+ });
219
+ // Complex time ago
220
+ var result3 = parseRelativeTime('now-1w2d3h');
221
+ expect(result3).toHaveLength(1);
222
+ expect(result3[0]).toEqual({
223
+ operator: '-',
224
+ duration: dayjs.duration({
225
+ weeks: 1,
226
+ days: 2,
227
+ hours: 3,
228
+ }),
229
+ });
230
+ });
247
231
  });
@@ -1,77 +1,78 @@
1
- import { parseDate } from "@wener/utils";
2
- import dayjs from "dayjs";
3
- import duration from "dayjs/plugin/duration";
4
- import quarterOfYear from "dayjs/plugin/quarterOfYear";
5
- import weekOfYear from "dayjs/plugin/weekOfYear";
1
+ import { parseDate } from '@wener/utils';
2
+ import dayjs from 'dayjs';
3
+ import duration from 'dayjs/plugin/duration';
4
+ import quarterOfYear from 'dayjs/plugin/quarterOfYear';
5
+ import weekOfYear from 'dayjs/plugin/weekOfYear';
6
+
6
7
  dayjs.extend(duration);
7
8
  dayjs.extend(quarterOfYear);
8
9
  dayjs.extend(weekOfYear);
9
10
  export function resolveRelativeTime(s, now) {
10
- {
11
- let out = parseDate(s);
12
- if (out) {
13
- return out;
14
- }
15
- if (s == null) {
16
- return;
17
- }
18
- }
19
- if (typeof s === 'string' && s.startsWith('now')) {
20
- // valid
21
- } else {
22
- throw new Error(`Invalid relative time format: ${s}`);
23
- }
24
- let current = dayjs(now);
25
- const remaining = s.slice(3); // Remove 'now'
26
- if (!remaining) {
27
- return current.toDate();
28
- }
29
- // Split by operators while keeping them in the result
30
- const parts = remaining.split(/([+\-/\\])/).filter((part)=>part !== '');
31
- for(let i = 0; i < parts.length; i += 2){
32
- const operator = parts[i];
33
- const operand = parts[i + 1];
34
- if (!operator || !operand) {
35
- throw new Error(`Invalid relative time format: ${s}`);
36
- }
37
- if (operator === '/') {
38
- // Truncation to start operation
39
- checkDurationUnit(operand);
40
- current = current.startOf(operand);
41
- } else if (operator === '\\') {
42
- // Truncation to end operation
43
- checkDurationUnit(operand);
44
- current = current.endOf(operand);
45
- } else {
46
- // Addition or subtraction operation
47
- const match = operand.match(/^(\d+)([smhdwMQy])$/);
48
- if (!match) {
49
- throw new Error(`Invalid time operand: ${operand}`);
50
- }
51
- const [, amountStr, unit] = match;
52
- checkDurationUnit(unit);
53
- const amount = parseInt(amountStr, 10);
54
- if (operator === '+') {
55
- current = current.add(amount, unit);
56
- } else if (operator === '-') {
57
- current = current.subtract(amount, unit);
58
- } else {
59
- throw new Error(`Invalid operator: ${operator}`);
60
- }
61
- }
62
- }
63
- return current.toDate();
11
+ {
12
+ let out = parseDate(s);
13
+ if (out) {
14
+ return out;
15
+ }
16
+ if (s == null) {
17
+ return;
18
+ }
19
+ }
20
+ if (typeof s === 'string' && s.startsWith('now')) {
21
+ // valid
22
+ } else {
23
+ throw new Error(`Invalid relative time format: ${s}`);
24
+ }
25
+ let current = dayjs(now);
26
+ const remaining = s.slice(3); // Remove 'now'
27
+ if (!remaining) {
28
+ return current.toDate();
29
+ }
30
+ // Split by operators while keeping them in the result
31
+ const parts = remaining.split(/([+\-/\\])/).filter((part) => part !== '');
32
+ for (let i = 0; i < parts.length; i += 2) {
33
+ const operator = parts[i];
34
+ const operand = parts[i + 1];
35
+ if (!operator || !operand) {
36
+ throw new Error(`Invalid relative time format: ${s}`);
37
+ }
38
+ if (operator === '/') {
39
+ // Truncation to start operation
40
+ checkDurationUnit(operand);
41
+ current = current.startOf(operand);
42
+ } else if (operator === '\\') {
43
+ // Truncation to end operation
44
+ checkDurationUnit(operand);
45
+ current = current.endOf(operand);
46
+ } else {
47
+ // Addition or subtraction operation
48
+ const match = operand.match(/^(\d+)([smhdwMQy])$/);
49
+ if (!match) {
50
+ throw new Error(`Invalid time operand: ${operand}`);
51
+ }
52
+ const [, amountStr, unit] = match;
53
+ checkDurationUnit(unit);
54
+ const amount = parseInt(amountStr, 10);
55
+ if (operator === '+') {
56
+ current = current.add(amount, unit);
57
+ } else if (operator === '-') {
58
+ current = current.subtract(amount, unit);
59
+ } else {
60
+ throw new Error(`Invalid operator: ${operator}`);
61
+ }
62
+ }
63
+ }
64
+ return current.toDate();
64
65
  }
65
66
  /** Array of all valid time units */ export const RelativeDurationUnits = [
66
- 'ms',
67
- 's',
68
- 'm',
69
- 'h',
70
- 'd',
71
- 'w',
72
- 'M',
73
- 'Q',
74
- 'y'
67
+ 'ms',
68
+ 's',
69
+ 'm',
70
+ 'h',
71
+ 'd',
72
+ 'w',
73
+ 'M',
74
+ 'Q',
75
+ 'y',
75
76
  ];
76
77
  /**
77
78
  * Type guard to validate and assert that a string is a valid time unit.
@@ -79,9 +80,9 @@ export function resolveRelativeTime(s, now) {
79
80
  * @param u - The string to check
80
81
  * @throws {Error} When the unit is not supported
81
82
  */ export function checkDurationUnit(u) {
82
- if (!RelativeDurationUnits.includes(u)) {
83
- throw new Error(`Invalid unit: ${u}`);
84
- }
83
+ if (!RelativeDurationUnits.includes(u)) {
84
+ throw new Error(`Invalid unit: ${u}`);
85
+ }
85
86
  }
86
87
 
87
- //# sourceMappingURL=resolveRelativeTime.js.map
88
+ //# sourceMappingURL=resolveRelativeTime.js.map