typeshi 2.1.1 → 2.1.3

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.
@@ -5,3 +5,4 @@ export * from "./io";
5
5
  export * from "./regex";
6
6
  export * from "./argumentValidation";
7
7
  export * from "./typeValidation";
8
+ export * from "./object";
@@ -21,3 +21,4 @@ __exportStar(require("./io"), exports);
21
21
  __exportStar(require("./regex"), exports);
22
22
  __exportStar(require("./argumentValidation"), exports);
23
23
  __exportStar(require("./typeValidation"), exports);
24
+ __exportStar(require("./object"), exports);
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @file src/utils/object.ts
3
+ * @TODO just use zod instead
4
+ */
5
+ /**
6
+ * @returns `boolean`
7
+ * - `true` if all keys in obj are also in validKeys
8
+ * - `false` if there exists a key in obj that is not in validKeys
9
+ */
10
+ export declare function hasValidKeysOnly<T extends object>(obj: object, validKeys: readonly (keyof T)[]): obj is T;
11
+ /**
12
+ * @returns a new object containing only the specified keys.
13
+ */
14
+ export declare function picked<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
15
+ export declare class Restrict {
16
+ /**
17
+ * `hasValidKeysOnly`
18
+ * @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
19
+ */
20
+ static keys: typeof hasValidKeysOnly;
21
+ /**
22
+ * @returns a new object containing only the specified keys.
23
+ */
24
+ static toPicked: typeof picked;
25
+ }
26
+ /**
27
+ * **`values`** are either:
28
+ * - a `function` where only need to pass in single value to get the transformed value `T[K]`
29
+ * - an object with two props: `transform` & `args` where `args` is the array of arguments
30
+ * passed into `transform` using the spread operator.
31
+ */
32
+ export type TransformationSchema<T> = {
33
+ [K in keyof T]?: ((val: any) => T[K]) | {
34
+ transform: (val: any, ...args: any[]) => T[K];
35
+ args: any[];
36
+ };
37
+ };
38
+ /**
39
+ * @param obj `any` - source object (e.g., Request Body)
40
+ * @param schema {@link TransformationSchema}`<T>` - map of keys to transformation functions
41
+ * @param passThroughKeys `(keyof T)[]` - keys to move over without transformation (Identity mapping)
42
+ * @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
43
+ */
44
+ export declare function sanitizeAndMap<T extends object>(obj: any, schema: TransformationSchema<T>, passThroughKeys?: (keyof T)[]): T;
45
+ /**
46
+ * @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
47
+ */
48
+ export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T): boolean;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ /**
3
+ * @file src/utils/object.ts
4
+ * @TODO just use zod instead
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.Restrict = void 0;
8
+ exports.hasValidKeysOnly = hasValidKeysOnly;
9
+ exports.picked = picked;
10
+ exports.sanitizeAndMap = sanitizeAndMap;
11
+ exports.hasDefinedEntry = hasDefinedEntry;
12
+ const typeValidation_1 = require("./typeValidation");
13
+ /**
14
+ * @returns `boolean`
15
+ * - `true` if all keys in obj are also in validKeys
16
+ * - `false` if there exists a key in obj that is not in validKeys
17
+ */
18
+ function hasValidKeysOnly(obj, validKeys) {
19
+ return Object.keys(obj).every(key => validKeys.includes(key));
20
+ }
21
+ /**
22
+ * @returns a new object containing only the specified keys.
23
+ */
24
+ function picked(obj, keys) {
25
+ const result = {};
26
+ for (const key of keys) {
27
+ if (key in obj && obj[key] !== undefined)
28
+ result[key] = obj[key];
29
+ }
30
+ return result;
31
+ }
32
+ class Restrict {
33
+ }
34
+ exports.Restrict = Restrict;
35
+ /**
36
+ * `hasValidKeysOnly`
37
+ * @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
38
+ */
39
+ Restrict.keys = hasValidKeysOnly;
40
+ /**
41
+ * @returns a new object containing only the specified keys.
42
+ */
43
+ Restrict.toPicked = picked;
44
+ /**
45
+ * @param obj `any` - source object (e.g., Request Body)
46
+ * @param schema {@link TransformationSchema}`<T>` - map of keys to transformation functions
47
+ * @param passThroughKeys `(keyof T)[]` - keys to move over without transformation (Identity mapping)
48
+ * @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
49
+ */
50
+ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
51
+ const data = {};
52
+ // 1. Handle explicit transformations
53
+ for (const key in schema) {
54
+ if (schema[key] && hasDefinedEntry(obj, key)) {
55
+ if ((0, typeValidation_1.isFunction)(schema[key])) {
56
+ data[key] = schema[key](obj[key]);
57
+ }
58
+ else {
59
+ data[key] = schema[key].transform(obj[key], ...schema[key].args);
60
+ }
61
+ }
62
+ }
63
+ // 2. Handle simple pass-throughs (Identity mapping)
64
+ for (const key of passThroughKeys) {
65
+ if (!data[key] && hasDefinedEntry(obj, key)) {
66
+ data[key] = obj[key];
67
+ }
68
+ }
69
+ return data;
70
+ }
71
+ /**
72
+ * @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
73
+ */
74
+ function hasDefinedEntry(obj, key) {
75
+ return Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;
76
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",