0.0.5 • Published 5 months ago
@itrocks/data-to-object v0.0.5
data-to-object
Transforms raw string-based data into a business object with type-safe values.
Installation
npm i @itrocks/data-to-object
Usage
import { dataToObject } from '@itrocks/data-to-object'
class User {
name!: string
age!: number
}
const rawData = {
name: 'John Doe',
age: '30'
}
const user = new User()
await dataToObject(user, rawData)
console.log(user)
// Output: { name: 'John Doe', age: 30 }
dataToObject Function
Converts raw data (e.g., JSON, web forms) into a business object by applying type-appropriate transformations to each property.
Parameters
object
(T extends object) – The target object where the transformed values will be assigned.data
(RecursiveStringObject) – The raw data source with string values.
Behavior
- Inspect the object's properties.
- Applies transformations via @itrocks/transformer with HTML and INPUT contexts.
- Ignores properties that are not present in the target object.
- Skips properties explicitly marked as IGNORE by the transformer.
Example Use Cases
- Processing web form inputs safely (e.g. @itrocks/save).
- Mapping JSON API responses to strongly-typed objects.
- Cleaning and sanitizing data before storage or further processing.