@itrocks/data-to-object 0.0.6 → 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.
- package/README.md +35 -20
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
# data-to-object
|
|
8
8
|
|
|
9
|
-
Transforms raw
|
|
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
|
-
//
|
|
36
|
+
// { name: 'John Doe', age: 30 }
|
|
38
37
|
```
|
|
39
38
|
|
|
40
|
-
## dataToObject
|
|
39
|
+
## dataToObject()
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
48
|
-
- `data
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
75
|
+
- Processing web form submissions safely.
|
|
76
|
+
- Mapping request payloads to domain objects.
|
|
77
|
+
- Centralising input sanitisation and type coercion.
|
|
61
78
|
|
|
62
|
-
|
|
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
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
},
|
|
12
12
|
"description": "Transforms raw string-based data into a business object with type-safe values",
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"typescript": "~5.
|
|
14
|
+
"typescript": "~5.9"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">=18"
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"build": "tsc"
|
|
53
53
|
},
|
|
54
54
|
"types": "./cjs/data-to-object.d.ts",
|
|
55
|
-
"version": "0.0
|
|
55
|
+
"version": "0.1.0"
|
|
56
56
|
}
|