class-transformer
Advanced tools
Comparing version 0.1.0-beta.6 to 0.1.0-beta.7
@@ -8,23 +8,23 @@ "use strict"; | ||
var executor = new TransformOperationExecutor_1.TransformOperationExecutor("classToPlain", options || {}); | ||
return executor.transform(undefined, object, undefined, undefined, undefined); | ||
return executor.transform(undefined, object, undefined, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.classToPlainFromExist = function (object, plainObject, options) { | ||
var executor = new TransformOperationExecutor_1.TransformOperationExecutor("classToPlain", options || {}); | ||
return executor.transform(plainObject, object, undefined, undefined, undefined); | ||
return executor.transform(plainObject, object, undefined, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.plainToClass = function (cls, plain, options) { | ||
var executor = new TransformOperationExecutor_1.TransformOperationExecutor("plainToClass", options || {}); | ||
return executor.transform(undefined, plain, cls, undefined, undefined); | ||
return executor.transform(undefined, plain, cls, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.plainToClassFromExist = function (clsObject, plain, options) { | ||
var executor = new TransformOperationExecutor_1.TransformOperationExecutor("plainToClass", options || {}); | ||
return executor.transform(clsObject, plain, undefined, undefined, undefined); | ||
return executor.transform(clsObject, plain, undefined, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.classToClass = function (object, options) { | ||
var executor = new TransformOperationExecutor_1.TransformOperationExecutor("classToClass", options || {}); | ||
return executor.transform(undefined, object, undefined, undefined, undefined); | ||
return executor.transform(undefined, object, undefined, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.classToClassFromExist = function (object, fromObject, options) { | ||
var executor = new TransformOperationExecutor_1.TransformOperationExecutor("classToClass", options || {}); | ||
return executor.transform(fromObject, object, undefined, undefined, undefined); | ||
return executor.transform(fromObject, object, undefined, undefined, undefined, undefined); | ||
}; | ||
@@ -31,0 +31,0 @@ ClassTransformer.prototype.serialize = function (object, options) { |
{ | ||
"name": "class-transformer", | ||
"version": "0.1.0-beta.6", | ||
"version": "0.1.0-beta.7", | ||
"description": "Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -8,6 +8,5 @@ import { ClassTransformOptions } from "./ClassTransformOptions"; | ||
constructor(transformationType: TransformationType, options: ClassTransformOptions); | ||
transform(source: Object | Object[] | any, value: Object | Object[] | any, targetType: Function, arrayType: Function, fromProperty: string, level?: number): any; | ||
transform(source: Object | Object[] | any, value: Object | Object[] | any, targetType: Function, arrayType: Function, isMap: boolean, fromProperty: string, level?: number): any; | ||
private applyCustomTransformations(value, target, key); | ||
private isCircular(object, level); | ||
private getKeyType(newObject, object, target, key); | ||
private getReflectedType(target, propertyName); | ||
@@ -14,0 +13,0 @@ private getKeys(target, object); |
@@ -18,3 +18,3 @@ "use strict"; | ||
// ------------------------------------------------------------------------- | ||
TransformOperationExecutor.prototype.transform = function (source, value, targetType, arrayType, fromProperty, level) { | ||
TransformOperationExecutor.prototype.transform = function (source, value, targetType, arrayType, isMap, fromProperty, level) { | ||
var _this = this; | ||
@@ -27,3 +27,3 @@ if (level === void 0) { level = 0; } | ||
if (!_this.isCircular(subValue, level)) { | ||
var value_1 = _this.transform(subSource, subValue, targetType, undefined, String(index), level + 1); | ||
var value_1 = _this.transform(subSource, subValue, targetType, undefined, subValue instanceof Map, fromProperty + "[" + String(index) + "]", level + 1); | ||
if (newValue_1 instanceof Set) { | ||
@@ -47,12 +47,12 @@ newValue_1.add(value_1); | ||
} | ||
else if (targetType === String) { | ||
else if (targetType === String && !isMap) { | ||
return String(value); | ||
} | ||
else if (targetType === Number) { | ||
else if (targetType === Number && !isMap) { | ||
return Number(value); | ||
} | ||
else if (targetType === Boolean) { | ||
else if (targetType === Boolean && !isMap) { | ||
return Boolean(value); | ||
} | ||
else if (targetType === Date || value instanceof Date) { | ||
else if ((targetType === Date || value instanceof Date) && !isMap) { | ||
if (value instanceof Date) { | ||
@@ -74,5 +74,12 @@ return new Date(value.valueOf()); | ||
if (!source && (this.transformationType === "plainToClass" || this.transformationType === "classToClass")) { | ||
if (!targetType) | ||
throw new Error("Cannot determine type for " + targetType.name + "." + fromProperty + ", did you forget to specify a @Type?"); | ||
newValue = new targetType(); | ||
if (!targetType) { | ||
console.log(value); | ||
throw new Error("Cannot determine type for " + fromProperty + ", did you forget to specify a @Type?"); | ||
} | ||
if (isMap) { | ||
newValue = new Map(); | ||
} | ||
else { | ||
newValue = new targetType(); | ||
} | ||
} | ||
@@ -97,7 +104,3 @@ // traverse over keys | ||
} | ||
var type = this.getKeyType(newValue, value, targetType, propertyName); | ||
// if value is an array try to get its custom array type | ||
var arrayType_1 = value[valueKey] instanceof Array ? this.getReflectedType(targetType, propertyName) : undefined; | ||
// const subValueKey = operationType === "plainToClass" && newKeyName ? newKeyName : key; | ||
var subSource = source ? source[valueKey] : undefined; | ||
// get a subvalue | ||
var subValue = undefined; | ||
@@ -113,2 +116,19 @@ if (value instanceof Map) { | ||
} | ||
// determine a type | ||
var type = undefined, isSubValueMap = subValue instanceof Map; | ||
if (targetType && isMap) { | ||
type = targetType; | ||
} | ||
else if (targetType) { | ||
var metadata = index_1.defaultMetadataStorage.findTypeMetadata(targetType, propertyName); | ||
if (metadata) { | ||
var options = { newObject: newValue, object: value, property: propertyName }; | ||
type = metadata.typeFunction(options); | ||
isSubValueMap = isSubValueMap || metadata.reflectedType === Map; | ||
} | ||
} | ||
// if value is an array try to get its custom array type | ||
var arrayType_1 = value[valueKey] instanceof Array ? this.getReflectedType(targetType, propertyName) : undefined; | ||
// const subValueKey = operationType === "plainToClass" && newKeyName ? newKeyName : key; | ||
var subSource = source ? source[valueKey] : undefined; | ||
// if its deserialization then type if required | ||
@@ -127,3 +147,3 @@ // if we uncomment this types like string[] will not work | ||
if (!this.isCircular(subValue, level)) { | ||
var finalValue = this.transform(subSource, subValue, type, arrayType_1, propertyName, level + 1); | ||
var finalValue = this.transform(subSource, subValue, type, arrayType_1, isSubValueMap, targetType.name + "." + propertyName, level + 1); | ||
finalValue = this.applyCustomTransformations(finalValue, targetType, key); | ||
@@ -189,9 +209,2 @@ if (newValue instanceof Map) { | ||
}; | ||
TransformOperationExecutor.prototype.getKeyType = function (newObject, object, target, key) { | ||
if (!target) | ||
return undefined; | ||
var metadata = index_1.defaultMetadataStorage.findTypeMetadata(target, key); | ||
var options = { newObject: newObject, object: object, property: key }; | ||
return metadata ? metadata.typeFunction(options) : undefined; | ||
}; | ||
TransformOperationExecutor.prototype.getReflectedType = function (target, propertyName) { | ||
@@ -198,0 +211,0 @@ if (!target) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
123762
923