@pawells/typescript-common 2.1.5 → 2.1.6

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.
Files changed (61) hide show
  1. package/README.md +22 -1
  2. package/dist/array/index.d.ts +1 -0
  3. package/dist/array/index.d.ts.map +1 -1
  4. package/dist/array/index.js +1 -0
  5. package/dist/array/iterators.d.ts +40 -0
  6. package/dist/array/iterators.d.ts.map +1 -0
  7. package/dist/array/iterators.js +54 -0
  8. package/dist/asserts/generic.d.ts +1 -1
  9. package/dist/asserts/generic.d.ts.map +1 -1
  10. package/dist/asserts/generic.js +8 -2
  11. package/dist/asserts/utils.d.ts.map +1 -1
  12. package/dist/asserts/utils.js +2 -3
  13. package/dist/enum/index.d.ts +1 -1
  14. package/dist/enum/index.d.ts.map +1 -1
  15. package/dist/enum/index.js +1 -1
  16. package/dist/function/memoize.d.ts +13 -0
  17. package/dist/function/memoize.d.ts.map +1 -1
  18. package/dist/function/memoize.js +13 -0
  19. package/dist/index.d.ts +3 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +3 -0
  22. package/dist/json.sanitization.d.ts +14 -0
  23. package/dist/json.sanitization.d.ts.map +1 -0
  24. package/dist/json.sanitization.js +37 -0
  25. package/dist/object/clone.d.ts +6 -0
  26. package/dist/object/clone.d.ts.map +1 -1
  27. package/dist/object/clone.js +7 -1
  28. package/dist/object/filter-cached.d.ts +0 -18
  29. package/dist/object/filter-cached.d.ts.map +1 -1
  30. package/dist/object/filter-cached.js +0 -18
  31. package/dist/object/filter.d.ts.map +1 -1
  32. package/dist/object/filter.js +10 -1
  33. package/dist/object/hash.js +2 -2
  34. package/dist/object/index.d.ts +1 -1
  35. package/dist/object/index.d.ts.map +1 -1
  36. package/dist/object/object-flatten.js +4 -0
  37. package/dist/object/property-paths.d.ts +2 -0
  38. package/dist/object/property-paths.d.ts.map +1 -1
  39. package/dist/object/property-paths.js +2 -0
  40. package/dist/object/security-utils.d.ts.map +1 -1
  41. package/dist/object/security-utils.js +8 -1
  42. package/dist/object/sort-keys.d.ts +14 -0
  43. package/dist/object/sort-keys.d.ts.map +1 -1
  44. package/dist/object/sort-keys.js +14 -0
  45. package/dist/object/types.d.ts +0 -102
  46. package/dist/object/types.d.ts.map +1 -1
  47. package/dist/string/assert.d.ts +19 -0
  48. package/dist/string/assert.d.ts.map +1 -1
  49. package/dist/string/assert.js +19 -0
  50. package/dist/time/elapsed-time/elapsed-time.d.ts +7 -7
  51. package/dist/time/elapsed-time/elapsed-time.js +25 -25
  52. package/dist/time/index.d.ts +1 -3
  53. package/dist/time/index.d.ts.map +1 -1
  54. package/dist/time/index.js +0 -3
  55. package/dist/time/stopwatch/stopwatch.d.ts +18 -0
  56. package/dist/time/stopwatch/stopwatch.d.ts.map +1 -1
  57. package/dist/time/stopwatch/stopwatch.js +23 -0
  58. package/dist/zod-util.d.ts +145 -0
  59. package/dist/zod-util.d.ts.map +1 -0
  60. package/dist/zod-util.js +126 -0
  61. package/package.json +65 -58
