README
LightMapper
Advanced object mapping tool, especially useful for third-party object mapping and transformation.
Install
npm install @ht14x/light-mapper
Documentation
Mapping decorator@Mapping
REQUIRED 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' }
NULLABLE mapping
// 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 }
OPTIONAL mapping
// 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 {}
Mapping optionsMappingOpts
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 }
option from
// 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' }
option transformation
// 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' } } ] */
Inlinetransform
// 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' }
Inlinereplace
// 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' }