Advanced object mapping tool, especially useful for third-party object mapping and transformation.
Install
npm install @ht14x/light-mapper
Documentation
-
@Mapping
// Target object class Target { @Mapping(MappingRequirement.REQUIRED) property: any; } // Source object class Source { property = "value"; } const target: Target = new LightMapper().map<Target>(Target, new Source()); console.log(target); // Target { property: 'value' }
// Target object class Target { @Mapping(MappingRequirement.NULLABLE) property: any; } // Source object class Source {} const target: Target = new LightMapper().map<Target>(Target, new Source()); console.log(target); // Target { property: null }
// Target object class Target { @Mapping(MappingRequirement.OPTIONAL) property: any; } // Source object class Source {} const target: Target = new LightMapper().map<Target>(Target, new Source()); console.log(target); // Target {}
-
MappingOpts
export interface MappingOpts<T = any> { requirement: MappingRequirement; from?: string | string[]; // from which property of the source object is to be mapped name?: string; // name for getDescription() config?: T; // config for getDescription() transformation?: (value: any) => any; // transfrom function before map }
from
option// Target object class Target { @Mapping({ requirement: MappingRequirement.OPTIONAL, from: "sourceProperty", // or from: ["sourceA", "sourceB"] }) property: any; } // Source object class Source { sourceProperty = "value"; } const target: Target = new LightMapper().map<Target>(Target, new Source()); console.log(target); // Target { property: 'value' }
transformation
option// Target object class Target { @Mapping({ requirement: MappingRequirement.OPTIONAL, transformation: (value: any) => { return `${value} - transformed`; }, }) property: any; } // Source object class Source { property = "value"; } const target: Target = new LightMapper().map<Target>(Target, new Source()); console.log(target); // Target { property: 'value - transformed' }
name
&config
option// Target object class Target { @Mapping({ requirement: MappingRequirement.OPTIONAL, name: "Property Label", config: { custom: "params" }, }) property: any; } const description = new LightMapper().getDescription<Target>(Target); console.log(description); /* [ { name: 'Property Label', field: 'property', config: { custom: 'params' } } ] */
-
transform
// Target object class Target { @Mapping(MappingRequirement.REQUIRED) property: any; } // Source object class Source { property = "value"; } const target: Target = new LightMapper() .transform("property", (value) => `${value} transform inline`) .map<Target>(Target, new Source()); console.log(target); // Target { property: 'value transform inline' }
-
replace
// Target object class Target { @Mapping(MappingRequirement.REQUIRED) property: any; } // Source object class Source { property = "value"; } const target: Target = new LightMapper() .replace("property", "replaced property value inline") .map<Target>(Target, new Source()); console.log(target); // Target { property: 'replaced property value inline' }