@clerc/advanced-types 1.1.0 → 1.2.0
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/index.mjs +1 -68
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,68 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var FlagValidationError = class extends Error {};
|
|
3
|
-
|
|
4
|
-
//#endregion
|
|
5
|
-
//#region src/index.ts
|
|
6
|
-
/**
|
|
7
|
-
* Creates a Enum type function that validates the input against allowed values.
|
|
8
|
-
* The display name will be formatted as "value1 | value2 | ..." for help
|
|
9
|
-
* output.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```typescript
|
|
14
|
-
* const format = Enum(["json", "yaml", "xml"]);
|
|
15
|
-
* // Help output will show: json | yaml | xml
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @param values - Array of allowed string values
|
|
19
|
-
* @returns A TypeFunction that validates and returns the input value
|
|
20
|
-
* @throws {Error} If the value is not in the allowed values list
|
|
21
|
-
*/
|
|
22
|
-
function Enum(...values) {
|
|
23
|
-
const fn = ((value) => {
|
|
24
|
-
if (!values.includes(value)) throw new FlagValidationError(`Invalid value: ${value}. Must be one of: ${values.join(", ")}`);
|
|
25
|
-
return value;
|
|
26
|
-
});
|
|
27
|
-
fn.display = values.join(" | ");
|
|
28
|
-
return fn;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Creates a range type function that validates the input is a number within the
|
|
32
|
-
* specified range.
|
|
33
|
-
*
|
|
34
|
-
* @param min - The minimum acceptable value (inclusive)
|
|
35
|
-
* @param max - The maximum acceptable value (inclusive)
|
|
36
|
-
* @returns A TypeFunction that validates the input value
|
|
37
|
-
* @throws {Error} If the value is not a number or is outside the specified
|
|
38
|
-
* range
|
|
39
|
-
*/
|
|
40
|
-
function Range(min, max) {
|
|
41
|
-
const fn = ((value) => {
|
|
42
|
-
const num = Number(value);
|
|
43
|
-
if (Number.isNaN(num) || num < min || num > max) throw new FlagValidationError(`Invalid value: ${value}. Must be a number between ${min} and ${max}`);
|
|
44
|
-
return num;
|
|
45
|
-
});
|
|
46
|
-
fn.display = `${min} - ${max}`;
|
|
47
|
-
return fn;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Creates a regex type function that validates the input against the provided
|
|
51
|
-
* pattern.
|
|
52
|
-
*
|
|
53
|
-
* @param pattern - The regular expression pattern to validate against
|
|
54
|
-
* @param description - Optional description for display purposes
|
|
55
|
-
* @returns A TypeFunction that validates the input value
|
|
56
|
-
* @throws {Error} If the value does not match the regex pattern
|
|
57
|
-
*/
|
|
58
|
-
function Regex(pattern, description) {
|
|
59
|
-
const fn = ((value) => {
|
|
60
|
-
if (!pattern.test(value)) throw new FlagValidationError(`Invalid value: ${value}. Must match pattern: ${pattern}`);
|
|
61
|
-
return value;
|
|
62
|
-
});
|
|
63
|
-
fn.display = description ?? `Regex: ${pattern.toString()}`;
|
|
64
|
-
return fn;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
//#endregion
|
|
68
|
-
export { Enum, FlagValidationError, Range, Regex };
|
|
1
|
+
var e=class extends Error{};function t(...t){let n=(n=>{if(!t.includes(n))throw new e(`Invalid value: ${n}. Must be one of: ${t.join(`, `)}`);return n});return n.display=t.join(` | `),n}function n(t,n){let r=(r=>{let i=Number(r);if(Number.isNaN(i)||i<t||i>n)throw new e(`Invalid value: ${r}. Must be a number between ${t} and ${n}`);return i});return r.display=`${t} - ${n}`,r}function r(t,n){let r=(n=>{if(!t.test(n))throw new e(`Invalid value: ${n}. Must match pattern: ${t}`);return n});return r.display=n??`Regex: ${t.toString()}`,r}export{t as Enum,e as FlagValidationError,n as Range,r as Regex};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/advanced-types",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc advanced types",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@clerc/parser": "1.
|
|
39
|
+
"@clerc/parser": "1.2.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@clerc/parser": "*"
|