@@ -0,0 +1,145 @@
1
+ import { SemVer } from 'semver';
2
+ import { z } from 'zod/v4';
3
+ /**
4
+ * Zod schema for MongoDB ObjectId validation.
5
+ *
6
+ * Validates 24-character hexadecimal strings that conform to MongoDB's ObjectId format.
7
+ *
8
+ * @returns {z.ZodType<string>} Zod schema that validates ObjectId format
9
+ *
10
+ * @example
11
+ * const id = OBJECT_ID_SCHEMA.parse('507f1f77bcf86cd799439011');
12
+ * // Type: string
13
+ *
14
+ * @example
15
+ * const result = OBJECT_ID_SCHEMA.safeParse('invalid');
16
+ * // { success: false, error: ZodError }
17
+ */
18
+ export declare const OBJECT_ID_SCHEMA: z.ZodString;
19
+ /**
20
+ * Type inferred from OBJECT_ID_SCHEMA.
21
+ *
22
+ * Represents a valid MongoDB ObjectId string (24 hex characters).
23
+ */
24
+ export type TObjectId = z.infer<typeof OBJECT_ID_SCHEMA>;
25
+ /**
26
+ * Zod schema for semantic version validation.
27
+ *
28
+ * Transforms valid semantic version strings into SemVer instances. Zod schemas validate
29
+ * and return results rather than throw errors. Use .parse() to throw on invalid input,
30
+ * or .safeParse() to return a Result object.
31
+ *
32
+ * @returns {z.ZodType<SemVer>} Zod schema that parses and transforms to SemVer instance
33
+ *
34
+ * @remarks
35
+ * Invalid inputs throw errors when using .parse(), but .safeParse() returns a failure Result
36
+ * without throwing. The transform produces a SemVer instance rather than a string.
37
+ *
38
+ * @example
39
+ * const version = SEMVER_SCHEMA.parse('1.2.3');
40
+ * console.log(version.major); // 1
41
+ *
42
+ * @example
43
+ * const result = SEMVER_SCHEMA.safeParse('invalid');
44
+ * if (!result.success) {
45
+ * console.error('Invalid semver:', result.error);
46
+ * }
47
+ */
48
+ export declare const SEMVER_SCHEMA: z.ZodPipe<z.ZodString, z.ZodTransform<SemVer, string>>;
49
+ /**
50
+ * Zod validator for ZodObject schemas (z.object()).
51
+ *
52
+ * Uses instanceof check to validate that a value is a Zod object schema.
53
+ * Useful for runtime type guards when working with Zod schemas dynamically.
54
+ *
55
+ * @returns {z.ZodType<ZodObject<ZodRawShape>>} Zod schema that validates ZodObject instances
56
+ *
57
+ * @warning When using across module boundaries or in bundled environments,
58
+ * instanceof checks may fail due to multiple ZodObject constructor instances.
59
+ * If you encounter false negatives, consider using ZodObject method inspection
60
+ * as an alternative validation strategy.
61
+ *
62
+ * @example
63
+ * const schema = z.object({ name: z.string() });
64
+ * ZOD_OBJECT_SCHEMA.parse(schema); // OK
65
+ * ZOD_OBJECT_SCHEMA.parse("not a schema"); // throws ZodError
66
+ */
67
+ export declare const ZOD_OBJECT_SCHEMA: z.ZodCustom<z.ZodObject<Readonly<{
68
+ [k: string]: z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
69
+ }>, z.core.$strip>, z.ZodObject<Readonly<{
70
+ [k: string]: z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
71
+ }>, z.core.$strip>>;
72
+ /**
73
+ * Type inferred from ZOD_OBJECT_SCHEMA.
74
+ *
75
+ * Represents a Zod object schema with arbitrary shape.
76
+ */
77
+ export type TZodObjectSchema = z.infer<typeof ZOD_OBJECT_SCHEMA>;
78
+ /**
79
+ * Recursive type for JSON-serializable values.
80
+ *
81
+ * Represents any value that can be safely serialized to JSON: primitives, arrays, or objects
82
+ * with string keys and JSON-serializable values. Null is included but undefined is not.
83
+ */
84
+ export type TJSONSerializable = string | number | boolean | null | TJSONSerializable[] | {
85
+ [key: string]: TJSONSerializable;
86
+ };
87
+ /**
88
+ * Zod schema for generic JSON-serializable values.
89
+ *
90
+ * Validates that input conforms to the JSON-serializable type: strings, numbers, booleans,
91
+ * null, arrays, and objects with string keys. Uses lazy evaluation to support recursive
92
+ * structures without infinite type expansion.
93
+ *
94
+ * @returns {z.ZodType<TJSONSerializable>} Zod schema that validates JSON-serializable values
95
+ *
96
+ * @remarks
97
+ * This schema uses z.lazy() to enable recursive validation of nested structures.
98
+ * Undefined values and functions are rejected; only JSON-safe primitives and containers are allowed.
99
+ *
100
+ * @example
101
+ * JSON_SERIALIZABLE_SCHEMA.parse({ name: 'Alice', age: 30, tags: ['a', 'b'] }); // OK
102
+ *
103
+ * @example
104
+ * JSON_SERIALIZABLE_SCHEMA.parse({ fn: () => {} }); // throws ZodError (function not allowed)
105
+ */
106
+ export declare const JSON_SERIALIZABLE_SCHEMA: z.ZodType<TJSONSerializable>;
107
+ /**
108
+ * Zod schema for generic JSON objects.
109
+ *
110
+ * Validates that input is an object with string keys and JSON-serializable values.
111
+ * More restrictive than JSON_SERIALIZABLE_SCHEMA because it disallows arrays and primitives
112
+ * at the root level.
113
+ *
114
+ * @returns {z.ZodType} Zod schema that validates JSON objects with string keys
115
+ *
116
+ * @example
117
+ * JSON_OBJECT_SCHEMA.parse({ a: 1, b: 'hello', c: null }); // OK
118
+ *
119
+ * @example
120
+ * JSON_OBJECT_SCHEMA.parse([1, 2, 3]); // throws ZodError (array not allowed at root)
121
+ */
122
+ export declare const JSON_OBJECT_SCHEMA: z.ZodRecord<z.ZodString, z.ZodType<TJSONSerializable, unknown, z.core.$ZodTypeInternals<TJSONSerializable, unknown>>>;
123
+ /**
124
+ * Zod schema for JSON date string validation.
125
+ *
126
+ * Transforms date strings into Date instances. Accepts any string format recognized by the Date constructor
127
+ * (ISO 8601, timestamp, etc.). Note that invalid date strings produce Invalid Date objects (NaN value)
128
+ * rather than throwing errors.
129
+ *
130
+ * @returns {z.ZodType<Date>} Zod schema that parses and transforms strings to Date instances
131
+ *
132
+ * @remarks
133
+ * Invalid date strings produce Invalid Date objects (NaN value), not errors. Use `.refine(d => !isNaN(d.getTime()))`
134
+ * if you need to reject invalid dates and only accept valid date strings.
135
+ *
136
+ * @example
137
+ * const date = JSON_DATE_SCHEMA.parse('2024-01-15T10:30:00Z');
138
+ * console.log(date.getFullYear()); // 2024
139
+ *
140
+ * @example
141
+ * const invalidDate = JSON_DATE_SCHEMA.parse('not-a-date');
142
+ * console.log(invalidDate.getTime()); // NaN
143
+ */
144
+ export declare const JSON_DATE_SCHEMA: z.ZodPipe<z.ZodString, z.ZodTransform<Date, string>>;
145
+ //# sourceMappingURL=zod-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod-util.d.ts","sourceRoot":"","sources":["../src/zod-util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,CAAC,EAAoC,MAAM,QAAQ,CAAC;AAE7D;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,gBAAgB,aAAyD,CAAC;AAEvF;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,aAAa,wDAAiD,CAAC;AAE5E;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,iBAAiB;;;;mBAG7B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,iBAAiB,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAA;CAAE,CAAC;AAE9H;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,wBAAwB,EAAE,CAAC,CAAC,OAAO,CAAC,iBAAiB,CASjE,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,kBAAkB,uHAAiD,CAAC;AAEjF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,gBAAgB,sDAA+C,CAAC"}
@@ -0,0 +1,126 @@
1
+ import { SemVer } from 'semver';
2
+ import { z } from 'zod/v4';
3
+ /**
4
+ * Zod schema for MongoDB ObjectId validation.
5
+ *
6
+ * Validates 24-character hexadecimal strings that conform to MongoDB's ObjectId format.
7
+ *
8
+ * @returns {z.ZodType<string>} Zod schema that validates ObjectId format
9
+ *
10
+ * @example
11
+ * const id = OBJECT_ID_SCHEMA.parse('507f1f77bcf86cd799439011');
12
+ * // Type: string
13
+ *
14
+ * @example
15
+ * const result = OBJECT_ID_SCHEMA.safeParse('invalid');
16
+ * // { success: false, error: ZodError }
17
+ */
18
+ export const OBJECT_ID_SCHEMA = z.string().regex(/^[a-f\d]{24}$/i, 'Invalid ObjectId');
19
+ /**
20
+ * Zod schema for semantic version validation.
21
+ *
22
+ * Transforms valid semantic version strings into SemVer instances. Zod schemas validate
23
+ * and return results rather than throw errors. Use .parse() to throw on invalid input,
24
+ * or .safeParse() to return a Result object.
25
+ *
26
+ * @returns {z.ZodType<SemVer>} Zod schema that parses and transforms to SemVer instance
27
+ *
28
+ * @remarks
29
+ * Invalid inputs throw errors when using .parse(), but .safeParse() returns a failure Result
30
+ * without throwing. The transform produces a SemVer instance rather than a string.
31
+ *
32
+ * @example
33
+ * const version = SEMVER_SCHEMA.parse('1.2.3');
34
+ * console.log(version.major); // 1
35
+ *
36
+ * @example
37
+ * const result = SEMVER_SCHEMA.safeParse('invalid');
38
+ * if (!result.success) {
39
+ * console.error('Invalid semver:', result.error);
40
+ * }
41
+ */
42
+ export const SEMVER_SCHEMA = z.string().transform((val) => new SemVer(val));
43
+ /**
44
+ * Zod validator for ZodObject schemas (z.object()).
45
+ *
46
+ * Uses instanceof check to validate that a value is a Zod object schema.
47
+ * Useful for runtime type guards when working with Zod schemas dynamically.
48
+ *
49
+ * @returns {z.ZodType<ZodObject<ZodRawShape>>} Zod schema that validates ZodObject instances
50
+ *
51
+ * @warning When using across module boundaries or in bundled environments,
52
+ * instanceof checks may fail due to multiple ZodObject constructor instances.
53
+ * If you encounter false negatives, consider using ZodObject method inspection
54
+ * as an alternative validation strategy.
55
+ *
56
+ * @example
57
+ * const schema = z.object({ name: z.string() });
58
+ * ZOD_OBJECT_SCHEMA.parse(schema); // OK
59
+ * ZOD_OBJECT_SCHEMA.parse("not a schema"); // throws ZodError
60
+ */
61
+ export const ZOD_OBJECT_SCHEMA = z.custom((val) => val instanceof z.ZodObject, { message: "Must be a Zod object schema" });
62
+ /**
63
+ * Zod schema for generic JSON-serializable values.
64
+ *
65
+ * Validates that input conforms to the JSON-serializable type: strings, numbers, booleans,
66
+ * null, arrays, and objects with string keys. Uses lazy evaluation to support recursive
67
+ * structures without infinite type expansion.
68
+ *
69
+ * @returns {z.ZodType<TJSONSerializable>} Zod schema that validates JSON-serializable values
70
+ *
71
+ * @remarks
72
+ * This schema uses z.lazy() to enable recursive validation of nested structures.
73
+ * Undefined values and functions are rejected; only JSON-safe primitives and containers are allowed.
74
+ *
75
+ * @example
76
+ * JSON_SERIALIZABLE_SCHEMA.parse({ name: 'Alice', age: 30, tags: ['a', 'b'] }); // OK
77
+ *
78
+ * @example
79
+ * JSON_SERIALIZABLE_SCHEMA.parse({ fn: () => {} }); // throws ZodError (function not allowed)
80
+ */
81
+ export const JSON_SERIALIZABLE_SCHEMA = z.lazy(() => z.union([
82
+ z.string(),
83
+ z.number(),
84
+ z.boolean(),
85
+ z.null(),
86
+ z.array(JSON_SERIALIZABLE_SCHEMA),
87
+ z.record(z.string(), JSON_SERIALIZABLE_SCHEMA),
88
+ ]));
89
+ /**
90
+ * Zod schema for generic JSON objects.
91
+ *
92
+ * Validates that input is an object with string keys and JSON-serializable values.
93
+ * More restrictive than JSON_SERIALIZABLE_SCHEMA because it disallows arrays and primitives
94
+ * at the root level.
95
+ *
96
+ * @returns {z.ZodType} Zod schema that validates JSON objects with string keys
97
+ *
98
+ * @example
99
+ * JSON_OBJECT_SCHEMA.parse({ a: 1, b: 'hello', c: null }); // OK
100
+ *
101
+ * @example
102
+ * JSON_OBJECT_SCHEMA.parse([1, 2, 3]); // throws ZodError (array not allowed at root)
103
+ */
104
+ export const JSON_OBJECT_SCHEMA = z.record(z.string(), JSON_SERIALIZABLE_SCHEMA);
105
+ /**
106
+ * Zod schema for JSON date string validation.
107
+ *
108
+ * Transforms date strings into Date instances. Accepts any string format recognized by the Date constructor
109
+ * (ISO 8601, timestamp, etc.). Note that invalid date strings produce Invalid Date objects (NaN value)
110
+ * rather than throwing errors.
111
+ *
112
+ * @returns {z.ZodType<Date>} Zod schema that parses and transforms strings to Date instances
113
+ *
114
+ * @remarks
115
+ * Invalid date strings produce Invalid Date objects (NaN value), not errors. Use `.refine(d => !isNaN(d.getTime()))`
116
+ * if you need to reject invalid dates and only accept valid date strings.
117
+ *
118
+ * @example
119
+ * const date = JSON_DATE_SCHEMA.parse('2024-01-15T10:30:00Z');
120
+ * console.log(date.getFullYear()); // 2024
121
+ *
122
+ * @example
123
+ * const invalidDate = JSON_DATE_SCHEMA.parse('not-a-date');
124
+ * console.log(invalidDate.getTime()); // NaN
125
+ */
126
+ export const JSON_DATE_SCHEMA = z.string().transform((val) => new Date(val));
package/package.json CHANGED
@@ -1,60 +1,67 @@
1
1
  {
2
- "name": "@pawells/typescript-common",
3
- "displayName": "TypeScript Common Library",
4
- "description": "Shared TypeScript utility library",
5
- "version": "2.1.5",
6
- "license": "MIT",
7
- "author": {
8
- "name": "Phillip Aaron Wells",
9
- "email": "phillip.aaron.wells@gmail.com"
10
- },
11
- "homepage": "https://github.com/PhillipAWells/typescript-common",
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/PhillipAWells/typescript-common.git"
15
- },
16
- "funding": {
17
- "type": "github",
18
- "url": "https://github.com/sponsors/PhillipAWells"
19
- },
20
- "type": "module",
21
- "main": "./dist/index.js",
22
- "types": "./dist/index.d.ts",
23
- "exports": {
24
- "./package.json": "./package.json",
25
- ".": {
26
- "@pawells/typescript-common-workspace": "./src/index.ts",
27
- "types": "./dist/index.d.ts",
28
- "import": "./dist/index.js",
29
- "default": "./dist/index.js"
30
- }
31
- },
32
- "files": [
33
- "dist",
34
- "!**/*.tsbuildinfo"
35
- ],
36
- "keywords": [
37
- "typescript",
38
- "utilities",
39
- "common",
40
- "library"
41
- ],
42
- "engines": {
43
- "node": ">=22.0.0"
44
- },
45
- "nx": {
46
- "name": "@pawells/typescript-common",
47
- "tags": [
48
- "npm:public"
49
- ],
50
- "targets": {
51
- "clean": {
52
- "executor": "nx:run-commands",
53
- "options": {
54
- "command": "rm -rf dist tsconfig.*.tsbuildinfo",
55
- "cwd": "packages/typescript-common"
56
- }
57
- }
58
- }
59
- }
2
+ "name": "@pawells/typescript-common",
3
+ "displayName": "TypeScript Common Library",
4
+ "description": "Shared TypeScript utility library",
5
+ "version": "2.1.6",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Phillip Aaron Wells",
9
+ "email": "phillip.aaron.wells@gmail.com"
10
+ },
11
+ "homepage": "https://github.com/PhillipAWells/common",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/PhillipAWells/common.git"
15
+ },
16
+ "funding": {
17
+ "type": "github",
18
+ "url": "https://github.com/sponsors/PhillipAWells"
19
+ },
20
+ "type": "module",
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ "./package.json": "./package.json",
25
+ ".": {
26
+ "@pawells/typescript-common-workspace": "./src/index.ts",
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "!**/*.tsbuildinfo"
35
+ ],
36
+ "keywords": [
37
+ "typescript",
38
+ "utilities",
39
+ "common",
40
+ "library"
41
+ ],
42
+ "engines": {
43
+ "node": ">=22.0.0"
44
+ },
45
+ "nx": {
46
+ "name": "@pawells/typescript-common",
47
+ "tags": [
48
+ "npm:public"
49
+ ],
50
+ "targets": {
51
+ "clean": {
52
+ "executor": "nx:run-commands",
53
+ "options": {
54
+ "command": "rm -rf dist tsconfig.*.tsbuildinfo",
55
+ "cwd": "packages/typescript-common"
56
+ }
57
+ }
58
+ }
59
+ },
60
+ "dependencies": {
61
+ "semver": "7.8.0",
62
+ "zod": "^4.4.3"
63
+ },
64
+ "devDependencies": {
65
+ "@types/semver": "^7.7.1"
66
+ }
60
67
  }