typeshi 2.1.3 → 2.1.4

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.
@@ -24,15 +24,23 @@ export declare class Restrict {
24
24
  static toPicked: typeof picked;
25
25
  }
26
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.
27
+ * define a `TransformationSchema<T>` to use in `sanitizeAndMap` to convert
28
+ * an initial object `any` to `T`. the `"initial"` object is assumed to share props with `T`
29
+ * (hence the need to transform its values)
30
+ *
31
+ * values are either:
32
+ * 1. a `function` where only need to pass in `initial[K]` to get the transformed value `T[K]`
33
+ * 2. an `object` with props:
34
+ * - `transform (function, optional)` - function that uses `initial[K]` & `args` to compute `T[K]`
35
+ * - `args (any[], optional)` - the array of arguments passed into `transform` using the spread operator.
36
+ * - `defaultValue (T[K], optional)` - assign this value to `K` when `transform` is `undefined`
37
+ * and `initial[K]` is `undefined`
31
38
  */
32
39
  export type TransformationSchema<T> = {
33
40
  [K in keyof T]?: ((val: any) => T[K]) | {
34
- transform: (val: any, ...args: any[]) => T[K];
35
- args: any[];
41
+ transform?: (val: any, ...args: any[]) => T[K];
42
+ args?: any[];
43
+ defaultValue?: T[K];
36
44
  };
37
45
  };
38
46
  /**
@@ -51,14 +51,20 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
51
51
  const data = {};
52
52
  // 1. Handle explicit transformations
53
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]);
54
+ const schemaValue = schema[key];
55
+ if (!schemaValue)
56
+ continue;
57
+ if (hasDefinedEntry(obj, key)) {
58
+ if ((0, typeValidation_1.isFunction)(schemaValue)) {
59
+ data[key] = schemaValue(obj[key]);
57
60
  }
58
- else {
59
- data[key] = schema[key].transform(obj[key], ...schema[key].args);
61
+ else if ((0, typeValidation_1.isFunction)(schemaValue.transform)) {
62
+ data[key] = schemaValue.transform(obj[key], ...(schemaValue.args ?? []));
60
63
  }
61
64
  }
65
+ else if (!(0, typeValidation_1.isFunction)(schemaValue) && 'defaultValue' in schemaValue) { // can apply defaultValue
66
+ data[key] = schemaValue.defaultValue;
67
+ }
62
68
  }
63
69
  // 2. Handle simple pass-throughs (Identity mapping)
64
70
  for (const key of passThroughKeys) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",