You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A tool that transfers properties from one object to another according to a schema, which outlines the properties that should be mapped and the manner in which they should be mapped, with a runtime type verification.
Installation
$ npm install meta-shape
How to Use
In a schema object, the "key" represents the final "destination," and the "value" is a "command" that selects data from "source", makes modifications, and sets it to the "destination" object as its "value."
Base usage
import{pick,convert}from"meta-shape";constdata={name: "Bird",surname: "Ramsey",age: "23 years",gender: "male",company: "NIMON",contacts: {email: {primary: "birdramsey@nimon.com",},},};constschema={id: pick<number>().fallback(null),fullName: pick("name","surname").pipe((name,surname)=>`${name}${surname}`),age: pick().pipe((age)=>Number.parseInt(age)),gender: pick().pipe((gender: string)=>gender.slice(0,1)).pipe((gender: string)=>gender.toUpperCase()),"details.company.name": pick("company").type(String),email: pick<string>("contacts.email.primary").fallback("No email"),};constoutput=convert({ schema, data });/** * When generics are used or fallback type can be calculated * output properties will be types * path intellisense is also available *//* id: number | null */output.id/* fullName: string */output.fullName/* age: number */output.age/* gender: string */output.gender/* name: string */output.details.company.name/* email: string */output.emailconstoutput={id: null,fullName: "Bird Ramsey",age: 23,gender: "M",details: {company: {name: "NIMON"}},email: "birdramsey@nimon.com",};