@infra-blocks/zod-utils 0.30.0 → 0.31.0-alpha.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 CHANGED
@@ -55,6 +55,20 @@ zu.aws.region().parse("us-east-1");
55
55
 
56
56
  The `zu.codec` module contains codecs.
57
57
 
58
+ #### bytes
59
+
60
+ The `zu.codec.bytes(options)` utility is a factory returning a codec where the schemas are
61
+ `z.string()` and `z.number()`. The transformations in both directions are handled by the
62
+ [bytes](https://www.npmjs.com/package/bytes) package. When encoding, the codec passes the options
63
+ provided to `bytes.format(number, options)`.
64
+
65
+ ```typescript
66
+ import { zu } from "@infra-blocks/zod-utils";
67
+
68
+ const item: number = zu.codec.bytes().parse("1tb"); // item is 1099511627776.
69
+ zu.codec.bytes({ unit: "mb" }).encode(item); // result is "1048576mb".
70
+ ```
71
+
58
72
  #### csv
59
73
 
60
74
  The `zu.codec.csv()` utility is a codec transforming a string into an array of string using the string `split` method
@@ -0,0 +1,14 @@
1
+ import { type BytesOptions } from "bytes";
2
+ import { z } from "zod";
3
+ /**
4
+ * A codec factory that returns a codec parsing strings formatted
5
+ * as expected by the [bytes](https://www.npmjs.com/package/bytes) and turning them
6
+ * into their equivalent number in bytes.
7
+ *
8
+ * @param options - Those options are passed to `ms` when turning the milliseconds
9
+ * number into a formatted string.
10
+ *
11
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
12
+ * and where the transforms between each are handled by the `ms` package.
13
+ */
14
+ export declare const bytes: (options?: BytesOptions) => z.ZodCodec<z.ZodString, z.ZodNumber>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.bytes = void 0;
7
+ const bytes_1 = __importDefault(require("bytes"));
8
+ const zod_1 = require("zod");
9
+ /**
10
+ * A codec factory that returns a codec parsing strings formatted
11
+ * as expected by the [bytes](https://www.npmjs.com/package/bytes) and turning them
12
+ * into their equivalent number in bytes.
13
+ *
14
+ * @param options - Those options are passed to `ms` when turning the milliseconds
15
+ * number into a formatted string.
16
+ *
17
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
18
+ * and where the transforms between each are handled by the `ms` package.
19
+ */
20
+ const bytes = (options) => zod_1.z.codec(zod_1.z.string(), zod_1.z.number(), {
21
+ decode: (str) => bytes_1.default.parse(str) ?? zod_1.z.NEVER,
22
+ encode: (bytes) => bytes_1.default.format(bytes, options) ?? zod_1.z.NEVER,
23
+ });
24
+ exports.bytes = bytes;
25
+ //# sourceMappingURL=bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../../src/codec/bytes.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAgD;AAChD,6BAAwB;AAExB;;;;;;;;;;GAUG;AACI,MAAM,KAAK,GAAG,CAAC,OAAsB,EAAE,EAAE,CAC9C,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE,EAAE;IAC9B,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,eAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,OAAC,CAAC,KAAK;IAC3C,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,eAAI,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,OAAC,CAAC,KAAK;CAC1D,CAAC,CAAC;AAJQ,QAAA,KAAK,SAIb"}
@@ -1,4 +1,5 @@
1
1
  export declare const codec: {
2
+ bytes: (options?: import("bytes").BytesOptions) => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodNumber>;
2
3
  csv: () => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodArray<import("zod").ZodString>>;
3
4
  jsonParse: <S extends import("zod").ZodType>(schema: S) => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "JsonString">, S>;
4
5
  ms: (options?: {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.codec = void 0;
4
+ const bytes_js_1 = require("./bytes.js");
4
5
  const csv_js_1 = require("./csv.js");
5
6
  const json_parse_js_1 = require("./json-parse.js");
6
7
  const ms_js_1 = require("./ms.js");
@@ -10,6 +11,7 @@ const string_to_json_js_1 = require("./string-to-json.js");
10
11
  const string_to_positive_integer_js_1 = require("./string-to-positive-integer.js");
11
12
  const string_to_url_js_1 = require("./string-to-url.js");
12
13
  exports.codec = {
14
+ bytes: bytes_js_1.bytes,
13
15
  csv: csv_js_1.csv,
14
16
  jsonParse: json_parse_js_1.jsonParse,
15
17
  ms: ms_js_1.ms,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":";;;AAAA,qCAA+B;AAC/B,mDAA4C;AAC5C,mCAA6B;AAC7B,+DAAuD;AACvD,iEAAyD;AACzD,2DAAmD;AACnD,mFAA0E;AAC1E,yDAAiD;AAEpC,QAAA,KAAK,GAAG;IACnB,GAAG,EAAH,YAAG;IACH,SAAS,EAAT,yBAAS;IACT,EAAE,EAAF,UAAE;IACF,cAAc,EAAd,oCAAc;IACd,eAAe,EAAf,sCAAe;IACf,YAAY,EAAZ,gCAAY;IACZ,uBAAuB,EAAvB,uDAAuB;IACvB,WAAW,EAAX,8BAAW;CACZ,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":";;;AAAA,yCAAmC;AACnC,qCAA+B;AAC/B,mDAA4C;AAC5C,mCAA6B;AAC7B,+DAAuD;AACvD,iEAAyD;AACzD,2DAAmD;AACnD,mFAA0E;AAC1E,yDAAiD;AAEpC,QAAA,KAAK,GAAG;IACnB,KAAK,EAAL,gBAAK;IACL,GAAG,EAAH,YAAG;IACH,SAAS,EAAT,yBAAS;IACT,EAAE,EAAF,UAAE;IACF,cAAc,EAAd,oCAAc;IACd,eAAe,EAAf,sCAAe;IACf,YAAY,EAAZ,gCAAY;IACZ,uBAAuB,EAAvB,uDAAuB;IACvB,WAAW,EAAX,8BAAW;CACZ,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { type BytesOptions } from "bytes";
2
+ import { z } from "zod";
3
+ /**
4
+ * A codec factory that returns a codec parsing strings formatted
5
+ * as expected by the [bytes](https://www.npmjs.com/package/bytes) and turning them
6
+ * into their equivalent number in bytes.
7
+ *
8
+ * @param options - Those options are passed to `ms` when turning the milliseconds
9
+ * number into a formatted string.
10
+ *
11
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
12
+ * and where the transforms between each are handled by the `ms` package.
13
+ */
14
+ export declare const bytes: (options?: BytesOptions) => z.ZodCodec<z.ZodString, z.ZodNumber>;
@@ -0,0 +1,18 @@
1
+ import impl from "bytes";
2
+ import { z } from "zod";
3
+ /**
4
+ * A codec factory that returns a codec parsing strings formatted
5
+ * as expected by the [bytes](https://www.npmjs.com/package/bytes) and turning them
6
+ * into their equivalent number in bytes.
7
+ *
8
+ * @param options - Those options are passed to `ms` when turning the milliseconds
9
+ * number into a formatted string.
10
+ *
11
+ * @returns A codec where the schemas are {@link z.string} and {@link z.number},
12
+ * and where the transforms between each are handled by the `ms` package.
13
+ */
14
+ export const bytes = (options) => z.codec(z.string(), z.number(), {
15
+ decode: (str) => impl.parse(str) ?? z.NEVER,
16
+ encode: (bytes) => impl.format(bytes, options) ?? z.NEVER,
17
+ });
18
+ //# sourceMappingURL=bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../../src/codec/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,IAA2B,MAAM,OAAO,CAAC;AAChD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,OAAsB,EAAE,EAAE,CAC9C,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE;IAC9B,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK;IAC3C,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK;CAC1D,CAAC,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export declare const codec: {
2
+ bytes: (options?: import("bytes").BytesOptions) => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodNumber>;
2
3
  csv: () => import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodArray<import("zod").ZodString>>;
3
4
  jsonParse: <S extends import("zod").ZodType>(schema: S) => import("zod").ZodCodec<import("zod/v4/core").$ZodBranded<import("zod").ZodString, "JsonString">, S>;
4
5
  ms: (options?: {
@@ -1,3 +1,4 @@
1
+ import { bytes } from "./bytes.js";
1
2
  import { csv } from "./csv.js";
2
3
  import { jsonParse } from "./json-parse.js";
3
4
  import { ms } from "./ms.js";
@@ -7,6 +8,7 @@ import { stringToJson } from "./string-to-json.js";
7
8
  import { stringToPositiveInteger } from "./string-to-positive-integer.js";
8
9
  import { stringToUrl } from "./string-to-url.js";
9
10
  export const codec = {
11
+ bytes,
10
12
  csv,
11
13
  jsonParse,
12
14
  ms,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,GAAG;IACH,SAAS;IACT,EAAE;IACF,cAAc;IACd,eAAe;IACf,YAAY;IACZ,uBAAuB;IACvB,WAAW;CACZ,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/codec/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,KAAK;IACL,GAAG;IACH,SAAS;IACT,EAAE;IACF,cAAc;IACd,eAAe;IACf,YAAY;IACZ,uBAAuB;IACvB,WAAW;CACZ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infra-blocks/zod-utils",
3
- "version": "0.30.0",
3
+ "version": "0.31.0-alpha.0",
4
4
  "description": "Extensions to the zod package.",
5
5
  "keywords": [
6
6
  "zod",
@@ -37,11 +37,11 @@
37
37
  "test": "npm run test:unit",
38
38
  "test:coverage": "c8 npm run test",
39
39
  "test:coverage:lcov": "c8 --reporter=lcov npm run test",
40
- "test:integration": "mocha --config test/integration/.mocharc.js 'test/integration/**/*.spec.ts'",
41
- "test:unit": "mocha --config test/unit/.mocharc.cjs 'test/unit/**/*.spec.ts'"
40
+ "test:unit": "node --import tsx --test 'test/unit/**/*.spec.ts'"
42
41
  },
43
42
  "dependencies": {
44
43
  "@infra-blocks/types": "^0.26.0",
44
+ "bytes": "^3.1.2",
45
45
  "currency-codes": "^2.2.0",
46
46
  "iso-3166": "^4.4.0",
47
47
  "ms": "^2.1.3",
@@ -50,12 +50,11 @@
50
50
  "devDependencies": {
51
51
  "@biomejs/biome": "^2.3.10",
52
52
  "@infra-blocks/test": "^0.6.0",
53
- "@types/mocha": "^10.0.1",
53
+ "@types/bytes": "^3.1.5",
54
54
  "@types/ms": "^2.1.0",
55
55
  "@types/node": "^24.10.4",
56
56
  "c8": "^8.0.0",
57
57
  "lefthook": "^2.0.13",
58
- "mocha": "^10.2.0",
59
58
  "tsx": "^4.21.0",
60
59
  "typescript": "^5.9.3"
61
60
  },