@guillaume-docquier/tools-ts 9.0.0 → 9.1.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/README.md +9 -0
- package/dist/entry.tools-ts.d.ts +1 -0
- package/dist/entry.tools-ts.js +1 -0
- package/dist/entry.tools-ts.js.map +1 -1
- package/dist/typedParse.d.ts +29 -0
- package/dist/typedParse.js +32 -0
- package/dist/typedParse.js.map +1 -0
- package/package.json +11 -2
package/README.md
CHANGED
|
@@ -35,6 +35,15 @@ external APIs, CLI input, local storage, or other unknown data.
|
|
|
35
35
|
| `getEnumKey` | Looks up the enum key associated with a value. | You have an enum value and need the more descriptive key for display, logs, or diagnostics. |
|
|
36
36
|
| `asArray` | Normalizes a single value or array into an array. | An API accepts both `T` and `T[]`, but downstream logic should iterate uniformly. |
|
|
37
37
|
|
|
38
|
+
### Zod Parsing
|
|
39
|
+
|
|
40
|
+
Use these tools when constructing a value from trusted, typed data through a Zod schema. Install Zod in the consuming project to use them.
|
|
41
|
+
|
|
42
|
+
| Export | What it is | Use it when |
|
|
43
|
+
| -------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
|
|
44
|
+
| typedParse | Parses a typed, deeply unbranded value with a Zod schema and returns its output. | Invalid data is an internal invariant violation that should throw. |
|
|
45
|
+
| safeTypedParse | The non-throwing counterpart, returning a Zod safe parse result. | Validation may fail and the caller will handle the result. |
|
|
46
|
+
|
|
38
47
|
### Numeric Utilities
|
|
39
48
|
|
|
40
49
|
Use these tools when code needs reusable numeric operations or validated
|
package/dist/entry.tools-ts.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { Profile } from "./Profile.js";
|
|
|
13
13
|
export { Range } from "./Range.js";
|
|
14
14
|
export { Scalar } from "./Scalar.js";
|
|
15
15
|
export { TypeGuard } from "./TypeGuard.js";
|
|
16
|
+
export { typedParse, safeTypedParse } from "./typedParse.js";
|
|
16
17
|
export { setTimeoutAsync } from "./setTimeoutAsync.js";
|
|
17
18
|
export { noop, asyncNoop } from "./noop.js";
|
|
18
19
|
export { Logger } from "./logging/Logger.js";
|
package/dist/entry.tools-ts.js
CHANGED
|
@@ -12,6 +12,7 @@ export { Profile } from "./Profile.js";
|
|
|
12
12
|
export { Range } from "./Range.js";
|
|
13
13
|
export { Scalar } from "./Scalar.js";
|
|
14
14
|
export { TypeGuard } from "./TypeGuard.js";
|
|
15
|
+
export { typedParse, safeTypedParse } from "./typedParse.js";
|
|
15
16
|
export { setTimeoutAsync } from "./setTimeoutAsync.js";
|
|
16
17
|
export { noop, asyncNoop } from "./noop.js";
|
|
17
18
|
export { Logger } from "./logging/Logger.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry.tools-ts.js","sourceRoot":"","sources":["../src/entry.tools-ts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAA8B,MAAM,aAAa,CAAA;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,E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AiB,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAA;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AACrE,OAAO,EAAE,OAAO,EAAW,MAAM,2BAA2B,CAAA;AAC5D,OAAO,EAAE,OAAO,EAAY,MAAM,2BAA2B,CAAA;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAgB,OAAO,EAAgE,MAAM,YAAY,CAAA;AAChH,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { DeepUnbranded } from "./Brand.js";
|
|
3
|
+
/**
|
|
4
|
+
* We use zod schemas on unknown inputs to parse & validate the data.
|
|
5
|
+
* However, when you have already parsed data (usually because it is trusted data) and want to validate, using schema.parse is bad because the parameter is `unknown`, so you don't get feedback from TypeScript.
|
|
6
|
+
*
|
|
7
|
+
* Use this `typedParse` method instead, which accepts as input a strongly typed but unbranded version of the data and outputs a branded version of it, after validation.
|
|
8
|
+
*
|
|
9
|
+
* This is really just a schema.parse() with type safe inputs when you only need to run the validation part of the schema.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* typedParse(IntegerSchema, 42) // ✓
|
|
14
|
+
* typedParse(IntegerSchema, 42.5) // throws
|
|
15
|
+
* typedParse(IntegerSchema, "42") // TS error
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function typedParse<TSchema extends z.ZodType>(schema: TSchema, value: DeepUnbranded<z.output<TSchema>>): z.output<TSchema>;
|
|
19
|
+
/**
|
|
20
|
+
* The safeParse counterpart to {@link typedParse}
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* safeTypedParse(IntegerSchema, 42) // ✓
|
|
25
|
+
* safeTypedParse(IntegerSchema, 42.5) // returns success = false
|
|
26
|
+
* safeTypedParse(IntegerSchema, "42") // TS error
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function safeTypedParse<TSchema extends z.ZodType>(schema: TSchema, value: DeepUnbranded<z.output<TSchema>>): z.ZodSafeParseResult<z.output<TSchema>>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* We use zod schemas on unknown inputs to parse & validate the data.
|
|
3
|
+
* However, when you have already parsed data (usually because it is trusted data) and want to validate, using schema.parse is bad because the parameter is `unknown`, so you don't get feedback from TypeScript.
|
|
4
|
+
*
|
|
5
|
+
* Use this `typedParse` method instead, which accepts as input a strongly typed but unbranded version of the data and outputs a branded version of it, after validation.
|
|
6
|
+
*
|
|
7
|
+
* This is really just a schema.parse() with type safe inputs when you only need to run the validation part of the schema.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* typedParse(IntegerSchema, 42) // ✓
|
|
12
|
+
* typedParse(IntegerSchema, 42.5) // throws
|
|
13
|
+
* typedParse(IntegerSchema, "42") // TS error
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function typedParse(schema, value) {
|
|
17
|
+
return schema.parse(value);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The safeParse counterpart to {@link typedParse}
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* safeTypedParse(IntegerSchema, 42) // ✓
|
|
25
|
+
* safeTypedParse(IntegerSchema, 42.5) // returns success = false
|
|
26
|
+
* safeTypedParse(IntegerSchema, "42") // TS error
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function safeTypedParse(schema, value) {
|
|
30
|
+
return schema.safeParse(value);
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=typedParse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typedParse.js","sourceRoot":"","sources":["../src/typedParse.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CAA4B,MAAe,EAAE,KAAuC;IAC5G,OAAO,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC5B,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAe,EACf,KAAuC;IAEvC,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;AAChC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guillaume-docquier/tools-ts",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "My personal collection of typescript tools",
|
|
5
5
|
"homepage": "https://github.com/Guillaume-Docquier/tools-ts",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,7 +47,16 @@
|
|
|
47
47
|
"semantic-release": "^25.0.9",
|
|
48
48
|
"typescript": "~7.0.2",
|
|
49
49
|
"vite": "^8.3.0",
|
|
50
|
-
"vitest": "^5.0.0"
|
|
50
|
+
"vitest": "^5.0.0",
|
|
51
|
+
"zod": "^4.6.5"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"zod": "^4.6.5"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"zod": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
51
60
|
},
|
|
52
61
|
"lint-staged": {
|
|
53
62
|
"*.{md,json,yml,yaml,css,scss}": "oxfmt --no-error-on-unmatched-pattern",
|