custom-automapper
Version:
A powerful, type-safe object mapping library for TypeScript and NestJS
131 lines • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MappingProfile = exports.MappingBuilder = void 0;
class MappingBuilder {
constructor(mapper, source, destination) {
this.mapper = mapper;
this.source = source;
this.destination = destination;
this.config = {};
this.options = {};
}
forMember(destinationKey, mapperFn) {
this.config[destinationKey] = mapperFn;
return this;
}
forPath(destinationKey, sourcePath) {
this.config[destinationKey] = ((source) => {
const path = sourcePath.split('.');
let value = source;
for (const key of path) {
value = value?.[key];
}
return value;
});
return this;
}
ignore(destinationKey) {
this.config[destinationKey] = (() => undefined);
return this;
}
forMemberIf(destinationKey, condition, trueMapper, falseMapper) {
this.config[destinationKey] = ((source) => {
return condition(source)
? trueMapper(source)
: falseMapper
? falseMapper(source)
: undefined;
});
return this;
}
forNestedObject(destinationKey, sourceProperty, nestedDestClass) {
this.config[destinationKey] = ((source) => {
const value = sourceProperty(source);
if (!value)
return undefined;
return this.mapper.map(value, nestedDestClass);
});
return this;
}
forNestedArray(destinationKey, sourceProperty, itemDestClass) {
this.config[destinationKey] = ((source) => {
const value = sourceProperty(source);
if (!value || !Array.isArray(value))
return [];
return this.mapper.mapArray(value, itemDestClass);
});
return this;
}
transform(destinationKey, sourceProperty, transformer) {
this.config[destinationKey] = ((source) => {
const value = sourceProperty(source);
return transformer(value);
});
return this;
}
withDefault(destinationKey, sourceProperty, defaultValue) {
this.config[destinationKey] = ((source) => {
const value = sourceProperty(source);
return value ?? defaultValue;
});
return this;
}
concat(destinationKey, ...sourceProperties) {
this.config[destinationKey] = ((source) => {
return sourceProperties.map(fn => fn(source) || '').join(' ');
});
return this;
}
withOptions(options) {
this.options = { ...this.options, ...options };
return this;
}
skipNulls(skip = true) {
this.options.skipNulls = skip;
return this;
}
skipUndefined(skip = true) {
this.options.skipUndefined = skip;
return this;
}
deepClone(enable = true) {
this.options.deepClone = enable;
return this;
}
strict(enable = true) {
this.options.strict = enable;
return this;
}
beforeMap(hook) {
this.options.beforeMap = hook;
return this;
}
afterMap(hook) {
this.options.afterMap = hook;
return this;
}
register() {
this.mapper.createMap(this.source, this.destination, this.config, this.options);
return this.mapper;
}
reverseMap() {
this.register();
this.mapper.createReverseMap(this.source, this.destination);
return this.mapper;
}
}
exports.MappingBuilder = MappingBuilder;
class MappingProfile {
constructor(mapper) {
this.mapper = mapper;
this.configure();
}
createMap(source, destination) {
return new MappingBuilder(this.mapper, source, destination);
}
registerMap(source, destination, config, options) {
this.mapper.createMap(source, destination, config, options);
}
}
exports.MappingProfile = MappingProfile;
//# sourceMappingURL=mapping-profile.js.map