Transforms raw string-based data into a business object with type-safe values.
npm i @itrocks/data-to-object
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 }
Converts raw data (e.g., JSON, web forms) into a business object by applying type-appropriate transformations to each property.
-
object
(T extends object) – The target object where the transformed values will be assigned. -
data
(RecursiveStringObject) – The raw data source with string values.
- 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.
- 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.