@wener/common 2.0.3 → 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.
- package/lib/ai/qwen3vl/index.js +1 -1
- package/lib/ai/qwen3vl/utils.js +15 -15
- package/lib/ai/qwen3vl/utils.js.map +1 -1
- package/lib/ai/vision/DocLayoutElementTypeSchema.js +22 -22
- package/lib/ai/vision/ImageAnnotationSchema.js +63 -47
- package/lib/ai/vision/index.js +2 -2
- package/lib/ai/vision/resolveImageAnnotation.js +81 -95
- package/lib/cn/ChineseResidentIdNo.js +55 -41
- package/lib/cn/ChineseResidentIdNo.js.map +1 -1
- package/lib/cn/ChineseResidentIdNo.mod.js +6 -1
- package/lib/cn/ChineseResidentIdNo.test.js +22 -21
- package/lib/cn/DivisionCode.js +220 -235
- package/lib/cn/DivisionCode.mod.js +6 -1
- package/lib/cn/DivisionCode.test.js +92 -121
- package/lib/cn/Mod11.js +18 -37
- package/lib/cn/Mod11.js.map +1 -1
- package/lib/cn/Mod31.js +23 -41
- package/lib/cn/UnifiedSocialCreditCode.js +143 -137
- package/lib/cn/UnifiedSocialCreditCode.mod.js +6 -1
- package/lib/cn/UnifiedSocialCreditCode.test.js +21 -15
- package/lib/cn/formatChineseAmount.js +46 -71
- package/lib/cn/index.js +6 -6
- package/lib/cn/mod.js +5 -3
- package/lib/cn/parseChineseNumber.js +81 -85
- package/lib/cn/parseChineseNumber.test.js +183 -261
- package/lib/cn/pinyin/cartesianProduct.js +19 -19
- package/lib/cn/pinyin/cartesianProduct.test.js +78 -178
- package/lib/cn/pinyin/loader.js +13 -11
- package/lib/cn/pinyin/preload.js +2 -1
- package/lib/cn/pinyin/toPinyin.test.js +149 -161
- package/lib/cn/pinyin/toPinyinPure.js +28 -23
- package/lib/cn/pinyin/transform.js +11 -11
- package/lib/cn/types.d.js +2 -2
- package/lib/consola/createStandardConsolaReporter.js +14 -15
- package/lib/consola/formatLogObject.js +149 -133
- package/lib/consola/formatLogObject.js.map +1 -1
- package/lib/consola/formatLogObject.test.js +167 -178
- package/lib/consola/index.js +2 -2
- package/lib/data/formatSort.js +14 -12
- package/lib/data/formatSort.test.js +33 -33
- package/lib/data/index.js +3 -3
- package/lib/data/maybeNumber.js +23 -23
- package/lib/data/maybeNumber.js.map +1 -1
- package/lib/data/parseSort.js +75 -68
- package/lib/data/parseSort.test.js +196 -187
- package/lib/data/resolvePagination.js +38 -39
- package/lib/data/resolvePagination.test.js +228 -218
- package/lib/data/types.d.js +2 -2
- package/lib/data/types.d.js.map +1 -1
- package/lib/dayjs/dayjs.js +20 -20
- package/lib/dayjs/formatDuration.js +56 -56
- package/lib/dayjs/formatDuration.js.map +1 -1
- package/lib/dayjs/formatDuration.test.js +63 -77
- package/lib/dayjs/index.js +4 -4
- package/lib/dayjs/parseDuration.js +21 -26
- package/lib/dayjs/parseRelativeTime.js +65 -66
- package/lib/dayjs/parseRelativeTime.test.js +227 -243
- package/lib/dayjs/resolveRelativeTime.js +74 -144
- package/lib/dayjs/resolveRelativeTime.js.map +1 -1
- package/lib/dayjs/resolveRelativeTime.test.js +296 -307
- package/lib/decimal/index.js +1 -1
- package/lib/decimal/parseDecimal.js +12 -12
- package/lib/drain3/Drain.js +321 -0
- package/lib/drain3/Drain.js.map +1 -0
- package/lib/drain3/LogCluster.js +38 -0
- package/lib/drain3/LogCluster.js.map +1 -0
- package/lib/drain3/Node.js +39 -0
- package/lib/drain3/Node.js.map +1 -0
- package/lib/drain3/TemplateMiner.js +205 -0
- package/lib/drain3/TemplateMiner.js.map +1 -0
- package/lib/drain3/index.js +31 -0
- package/lib/drain3/index.js.map +1 -0
- package/lib/drain3/persistence/FilePersistence.js +24 -0
- package/lib/drain3/persistence/FilePersistence.js.map +1 -0
- package/lib/drain3/persistence/MemoryPersistence.js +18 -0
- package/lib/drain3/persistence/MemoryPersistence.js.map +1 -0
- package/lib/drain3/persistence/PersistenceHandler.js +5 -0
- package/lib/drain3/persistence/PersistenceHandler.js.map +1 -0
- package/lib/drain3/types.js +7 -0
- package/lib/drain3/types.js.map +1 -0
- package/lib/emittery/emitter.js +7 -7
- package/lib/emittery/index.js +1 -1
- package/lib/foundation/schema/SexType.js +15 -12
- package/lib/foundation/schema/index.js +1 -1
- package/lib/foundation/schema/parseSexType.js +15 -16
- package/lib/foundation/schema/types.js +8 -6
- package/lib/fs/FileSystemError.js +18 -18
- package/lib/fs/IFileSystem.d.js +2 -2
- package/lib/fs/IFileSystem.d.js.map +1 -1
- package/lib/fs/MemoryFileSystem.test.js +172 -181
- package/lib/fs/createBrowserFileSystem.js +222 -233
- package/lib/fs/createBrowserFileSystem.js.map +1 -1
- package/lib/fs/createMemoryFileSystem.js +473 -510
- package/lib/fs/createMemoryFileSystem.js.map +1 -1
- package/lib/fs/createSandboxFileSystem.js +102 -101
- package/lib/fs/createSandboxFileSystem.js.map +1 -1
- package/lib/fs/createWebDavFileSystem.js +162 -132
- package/lib/fs/createWebDavFileSystem.js.map +1 -1
- package/lib/fs/createWebFileSystem.js +202 -0
- package/lib/fs/createWebFileSystem.js.map +1 -0
- package/lib/fs/findMimeType.js +14 -14
- package/lib/fs/findMimeType.js.map +1 -1
- package/lib/fs/index.js +7 -7
- package/lib/fs/index.js.map +1 -1
- package/lib/fs/minio/createMinioFileSystem.js +977 -0
- package/lib/fs/minio/createMinioFileSystem.js.map +1 -0
- package/lib/fs/minio/index.js +2 -0
- package/lib/fs/minio/index.js.map +1 -0
- package/lib/fs/orpc/FileSystemContract.js +57 -57
- package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
- package/lib/fs/orpc/createContractClientFileSystem.js.map +1 -1
- package/lib/fs/orpc/index.js +2 -2
- package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
- package/lib/fs/orpc/server/createFileSystemContractImpl.js.map +1 -1
- package/lib/fs/orpc/server/index.js +1 -1
- package/lib/fs/s3/createS3MiniFileSystem.js +756 -689
- package/lib/fs/s3/createS3MiniFileSystem.js.map +1 -1
- package/lib/fs/s3/index.js +1 -1
- package/lib/fs/s3/s3mini.test.js +524 -553
- package/lib/fs/scandir.js +56 -56
- package/lib/fs/server/createDatabaseFileSystem.js +834 -741
- package/lib/fs/server/createDatabaseFileSystem.js.map +1 -1
- package/lib/fs/server/createNodeFileSystem.js +407 -380
- package/lib/fs/server/createNodeFileSystem.js.map +1 -1
- package/lib/fs/server/dbfs.test.js +201 -214
- package/lib/fs/server/index.js +1 -1
- package/lib/fs/server/loadTestDatabase.js +40 -43
- package/lib/fs/tests/runFileSystemTest.js +352 -315
- package/lib/fs/tests/runFileSystemTest.js.map +1 -1
- package/lib/fs/types.js +17 -20
- package/lib/fs/utils/getFileUrl.js +24 -30
- package/lib/fs/utils.js +17 -17
- package/lib/fs/utils.js.map +1 -1
- package/lib/fs/webdav/index.js +2 -0
- package/lib/fs/webdav/index.js.map +1 -0
- package/lib/index.js +2 -2
- package/lib/jsonschema/JsonSchema.js +216 -155
- package/lib/jsonschema/JsonSchema.js.map +1 -1
- package/lib/jsonschema/JsonSchema.test.js +123 -124
- package/lib/jsonschema/forEachJsonSchema.js +41 -41
- package/lib/jsonschema/forEachJsonSchema.js.map +1 -1
- package/lib/jsonschema/index.js +2 -2
- package/lib/jsonschema/types.d.js +2 -2
- package/lib/jsonschema/types.d.js.map +1 -1
- package/lib/meta/defineFileType.js +32 -38
- package/lib/meta/defineInit.js +39 -35
- package/lib/meta/defineMetadata.js +37 -34
- package/lib/meta/defineMetadata.js.map +1 -1
- package/lib/meta/defineMetadata.test.js +13 -12
- package/lib/meta/index.js +3 -3
- package/lib/orpc/createOpenApiContractClient.js +26 -24
- package/lib/orpc/createOpenApiContractClient.js.map +1 -1
- package/lib/orpc/createRpcContractClient.js +37 -31
- package/lib/orpc/index.js +2 -2
- package/lib/orpc/resolveLinkPlugins.js +25 -25
- package/lib/password/PHC.js +187 -189
- package/lib/password/PHC.js.map +1 -1
- package/lib/password/PHC.test.js +517 -535
- package/lib/password/Password.js +85 -80
- package/lib/password/Password.test.js +330 -364
- package/lib/password/createArgon2PasswordAlgorithm.js +50 -51
- package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBase64PasswordAlgorithm.js +11 -11
- package/lib/password/createBase64PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
- package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -1
- package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createScryptPasswordAlgorithm.js +74 -63
- package/lib/password/createScryptPasswordAlgorithm.js.map +1 -1
- package/lib/password/index.js +5 -5
- package/lib/password/server/index.js +1 -1
- package/lib/resource/Identifiable.js +2 -2
- package/lib/resource/ListQuery.js +42 -42
- package/lib/resource/ListQuery.js.map +1 -1
- package/lib/resource/getTitleOfResource.js +5 -5
- package/lib/resource/index.js +2 -2
- package/lib/resource/index.js.map +1 -1
- package/lib/resource/schema/AnyResourceSchema.js +91 -89
- package/lib/resource/schema/BaseResourceSchema.js +26 -26
- package/lib/resource/schema/ResourceActionType.js +117 -115
- package/lib/resource/schema/ResourceStatus.js +94 -92
- package/lib/resource/schema/ResourceType.js +25 -23
- package/lib/resource/schema/index.js +5 -5
- package/lib/resource/schema/types.js +86 -55
- package/lib/resource/schema/types.test.js +16 -13
- package/lib/s3/formatS3Url.js +60 -60
- package/lib/s3/formatS3Url.js.map +1 -1
- package/lib/s3/formatS3Url.test.js +238 -261
- package/lib/s3/index.js +2 -2
- package/lib/s3/parseS3Url.js +61 -60
- package/lib/s3/parseS3Url.js.map +1 -1
- package/lib/s3/parseS3Url.test.js +270 -269
- package/lib/schema/SchemaRegistry.js +41 -42
- package/lib/schema/SchemaRegistry.js.map +1 -1
- package/lib/schema/SchemaRegistry.mod.js +1 -1
- package/lib/schema/TypeSchema.d.js +2 -2
- package/lib/schema/TypeSchema.d.js.map +1 -1
- package/lib/schema/createSchemaData.js +113 -67
- package/lib/schema/createSchemaData.js.map +1 -1
- package/lib/schema/findJsonSchemaByPath.js +28 -23
- package/lib/schema/findJsonSchemaByPath.js.map +1 -1
- package/lib/schema/formatZodError.js +113 -134
- package/lib/schema/formatZodError.js.map +1 -1
- package/lib/schema/formatZodError.test.js +192 -195
- package/lib/schema/getSchemaCache.js +7 -7
- package/lib/schema/getSchemaOptions.js +17 -16
- package/lib/schema/index.js +6 -6
- package/lib/schema/toJsonSchema.js +196 -190
- package/lib/schema/toJsonSchema.js.map +1 -1
- package/lib/schema/toJsonSchema.test.js +34 -26
- package/lib/schema/validate.js +106 -97
- package/lib/schema/validate.js.map +1 -1
- package/lib/tools/generateSchema.js +40 -40
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +74 -74
- package/lib/utils/buildBaseUrl.js +8 -8
- package/lib/utils/buildRedactorFormSchema.js +55 -54
- package/lib/utils/buildRedactorFormSchema.js.map +1 -1
- package/lib/utils/getEstimateProcessTime.js +24 -19
- package/lib/utils/index.js +3 -3
- package/lib/utils/resolveFeatureOptions.js +9 -9
- package/package.json +37 -18
- package/src/ai/qwen3vl/utils.ts +1 -1
- package/src/ai/vision/index.ts +2 -2
- package/src/cn/ChineseResidentIdNo.ts +1 -1
- package/src/cn/Mod11.ts +1 -1
- package/src/cn/__snapshots__/ChineseResidentIdNo.test.ts.snap +1 -1
- package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +0 -23
- package/src/cn/index.ts +1 -2
- package/src/cn/parseChineseNumber.test.ts +4 -4
- package/src/consola/formatLogObject.ts +6 -6
- package/src/consola/index.ts +1 -1
- package/src/data/index.ts +3 -4
- package/src/data/maybeNumber.ts +1 -1
- package/src/data/parseSort.test.ts +0 -1
- package/src/data/resolvePagination.ts +2 -2
- package/src/data/types.d.ts +2 -2
- package/src/dayjs/formatDuration.ts +10 -11
- package/src/dayjs/index.ts +1 -1
- package/src/dayjs/parseRelativeTime.ts +1 -1
- package/src/dayjs/resolveRelativeTime.ts +11 -14
- package/src/drain3/Drain.test.ts +378 -0
- package/src/drain3/Drain.ts +394 -0
- package/src/drain3/LogCluster.ts +46 -0
- package/src/drain3/Node.ts +53 -0
- package/src/drain3/TemplateMiner.ts +246 -0
- package/src/drain3/index.ts +34 -0
- package/src/drain3/persistence/FilePersistence.ts +24 -0
- package/src/drain3/persistence/MemoryPersistence.ts +23 -0
- package/src/drain3/persistence/PersistenceHandler.ts +19 -0
- package/src/drain3/types.ts +75 -0
- package/src/fs/IFileSystem.d.ts +1 -2
- package/src/fs/createBrowserFileSystem.ts +7 -5
- package/src/fs/createMemoryFileSystem.ts +9 -13
- package/src/fs/createSandboxFileSystem.ts +1 -1
- package/src/fs/createWebDavFileSystem.ts +30 -17
- package/src/fs/createWebFileSystem.ts +242 -0
- package/src/fs/findMimeType.ts +1 -4
- package/src/fs/index.ts +5 -5
- package/src/fs/minio/createMinioFileSystem.ts +1148 -0
- package/src/fs/minio/index.ts +1 -0
- package/src/fs/orpc/createContractClientFileSystem.ts +5 -5
- package/src/fs/orpc/server/createFileSystemContractImpl.ts +1 -1
- package/src/fs/s3/createS3MiniFileSystem.ts +120 -79
- package/src/fs/s3/s3fs.test.ts +441 -0
- package/src/fs/s3/s3mini.test.ts +2 -2
- package/src/fs/server/createDatabaseFileSystem.ts +78 -114
- package/src/fs/server/createNodeFileSystem.ts +32 -13
- package/src/fs/server/dbfs.test.ts +13 -8
- package/src/fs/server/index.ts +1 -0
- package/src/fs/server/loadTestDatabase.ts +8 -119
- package/src/fs/tests/runFileSystemTest.ts +29 -28
- package/src/fs/utils.ts +1 -1
- package/src/fs/webdav/index.ts +1 -0
- package/src/jsonschema/JsonSchema.ts +5 -5
- package/src/jsonschema/forEachJsonSchema.ts +1 -1
- package/src/jsonschema/index.ts +1 -1
- package/src/jsonschema/types.d.ts +1 -1
- package/src/meta/defineMetadata.ts +1 -1
- package/src/meta/index.ts +2 -3
- package/src/orm/createSqliteDialect.ts +17 -0
- package/src/orm/index.ts +1 -0
- package/src/orpc/createOpenApiContractClient.ts +3 -3
- package/src/orpc/index.ts +1 -1
- package/src/password/PHC.ts +3 -3
- package/src/password/createArgon2PasswordAlgorithm.ts +2 -2
- package/src/password/createBase64PasswordAlgorithm.ts +2 -2
- package/src/password/createBcryptPasswordAlgorithm.ts +4 -2
- package/src/password/createPBKDF2PasswordAlgorithm.ts +2 -2
- package/src/password/createScryptPasswordAlgorithm.ts +4 -4
- package/src/password/index.ts +2 -2
- package/src/resource/ListQuery.ts +4 -1
- package/src/resource/index.ts +3 -3
- package/src/resource/schema/index.ts +4 -4
- package/src/s3/formatS3Url.test.ts +1 -1
- package/src/s3/formatS3Url.ts +2 -2
- package/src/s3/index.ts +1 -1
- package/src/s3/parseS3Url.ts +1 -1
- package/src/schema/SchemaRegistry.ts +2 -2
- package/src/schema/TypeSchema.d.ts +6 -6
- package/src/schema/createSchemaData.ts +5 -5
- package/src/schema/findJsonSchemaByPath.ts +5 -5
- package/src/schema/formatZodError.test.ts +2 -1
- package/src/schema/formatZodError.ts +50 -62
- package/src/schema/index.ts +5 -5
- package/src/schema/toJsonSchema.ts +6 -6
- package/src/schema/validate.ts +2 -2
- package/src/utils/buildRedactorFormSchema.ts +4 -4
- package/src/utils/formatNumber.ts +18 -0
- package/src/utils/formatPercent.ts +17 -0
- package/src/utils/index.ts +3 -3
- package/src/utils/resolveFeatureOptions.ts +1 -1
|
@@ -1,148 +1,78 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
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
|
+
|
|
5
7
|
dayjs.extend(duration);
|
|
6
8
|
dayjs.extend(quarterOfYear);
|
|
7
9
|
dayjs.extend(weekOfYear);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
* resolveRelativeTime('now\\y') // End of current year
|
|
64
|
-
*
|
|
65
|
-
* // Complex expressions
|
|
66
|
-
* resolveRelativeTime('now/d+1d-1s') // End of current day (alternative method)
|
|
67
|
-
* resolveRelativeTime('now\\d') // End of current day (direct method)
|
|
68
|
-
* resolveRelativeTime('now-1w+2d-3h') // 1 week ago, plus 2 days, minus 3 hours
|
|
69
|
-
*
|
|
70
|
-
* // With reference date
|
|
71
|
-
* const refDate = new Date('2024-03-15T14:30:45.123Z');
|
|
72
|
-
* resolveRelativeTime('now-1h', refDate); // One hour before reference date
|
|
73
|
-
* resolveRelativeTime('now\\d', refDate); // End of reference day
|
|
74
|
-
* ```
|
|
75
|
-
*/ export function resolveRelativeTime(s, now) {
|
|
76
|
-
{
|
|
77
|
-
let out;
|
|
78
|
-
if (!s) {} else if (s instanceof Date) {
|
|
79
|
-
out = s;
|
|
80
|
-
} else if (typeof s === 'number') {
|
|
81
|
-
out = new Date(s);
|
|
82
|
-
} else if (!s.startsWith('now')) {
|
|
83
|
-
out = new Date(s);
|
|
84
|
-
}
|
|
85
|
-
if (out) {
|
|
86
|
-
if (isNaN(out.getTime())) {
|
|
87
|
-
throw new Error(`Invalid date: ${s}`);
|
|
88
|
-
}
|
|
89
|
-
return out;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (typeof s !== 'string' || !s.startsWith('now')) {
|
|
93
|
-
throw new Error(`Invalid relative time format: ${s}`);
|
|
94
|
-
}
|
|
95
|
-
let current = dayjs(now);
|
|
96
|
-
const remaining = s.slice(3); // Remove 'now'
|
|
97
|
-
if (!remaining) {
|
|
98
|
-
return current.toDate();
|
|
99
|
-
}
|
|
100
|
-
// Split by operators while keeping them in the result
|
|
101
|
-
const parts = remaining.split(/([+\-/\\])/).filter((part)=>part !== '');
|
|
102
|
-
for(let i = 0; i < parts.length; i += 2){
|
|
103
|
-
const operator = parts[i];
|
|
104
|
-
const operand = parts[i + 1];
|
|
105
|
-
if (!operator || !operand) {
|
|
106
|
-
throw new Error(`Invalid relative time format: ${s}`);
|
|
107
|
-
}
|
|
108
|
-
if (operator === '/') {
|
|
109
|
-
// Truncation to start operation
|
|
110
|
-
checkDurationUnit(operand);
|
|
111
|
-
current = current.startOf(operand);
|
|
112
|
-
} else if (operator === '\\') {
|
|
113
|
-
// Truncation to end operation
|
|
114
|
-
checkDurationUnit(operand);
|
|
115
|
-
current = current.endOf(operand);
|
|
116
|
-
} else {
|
|
117
|
-
// Addition or subtraction operation
|
|
118
|
-
const match = operand.match(/^(\d+)([smhdwMQy])$/);
|
|
119
|
-
if (!match) {
|
|
120
|
-
throw new Error(`Invalid time operand: ${operand}`);
|
|
121
|
-
}
|
|
122
|
-
const [, amountStr, unit] = match;
|
|
123
|
-
checkDurationUnit(unit);
|
|
124
|
-
const amount = parseInt(amountStr, 10);
|
|
125
|
-
if (operator === '+') {
|
|
126
|
-
current = current.add(amount, unit);
|
|
127
|
-
} else if (operator === '-') {
|
|
128
|
-
current = current.subtract(amount, unit);
|
|
129
|
-
} else {
|
|
130
|
-
throw new Error(`Invalid operator: ${operator}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return current.toDate();
|
|
10
|
+
export function resolveRelativeTime(s, now) {
|
|
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();
|
|
135
65
|
}
|
|
136
66
|
/** Array of all valid time units */ export const RelativeDurationUnits = [
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
67
|
+
'ms',
|
|
68
|
+
's',
|
|
69
|
+
'm',
|
|
70
|
+
'h',
|
|
71
|
+
'd',
|
|
72
|
+
'w',
|
|
73
|
+
'M',
|
|
74
|
+
'Q',
|
|
75
|
+
'y',
|
|
146
76
|
];
|
|
147
77
|
/**
|
|
148
78
|
* Type guard to validate and assert that a string is a valid time unit.
|
|
@@ -150,9 +80,9 @@ dayjs.extend(weekOfYear);
|
|
|
150
80
|
* @param u - The string to check
|
|
151
81
|
* @throws {Error} When the unit is not supported
|
|
152
82
|
*/ export function checkDurationUnit(u) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
83
|
+
if (!RelativeDurationUnits.includes(u)) {
|
|
84
|
+
throw new Error(`Invalid unit: ${u}`);
|
|
85
|
+
}
|
|
156
86
|
}
|
|
157
87
|
|
|
158
|
-
//# sourceMappingURL=resolveRelativeTime.js.map
|
|
88
|
+
//# sourceMappingURL=resolveRelativeTime.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/dayjs/resolveRelativeTime.ts"],"sourcesContent":["import dayjs from 'dayjs';\nimport duration from 'dayjs/plugin/duration';\nimport quarterOfYear from 'dayjs/plugin/quarterOfYear';\nimport weekOfYear from 'dayjs/plugin/weekOfYear';\n\ndayjs.extend(duration);\ndayjs.extend(quarterOfYear);\ndayjs.extend(weekOfYear);\n\n/**\n * Resolves a Grafana-style relative time string and returns the corresponding Date.\n *\n * @param s - The relative time string to parse (must start with \"now\")\n * @param now - Optional reference date. If undefined, uses current time\n * @returns The calculated Date based on the relative time expression\n *\n * @throws {Error} When the format is invalid or contains unsupported operators/units\n *\n * ## Syntax\n *\n * All expressions must start with `now` followed by optional operations:\n *\n * ### Operators\n * - `-` - Subtraction (e.g., `now-1h` for one hour ago)\n * - `+` - Addition (e.g., `now+1d` for one day from now)\n * - `/` - Truncation to beginning of time unit (e.g., `now/d` for start of current day)\n * - `\\` - Truncation to end of time unit (e.g., `now\\d` for end of current day)\n *\n * ### Time Units\n * - `s` - seconds\n * - `m` - minutes\n * - `h` - hours\n * - `d` - days\n * - `w` - weeks\n * - `M` - months\n * - `Q` - quarters\n * - `y` - years\n *\n * ### Operations\n * Operations are processed sequentially from left to right:\n * - **Addition/Subtraction**: `now±{count}{unit}` (e.g., `now-24h`, `now+1d`)\n * - **Truncation to start**: `now/{unit}` (e.g., `now/d`, `now/M`)\n * - **Truncation to end**: `now\\{unit}` (e.g., `now\\d`, `now\\M`)\n * - **Complex**: Multiple operations can be chained (e.g., `now-1M/M`, `now\\d`, `now-1y\\y`)\n *\n * @example\n * ```typescript\n * // Basic usage\n * resolveRelativeTime('now') // Current time\n * resolveRelativeTime('now-1h') // One hour ago\n * resolveRelativeTime('now+1d') // One day from now\n * resolveRelativeTime('now/d') // Start of current day\n * resolveRelativeTime('now\\\\d') // End of current day\n *\n * // Common patterns\n * resolveRelativeTime('now-24h') // Last 24 hours\n * resolveRelativeTime('now-7d') // Last 7 days\n * resolveRelativeTime('now/M') // Start of current month\n * resolveRelativeTime('now\\\\M') // End of current month\n * resolveRelativeTime('now-1M/M') // Start of previous month\n * resolveRelativeTime('now-1M\\\\M') // End of previous month\n * resolveRelativeTime('now/w') // Start of current week\n * resolveRelativeTime('now\\\\w') // End of current week\n * resolveRelativeTime('now/y') // Start of current year\n * resolveRelativeTime('now\\\\y') // End of current year\n *\n * // Complex expressions\n * resolveRelativeTime('now/d+1d-1s') // End of current day (alternative method)\n * resolveRelativeTime('now\\\\d') // End of current day (direct method)\n * resolveRelativeTime('now-1w+2d-3h') // 1 week ago, plus 2 days, minus 3 hours\n *\n * // With reference date\n * const refDate = new Date('2024-03-15T14:30:45.123Z');\n * resolveRelativeTime('now-1h', refDate); // One hour before reference date\n * resolveRelativeTime('now\\\\d', refDate); // End of reference day\n * ```\n */\nexport function resolveRelativeTime(s: string | Date | number, now?: Date | undefined): Date
|
|
1
|
+
{"version":3,"sources":["../../src/dayjs/resolveRelativeTime.ts"],"sourcesContent":["import { parseDate, type MaybeDate } from '@wener/utils';\nimport dayjs from 'dayjs';\nimport duration from 'dayjs/plugin/duration';\nimport quarterOfYear from 'dayjs/plugin/quarterOfYear';\nimport weekOfYear from 'dayjs/plugin/weekOfYear';\n\ndayjs.extend(duration);\ndayjs.extend(quarterOfYear);\ndayjs.extend(weekOfYear);\n\n/**\n * Resolves a Grafana-style relative time string and returns the corresponding Date.\n *\n * @param s - The relative time string to parse (must start with \"now\")\n * @param now - Optional reference date. If undefined, uses current time\n * @returns The calculated Date based on the relative time expression\n *\n * @throws {Error} When the format is invalid or contains unsupported operators/units\n *\n * ## Syntax\n *\n * All expressions must start with `now` followed by optional operations:\n *\n * ### Operators\n * - `-` - Subtraction (e.g., `now-1h` for one hour ago)\n * - `+` - Addition (e.g., `now+1d` for one day from now)\n * - `/` - Truncation to beginning of time unit (e.g., `now/d` for start of current day)\n * - `\\` - Truncation to end of time unit (e.g., `now\\d` for end of current day)\n *\n * ### Time Units\n * - `s` - seconds\n * - `m` - minutes\n * - `h` - hours\n * - `d` - days\n * - `w` - weeks\n * - `M` - months\n * - `Q` - quarters\n * - `y` - years\n *\n * ### Operations\n * Operations are processed sequentially from left to right:\n * - **Addition/Subtraction**: `now±{count}{unit}` (e.g., `now-24h`, `now+1d`)\n * - **Truncation to start**: `now/{unit}` (e.g., `now/d`, `now/M`)\n * - **Truncation to end**: `now\\{unit}` (e.g., `now\\d`, `now\\M`)\n * - **Complex**: Multiple operations can be chained (e.g., `now-1M/M`, `now\\d`, `now-1y\\y`)\n *\n * @example\n * ```typescript\n * // Basic usage\n * resolveRelativeTime('now') // Current time\n * resolveRelativeTime('now-1h') // One hour ago\n * resolveRelativeTime('now+1d') // One day from now\n * resolveRelativeTime('now/d') // Start of current day\n * resolveRelativeTime('now\\\\d') // End of current day\n *\n * // Common patterns\n * resolveRelativeTime('now-24h') // Last 24 hours\n * resolveRelativeTime('now-7d') // Last 7 days\n * resolveRelativeTime('now/M') // Start of current month\n * resolveRelativeTime('now\\\\M') // End of current month\n * resolveRelativeTime('now-1M/M') // Start of previous month\n * resolveRelativeTime('now-1M\\\\M') // End of previous month\n * resolveRelativeTime('now/w') // Start of current week\n * resolveRelativeTime('now\\\\w') // End of current week\n * resolveRelativeTime('now/y') // Start of current year\n * resolveRelativeTime('now\\\\y') // End of current year\n *\n * // Complex expressions\n * resolveRelativeTime('now/d+1d-1s') // End of current day (alternative method)\n * resolveRelativeTime('now\\\\d') // End of current day (direct method)\n * resolveRelativeTime('now-1w+2d-3h') // 1 week ago, plus 2 days, minus 3 hours\n *\n * // With reference date\n * const refDate = new Date('2024-03-15T14:30:45.123Z');\n * resolveRelativeTime('now-1h', refDate); // One hour before reference date\n * resolveRelativeTime('now\\\\d', refDate); // End of reference day\n * ```\n */\nexport function resolveRelativeTime(s: string | Date | number, now?: Date | undefined): Date;\nexport function resolveRelativeTime(s: MaybeDate, now?: Date | undefined): Date | undefined;\nexport function resolveRelativeTime(s: MaybeDate, now?: Date | undefined): Date | undefined {\n\t{\n\t\tlet out = parseDate(s);\n\t\tif (out) {\n\t\t\treturn out;\n\t\t}\n\t\tif (s == null) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tif (typeof s === 'string' && s.startsWith('now')) {\n\t\t// valid\n\t} else {\n\t\tthrow new Error(`Invalid relative time format: ${s}`);\n\t}\n\n\tlet current = dayjs(now);\n\tconst remaining = s.slice(3); // Remove 'now'\n\n\tif (!remaining) {\n\t\treturn current.toDate();\n\t}\n\n\t// Split by operators while keeping them in the result\n\tconst parts = remaining.split(/([+\\-/\\\\])/).filter((part) => part !== '');\n\n\tfor (let i = 0; i < parts.length; i += 2) {\n\t\tconst operator = parts[i];\n\t\tconst operand = parts[i + 1];\n\n\t\tif (!operator || !operand) {\n\t\t\tthrow new Error(`Invalid relative time format: ${s}`);\n\t\t}\n\n\t\tif (operator === '/') {\n\t\t\t// Truncation to start operation\n\t\t\tcheckDurationUnit(operand);\n\t\t\tcurrent = current.startOf(operand);\n\t\t} else if (operator === '\\\\') {\n\t\t\t// Truncation to end operation\n\t\t\tcheckDurationUnit(operand);\n\t\t\tcurrent = current.endOf(operand);\n\t\t} else {\n\t\t\t// Addition or subtraction operation\n\t\t\tconst match = operand.match(/^(\\d+)([smhdwMQy])$/);\n\t\t\tif (!match) {\n\t\t\t\tthrow new Error(`Invalid time operand: ${operand}`);\n\t\t\t}\n\n\t\t\tconst [, amountStr, unit] = match;\n\t\t\tcheckDurationUnit(unit);\n\t\t\tconst amount = parseInt(amountStr, 10);\n\n\t\t\tif (operator === '+') {\n\t\t\t\tcurrent = current.add(amount, unit as 'w');\n\t\t\t} else if (operator === '-') {\n\t\t\t\tcurrent = current.subtract(amount, unit as 'Q');\n\t\t\t} else {\n\t\t\t\tthrow new Error(`Invalid operator: ${operator}`);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn current.toDate();\n}\n\n/** Array of all valid time units */\nexport const RelativeDurationUnits = ['ms', 's', 'm', 'h', 'd', 'w', 'M', 'Q', 'y'] as const;\n\n/** Valid time units for relative time expressions */\nexport type RelativeDurationUnit = (typeof RelativeDurationUnits)[number];\n\n/**\n * Type guard to validate and assert that a string is a valid time unit.\n *\n * @param u - The string to check\n * @throws {Error} When the unit is not supported\n */\nexport function checkDurationUnit(u: string): asserts u is RelativeDurationUnit {\n\tif (!RelativeDurationUnits.includes(u as RelativeDurationUnit)) {\n\t\tthrow new Error(`Invalid unit: ${u}`);\n\t}\n}\n"],"names":["parseDate","dayjs","duration","quarterOfYear","weekOfYear","extend","resolveRelativeTime","s","now","out","startsWith","Error","current","remaining","slice","toDate","parts","split","filter","part","i","length","operator","operand","checkDurationUnit","startOf","endOf","match","amountStr","unit","amount","parseInt","add","subtract","RelativeDurationUnits","u","includes"],"mappings":"AAAA,SAASA,SAAS,QAAwB,eAAe;AACzD,OAAOC,WAAW,QAAQ;AAC1B,OAAOC,cAAc,wBAAwB;AAC7C,OAAOC,mBAAmB,6BAA6B;AACvD,OAAOC,gBAAgB,0BAA0B;AAEjDH,MAAMI,MAAM,CAACH;AACbD,MAAMI,MAAM,CAACF;AACbF,MAAMI,MAAM,CAACD;AAwEb,OAAO,SAASE,oBAAoBC,CAAY,EAAEC,GAAsB;IACvE;QACC,IAAIC,MAAMT,UAAUO;QACpB,IAAIE,KAAK;YACR,OAAOA;QACR;QACA,IAAIF,KAAK,MAAM;YACd;QACD;IACD;IAEA,IAAI,OAAOA,MAAM,YAAYA,EAAEG,UAAU,CAAC,QAAQ;IACjD,QAAQ;IACT,OAAO;QACN,MAAM,IAAIC,MAAM,CAAC,8BAA8B,EAAEJ,GAAG;IACrD;IAEA,IAAIK,UAAUX,MAAMO;IACpB,MAAMK,YAAYN,EAAEO,KAAK,CAAC,IAAI,eAAe;IAE7C,IAAI,CAACD,WAAW;QACf,OAAOD,QAAQG,MAAM;IACtB;IAEA,sDAAsD;IACtD,MAAMC,QAAQH,UAAUI,KAAK,CAAC,cAAcC,MAAM,CAAC,CAACC,OAASA,SAAS;IAEtE,IAAK,IAAIC,IAAI,GAAGA,IAAIJ,MAAMK,MAAM,EAAED,KAAK,EAAG;QACzC,MAAME,WAAWN,KAAK,CAACI,EAAE;QACzB,MAAMG,UAAUP,KAAK,CAACI,IAAI,EAAE;QAE5B,IAAI,CAACE,YAAY,CAACC,SAAS;YAC1B,MAAM,IAAIZ,MAAM,CAAC,8BAA8B,EAAEJ,GAAG;QACrD;QAEA,IAAIe,aAAa,KAAK;YACrB,gCAAgC;YAChCE,kBAAkBD;YAClBX,UAAUA,QAAQa,OAAO,CAACF;QAC3B,OAAO,IAAID,aAAa,MAAM;YAC7B,8BAA8B;YAC9BE,kBAAkBD;YAClBX,UAAUA,QAAQc,KAAK,CAACH;QACzB,OAAO;YACN,oCAAoC;YACpC,MAAMI,QAAQJ,QAAQI,KAAK,CAAC;YAC5B,IAAI,CAACA,OAAO;gBACX,MAAM,IAAIhB,MAAM,CAAC,sBAAsB,EAAEY,SAAS;YACnD;YAEA,MAAM,GAAGK,WAAWC,KAAK,GAAGF;YAC5BH,kBAAkBK;YAClB,MAAMC,SAASC,SAASH,WAAW;YAEnC,IAAIN,aAAa,KAAK;gBACrBV,UAAUA,QAAQoB,GAAG,CAACF,QAAQD;YAC/B,OAAO,IAAIP,aAAa,KAAK;gBAC5BV,UAAUA,QAAQqB,QAAQ,CAACH,QAAQD;YACpC,OAAO;gBACN,MAAM,IAAIlB,MAAM,CAAC,kBAAkB,EAAEW,UAAU;YAChD;QACD;IACD;IAEA,OAAOV,QAAQG,MAAM;AACtB;AAEA,kCAAkC,GAClC,OAAO,MAAMmB,wBAAwB;IAAC;IAAM;IAAK;IAAK;IAAK;IAAK;IAAK;IAAK;IAAK;CAAI,CAAU;AAK7F;;;;;CAKC,GACD,OAAO,SAASV,kBAAkBW,CAAS;IAC1C,IAAI,CAACD,sBAAsBE,QAAQ,CAACD,IAA4B;QAC/D,MAAM,IAAIxB,MAAM,CAAC,cAAc,EAAEwB,GAAG;IACrC;AACD"}
|