@itrocks/data-to-object 0.0.7 → 0.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.
Files changed (2) hide show
  1. package/README.md +35 -20
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  # data-to-object
8
8
 
9
- Transforms raw string-based data into a business object with type-safe values.
9
+ Transforms raw input data into a business object with type-safe values.
10
10
 
11
11
  ## Installation
12
12
 
@@ -30,35 +30,50 @@ const rawData = {
30
30
  }
31
31
 
32
32
  const user = new User()
33
-
34
33
  await dataToObject(user, rawData)
35
34
 
36
35
  console.log(user)
37
- // Output: { name: 'John Doe', age: 30 }
36
+ // { name: 'John Doe', age: 30 }
38
37
  ```
39
38
 
40
- ## dataToObject Function
39
+ ## dataToObject()
41
40
 
42
- Converts raw data (e.g., JSON, web forms) into a business object
43
- by applying type-appropriate transformations to each property.
41
+ ```ts
42
+ export async function dataToObject<T extends object>(
43
+ object: T,
44
+ data: RecursiveValueObject
45
+ ): Promise<T>
46
+ ```
47
+
48
+ Converts raw data (JSON payloads, form inputs, query params…) into a business object
49
+ by applying type-aware transformations on each input key that resolves to a property
50
+ declared on the target object.
44
51
 
45
52
  ### Parameters
46
53
 
47
- - `object` (*T extends object*) The target object where the transformed values will be assigned.
48
- - `data` (*[RecursiveStringObject](https://github.com/itrocks-ts/request-response#recursivestringobject)*) The raw data source with string values.
54
+ - `object`: Target business object to populate.
55
+ - `data`: Raw input data. Values may be strings or already-typed values.\
56
+ *([RecursiveValueObject](https://github.com/itrocks-ts/request-response#recursivevalueobject))*
57
+
58
+ ### Behaviour
49
59
 
50
- ### Behavior
60
+ - Only properties **declared on the target object** are assigned.
61
+ - Input keys are normalised from form field to property names using `@itrocks/rename` (`toProperty`).
62
+ - Fields ending with `_id` fall back to their base field name (e.g. `user_id` → `user`)
63
+ only when the `_id` property does not exist on the target object,
64
+ and only if the base field name is not already present in the input data.
65
+ - Each value is transformed using its matching transformer
66
+ ([@itrocks/transformer](https://github.com/itrocks-ts/transformer))
67
+ with the `HTML` and `INPUT` contexts.
68
+ - The transformer may return a value to be assigned,
69
+ or mutate the target object directly
70
+ and return [IGNORE](https://github.com/itrocks-ts/transformer#ignoring-a-transformation-result)
71
+ to prevent any automatic assignment.
51
72
 
52
- - Inspect the object's properties.
53
- - Applies transformations via [@itrocks/transformer](https://github.com/itrocks-ts/transformer#applytransformer)
54
- with [HTML and INPUT contexts](https://github.com/itrocks-ts/transformer#constants).
55
- - Ignores properties that are not present in the target object.
56
- - Skips properties explicitly marked as
57
- [IGNORE](https://github.com/itrocks-ts/transformer#ignoring-a-transformation-result)
58
- by the transformer.
73
+ ### Typical use cases
59
74
 
60
- ### Example Use Cases
75
+ - Processing web form submissions safely.
76
+ - Mapping request payloads to domain objects.
77
+ - Centralising input sanitisation and type coercion.
61
78
 
62
- - Processing web form inputs safely (e.g. [@itrocks/save](https://github.com/itrocks-ts/save)).
63
- - Mapping JSON API responses to strongly-typed objects.
64
- - Cleaning and sanitizing data before storage or further processing.
79
+ This function is commonly used by higher-level helpers such as [@itrocks/save](https://github.com/itrocks-ts/save).
package/package.json CHANGED
@@ -52,5 +52,5 @@
52
52
  "build": "tsc"
53
53
  },
54
54
  "types": "./cjs/data-to-object.d.ts",
55
- "version": "0.0.7"
55
+ "version": "0.1.0"
56
56
  }