@itrocks/data-to-object 0.0.1
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/LICENSE +14 -0
- package/README.md +64 -0
- package/cjs/data-to-object.d.ts +2 -0
- package/cjs/data-to-object.js +18 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
Copyright (C) 2024, Baptiste Pillot <baptiste@pillot.fr>
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
copyright notice and this permission notice appear in all copies.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
13
|
+
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
14
|
+
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[](https://www.npmjs.org/package/@itrocks/data-to-object)
|
|
2
|
+
[](https://www.npmjs.org/package/@itrocks/data-to-object)
|
|
3
|
+
[](https://github.com/itrocks-ts/data-to-object)
|
|
4
|
+
[](https://github.com/itrocks-ts/data-to-object/issues)
|
|
5
|
+
[](https://25.re/ditr)
|
|
6
|
+
|
|
7
|
+
# data-to-object
|
|
8
|
+
|
|
9
|
+
Transforms raw string-based data into a business object with type-safe values.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm i @itrocks/data-to-object
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { dataToObject } from '@itrocks/data-to-object'
|
|
21
|
+
|
|
22
|
+
class User {
|
|
23
|
+
name!: string
|
|
24
|
+
age!: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const rawData = {
|
|
28
|
+
name: 'John Doe',
|
|
29
|
+
age: '30'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const user = new User()
|
|
33
|
+
|
|
34
|
+
await dataToObject(user, rawData)
|
|
35
|
+
|
|
36
|
+
console.log(user)
|
|
37
|
+
// Output: { name: 'John Doe', age: 30 }
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## dataToObject Function
|
|
41
|
+
|
|
42
|
+
Converts raw data (e.g., JSON, web forms) into a business object
|
|
43
|
+
by applying type-appropriate transformations to each property.
|
|
44
|
+
|
|
45
|
+
### Parameters
|
|
46
|
+
|
|
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.
|
|
49
|
+
|
|
50
|
+
### Behavior
|
|
51
|
+
|
|
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.
|
|
59
|
+
|
|
60
|
+
### Example Use Cases
|
|
61
|
+
|
|
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.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dataToObject = dataToObject;
|
|
4
|
+
const reflect_1 = require("@itrocks/reflect");
|
|
5
|
+
const transformer_1 = require("@itrocks/transformer");
|
|
6
|
+
const transformer_2 = require("@itrocks/transformer");
|
|
7
|
+
async function dataToObject(object, data) {
|
|
8
|
+
const properties = new reflect_1.ReflectClass(object).propertyNames;
|
|
9
|
+
for (const property in data) {
|
|
10
|
+
if (!properties.includes(property))
|
|
11
|
+
continue;
|
|
12
|
+
const value = await (0, transformer_1.applyTransformer)(data[property], object, property, transformer_2.HTML, transformer_2.INPUT, data);
|
|
13
|
+
if (value === transformer_2.IGNORE)
|
|
14
|
+
continue;
|
|
15
|
+
object[property] = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=data-to-object.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": {
|
|
3
|
+
"name": "Baptiste Pillot",
|
|
4
|
+
"email": "baptiste@pillot.fr"
|
|
5
|
+
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@itrocks/reflect": "latest",
|
|
8
|
+
"@itrocks/request-response": "latest",
|
|
9
|
+
"@itrocks/transformer": "latest"
|
|
10
|
+
},
|
|
11
|
+
"description": "Transforms raw string-based data into a business object with type-safe values",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"typescript": "~5.6"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"exports": "./cjs/data-to-object.js",
|
|
19
|
+
"homepage": "https://it.rocks",
|
|
20
|
+
"files": [
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.md",
|
|
23
|
+
"cjs/*",
|
|
24
|
+
"!*.map"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"backend",
|
|
28
|
+
"conversion",
|
|
29
|
+
"convert",
|
|
30
|
+
"data",
|
|
31
|
+
"form",
|
|
32
|
+
"handling",
|
|
33
|
+
"input",
|
|
34
|
+
"it.rocks",
|
|
35
|
+
"json",
|
|
36
|
+
"object",
|
|
37
|
+
"mapping",
|
|
38
|
+
"processing",
|
|
39
|
+
"transform",
|
|
40
|
+
"transformation",
|
|
41
|
+
"transformer",
|
|
42
|
+
"type"
|
|
43
|
+
],
|
|
44
|
+
"license": "ISC",
|
|
45
|
+
"name": "@itrocks/data-to-object",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/itrocks-ts/data-to-object.git"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.json"
|
|
52
|
+
},
|
|
53
|
+
"types": "./cjs/data-to-object.d.ts",
|
|
54
|
+
"version": "0.0.1"
|
|
55
|
+
}
|