@optique/core 0.5.0-dev.79 → 0.5.0-dev.82
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/constructs.cjs +754 -0
- package/dist/constructs.d.cts +1100 -0
- package/dist/constructs.d.ts +1100 -0
- package/dist/constructs.js +748 -0
- package/dist/facade.cjs +56 -46
- package/dist/facade.js +32 -22
- package/dist/index.cjs +20 -17
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -1
- package/dist/modifiers.cjs +300 -0
- package/dist/modifiers.d.cts +192 -0
- package/dist/modifiers.d.ts +192 -0
- package/dist/modifiers.js +296 -0
- package/dist/parser.cjs +20 -1581
- package/dist/parser.d.cts +6 -1279
- package/dist/parser.d.ts +6 -1279
- package/dist/parser.js +4 -1565
- package/dist/primitives.cjs +595 -0
- package/dist/primitives.d.cts +274 -0
- package/dist/primitives.d.ts +274 -0
- package/dist/primitives.js +591 -0
- package/dist/valueparser.cjs +28 -28
- package/dist/valueparser.d.cts +144 -0
- package/dist/valueparser.d.ts +144 -0
- package/dist/valueparser.js +28 -28
- package/package.json +25 -1
package/dist/valueparser.js
CHANGED
|
@@ -30,7 +30,7 @@ function choice(values, options = {}) {
|
|
|
30
30
|
const index = normalizedValues.indexOf(normalizedInput);
|
|
31
31
|
if (index < 0) return {
|
|
32
32
|
success: false,
|
|
33
|
-
error: message`Expected one of ${values.join(", ")}, but got ${input}.`
|
|
33
|
+
error: options.errors?.invalidChoice ? typeof options.errors.invalidChoice === "function" ? options.errors.invalidChoice(input, values) : options.errors.invalidChoice : message`Expected one of ${values.join(", ")}, but got ${input}.`
|
|
34
34
|
};
|
|
35
35
|
return {
|
|
36
36
|
success: true,
|
|
@@ -57,7 +57,7 @@ function string(options = {}) {
|
|
|
57
57
|
parse(input) {
|
|
58
58
|
if (options.pattern != null && !options.pattern.test(input)) return {
|
|
59
59
|
success: false,
|
|
60
|
-
error: message`Expected a string matching pattern ${text(options.pattern.source)}, but got ${input}.`
|
|
60
|
+
error: options.errors?.patternMismatch ? typeof options.errors.patternMismatch === "function" ? options.errors.patternMismatch(input, options.pattern) : options.errors.patternMismatch : message`Expected a string matching pattern ${text(options.pattern.source)}, but got ${input}.`
|
|
61
61
|
};
|
|
62
62
|
return {
|
|
63
63
|
success: true,
|
|
@@ -112,17 +112,17 @@ function integer(options) {
|
|
|
112
112
|
} catch (e) {
|
|
113
113
|
if (e instanceof SyntaxError) return {
|
|
114
114
|
success: false,
|
|
115
|
-
error: message`Expected a valid integer, but got ${input}.`
|
|
115
|
+
error: options.errors?.invalidInteger ? typeof options.errors.invalidInteger === "function" ? options.errors.invalidInteger(input) : options.errors.invalidInteger : message`Expected a valid integer, but got ${input}.`
|
|
116
116
|
};
|
|
117
117
|
throw e;
|
|
118
118
|
}
|
|
119
119
|
if (options.min != null && value < options.min) return {
|
|
120
120
|
success: false,
|
|
121
|
-
error: message`Expected a value greater than or equal to ${text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
121
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, options.min) : options.errors.belowMinimum : message`Expected a value greater than or equal to ${text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
122
122
|
};
|
|
123
123
|
else if (options.max != null && value > options.max) return {
|
|
124
124
|
success: false,
|
|
125
|
-
error: message`Expected a value less than or equal to ${text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
125
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, options.max) : options.errors.aboveMaximum : message`Expected a value less than or equal to ${text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
126
126
|
};
|
|
127
127
|
return {
|
|
128
128
|
success: true,
|
|
@@ -138,16 +138,16 @@ function integer(options) {
|
|
|
138
138
|
parse(input) {
|
|
139
139
|
if (!input.match(/^\d+$/)) return {
|
|
140
140
|
success: false,
|
|
141
|
-
error: message`Expected a valid integer, but got ${input}.`
|
|
141
|
+
error: options?.errors?.invalidInteger ? typeof options.errors.invalidInteger === "function" ? options.errors.invalidInteger(input) : options.errors.invalidInteger : message`Expected a valid integer, but got ${input}.`
|
|
142
142
|
};
|
|
143
143
|
const value = Number.parseInt(input);
|
|
144
144
|
if (options?.min != null && value < options.min) return {
|
|
145
145
|
success: false,
|
|
146
|
-
error: message`Expected a value greater than or equal to ${text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
146
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, options.min) : options.errors.belowMinimum : message`Expected a value greater than or equal to ${text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
147
147
|
};
|
|
148
148
|
else if (options?.max != null && value > options.max) return {
|
|
149
149
|
success: false,
|
|
150
|
-
error: message`Expected a value less than or equal to ${text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
150
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, options.max) : options.errors.aboveMaximum : message`Expected a value less than or equal to ${text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
151
151
|
};
|
|
152
152
|
return {
|
|
153
153
|
success: true,
|
|
@@ -182,19 +182,19 @@ function float(options = {}) {
|
|
|
182
182
|
value = Number(input);
|
|
183
183
|
if (Number.isNaN(value)) return {
|
|
184
184
|
success: false,
|
|
185
|
-
error: message`Expected a valid number, but got ${input}.`
|
|
185
|
+
error: options.errors?.invalidNumber ? typeof options.errors.invalidNumber === "function" ? options.errors.invalidNumber(input) : options.errors.invalidNumber : message`Expected a valid number, but got ${input}.`
|
|
186
186
|
};
|
|
187
187
|
} else return {
|
|
188
188
|
success: false,
|
|
189
|
-
error: message`Expected a valid number, but got ${input}.`
|
|
189
|
+
error: options.errors?.invalidNumber ? typeof options.errors.invalidNumber === "function" ? options.errors.invalidNumber(input) : options.errors.invalidNumber : message`Expected a valid number, but got ${input}.`
|
|
190
190
|
};
|
|
191
191
|
if (options.min != null && value < options.min) return {
|
|
192
192
|
success: false,
|
|
193
|
-
error: message`Expected a value greater than or equal to ${text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
193
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, options.min) : options.errors.belowMinimum : message`Expected a value greater than or equal to ${text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
194
194
|
};
|
|
195
195
|
else if (options.max != null && value > options.max) return {
|
|
196
196
|
success: false,
|
|
197
|
-
error: message`Expected a value less than or equal to ${text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
197
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, options.max) : options.errors.aboveMaximum : message`Expected a value less than or equal to ${text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
198
198
|
};
|
|
199
199
|
return {
|
|
200
200
|
success: true,
|
|
@@ -222,12 +222,12 @@ function url(options = {}) {
|
|
|
222
222
|
parse(input) {
|
|
223
223
|
if (!URL.canParse(input)) return {
|
|
224
224
|
success: false,
|
|
225
|
-
error: message`Invalid URL: ${input}.`
|
|
225
|
+
error: options.errors?.invalidUrl ? typeof options.errors.invalidUrl === "function" ? options.errors.invalidUrl(input) : options.errors.invalidUrl : message`Invalid URL: ${input}.`
|
|
226
226
|
};
|
|
227
227
|
const url$1 = new URL(input);
|
|
228
228
|
if (allowedProtocols != null && !allowedProtocols.includes(url$1.protocol)) return {
|
|
229
229
|
success: false,
|
|
230
|
-
error: message`URL protocol ${url$1.protocol} is not allowed. Allowed protocols: ${allowedProtocols.join(", ")}.`
|
|
230
|
+
error: options.errors?.disallowedProtocol ? typeof options.errors.disallowedProtocol === "function" ? options.errors.disallowedProtocol(url$1.protocol, options.allowedProtocols) : options.errors.disallowedProtocol : message`URL protocol ${url$1.protocol} is not allowed. Allowed protocols: ${allowedProtocols.join(", ")}.`
|
|
231
231
|
};
|
|
232
232
|
return {
|
|
233
233
|
success: true,
|
|
@@ -259,7 +259,7 @@ function locale(options = {}) {
|
|
|
259
259
|
} catch (e) {
|
|
260
260
|
if (e instanceof RangeError) return {
|
|
261
261
|
success: false,
|
|
262
|
-
error: message`Invalid locale: ${input}.`
|
|
262
|
+
error: options.errors?.invalidLocale ? typeof options.errors.invalidLocale === "function" ? options.errors.invalidLocale(input) : options.errors.invalidLocale : message`Invalid locale: ${input}.`
|
|
263
263
|
};
|
|
264
264
|
throw e;
|
|
265
265
|
}
|
|
@@ -292,23 +292,23 @@ function uuid(options = {}) {
|
|
|
292
292
|
parse(input) {
|
|
293
293
|
if (!uuidRegex.test(input)) return {
|
|
294
294
|
success: false,
|
|
295
|
-
error: message`Expected a valid UUID in format ${"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}, but got ${input}.`
|
|
295
|
+
error: options.errors?.invalidUuid ? typeof options.errors.invalidUuid === "function" ? options.errors.invalidUuid(input) : options.errors.invalidUuid : message`Expected a valid UUID in format ${"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}, but got ${input}.`
|
|
296
296
|
};
|
|
297
297
|
if (options.allowedVersions != null && options.allowedVersions.length > 0) {
|
|
298
298
|
const versionChar = input.charAt(14);
|
|
299
299
|
const version = parseInt(versionChar, 16);
|
|
300
|
-
if (!options.allowedVersions.includes(version)) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
}
|
|
300
|
+
if (!options.allowedVersions.includes(version)) return {
|
|
301
|
+
success: false,
|
|
302
|
+
error: options.errors?.disallowedVersion ? typeof options.errors.disallowedVersion === "function" ? options.errors.disallowedVersion(version, options.allowedVersions) : options.errors.disallowedVersion : (() => {
|
|
303
|
+
let expectedVersions = message``;
|
|
304
|
+
let i = 0;
|
|
305
|
+
for (const v of options.allowedVersions) {
|
|
306
|
+
expectedVersions = i < 1 ? message`${expectedVersions}${v.toLocaleString("en")}` : i + 1 >= options.allowedVersions.length ? message`${expectedVersions}, or ${v.toLocaleString("en")}` : message`${expectedVersions}, ${v.toLocaleString("en")}`;
|
|
307
|
+
i++;
|
|
308
|
+
}
|
|
309
|
+
return message`Expected UUID version ${expectedVersions}, but got version ${version.toLocaleString("en")}.`;
|
|
310
|
+
})()
|
|
311
|
+
};
|
|
312
312
|
}
|
|
313
313
|
return {
|
|
314
314
|
success: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@optique/core",
|
|
3
|
-
"version": "0.5.0-dev.
|
|
3
|
+
"version": "0.5.0-dev.82+3a10f0da",
|
|
4
4
|
"description": "Type-safe combinatorial command-line interface parser",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CLI",
|
|
@@ -51,6 +51,14 @@
|
|
|
51
51
|
"import": "./dist/index.js",
|
|
52
52
|
"require": "./dist/index.cjs"
|
|
53
53
|
},
|
|
54
|
+
"./constructs": {
|
|
55
|
+
"types": {
|
|
56
|
+
"import": "./dist/constructs.d.ts",
|
|
57
|
+
"require": "./dist/constructs.cts"
|
|
58
|
+
},
|
|
59
|
+
"import": "./dist/constructs.js",
|
|
60
|
+
"require": "./dist/constructs.cjs"
|
|
61
|
+
},
|
|
54
62
|
"./doc": {
|
|
55
63
|
"types": {
|
|
56
64
|
"import": "./dist/doc.d.ts",
|
|
@@ -75,6 +83,14 @@
|
|
|
75
83
|
"import": "./dist/message.js",
|
|
76
84
|
"require": "./dist/message.cjs"
|
|
77
85
|
},
|
|
86
|
+
"./modifiers": {
|
|
87
|
+
"types": {
|
|
88
|
+
"import": "./dist/modifiers.d.ts",
|
|
89
|
+
"require": "./dist/modifiers.cts"
|
|
90
|
+
},
|
|
91
|
+
"import": "./dist/modifiers.js",
|
|
92
|
+
"require": "./dist/modifiers.cjs"
|
|
93
|
+
},
|
|
78
94
|
"./parser": {
|
|
79
95
|
"types": {
|
|
80
96
|
"import": "./dist/parser.d.ts",
|
|
@@ -83,6 +99,14 @@
|
|
|
83
99
|
"import": "./dist/parser.js",
|
|
84
100
|
"require": "./dist/parser.cjs"
|
|
85
101
|
},
|
|
102
|
+
"./primitives": {
|
|
103
|
+
"types": {
|
|
104
|
+
"import": "./dist/primitives.d.ts",
|
|
105
|
+
"require": "./dist/primitives.cts"
|
|
106
|
+
},
|
|
107
|
+
"import": "./dist/primitives.js",
|
|
108
|
+
"require": "./dist/primitives.cjs"
|
|
109
|
+
},
|
|
86
110
|
"./usage": {
|
|
87
111
|
"types": {
|
|
88
112
|
"import": "./dist/usage.d.ts",
|