@twin.org/core 0.0.4-next.13 → 0.0.4-next.14
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/dist/es/index.js +1 -0
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/IDuration.js.map +1 -1
- package/dist/es/types/duration.js +127 -26
- package/dist/es/types/duration.js.map +1 -1
- package/dist/es/types/durationRegExp.js +4 -0
- package/dist/es/types/durationRegExp.js.map +1 -0
- package/dist/es/utils/guards.js +1 -1
- package/dist/es/utils/guards.js.map +1 -1
- package/dist/es/utils/is.js +42 -9
- package/dist/es/utils/is.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/models/IDuration.d.ts +12 -0
- package/dist/types/types/durationRegExp.d.ts +1 -0
- package/dist/types/utils/guards.d.ts +3 -2
- package/docs/changelog.md +17 -0
- package/docs/reference/classes/Guards.md +3 -3
- package/docs/reference/index.md +1 -0
- package/docs/reference/interfaces/IDuration.md +24 -0
- package/docs/reference/variables/DURATION_REG_EXP.md +3 -0
- package/package.json +2 -2
package/dist/es/index.js
CHANGED
|
@@ -51,6 +51,7 @@ export * from "./models/mutexMessageTypes.js";
|
|
|
51
51
|
export * from "./models/sharedObjectBufferMessageTypes.js";
|
|
52
52
|
export * from "./types/bitString.js";
|
|
53
53
|
export * from "./types/duration.js";
|
|
54
|
+
export * from "./types/durationRegExp.js";
|
|
54
55
|
export * from "./types/objectOrArray.js";
|
|
55
56
|
export * from "./types/singleOccurrenceArray.js";
|
|
56
57
|
export * from "./types/singleOccurrenceArrayDepthHelper.js";
|
package/dist/es/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB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AAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AACjD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./encoding/base32.js\";\nexport * from \"./encoding/base58.js\";\nexport * from \"./encoding/base64.js\";\nexport * from \"./encoding/base64Url.js\";\nexport * from \"./errors/alreadyExistsError.js\";\nexport * from \"./errors/baseError.js\";\nexport * from \"./errors/conflictError.js\";\nexport * from \"./errors/generalError.js\";\nexport * from \"./errors/guardError.js\";\nexport * from \"./errors/notFoundError.js\";\nexport * from \"./errors/notImplementedError.js\";\nexport * from \"./errors/notSupportedError.js\";\nexport * from \"./errors/unauthorizedError.js\";\nexport * from \"./errors/unprocessableError.js\";\nexport * from \"./errors/validationError.js\";\nexport * from \"./factories/componentFactory.js\";\nexport * from \"./factories/factory.js\";\nexport * from \"./helpers/arrayHelper.js\";\nexport * from \"./helpers/envHelper.js\";\nexport * from \"./helpers/errorHelper.js\";\nexport * from \"./helpers/filenameHelper.js\";\nexport * from \"./helpers/hexHelper.js\";\nexport * from \"./helpers/jsonHelper.js\";\nexport * from \"./helpers/numberHelper.js\";\nexport * from \"./helpers/objectHelper.js\";\nexport * from \"./helpers/randomHelper.js\";\nexport * from \"./helpers/stringHelper.js\";\nexport * from \"./helpers/uint8ArrayHelper.js\";\nexport * from \"./models/coerceType.js\";\nexport * from \"./models/IDuration.js\";\nexport * from \"./models/compressionType.js\";\nexport * from \"./models/healthStatus.js\";\nexport * from \"./models/IComponent.js\";\nexport * from \"./models/IError.js\";\nexport * from \"./models/IHealth.js\";\nexport * from \"./models/II18nShared.js\";\nexport * from \"./models/IKeyValue.js\";\nexport * from \"./models/ILabelledValue.js\";\nexport * from \"./models/ILocale.js\";\nexport * from \"./models/ILocaleDictionary.js\";\nexport * from \"./models/ILocalesIndex.js\";\nexport * from \"./models/IMutexWorkerMessage.js\";\nexport * from \"./models/IPatchOperation.js\";\nexport * from \"./models/ISharedObjectBufferOptions.js\";\nexport * from \"./models/ISharedObjectBufferWorkerMessage.js\";\nexport * from \"./models/IUrlParts.js\";\nexport * from \"./models/IValidationFailure.js\";\nexport * from \"./models/mutexMessageTypes.js\";\nexport * from \"./models/sharedObjectBufferMessageTypes.js\";\nexport * from \"./types/bitString.js\";\nexport * from \"./types/duration.js\";\nexport * from \"./types/objectOrArray.js\";\nexport * from \"./types/singleOccurrenceArray.js\";\nexport * from \"./types/singleOccurrenceArrayDepthHelper.js\";\nexport * from \"./types/url.js\";\nexport * from \"./types/urn.js\";\nexport * from \"./utils/asyncCache.js\";\nexport * from \"./utils/coerce.js\";\nexport * from \"./utils/compression.js\";\nexport * from \"./utils/converter.js\";\nexport * from \"./utils/guards.js\";\nexport * from \"./utils/i18n.js\";\nexport * from \"./utils/is.js\";\nexport * from \"./utils/mutex.js\";\nexport * from \"./utils/sharedObjectBuffer.js\";\nexport * from \"./utils/sharedStore.js\";\nexport * from \"./utils/validation.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cA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AC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AACjD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./encoding/base32.js\";\nexport * from \"./encoding/base58.js\";\nexport * from \"./encoding/base64.js\";\nexport * from \"./encoding/base64Url.js\";\nexport * from \"./errors/alreadyExistsError.js\";\nexport * from \"./errors/baseError.js\";\nexport * from \"./errors/conflictError.js\";\nexport * from \"./errors/generalError.js\";\nexport * from \"./errors/guardError.js\";\nexport * from \"./errors/notFoundError.js\";\nexport * from \"./errors/notImplementedError.js\";\nexport * from \"./errors/notSupportedError.js\";\nexport * from \"./errors/unauthorizedError.js\";\nexport * from \"./errors/unprocessableError.js\";\nexport * from \"./errors/validationError.js\";\nexport * from \"./factories/componentFactory.js\";\nexport * from \"./factories/factory.js\";\nexport * from \"./helpers/arrayHelper.js\";\nexport * from \"./helpers/envHelper.js\";\nexport * from \"./helpers/errorHelper.js\";\nexport * from \"./helpers/filenameHelper.js\";\nexport * from \"./helpers/hexHelper.js\";\nexport * from \"./helpers/jsonHelper.js\";\nexport * from \"./helpers/numberHelper.js\";\nexport * from \"./helpers/objectHelper.js\";\nexport * from \"./helpers/randomHelper.js\";\nexport * from \"./helpers/stringHelper.js\";\nexport * from \"./helpers/uint8ArrayHelper.js\";\nexport * from \"./models/coerceType.js\";\nexport * from \"./models/IDuration.js\";\nexport * from \"./models/compressionType.js\";\nexport * from \"./models/healthStatus.js\";\nexport * from \"./models/IComponent.js\";\nexport * from \"./models/IError.js\";\nexport * from \"./models/IHealth.js\";\nexport * from \"./models/II18nShared.js\";\nexport * from \"./models/IKeyValue.js\";\nexport * from \"./models/ILabelledValue.js\";\nexport * from \"./models/ILocale.js\";\nexport * from \"./models/ILocaleDictionary.js\";\nexport * from \"./models/ILocalesIndex.js\";\nexport * from \"./models/IMutexWorkerMessage.js\";\nexport * from \"./models/IPatchOperation.js\";\nexport * from \"./models/ISharedObjectBufferOptions.js\";\nexport * from \"./models/ISharedObjectBufferWorkerMessage.js\";\nexport * from \"./models/IUrlParts.js\";\nexport * from \"./models/IValidationFailure.js\";\nexport * from \"./models/mutexMessageTypes.js\";\nexport * from \"./models/sharedObjectBufferMessageTypes.js\";\nexport * from \"./types/bitString.js\";\nexport * from \"./types/duration.js\";\nexport * from \"./types/durationRegExp.js\";\nexport * from \"./types/objectOrArray.js\";\nexport * from \"./types/singleOccurrenceArray.js\";\nexport * from \"./types/singleOccurrenceArrayDepthHelper.js\";\nexport * from \"./types/url.js\";\nexport * from \"./types/urn.js\";\nexport * from \"./utils/asyncCache.js\";\nexport * from \"./utils/coerce.js\";\nexport * from \"./utils/compression.js\";\nexport * from \"./utils/converter.js\";\nexport * from \"./utils/guards.js\";\nexport * from \"./utils/i18n.js\";\nexport * from \"./utils/is.js\";\nexport * from \"./utils/mutex.js\";\nexport * from \"./utils/sharedObjectBuffer.js\";\nexport * from \"./utils/sharedStore.js\";\nexport * from \"./utils/validation.js\";\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IDuration.js","sourceRoot":"","sources":["../../../src/models/IDuration.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Represents a duration broken down into its component parts.\n */\nexport interface IDuration {\n\t/**\n\t * The number of years.\n\t */\n\tyears: number;\n\n\t/**\n\t * The number of months.\n\t */\n\tmonths: number;\n\n\t/**\n\t * The number of weeks.\n\t */\n\tweeks: number;\n\n\t/**\n\t * The number of days.\n\t */\n\tdays: number;\n\n\t/**\n\t * The number of hours.\n\t */\n\thours: number;\n\n\t/**\n\t * The number of minutes.\n\t */\n\tminutes: number;\n\n\t/**\n\t * The number of seconds.\n\t */\n\tseconds: number;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"IDuration.js","sourceRoot":"","sources":["../../../src/models/IDuration.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Represents a duration broken down into its component parts.\n */\nexport interface IDuration {\n\t/**\n\t * The number of years.\n\t */\n\tyears: number;\n\n\t/**\n\t * The number of months.\n\t */\n\tmonths: number;\n\n\t/**\n\t * The number of weeks.\n\t */\n\tweeks: number;\n\n\t/**\n\t * The number of days.\n\t */\n\tdays: number;\n\n\t/**\n\t * The number of hours.\n\t */\n\thours: number;\n\n\t/**\n\t * The number of minutes.\n\t */\n\tminutes: number;\n\n\t/**\n\t * The number of seconds.\n\t */\n\tseconds: number;\n\n\t/**\n\t * The number of milliseconds.\n\t */\n\tmilliseconds?: number;\n\n\t/**\n\t * The number of microseconds.\n\t */\n\tmicroseconds?: number;\n\n\t/**\n\t * The number of nanoseconds.\n\t */\n\tnanoseconds?: number;\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DURATION_REG_EXP } from "./durationRegExp.js";
|
|
1
2
|
import { Guards } from "../utils/guards.js";
|
|
2
3
|
/**
|
|
3
4
|
* Helper methods for working with ISO 8601 durations.
|
|
@@ -13,19 +14,36 @@ export class Duration {
|
|
|
13
14
|
* @returns The parsed duration, or undefined if the string is not a valid ISO 8601 duration.
|
|
14
15
|
*/
|
|
15
16
|
static parse(value) {
|
|
16
|
-
const match =
|
|
17
|
-
if (!match?.slice(
|
|
17
|
+
const match = DURATION_REG_EXP.exec(value);
|
|
18
|
+
if (!match?.slice(2).some(Boolean)) {
|
|
18
19
|
return undefined;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
const sign = match[1] === "-" ? -1 : 1;
|
|
22
|
+
const secondsValue = match[8] ?? "0";
|
|
23
|
+
const [secondsIntegerPart, secondsFractionPart] = secondsValue.split(".");
|
|
24
|
+
const fractionPadded = `${secondsFractionPart ?? ""}000000000`.slice(0, 9);
|
|
25
|
+
const milliseconds = Number.parseInt(fractionPadded.slice(0, 3), 10);
|
|
26
|
+
const microseconds = Number.parseInt(fractionPadded.slice(3, 6), 10);
|
|
27
|
+
const nanoseconds = Number.parseInt(fractionPadded.slice(6, 9), 10);
|
|
28
|
+
const duration = {
|
|
29
|
+
years: Duration.applySign(Number(match[2] ?? 0), sign),
|
|
30
|
+
months: Duration.applySign(Number(match[3] ?? 0), sign),
|
|
31
|
+
weeks: Duration.applySign(Number(match[4] ?? 0), sign),
|
|
32
|
+
days: Duration.applySign(Number(match[5] ?? 0), sign),
|
|
33
|
+
hours: Duration.applySign(Number(match[6] ?? 0), sign),
|
|
34
|
+
minutes: Duration.applySign(Number(match[7] ?? 0), sign),
|
|
35
|
+
seconds: Duration.applySign(Number(secondsIntegerPart ?? 0), sign)
|
|
28
36
|
};
|
|
37
|
+
if (milliseconds !== 0) {
|
|
38
|
+
duration.milliseconds = milliseconds * sign;
|
|
39
|
+
}
|
|
40
|
+
if (microseconds !== 0) {
|
|
41
|
+
duration.microseconds = microseconds * sign;
|
|
42
|
+
}
|
|
43
|
+
if (nanoseconds !== 0) {
|
|
44
|
+
duration.nanoseconds = nanoseconds * sign;
|
|
45
|
+
}
|
|
46
|
+
return duration;
|
|
29
47
|
}
|
|
30
48
|
/**
|
|
31
49
|
* Convert a duration object to an ISO 8601 duration string.
|
|
@@ -34,34 +52,44 @@ export class Duration {
|
|
|
34
52
|
*/
|
|
35
53
|
static toString(duration) {
|
|
36
54
|
Guards.object(Duration.CLASS_NAME, "duration", duration);
|
|
55
|
+
const sign = Duration.resolveSign(duration);
|
|
37
56
|
const dateParts = [];
|
|
38
|
-
|
|
39
|
-
|
|
57
|
+
const absYears = Duration.absolute(duration.years);
|
|
58
|
+
if (absYears !== 0) {
|
|
59
|
+
dateParts.push(`${absYears}Y`);
|
|
40
60
|
}
|
|
41
|
-
|
|
42
|
-
|
|
61
|
+
const absMonths = Duration.absolute(duration.months);
|
|
62
|
+
if (absMonths !== 0) {
|
|
63
|
+
dateParts.push(`${absMonths}M`);
|
|
43
64
|
}
|
|
44
|
-
|
|
45
|
-
|
|
65
|
+
const absWeeks = Duration.absolute(duration.weeks);
|
|
66
|
+
if (absWeeks !== 0) {
|
|
67
|
+
dateParts.push(`${absWeeks}W`);
|
|
46
68
|
}
|
|
47
|
-
|
|
48
|
-
|
|
69
|
+
const absDays = Duration.absolute(duration.days);
|
|
70
|
+
if (absDays !== 0) {
|
|
71
|
+
dateParts.push(`${absDays}D`);
|
|
49
72
|
}
|
|
50
73
|
const timeParts = [];
|
|
51
|
-
|
|
52
|
-
|
|
74
|
+
const absHours = Duration.absolute(duration.hours);
|
|
75
|
+
if (absHours !== 0) {
|
|
76
|
+
timeParts.push(`${absHours}H`);
|
|
53
77
|
}
|
|
54
|
-
|
|
55
|
-
|
|
78
|
+
const absMinutes = Duration.absolute(duration.minutes);
|
|
79
|
+
if (absMinutes !== 0) {
|
|
80
|
+
timeParts.push(`${absMinutes}M`);
|
|
56
81
|
}
|
|
57
|
-
|
|
58
|
-
|
|
82
|
+
const secondFraction = Duration.formatSecondFraction(Duration.absolute(duration.milliseconds), Duration.absolute(duration.microseconds), Duration.absolute(duration.nanoseconds));
|
|
83
|
+
const absSeconds = Duration.absolute(duration.seconds);
|
|
84
|
+
if (absSeconds !== 0 || secondFraction !== undefined) {
|
|
85
|
+
timeParts.push(secondFraction === undefined ? `${absSeconds}S` : `${absSeconds}.${secondFraction}S`);
|
|
59
86
|
}
|
|
60
87
|
if (dateParts.length === 0 && timeParts.length === 0) {
|
|
61
88
|
return "PT0S";
|
|
62
89
|
}
|
|
63
90
|
const timeSection = timeParts.length > 0 ? `T${timeParts.join("")}` : "";
|
|
64
|
-
|
|
91
|
+
const prefix = sign < 0 ? "-" : "";
|
|
92
|
+
return `${prefix}P${dateParts.join("")}${timeSection}`;
|
|
65
93
|
}
|
|
66
94
|
/**
|
|
67
95
|
* Convert a duration object to total seconds.
|
|
@@ -77,7 +105,80 @@ export class Duration {
|
|
|
77
105
|
const days = duration.days * 86_400;
|
|
78
106
|
const hours = duration.hours * 3_600;
|
|
79
107
|
const minutes = duration.minutes * 60;
|
|
80
|
-
|
|
108
|
+
const milliseconds = (duration.milliseconds ?? 0) / 1_000;
|
|
109
|
+
const microseconds = (duration.microseconds ?? 0) / 1_000_000;
|
|
110
|
+
const nanoseconds = (duration.nanoseconds ?? 0) / 1_000_000_000;
|
|
111
|
+
return (years +
|
|
112
|
+
months +
|
|
113
|
+
weeks +
|
|
114
|
+
days +
|
|
115
|
+
hours +
|
|
116
|
+
minutes +
|
|
117
|
+
duration.seconds +
|
|
118
|
+
milliseconds +
|
|
119
|
+
microseconds +
|
|
120
|
+
nanoseconds);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Resolve a common sign for all non-zero components.
|
|
124
|
+
* @param duration The duration.
|
|
125
|
+
* @returns -1 for negative, 1 for positive, 0 for zero.
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
static resolveSign(duration) {
|
|
129
|
+
const fields = [
|
|
130
|
+
duration.years,
|
|
131
|
+
duration.months,
|
|
132
|
+
duration.weeks,
|
|
133
|
+
duration.days,
|
|
134
|
+
duration.hours,
|
|
135
|
+
duration.minutes,
|
|
136
|
+
duration.seconds,
|
|
137
|
+
duration.milliseconds,
|
|
138
|
+
duration.microseconds,
|
|
139
|
+
duration.nanoseconds
|
|
140
|
+
];
|
|
141
|
+
for (const field of fields) {
|
|
142
|
+
const v = field ?? 0;
|
|
143
|
+
if (v !== 0) {
|
|
144
|
+
return v < 0 ? -1 : 1;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Format the sub-second components as a 9-digit fractional second string.
|
|
151
|
+
* @param milliseconds The milliseconds component.
|
|
152
|
+
* @param microseconds The microseconds component.
|
|
153
|
+
* @param nanoseconds The nanoseconds component.
|
|
154
|
+
* @returns The formatted fraction without a leading dot.
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
static formatSecondFraction(milliseconds, microseconds, nanoseconds) {
|
|
158
|
+
if (milliseconds === 0 && microseconds === 0 && nanoseconds === 0) {
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
const fraction = `${(milliseconds % 1000).toString().padStart(3, "0")}${(microseconds % 1000).toString().padStart(3, "0")}${(nanoseconds % 1000).toString().padStart(3, "0")}`;
|
|
162
|
+
return fraction.replace(/0+$/u, "");
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Normalize a numeric value to its absolute magnitude.
|
|
166
|
+
* @param value The value.
|
|
167
|
+
* @returns The absolute value, defaulting undefined to 0.
|
|
168
|
+
* @internal
|
|
169
|
+
*/
|
|
170
|
+
static absolute(value) {
|
|
171
|
+
return Math.abs(value ?? 0);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Apply a global sign while normalizing zero values.
|
|
175
|
+
* @param value The value.
|
|
176
|
+
* @param sign The sign.
|
|
177
|
+
* @returns The signed value with zero normalized to +0.
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
static applySign(value, sign) {
|
|
181
|
+
return value === 0 ? 0 : value * sign;
|
|
81
182
|
}
|
|
82
183
|
}
|
|
83
184
|
//# sourceMappingURL=duration.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duration.js","sourceRoot":"","sources":["../../../src/types/duration.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,QAAQ;IACpB;;OAEG;IACI,MAAM,CAAU,UAAU,cAA8B;IAE/D;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,KAAa;QAChC,MAAM,KAAK,GACV,8JAA8J,CAAC,IAAI,CAClK,KAAK,CACL,CAAC;QACH,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;YACpC,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,OAAO;YACN,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC5B,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC7B,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC5B,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC3B,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC5B,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC9B,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;SAC9B,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,QAAmB;QACzC,MAAM,CAAC,MAAM,CAAY,QAAQ,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;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CAAC,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACzE,OAAO,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,WAAW,EAAE,CAAC;IAC/C,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,SAAS,CAAC,QAAmB;QAC1C,MAAM,CAAC,MAAM,CAAY,QAAQ,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QAC1E,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,GAAG,UAAU,CAAC;QAC1C,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,GAAG,SAAS,CAAC;QAC3C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,GAAG,OAAO,CAAC;QACvC,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,GAAG,MAAM,CAAC;QACpC,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC;QACrC,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,GAAG,EAAE,CAAC;QACtC,OAAO,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC;IAC3E,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IDuration } from \"../models/IDuration.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Helper methods for working with ISO 8601 durations.\n */\nexport class Duration {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Duration>();\n\n\t/**\n\t * Parse an ISO 8601 duration string into its component parts.\n\t * @param value The string to parse.\n\t * @returns The parsed duration, or undefined if the string is not a valid ISO 8601 duration.\n\t */\n\tpublic static parse(value: string): IDuration | undefined {\n\t\tconst match =\n\t\t\t/^P(?:(\\d+(?:\\.\\d+)?)Y)?(?:(\\d+(?:\\.\\d+)?)M)?(?:(\\d+(?:\\.\\d+)?)W)?(?:(\\d+(?:\\.\\d+)?)D)?(?:T(?:(\\d+(?:\\.\\d+)?)H)?(?:(\\d+(?:\\.\\d+)?)M)?(?:(\\d+(?:\\.\\d+)?)S)?)?$/.exec(\n\t\t\t\tvalue\n\t\t\t);\n\t\tif (!match?.slice(1).some(Boolean)) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn {\n\t\t\tyears: Number(match[1] ?? 0),\n\t\t\tmonths: Number(match[2] ?? 0),\n\t\t\tweeks: Number(match[3] ?? 0),\n\t\t\tdays: Number(match[4] ?? 0),\n\t\t\thours: Number(match[5] ?? 0),\n\t\t\tminutes: Number(match[6] ?? 0),\n\t\t\tseconds: Number(match[7] ?? 0)\n\t\t};\n\t}\n\n\t/**\n\t * Convert a duration object to an ISO 8601 duration string.\n\t * @param duration The duration to convert.\n\t * @returns The ISO 8601 duration string (e.g. \"P1Y2M3DT4H5M6S\").\n\t */\n\tpublic static toString(duration: IDuration): string {\n\t\tGuards.object<IDuration>(Duration.CLASS_NAME, nameof(duration), duration);\n\t\tconst dateParts: string[] = [];\n\t\tif (duration.years !== 0) {\n\t\t\tdateParts.push(`${duration.years}Y`);\n\t\t}\n\t\tif (duration.months !== 0) {\n\t\t\tdateParts.push(`${duration.months}M`);\n\t\t}\n\t\tif (duration.weeks !== 0) {\n\t\t\tdateParts.push(`${duration.weeks}W`);\n\t\t}\n\t\tif (duration.days !== 0) {\n\t\t\tdateParts.push(`${duration.days}D`);\n\t\t}\n\n\t\tconst timeParts: string[] = [];\n\t\tif (duration.hours !== 0) {\n\t\t\ttimeParts.push(`${duration.hours}H`);\n\t\t}\n\t\tif (duration.minutes !== 0) {\n\t\t\ttimeParts.push(`${duration.minutes}M`);\n\t\t}\n\t\tif (duration.seconds !== 0) {\n\t\t\ttimeParts.push(`${duration.seconds}S`);\n\t\t}\n\n\t\tif (dateParts.length === 0 && timeParts.length === 0) {\n\t\t\treturn \"PT0S\";\n\t\t}\n\n\t\tconst timeSection = timeParts.length > 0 ? `T${timeParts.join(\"\")}` : \"\";\n\t\treturn `P${dateParts.join(\"\")}${timeSection}`;\n\t}\n\n\t/**\n\t * Convert a duration object to total seconds.\n\t * Year and month components use the average values 365.25 days and 30.4375 days.\n\t * @param duration The duration to convert.\n\t * @returns The total number of seconds.\n\t */\n\tpublic static toSeconds(duration: IDuration): number {\n\t\tGuards.object<IDuration>(Duration.CLASS_NAME, nameof(duration), duration);\n\t\tconst years = duration.years * 31_557_600;\n\t\tconst months = duration.months * 2_629_800;\n\t\tconst weeks = duration.weeks * 604_800;\n\t\tconst days = duration.days * 86_400;\n\t\tconst hours = duration.hours * 3_600;\n\t\tconst minutes = duration.minutes * 60;\n\t\treturn years + months + weeks + days + hours + minutes + duration.seconds;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"duration.js","sourceRoot":"","sources":["../../../src/types/duration.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,QAAQ;IACpB;;OAEG;IACI,MAAM,CAAU,UAAU,cAA8B;IAE/D;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,KAAa;QAChC,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;YACpC,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACvC,MAAM,YAAY,GAAG,KAAK,CAAC,C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CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;QAC/K,OAAO,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACrC,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,QAAQ,CAAC,KAAyB;QAChD,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACK,MAAM,CAAC,SAAS,CAAC,KAAa,EAAE,IAAY;QACnD,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC;IACvC,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { DURATION_REG_EXP } from \"./durationRegExp.js\";\nimport type { IDuration } from \"../models/IDuration.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Helper methods for working with ISO 8601 durations.\n */\nexport class Duration {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Duration>();\n\n\t/**\n\t * Parse an ISO 8601 duration string into its component parts.\n\t * @param value The string to parse.\n\t * @returns The parsed duration, or undefined if the string is not a valid ISO 8601 duration.\n\t */\n\tpublic static parse(value: string): IDuration | undefined {\n\t\tconst match = DURATION_REG_EXP.exec(value);\n\t\tif (!match?.slice(2).some(Boolean)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst sign = match[1] === \"-\" ? -1 : 1;\n\t\tconst secondsValue = match[8] ?? \"0\";\n\t\tconst [secondsIntegerPart, secondsFractionPart] = secondsValue.split(\".\");\n\t\tconst fractionPadded = `${secondsFractionPart ?? \"\"}000000000`.slice(0, 9);\n\t\tconst milliseconds = Number.parseInt(fractionPadded.slice(0, 3), 10);\n\t\tconst microseconds = Number.parseInt(fractionPadded.slice(3, 6), 10);\n\t\tconst nanoseconds = Number.parseInt(fractionPadded.slice(6, 9), 10);\n\n\t\tconst duration: IDuration = {\n\t\t\tyears: Duration.applySign(Number(match[2] ?? 0), sign),\n\t\t\tmonths: Duration.applySign(Number(match[3] ?? 0), sign),\n\t\t\tweeks: Duration.applySign(Number(match[4] ?? 0), sign),\n\t\t\tdays: Duration.applySign(Number(match[5] ?? 0), sign),\n\t\t\thours: Duration.applySign(Number(match[6] ?? 0), sign),\n\t\t\tminutes: Duration.applySign(Number(match[7] ?? 0), sign),\n\t\t\tseconds: Duration.applySign(Number(secondsIntegerPart ?? 0), sign)\n\t\t};\n\n\t\tif (milliseconds !== 0) {\n\t\t\tduration.milliseconds = milliseconds * sign;\n\t\t}\n\t\tif (microseconds !== 0) {\n\t\t\tduration.microseconds = microseconds * sign;\n\t\t}\n\t\tif (nanoseconds !== 0) {\n\t\t\tduration.nanoseconds = nanoseconds * sign;\n\t\t}\n\n\t\treturn duration;\n\t}\n\n\t/**\n\t * Convert a duration object to an ISO 8601 duration string.\n\t * @param duration The duration to convert.\n\t * @returns The ISO 8601 duration string (e.g. \"P1Y2M3DT4H5M6S\").\n\t */\n\tpublic static toString(duration: IDuration): string {\n\t\tGuards.object<IDuration>(Duration.CLASS_NAME, nameof(duration), duration);\n\t\tconst sign = Duration.resolveSign(duration);\n\t\tconst dateParts: string[] = [];\n\t\tconst absYears = Duration.absolute(duration.years);\n\t\tif (absYears !== 0) {\n\t\t\tdateParts.push(`${absYears}Y`);\n\t\t}\n\t\tconst absMonths = Duration.absolute(duration.months);\n\t\tif (absMonths !== 0) {\n\t\t\tdateParts.push(`${absMonths}M`);\n\t\t}\n\t\tconst absWeeks = Duration.absolute(duration.weeks);\n\t\tif (absWeeks !== 0) {\n\t\t\tdateParts.push(`${absWeeks}W`);\n\t\t}\n\t\tconst absDays = Duration.absolute(duration.days);\n\t\tif (absDays !== 0) {\n\t\t\tdateParts.push(`${absDays}D`);\n\t\t}\n\n\t\tconst timeParts: string[] = [];\n\t\tconst absHours = Duration.absolute(duration.hours);\n\t\tif (absHours !== 0) {\n\t\t\ttimeParts.push(`${absHours}H`);\n\t\t}\n\t\tconst absMinutes = Duration.absolute(duration.minutes);\n\t\tif (absMinutes !== 0) {\n\t\t\ttimeParts.push(`${absMinutes}M`);\n\t\t}\n\t\tconst secondFraction = Duration.formatSecondFraction(\n\t\t\tDuration.absolute(duration.milliseconds),\n\t\t\tDuration.absolute(duration.microseconds),\n\t\t\tDuration.absolute(duration.nanoseconds)\n\t\t);\n\t\tconst absSeconds = Duration.absolute(duration.seconds);\n\t\tif (absSeconds !== 0 || secondFraction !== undefined) {\n\t\t\ttimeParts.push(\n\t\t\t\tsecondFraction === undefined ? `${absSeconds}S` : `${absSeconds}.${secondFraction}S`\n\t\t\t);\n\t\t}\n\n\t\tif (dateParts.length === 0 && timeParts.length === 0) {\n\t\t\treturn \"PT0S\";\n\t\t}\n\n\t\tconst timeSection = timeParts.length > 0 ? `T${timeParts.join(\"\")}` : \"\";\n\t\tconst prefix = sign < 0 ? \"-\" : \"\";\n\t\treturn `${prefix}P${dateParts.join(\"\")}${timeSection}`;\n\t}\n\n\t/**\n\t * Convert a duration object to total seconds.\n\t * Year and month components use the average values 365.25 days and 30.4375 days.\n\t * @param duration The duration to convert.\n\t * @returns The total number of seconds.\n\t */\n\tpublic static toSeconds(duration: IDuration): number {\n\t\tGuards.object<IDuration>(Duration.CLASS_NAME, nameof(duration), duration);\n\t\tconst years = duration.years * 31_557_600;\n\t\tconst months = duration.months * 2_629_800;\n\t\tconst weeks = duration.weeks * 604_800;\n\t\tconst days = duration.days * 86_400;\n\t\tconst hours = duration.hours * 3_600;\n\t\tconst minutes = duration.minutes * 60;\n\t\tconst milliseconds = (duration.milliseconds ?? 0) / 1_000;\n\t\tconst microseconds = (duration.microseconds ?? 0) / 1_000_000;\n\t\tconst nanoseconds = (duration.nanoseconds ?? 0) / 1_000_000_000;\n\t\treturn (\n\t\t\tyears +\n\t\t\tmonths +\n\t\t\tweeks +\n\t\t\tdays +\n\t\t\thours +\n\t\t\tminutes +\n\t\t\tduration.seconds +\n\t\t\tmilliseconds +\n\t\t\tmicroseconds +\n\t\t\tnanoseconds\n\t\t);\n\t}\n\n\t/**\n\t * Resolve a common sign for all non-zero components.\n\t * @param duration The duration.\n\t * @returns -1 for negative, 1 for positive, 0 for zero.\n\t * @internal\n\t */\n\tprivate static resolveSign(duration: IDuration): number {\n\t\tconst fields = [\n\t\t\tduration.years,\n\t\t\tduration.months,\n\t\t\tduration.weeks,\n\t\t\tduration.days,\n\t\t\tduration.hours,\n\t\t\tduration.minutes,\n\t\t\tduration.seconds,\n\t\t\tduration.milliseconds,\n\t\t\tduration.microseconds,\n\t\t\tduration.nanoseconds\n\t\t];\n\t\tfor (const field of fields) {\n\t\t\tconst v = field ?? 0;\n\t\t\tif (v !== 0) {\n\t\t\t\treturn v < 0 ? -1 : 1;\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\t/**\n\t * Format the sub-second components as a 9-digit fractional second string.\n\t * @param milliseconds The milliseconds component.\n\t * @param microseconds The microseconds component.\n\t * @param nanoseconds The nanoseconds component.\n\t * @returns The formatted fraction without a leading dot.\n\t * @internal\n\t */\n\tprivate static formatSecondFraction(\n\t\tmilliseconds: number,\n\t\tmicroseconds: number,\n\t\tnanoseconds: number\n\t): string | undefined {\n\t\tif (milliseconds === 0 && microseconds === 0 && nanoseconds === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst fraction = `${(milliseconds % 1000).toString().padStart(3, \"0\")}${(microseconds % 1000).toString().padStart(3, \"0\")}${(nanoseconds % 1000).toString().padStart(3, \"0\")}`;\n\t\treturn fraction.replace(/0+$/u, \"\");\n\t}\n\n\t/**\n\t * Normalize a numeric value to its absolute magnitude.\n\t * @param value The value.\n\t * @returns The absolute value, defaulting undefined to 0.\n\t * @internal\n\t */\n\tprivate static absolute(value: number | undefined): number {\n\t\treturn Math.abs(value ?? 0);\n\t}\n\n\t/**\n\t * Apply a global sign while normalizing zero values.\n\t * @param value The value.\n\t * @param sign The sign.\n\t * @returns The signed value with zero normalized to +0.\n\t * @internal\n\t */\n\tprivate static applySign(value: number, sign: number): number {\n\t\treturn value === 0 ? 0 : value * sign;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// Copyright 2026 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
export const DURATION_REG_EXP = /^([+-])?P(?:(\d+(?:\.\d+)?)Y)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)W)?(?:(\d+(?:\.\d+)?)D)?(?:T(?:(\d+(?:\.\d+)?)H)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)S)?)?$/;
|
|
4
|
+
//# sourceMappingURL=durationRegExp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durationRegExp.js","sourceRoot":"","sources":["../../../src/types/durationRegExp.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,MAAM,CAAC,MAAM,gBAAgB,GAC5B,qKAAqK,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport const DURATION_REG_EXP =\n\t/^([+-])?P(?:(\\d+(?:\\.\\d+)?)Y)?(?:(\\d+(?:\\.\\d+)?)M)?(?:(\\d+(?:\\.\\d+)?)W)?(?:(\\d+(?:\\.\\d+)?)D)?(?:T(?:(\\d+(?:\\.\\d+)?)H)?(?:(\\d+(?:\\.\\d+)?)M)?(?:(\\d+(?:\\.\\d+)?)S)?)?$/;\n"]}
|
package/dist/es/utils/guards.js
CHANGED
|
@@ -418,7 +418,7 @@ export class Guards {
|
|
|
418
418
|
}
|
|
419
419
|
}
|
|
420
420
|
/**
|
|
421
|
-
* Is the property a valid ISO 8601 duration string.
|
|
421
|
+
* Is the property a valid ISO 8601 duration string or IDuration object.
|
|
422
422
|
* @param source The source of the error.
|
|
423
423
|
* @param property The name of the property.
|
|
424
424
|
* @param value The value to test.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;;;;;OAMG;IACI,MAAM,CAAC,OAAO,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACrE,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAClE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACpE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,WAAW,CACxB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CAAC,MAAM,EA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AAgB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { GuardError } from \"../errors/guardError.js\";\nimport { ArrayHelper } from \"../helpers/arrayHelper.js\";\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { ObjectOrArray } from \"../types/objectOrArray.js\";\n\n/**\n * Class to handle guard operations for parameters.\n */\nexport class Guards {\n\t/**\n\t * Is the property defined.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static defined(source: string, property: string, value: unknown): asserts value {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.undefined\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static string(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringValue(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.stringEmpty\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a JSON value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static json(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.json(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringJson\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 url string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64Url(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64Url(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64Url\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base58 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase58(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase58(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase58\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHex(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringValue(source, property, value);\n\t\tif (!HexHelper.isHex(value, allowPrefix)) {\n\t\t\tthrow new GuardError(source, \"guard.stringHex\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value with fixed length.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param length The length of the string to match.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHexLength(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringHex(source, property, value, allowPrefix);\n\t\tif (HexHelper.stripPrefix(value).length !== length) {\n\t\t\tthrow new GuardError(\n\t\t\t\tsource,\n\t\t\t\t\"guard.stringHexLength\",\n\t\t\t\tproperty,\n\t\t\t\tvalue.length,\n\t\t\t\tlength.toString()\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a number.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static number(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.number(value)) {\n\t\t\tthrow new GuardError(source, \"guard.number\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an integer.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static integer(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\tthrow new GuardError(source, \"guard.integer\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a bigint.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static bigint(source: string, property: string, value: unknown): asserts value is bigint {\n\t\tif (!Is.bigint(value)) {\n\t\t\tthrow new GuardError(source, \"guard.bigint\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a boolean.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static boolean(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is boolean {\n\t\tif (!Is.boolean(value)) {\n\t\t\tthrow new GuardError(source, \"guard.boolean\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static date(source: string, property: string, value: unknown): asserts value is Date {\n\t\tif (!Is.date(value)) {\n\t\t\tthrow new GuardError(source, \"guard.date\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-only string (ISO 8601 date, no time component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-time string (ISO 8601 with T separator).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateTimeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateTimeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateTimeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a time-only string (ISO 8601 time, no date component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.timeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in milliseconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampMilliseconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampMilliseconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampMilliseconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in seconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampSeconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampSeconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampSeconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an object with at least one property.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t\tif (Object.keys(value || {}).length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.objectValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static array<T>(source: string, property: string, value: unknown): asserts value is T[] {\n\t\tif (!Is.array<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array with at least one item.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayValue<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T[] {\n\t\tif (!Is.array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.arrayValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property one of a list of items.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayOneOf<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: T,\n\t\toptions: T[]\n\t): asserts value is T {\n\t\tif (!Is.array<T>(options)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (!options.includes(value)) {\n\t\t\tthrow new GuardError(source, \"guard.arrayOneOf\", property, value, options.join(\", \"));\n\t\t}\n\t}\n\n\t/**\n\t * Does the array start with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param startValues The values that must start the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayStartsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tstartValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst startValuesArray = ArrayHelper.fromObjectOrArray(startValues);\n\n\t\tif (!Is.arrayValue<T>(startValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, startValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < startValuesArray.length; i++) {\n\t\t\tif (value[i] !== startValuesArray[i]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayStartsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tstartValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Does the array end with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param endValues The values that must end the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayEndsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tendValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst endValuesArray = ArrayHelper.fromObjectOrArray(endValues);\n\n\t\tif (!Is.arrayValue<T>(endValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, endValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < endValuesArray.length; i++) {\n\t\t\tif (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayEndsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tendValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a Uint8Array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uint8Array(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is Uint8Array {\n\t\tif (!Is.uint8Array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.uint8Array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (!Is.function<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.function\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string formatted as an email address.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static email(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.email(value)) {\n\t\t\tthrow new GuardError(source, \"guard.email\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string containing uuidV7.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param format The format of the uuidV7, either standard or compact.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uuidV7(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tformat?: \"standard\" | \"compact\"\n\t): asserts value is string {\n\t\tif (!Is.uuidV7(value, format)) {\n\t\t\tif (format === \"compact\") {\n\t\t\t\tthrow new GuardError(source, \"guard.uuidV7Compact\", property, value);\n\t\t\t}\n\t\t\tthrow new GuardError(source, \"guard.uuidV7\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a valid ISO 8601 duration string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static duration(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.duration(value)) {\n\t\t\tthrow new GuardError(source, \"guard.duration\", property, value);\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAIpD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;;;;;OAMG;IACI,MAAM,CAAC,OAAO,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACrE,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAClE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACpE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC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AC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,QAAQ,CACrB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { GuardError } from \"../errors/guardError.js\";\nimport { ArrayHelper } from \"../helpers/arrayHelper.js\";\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { IDuration } from \"../models/IDuration.js\";\nimport type { ObjectOrArray } from \"../types/objectOrArray.js\";\n\n/**\n * Class to handle guard operations for parameters.\n */\nexport class Guards {\n\t/**\n\t * Is the property defined.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static defined(source: string, property: string, value: unknown): asserts value {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.undefined\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static string(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringValue(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.stringEmpty\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a JSON value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static json(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.json(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringJson\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 url string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64Url(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64Url(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64Url\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base58 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase58(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase58(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase58\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHex(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringValue(source, property, value);\n\t\tif (!HexHelper.isHex(value, allowPrefix)) {\n\t\t\tthrow new GuardError(source, \"guard.stringHex\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value with fixed length.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param length The length of the string to match.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHexLength(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringHex(source, property, value, allowPrefix);\n\t\tif (HexHelper.stripPrefix(value).length !== length) {\n\t\t\tthrow new GuardError(\n\t\t\t\tsource,\n\t\t\t\t\"guard.stringHexLength\",\n\t\t\t\tproperty,\n\t\t\t\tvalue.length,\n\t\t\t\tlength.toString()\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a number.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static number(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.number(value)) {\n\t\t\tthrow new GuardError(source, \"guard.number\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an integer.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static integer(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\tthrow new GuardError(source, \"guard.integer\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a bigint.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static bigint(source: string, property: string, value: unknown): asserts value is bigint {\n\t\tif (!Is.bigint(value)) {\n\t\t\tthrow new GuardError(source, \"guard.bigint\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a boolean.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static boolean(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is boolean {\n\t\tif (!Is.boolean(value)) {\n\t\t\tthrow new GuardError(source, \"guard.boolean\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static date(source: string, property: string, value: unknown): asserts value is Date {\n\t\tif (!Is.date(value)) {\n\t\t\tthrow new GuardError(source, \"guard.date\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-only string (ISO 8601 date, no time component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date-time string (ISO 8601 with T separator).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static dateTimeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.dateTimeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.dateTimeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a time-only string (ISO 8601 time, no date component).\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timeString(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.timeString(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timeString\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in milliseconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampMilliseconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampMilliseconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampMilliseconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in seconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampSeconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampSeconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampSeconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an object with at least one property.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t\tif (Object.keys(value || {}).length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.objectValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static array<T>(source: string, property: string, value: unknown): asserts value is T[] {\n\t\tif (!Is.array<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array with at least one item.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayValue<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T[] {\n\t\tif (!Is.array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.arrayValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property one of a list of items.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayOneOf<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: T,\n\t\toptions: T[]\n\t): asserts value is T {\n\t\tif (!Is.array<T>(options)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (!options.includes(value)) {\n\t\t\tthrow new GuardError(source, \"guard.arrayOneOf\", property, value, options.join(\", \"));\n\t\t}\n\t}\n\n\t/**\n\t * Does the array start with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param startValues The values that must start the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayStartsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tstartValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst startValuesArray = ArrayHelper.fromObjectOrArray(startValues);\n\n\t\tif (!Is.arrayValue<T>(startValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, startValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < startValuesArray.length; i++) {\n\t\t\tif (value[i] !== startValuesArray[i]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayStartsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tstartValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Does the array end with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param endValues The values that must end the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayEndsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tendValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst endValuesArray = ArrayHelper.fromObjectOrArray(endValues);\n\n\t\tif (!Is.arrayValue<T>(endValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, endValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < endValuesArray.length; i++) {\n\t\t\tif (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayEndsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tendValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a Uint8Array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uint8Array(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is Uint8Array {\n\t\tif (!Is.uint8Array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.uint8Array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (!Is.function<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.function\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string formatted as an email address.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static email(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.email(value)) {\n\t\t\tthrow new GuardError(source, \"guard.email\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string containing uuidV7.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param format The format of the uuidV7, either standard or compact.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uuidV7(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tformat?: \"standard\" | \"compact\"\n\t): asserts value is string {\n\t\tif (!Is.uuidV7(value, format)) {\n\t\t\tif (format === \"compact\") {\n\t\t\t\tthrow new GuardError(source, \"guard.uuidV7Compact\", property, value);\n\t\t\t}\n\t\t\tthrow new GuardError(source, \"guard.uuidV7\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a valid ISO 8601 duration string or IDuration object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static duration(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string | IDuration {\n\t\tif (!Is.duration(value)) {\n\t\t\tthrow new GuardError(source, \"guard.duration\", property, value);\n\t\t}\n\t}\n}\n"]}
|
package/dist/es/utils/is.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright 2024 IOTA Stiftung.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0.
|
|
3
3
|
import { HexHelper } from "../helpers/hexHelper.js";
|
|
4
|
+
import { DURATION_REG_EXP } from "../types/durationRegExp.js";
|
|
4
5
|
/**
|
|
5
6
|
* Class to check types of objects.
|
|
6
7
|
*/
|
|
@@ -370,19 +371,51 @@ export class Is {
|
|
|
370
371
|
*/
|
|
371
372
|
static duration(value) {
|
|
372
373
|
if (Is.object(value)) {
|
|
373
|
-
|
|
374
|
-
Is.number(value.months)
|
|
375
|
-
Is.number(value.weeks)
|
|
376
|
-
Is.number(value.days)
|
|
377
|
-
Is.number(value.hours)
|
|
378
|
-
Is.number(value.minutes)
|
|
379
|
-
Is.number(value.seconds)
|
|
374
|
+
if (!Is.number(value.years) ||
|
|
375
|
+
!Is.number(value.months) ||
|
|
376
|
+
!Is.number(value.weeks) ||
|
|
377
|
+
!Is.number(value.days) ||
|
|
378
|
+
!Is.number(value.hours) ||
|
|
379
|
+
!Is.number(value.minutes) ||
|
|
380
|
+
!Is.number(value.seconds) ||
|
|
381
|
+
(!Is.undefined(value.milliseconds) &&
|
|
382
|
+
(!Is.integer(value.milliseconds) || Math.abs(value.milliseconds) > 999)) ||
|
|
383
|
+
(!Is.undefined(value.microseconds) &&
|
|
384
|
+
(!Is.integer(value.microseconds) || Math.abs(value.microseconds) > 999)) ||
|
|
385
|
+
(!Is.undefined(value.nanoseconds) &&
|
|
386
|
+
(!Is.integer(value.nanoseconds) || Math.abs(value.nanoseconds) > 999))) {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
let sign = 0;
|
|
390
|
+
for (const component of [
|
|
391
|
+
value.years,
|
|
392
|
+
value.months,
|
|
393
|
+
value.weeks,
|
|
394
|
+
value.days,
|
|
395
|
+
value.hours,
|
|
396
|
+
value.minutes,
|
|
397
|
+
value.seconds,
|
|
398
|
+
value.milliseconds ?? 0,
|
|
399
|
+
value.microseconds ?? 0,
|
|
400
|
+
value.nanoseconds ?? 0
|
|
401
|
+
]) {
|
|
402
|
+
if (component !== 0) {
|
|
403
|
+
const componentSign = component < 0 ? -1 : 1;
|
|
404
|
+
if (sign === 0) {
|
|
405
|
+
sign = componentSign;
|
|
406
|
+
}
|
|
407
|
+
else if (sign !== componentSign) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return true;
|
|
380
413
|
}
|
|
381
414
|
if (!Is.stringValue(value)) {
|
|
382
415
|
return false;
|
|
383
416
|
}
|
|
384
|
-
const match =
|
|
385
|
-
return Boolean(match?.slice(
|
|
417
|
+
const match = DURATION_REG_EXP.exec(value);
|
|
418
|
+
return Boolean(match?.slice(2).some(Boolean));
|
|
386
419
|
}
|
|
387
420
|
}
|
|
388
421
|
//# sourceMappingURL=is.js.map
|
package/dist/es/ut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,CAAC,CAC/E,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,KAAc;QACpC,IAAI,EAAE,CAAC,MAAM,CAAY,KAAK,CAAC,EAAE,CAAC;YACjC,OAAO,CACN,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;gBACtB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;gBACvB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;gBACtB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;gBACrB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;gBACtB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC;gBACxB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CACxB,CAAC;QACH,CAAC;QACD,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,OAAO,KAAK,CAAC;QACd,CAAC;QACD,MAAM,KAAK,GACV,8JAA8J,CAAC,IAAI,CAClK,KAAK,CACL,CAAC;QACH,OAAO,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { IDuration } from \"../models/IDuration.js\";\n\n/**\n * Class to check types of objects.\n */\nexport class Is {\n\t/**\n\t * Is the property undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is undefined.\n\t */\n\tpublic static undefined(value: unknown): value is undefined {\n\t\treturn value === undefined;\n\t}\n\n\t/**\n\t * Is the property null.\n\t * @param value The value to test.\n\t * @returns True if the value is null.\n\t */\n\tpublic static null(value: unknown): value is null {\n\t\treturn value === null;\n\t}\n\n\t/**\n\t * Is the property null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is null or undefined.\n\t */\n\tpublic static empty(value: unknown): value is undefined | null {\n\t\treturn value === null || value === undefined;\n\t}\n\n\t/**\n\t * Is the property not null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is not null or undefined.\n\t */\n\tpublic static notEmpty(value: unknown): boolean {\n\t\treturn value !== null && value !== undefined;\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static string(value: unknown): value is string {\n\t\treturn typeof value === \"string\";\n\t}\n\n\t/**\n\t * Is the value a non-empty string.\n\t * @param value The value to test.\n\t * @returns True if the value is a non-empty string.\n\t */\n\tpublic static stringValue(value: unknown): value is string {\n\t\treturn Is.string(value) && value.trim().length > 0;\n\t}\n\n\t/**\n\t * Is the value a JSON string.\n\t * @param value The value to test.\n\t * @returns True if the value is a JSON string.\n\t */\n\tpublic static json(value: unknown): value is string {\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst json = JSON.parse(value);\n\t\t\treturn (\n\t\t\t\tIs.object(json) ||\n\t\t\t\tIs.array(json) ||\n\t\t\t\tIs.string(json) ||\n\t\t\t\tIs.number(json) ||\n\t\t\t\tIs.boolean(json) ||\n\t\t\t\tIs.null(json)\n\t\t\t);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Is the value a base64 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base64 url string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64Url(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^([A-Za-z0-9-_])*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base58 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base58 string.\n\t */\n\tpublic static stringBase58(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^[A-HJ-NP-Za-km-z1-9]*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a hex string.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string.\n\t */\n\tpublic static stringHex(value: unknown, allowPrefix: boolean = false): value is string {\n\t\treturn Is.string(value) && HexHelper.isHex(value, allowPrefix);\n\t}\n\n\t/**\n\t * Is the value a hex string of fixed length.\n\t * @param value The value to test.\n\t * @param length The length to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string of required length.\n\t */\n\tpublic static stringHexLength(\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): value is string {\n\t\treturn Is.stringHex(value, allowPrefix) && value.length === length;\n\t}\n\n\t/**\n\t * Is the value a number.\n\t * @param value The value to test.\n\t * @returns True if the value is a number.\n\t */\n\tpublic static number(value: unknown): value is number {\n\t\treturn typeof value === \"number\" && Number.isFinite(value) && !Number.isNaN(value);\n\t}\n\n\t/**\n\t * Is the value an integer.\n\t * @param value The value to test.\n\t * @returns True if the value is an integer.\n\t */\n\tpublic static integer(value: unknown): value is number {\n\t\treturn Is.number(value) && Number.isInteger(value);\n\t}\n\n\t/**\n\t * Is the value a big integer.\n\t * @param value The value to test.\n\t * @returns True if the value is a big integer.\n\t */\n\tpublic static bigint(value: unknown): value is bigint {\n\t\treturn typeof value === \"bigint\";\n\t}\n\n\t/**\n\t * Is the value a boolean.\n\t * @param value The value to test.\n\t * @returns True if the value is a boolean.\n\t */\n\tpublic static boolean(value: unknown): value is boolean {\n\t\treturn typeof value === \"boolean\";\n\t}\n\n\t/**\n\t * Is the value a date.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static date(value: unknown): value is Date {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\t!Number.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an empty date.\n\t * @param value The value to test.\n\t * @returns True if the value is an empty date.\n\t */\n\tpublic static dateEmpty(value: unknown): boolean {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\tNumber.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date format.\n\t */\n\tpublic static dateString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date/time format.\n\t */\n\tpublic static dateTimeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || !value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a time string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 time format.\n\t */\n\tpublic static timeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(`1970-01-01T${value}`));\n\t}\n\n\t/**\n\t * Is the value a timestamp in seconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in seconds.\n\t */\n\tpublic static timestampSeconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length < 12;\n\t}\n\n\t/**\n\t * Is the value a timestamp in milliseconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in milliseconds.\n\t */\n\tpublic static timestampMilliseconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length >= 12;\n\t}\n\n\t/**\n\t * Is the value an object.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an object with at least one property.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn (\n\t\t\ttypeof value === \"object\" &&\n\t\t\tvalue !== null &&\n\t\t\t!Array.isArray(value) &&\n\t\t\tObject.keys(value).length > 0\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an array.\n\t * @param value The value to test.\n\t * @returns True if the value is an array.\n\t */\n\tpublic static array<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @returns True if the value is an array with at least one element.\n\t */\n\tpublic static arrayValue<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value) && value.length > 0;\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @returns True if the value is an element from the options array.\n\t */\n\tpublic static arrayOneOf<T>(value: T, options: T[]): value is T {\n\t\tif (Is.empty(value) || !Is.array<T>(options) || !options.includes(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Is the value a Uint8Array.\n\t * @param value The value to test.\n\t * @returns True if the value is a Uint8Array.\n\t */\n\tpublic static uint8Array(value: unknown): value is Uint8Array {\n\t\t// This is the only way we can reliably check for Uint8Array\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Uint8Array;\n\t}\n\n\t/**\n\t * Is the value a TypedArray.\n\t * @param value The value to test.\n\t * @returns True if the value is a TypedArray.\n\t */\n\tpublic static typedArray(\n\t\tvalue: unknown\n\t): value is\n\t\t| Uint8Array\n\t\t| Int8Array\n\t\t| Uint16Array\n\t\t| Int16Array\n\t\t| Uint32Array\n\t\t| Int32Array\n\t\t| Float32Array\n\t\t| Float64Array {\n\t\t// This is the only way we can reliably check for TypedArray\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Object.getPrototypeOf(Uint8Array);\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param value The value to test.\n\t * @returns True if the value is a function.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tvalue: unknown\n\t): value is T {\n\t\treturn typeof value === \"function\";\n\t}\n\n\t/**\n\t * Is the value a string formatted as an email address.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static email(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/.test(\n\t\t\t\tvalue\n\t\t\t)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a promise.\n\t * @param value The value to test.\n\t * @returns True if the value is a promise.\n\t */\n\tpublic static promise<T = unknown>(value: unknown): value is Promise<T> {\n\t\t// This is the only way we can reliably check for Promise\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Promise;\n\t}\n\n\t/**\n\t * Is the value a regexp.\n\t * @param value The value to test.\n\t * @returns True if the value is a regexp.\n\t */\n\tpublic static regexp(value: unknown): value is RegExp {\n\t\t// This is the only way we can reliably check for RegExp\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof RegExp;\n\t}\n\n\t/**\n\t * Is the provided object a class constructor.\n\t * @param obj The object to check.\n\t * @returns True if the object is a class, false otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T {\n\t\tif (typeof obj !== \"function\") {\n\t\t\treturn false;\n\t\t}\n\t\tconst str = Function.prototype.toString.call(obj);\n\t\treturn /^class\\s/.test(str);\n\t}\n\n\t/**\n\t * Is the value a uuidV7 string.\n\t * @param value The value to test.\n\t * @param format The format of the UUIDv7 string.\n\t * @returns True if the value is a uuidV7 string.\n\t */\n\tpublic static uuidV7(value: unknown, format?: \"standard\" | \"compact\"): value is string {\n\t\tif (format === \"compact\") {\n\t\t\t// 32 hex chars, where:\n\t\t\t// - char 13 (0-based index 12) is the version (7)\n\t\t\t// - char 17 (0-based index 16) is the variant (8, 9, a, b)\n\t\t\treturn Is.stringValue(value) && /^[\\da-f]{12}7[\\da-f]{3}[89ab][\\da-f]{15}$/i.test(value);\n\t\t}\n\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\da-f]{8}-[\\da-f]{4}-7[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$/i.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a valid ISO 8601 duration string or an IDuration object.\n\t * @param value The value to test.\n\t * @returns True if the value is a valid ISO 8601 duration string or an IDuration object.\n\t */\n\tpublic static duration(value: unknown): value is string | IDuration {\n\t\tif (Is.object<IDuration>(value)) {\n\t\t\treturn (\n\t\t\t\tIs.number(value.years) &&\n\t\t\t\tIs.number(value.months) &&\n\t\t\t\tIs.number(value.weeks) &&\n\t\t\t\tIs.number(value.days) &&\n\t\t\t\tIs.number(value.hours) &&\n\t\t\t\tIs.number(value.minutes) &&\n\t\t\t\tIs.number(value.seconds)\n\t\t\t);\n\t\t}\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\t\tconst match =\n\t\t\t/^P(?:(\\d+(?:\\.\\d+)?)Y)?(?:(\\d+(?:\\.\\d+)?)M)?(?:(\\d+(?:\\.\\d+)?)W)?(?:(\\d+(?:\\.\\d+)?)D)?(?:T(?:(\\d+(?:\\.\\d+)?)H)?(?:(\\d+(?:\\.\\d+)?)M)?(?:(\\d+(?:\\.\\d+)?)S)?)?$/.exec(\n\t\t\t\tvalue\n\t\t\t);\n\t\treturn Boolean(match?.slice(1).some(Boolean));\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"is.js","sourceRoot":"","sources":["../../../src/utils/is.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAE9D;;GAEG;AACH,MAAM,OAAO,EAAE;IACd;;;;OAIG;IACI,MAAM,CAAC,SAAS,CAAC,KAAc;QACrC,OAAO,KAAK,KAAK,SAAS,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,IAAI,CAAC,KAAc;QAChC,OAAO,KAAK,KAAK,IAAI,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,KAAc;QACjC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,KAAc;QACpC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,MAAM,C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BACd,CAAC;gBACF,CAAC;YACF,CAAC;YACD,OAAO,IAAI,CAAC;QACb,CAAC;QACD,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,OAAO,KAAK,CAAC;QACd,CAAC;QACD,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3C,OAAO,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { IDuration } from \"../models/IDuration.js\";\nimport { DURATION_REG_EXP } from \"../types/durationRegExp.js\";\n\n/**\n * Class to check types of objects.\n */\nexport class Is {\n\t/**\n\t * Is the property undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is undefined.\n\t */\n\tpublic static undefined(value: unknown): value is undefined {\n\t\treturn value === undefined;\n\t}\n\n\t/**\n\t * Is the property null.\n\t * @param value The value to test.\n\t * @returns True if the value is null.\n\t */\n\tpublic static null(value: unknown): value is null {\n\t\treturn value === null;\n\t}\n\n\t/**\n\t * Is the property null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is null or undefined.\n\t */\n\tpublic static empty(value: unknown): value is undefined | null {\n\t\treturn value === null || value === undefined;\n\t}\n\n\t/**\n\t * Is the property not null or undefined.\n\t * @param value The value to test.\n\t * @returns True if the value is not null or undefined.\n\t */\n\tpublic static notEmpty(value: unknown): boolean {\n\t\treturn value !== null && value !== undefined;\n\t}\n\n\t/**\n\t * Is the value a string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static string(value: unknown): value is string {\n\t\treturn typeof value === \"string\";\n\t}\n\n\t/**\n\t * Is the value a non-empty string.\n\t * @param value The value to test.\n\t * @returns True if the value is a non-empty string.\n\t */\n\tpublic static stringValue(value: unknown): value is string {\n\t\treturn Is.string(value) && value.trim().length > 0;\n\t}\n\n\t/**\n\t * Is the value a JSON string.\n\t * @param value The value to test.\n\t * @returns True if the value is a JSON string.\n\t */\n\tpublic static json(value: unknown): value is string {\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst json = JSON.parse(value);\n\t\t\treturn (\n\t\t\t\tIs.object(json) ||\n\t\t\t\tIs.array(json) ||\n\t\t\t\tIs.string(json) ||\n\t\t\t\tIs.number(json) ||\n\t\t\t\tIs.boolean(json) ||\n\t\t\t\tIs.null(json)\n\t\t\t);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Is the value a base64 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base64 url string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base64 string.\n\t */\n\tpublic static stringBase64Url(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^([A-Za-z0-9-_])*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a base58 string.\n\t * @param value The value to test.\n\t * @returns True if the value is a base58 string.\n\t */\n\tpublic static stringBase58(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t// eslint-disable-next-line unicorn/better-regex\n\t\t\t/^[A-HJ-NP-Za-km-z1-9]*$/.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a hex string.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string.\n\t */\n\tpublic static stringHex(value: unknown, allowPrefix: boolean = false): value is string {\n\t\treturn Is.string(value) && HexHelper.isHex(value, allowPrefix);\n\t}\n\n\t/**\n\t * Is the value a hex string of fixed length.\n\t * @param value The value to test.\n\t * @param length The length to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the value is a hex string of required length.\n\t */\n\tpublic static stringHexLength(\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): value is string {\n\t\treturn Is.stringHex(value, allowPrefix) && value.length === length;\n\t}\n\n\t/**\n\t * Is the value a number.\n\t * @param value The value to test.\n\t * @returns True if the value is a number.\n\t */\n\tpublic static number(value: unknown): value is number {\n\t\treturn typeof value === \"number\" && Number.isFinite(value) && !Number.isNaN(value);\n\t}\n\n\t/**\n\t * Is the value an integer.\n\t * @param value The value to test.\n\t * @returns True if the value is an integer.\n\t */\n\tpublic static integer(value: unknown): value is number {\n\t\treturn Is.number(value) && Number.isInteger(value);\n\t}\n\n\t/**\n\t * Is the value a big integer.\n\t * @param value The value to test.\n\t * @returns True if the value is a big integer.\n\t */\n\tpublic static bigint(value: unknown): value is bigint {\n\t\treturn typeof value === \"bigint\";\n\t}\n\n\t/**\n\t * Is the value a boolean.\n\t * @param value The value to test.\n\t * @returns True if the value is a boolean.\n\t */\n\tpublic static boolean(value: unknown): value is boolean {\n\t\treturn typeof value === \"boolean\";\n\t}\n\n\t/**\n\t * Is the value a date.\n\t * @param value The value to test.\n\t * @returns True if the value is a date.\n\t */\n\tpublic static date(value: unknown): value is Date {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\t!Number.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an empty date.\n\t * @param value The value to test.\n\t * @returns True if the value is an empty date.\n\t */\n\tpublic static dateEmpty(value: unknown): boolean {\n\t\treturn (\n\t\t\tObject.prototype.toString.call(value) === \"[object Date]\" &&\n\t\t\tNumber.isNaN((value as Date).getTime())\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date format.\n\t */\n\tpublic static dateString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a date string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 date/time format.\n\t */\n\tpublic static dateTimeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || !value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(value));\n\t}\n\n\t/**\n\t * Is the value a time string.\n\t * @param value The value to test.\n\t * @returns True if the value is a string in ISO 8601 time format.\n\t */\n\tpublic static timeString(value: unknown): boolean {\n\t\tif (typeof value !== \"string\" || value.length === 0 || value.includes(\"T\")) {\n\t\t\treturn false;\n\t\t}\n\t\treturn !Number.isNaN(Date.parse(`1970-01-01T${value}`));\n\t}\n\n\t/**\n\t * Is the value a timestamp in seconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in seconds.\n\t */\n\tpublic static timestampSeconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length < 12;\n\t}\n\n\t/**\n\t * Is the value a timestamp in milliseconds.\n\t * @param value The value to test.\n\t * @returns True if the value is a timestamp in milliseconds.\n\t */\n\tpublic static timestampMilliseconds(value: unknown): value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn value.toString().length >= 12;\n\t}\n\n\t/**\n\t * Is the value an object.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an object with at least one property.\n\t * @param value The value to test.\n\t * @returns True if the value is a object.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(value: unknown): value is T {\n\t\treturn (\n\t\t\ttypeof value === \"object\" &&\n\t\t\tvalue !== null &&\n\t\t\t!Array.isArray(value) &&\n\t\t\tObject.keys(value).length > 0\n\t\t);\n\t}\n\n\t/**\n\t * Is the value an array.\n\t * @param value The value to test.\n\t * @returns True if the value is an array.\n\t */\n\tpublic static array<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value);\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @returns True if the value is an array with at least one element.\n\t */\n\tpublic static arrayValue<T>(value: unknown): value is T[] {\n\t\treturn Array.isArray(value) && value.length > 0;\n\t}\n\n\t/**\n\t * Is the value an array with at least one element.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @returns True if the value is an element from the options array.\n\t */\n\tpublic static arrayOneOf<T>(value: T, options: T[]): value is T {\n\t\tif (Is.empty(value) || !Is.array<T>(options) || !options.includes(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Is the value a Uint8Array.\n\t * @param value The value to test.\n\t * @returns True if the value is a Uint8Array.\n\t */\n\tpublic static uint8Array(value: unknown): value is Uint8Array {\n\t\t// This is the only way we can reliably check for Uint8Array\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Uint8Array;\n\t}\n\n\t/**\n\t * Is the value a TypedArray.\n\t * @param value The value to test.\n\t * @returns True if the value is a TypedArray.\n\t */\n\tpublic static typedArray(\n\t\tvalue: unknown\n\t): value is\n\t\t| Uint8Array\n\t\t| Int8Array\n\t\t| Uint16Array\n\t\t| Int16Array\n\t\t| Uint32Array\n\t\t| Int32Array\n\t\t| Float32Array\n\t\t| Float64Array {\n\t\t// This is the only way we can reliably check for TypedArray\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Object.getPrototypeOf(Uint8Array);\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param value The value to test.\n\t * @returns True if the value is a function.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tvalue: unknown\n\t): value is T {\n\t\treturn typeof value === \"function\";\n\t}\n\n\t/**\n\t * Is the value a string formatted as an email address.\n\t * @param value The value to test.\n\t * @returns True if the value is a string.\n\t */\n\tpublic static email(value: unknown): value is string {\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/.test(\n\t\t\t\tvalue\n\t\t\t)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a promise.\n\t * @param value The value to test.\n\t * @returns True if the value is a promise.\n\t */\n\tpublic static promise<T = unknown>(value: unknown): value is Promise<T> {\n\t\t// This is the only way we can reliably check for Promise\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof Promise;\n\t}\n\n\t/**\n\t * Is the value a regexp.\n\t * @param value The value to test.\n\t * @returns True if the value is a regexp.\n\t */\n\tpublic static regexp(value: unknown): value is RegExp {\n\t\t// This is the only way we can reliably check for RegExp\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn value instanceof RegExp;\n\t}\n\n\t/**\n\t * Is the provided object a class constructor.\n\t * @param obj The object to check.\n\t * @returns True if the object is a class, false otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static class<T = unknown>(obj: unknown): obj is new (...args: any[]) => T {\n\t\tif (typeof obj !== \"function\") {\n\t\t\treturn false;\n\t\t}\n\t\tconst str = Function.prototype.toString.call(obj);\n\t\treturn /^class\\s/.test(str);\n\t}\n\n\t/**\n\t * Is the value a uuidV7 string.\n\t * @param value The value to test.\n\t * @param format The format of the UUIDv7 string.\n\t * @returns True if the value is a uuidV7 string.\n\t */\n\tpublic static uuidV7(value: unknown, format?: \"standard\" | \"compact\"): value is string {\n\t\tif (format === \"compact\") {\n\t\t\t// 32 hex chars, where:\n\t\t\t// - char 13 (0-based index 12) is the version (7)\n\t\t\t// - char 17 (0-based index 16) is the variant (8, 9, a, b)\n\t\t\treturn Is.stringValue(value) && /^[\\da-f]{12}7[\\da-f]{3}[89ab][\\da-f]{15}$/i.test(value);\n\t\t}\n\n\t\treturn (\n\t\t\tIs.stringValue(value) &&\n\t\t\t/^[\\da-f]{8}-[\\da-f]{4}-7[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$/i.test(value)\n\t\t);\n\t}\n\n\t/**\n\t * Is the value a valid ISO 8601 duration string or an IDuration object.\n\t * @param value The value to test.\n\t * @returns True if the value is a valid ISO 8601 duration string or an IDuration object.\n\t */\n\tpublic static duration(value: unknown): value is string | IDuration {\n\t\tif (Is.object<IDuration>(value)) {\n\t\t\tif (\n\t\t\t\t!Is.number(value.years) ||\n\t\t\t\t!Is.number(value.months) ||\n\t\t\t\t!Is.number(value.weeks) ||\n\t\t\t\t!Is.number(value.days) ||\n\t\t\t\t!Is.number(value.hours) ||\n\t\t\t\t!Is.number(value.minutes) ||\n\t\t\t\t!Is.number(value.seconds) ||\n\t\t\t\t(!Is.undefined(value.milliseconds) &&\n\t\t\t\t\t(!Is.integer(value.milliseconds) || Math.abs(value.milliseconds) > 999)) ||\n\t\t\t\t(!Is.undefined(value.microseconds) &&\n\t\t\t\t\t(!Is.integer(value.microseconds) || Math.abs(value.microseconds) > 999)) ||\n\t\t\t\t(!Is.undefined(value.nanoseconds) &&\n\t\t\t\t\t(!Is.integer(value.nanoseconds) || Math.abs(value.nanoseconds) > 999))\n\t\t\t) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tlet sign = 0;\n\t\t\tfor (const component of [\n\t\t\t\tvalue.years,\n\t\t\t\tvalue.months,\n\t\t\t\tvalue.weeks,\n\t\t\t\tvalue.days,\n\t\t\t\tvalue.hours,\n\t\t\t\tvalue.minutes,\n\t\t\t\tvalue.seconds,\n\t\t\t\tvalue.milliseconds ?? 0,\n\t\t\t\tvalue.microseconds ?? 0,\n\t\t\t\tvalue.nanoseconds ?? 0\n\t\t\t]) {\n\t\t\t\tif (component !== 0) {\n\t\t\t\t\tconst componentSign = component < 0 ? -1 : 1;\n\t\t\t\t\tif (sign === 0) {\n\t\t\t\t\t\tsign = componentSign;\n\t\t\t\t\t} else if (sign !== componentSign) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\tif (!Is.stringValue(value)) {\n\t\t\treturn false;\n\t\t}\n\t\tconst match = DURATION_REG_EXP.exec(value);\n\t\treturn Boolean(match?.slice(2).some(Boolean));\n\t}\n}\n"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -49,6 +49,7 @@ export * from "./models/mutexMessageTypes.js";
|
|
|
49
49
|
export * from "./models/sharedObjectBufferMessageTypes.js";
|
|
50
50
|
export * from "./types/bitString.js";
|
|
51
51
|
export * from "./types/duration.js";
|
|
52
|
+
export * from "./types/durationRegExp.js";
|
|
52
53
|
export * from "./types/objectOrArray.js";
|
|
53
54
|
export * from "./types/singleOccurrenceArray.js";
|
|
54
55
|
export * from "./types/singleOccurrenceArrayDepthHelper.js";
|
|
@@ -30,4 +30,16 @@ export interface IDuration {
|
|
|
30
30
|
* The number of seconds.
|
|
31
31
|
*/
|
|
32
32
|
seconds: number;
|
|
33
|
+
/**
|
|
34
|
+
* The number of milliseconds.
|
|
35
|
+
*/
|
|
36
|
+
milliseconds?: number;
|
|
37
|
+
/**
|
|
38
|
+
* The number of microseconds.
|
|
39
|
+
*/
|
|
40
|
+
microseconds?: number;
|
|
41
|
+
/**
|
|
42
|
+
* The number of nanoseconds.
|
|
43
|
+
*/
|
|
44
|
+
nanoseconds?: number;
|
|
33
45
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DURATION_REG_EXP: RegExp;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IDuration } from "../models/IDuration.js";
|
|
1
2
|
import type { ObjectOrArray } from "../types/objectOrArray.js";
|
|
2
3
|
/**
|
|
3
4
|
* Class to handle guard operations for parameters.
|
|
@@ -255,11 +256,11 @@ export declare class Guards {
|
|
|
255
256
|
*/
|
|
256
257
|
static uuidV7(source: string, property: string, value: unknown, format?: "standard" | "compact"): asserts value is string;
|
|
257
258
|
/**
|
|
258
|
-
* Is the property a valid ISO 8601 duration string.
|
|
259
|
+
* Is the property a valid ISO 8601 duration string or IDuration object.
|
|
259
260
|
* @param source The source of the error.
|
|
260
261
|
* @param property The name of the property.
|
|
261
262
|
* @param value The value to test.
|
|
262
263
|
* @throws GuardError If the value does not match the assertion.
|
|
263
264
|
*/
|
|
264
|
-
static duration(source: string, property: string, value: unknown): asserts value is string;
|
|
265
|
+
static duration(source: string, property: string, value: unknown): asserts value is string | IDuration;
|
|
265
266
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.4-next.14](https://github.com/iotaledger/twin-framework/compare/core-v0.0.4-next.13...core-v0.0.4-next.14) (2026-06-18)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add partial second support ([39bf087](https://github.com/iotaledger/twin-framework/commit/39bf087ddc113a9bf73e0d0e99f738f6f60c2f2d))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/nameof bumped from 0.0.4-next.13 to 0.0.4-next.14
|
|
16
|
+
* devDependencies
|
|
17
|
+
* @twin.org/nameof-transformer bumped from 0.0.4-next.13 to 0.0.4-next.14
|
|
18
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.4-next.13 to 0.0.4-next.14
|
|
19
|
+
|
|
3
20
|
## [0.0.4-next.13](https://github.com/iotaledger/twin-framework/compare/core-v0.0.4-next.12...core-v0.0.4-next.13) (2026-06-18)
|
|
4
21
|
|
|
5
22
|
|
|
@@ -1186,9 +1186,9 @@ GuardError If the value does not match the assertion.
|
|
|
1186
1186
|
|
|
1187
1187
|
### duration() {#duration}
|
|
1188
1188
|
|
|
1189
|
-
> `static` **duration**(`source`, `property`, `value`):
|
|
1189
|
+
> `static` **duration**(`source`, `property`, `value`): asserts value is string \| IDuration
|
|
1190
1190
|
|
|
1191
|
-
Is the property a valid ISO 8601 duration string.
|
|
1191
|
+
Is the property a valid ISO 8601 duration string or IDuration object.
|
|
1192
1192
|
|
|
1193
1193
|
#### Parameters
|
|
1194
1194
|
|
|
@@ -1212,7 +1212,7 @@ The value to test.
|
|
|
1212
1212
|
|
|
1213
1213
|
#### Returns
|
|
1214
1214
|
|
|
1215
|
-
|
|
1215
|
+
asserts value is string \| IDuration
|
|
1216
1216
|
|
|
1217
1217
|
#### Throws
|
|
1218
1218
|
|
package/docs/reference/index.md
CHANGED
|
@@ -57,3 +57,27 @@ The number of minutes.
|
|
|
57
57
|
> **seconds**: `number`
|
|
58
58
|
|
|
59
59
|
The number of seconds.
|
|
60
|
+
|
|
61
|
+
***
|
|
62
|
+
|
|
63
|
+
### milliseconds? {#milliseconds}
|
|
64
|
+
|
|
65
|
+
> `optional` **milliseconds?**: `number`
|
|
66
|
+
|
|
67
|
+
The number of milliseconds.
|
|
68
|
+
|
|
69
|
+
***
|
|
70
|
+
|
|
71
|
+
### microseconds? {#microseconds}
|
|
72
|
+
|
|
73
|
+
> `optional` **microseconds?**: `number`
|
|
74
|
+
|
|
75
|
+
The number of microseconds.
|
|
76
|
+
|
|
77
|
+
***
|
|
78
|
+
|
|
79
|
+
### nanoseconds? {#nanoseconds}
|
|
80
|
+
|
|
81
|
+
> `optional` **nanoseconds?**: `number`
|
|
82
|
+
|
|
83
|
+
The number of nanoseconds.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/core",
|
|
3
|
-
"version": "0.0.4-next.
|
|
3
|
+
"version": "0.0.4-next.14",
|
|
4
4
|
"description": "Helper methods/classes for data type checking/validation/guarding/error handling",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/nameof": "0.0.4-next.
|
|
17
|
+
"@twin.org/nameof": "0.0.4-next.14",
|
|
18
18
|
"intl-messageformat": "11.2.6",
|
|
19
19
|
"rfc6902": "5.2.0"
|
|
20
20
|
},
|