constructor-utils
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -12,2 +12,3 @@ export declare type ConstructorFunction<T> = { | ||
constructorToPlain<T>(object: T, options?: ConstructorToPlainOptions): any; | ||
plainToConstructorFromObject<T>(object: T, json: Object, options?: PlainToConstructorOptions): T; | ||
plainToConstructor<T>(cls: ConstructorFunction<T>, json: Object, options?: PlainToConstructorOptions): T; | ||
@@ -17,4 +18,4 @@ plainToConstructorArray<T>(cls: ConstructorFunction<T>, json: Object[], options?: PlainToConstructorOptions): T[]; | ||
private isSkipped(target, propertyName, operationType); | ||
private getType(target, propertyName); | ||
private getType(target, propertyName, object); | ||
private getReflectedType(target, propertyName); | ||
} |
@@ -19,2 +19,5 @@ "use strict"; | ||
}; | ||
ConstructorUtils.prototype.plainToConstructorFromObject = function (object, json, options) { | ||
return this.convert(object, json, "plainToConstructor", options); | ||
}; | ||
ConstructorUtils.prototype.plainToConstructor = function (cls, json, options) { | ||
@@ -36,3 +39,12 @@ return this.convert(cls, json, "plainToConstructor", options); | ||
return object; | ||
var newObject = operationType === "constructorToPlain" ? {} : new cls(); | ||
var newObject = {}; | ||
if (operationType === "plainToConstructor") { | ||
if (cls instanceof Function) { | ||
newObject = new cls(); | ||
} | ||
else { | ||
newObject = cls; | ||
cls = newObject.constructor; | ||
} | ||
} | ||
var _loop_1 = function(key) { | ||
@@ -45,3 +57,18 @@ if (this_1.isSkipped(cls, key, operationType)) | ||
return "continue"; | ||
var type_1 = this_1.getType(cls, key); | ||
var type_1 = this_1.getType(cls, key, newObject); | ||
if (!type_1 && operationType === "constructorToPlain") { | ||
if (object[key] instanceof Array && object[key].length > 0) { | ||
// also check if type of each element in the array is equal | ||
var nonEmptyObjects_1 = object[key].filter(function (i) { return i !== null && i !== undefined; }); | ||
var isEachInArrayHasSameType = nonEmptyObjects_1.every(function (i) { return nonEmptyObjects_1[0].constructor; }); | ||
if (isEachInArrayHasSameType) { | ||
var probablyType = nonEmptyObjects_1[0].constructor; | ||
if (probablyType !== String && probablyType !== Number && probablyType !== Boolean && probablyType !== Date) | ||
type_1 = probablyType; | ||
} | ||
} | ||
else if (object[key] instanceof Object && object[key].constructor) { | ||
type_1 = object[key].constructor; | ||
} | ||
} | ||
if (object[key] instanceof Array) { | ||
@@ -63,3 +90,3 @@ // if (object[key].length > 0 && !type && operationType === "plainToConstructor") | ||
} | ||
else if (object[key] instanceof Date && type_1 === Date) { | ||
else if (object[key] instanceof Date && type_1 === Date && operationType === "constructorToPlain") { | ||
newObject[key] = object[key].toISOString(); | ||
@@ -104,7 +131,7 @@ } | ||
}; | ||
ConstructorUtils.prototype.getType = function (target, propertyName) { | ||
ConstructorUtils.prototype.getType = function (target, propertyName, object) { | ||
if (!target) | ||
return undefined; | ||
var meta = MetadataStorage_1.defaultMetadataStorage.findTypeMetadata(target, propertyName); | ||
return meta ? meta.typeFunction() : undefined; | ||
return meta ? meta.typeFunction(object) : undefined; | ||
}; | ||
@@ -111,0 +138,0 @@ ConstructorUtils.prototype.getReflectedType = function (target, propertyName) { |
@@ -15,4 +15,4 @@ /** | ||
export declare function Skip(options?: { | ||
constructorToPlain: boolean; | ||
plainToConstructor: boolean; | ||
constructorToPlain?: boolean; | ||
plainToConstructor?: boolean; | ||
}): (target: any, key: string) => void; |
@@ -7,3 +7,4 @@ import { ConstructorUtils, ConstructorFunction, PlainToConstructorOptions, ConstructorToPlainOptions } from "./ConstructorUtils"; | ||
export declare function constructorToPlain<T>(object: T, options?: ConstructorToPlainOptions): any; | ||
export declare function plainToConstructorFromObject<T>(cls: T, json: Object, options?: PlainToConstructorOptions): T; | ||
export declare function plainToConstructor<T>(cls: ConstructorFunction<T>, json: Object, options?: PlainToConstructorOptions): T; | ||
export declare function plainToConstructorArray<T>(cls: ConstructorFunction<T>, json: Object[], options?: PlainToConstructorOptions): T[]; |
@@ -15,2 +15,6 @@ "use strict"; | ||
exports.constructorToPlain = constructorToPlain; | ||
function plainToConstructorFromObject(cls, json, options) { | ||
return constructorUtils.plainToConstructorFromObject(cls, json, options); | ||
} | ||
exports.plainToConstructorFromObject = plainToConstructorFromObject; | ||
function plainToConstructor(cls, json, options) { | ||
@@ -17,0 +21,0 @@ return constructorUtils.plainToConstructor(cls, json, options); |
import { PropertyMetadata } from "./PropertyMetadata"; | ||
export interface SkipOptions { | ||
constructorToPlain: boolean; | ||
plainToConstructor: boolean; | ||
constructorToPlain?: boolean; | ||
plainToConstructor?: boolean; | ||
} | ||
@@ -6,0 +6,0 @@ export declare class SkipMetadata extends PropertyMetadata { |
@@ -7,5 +7,5 @@ import { PropertyMetadata } from "./PropertyMetadata"; | ||
constructor(target: Function, key: string, reflectedType: any, typeFunction: () => Function, isArray: boolean); | ||
typeFunction: () => Function; | ||
typeFunction: (obj?: any) => Function; | ||
reflectedType: any; | ||
isArray: boolean; | ||
} |
{ | ||
"name": "constructor-utils", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Constructor utilities to simplify work with classes and constructors. Provides plain javascript objects to classes transformation utilities.", | ||
@@ -27,4 +27,2 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"es6-shim": "^0.35.0", | ||
"reflect-metadata": "^0.1.3" | ||
}, | ||
@@ -46,4 +44,6 @@ "devDependencies": { | ||
"typescript": "^1.8.9", | ||
"typings": "^0.7.9" | ||
"typings": "^0.7.9", | ||
"es6-shim": "^0.35.0", | ||
"reflect-metadata": "^0.1.3" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # constructor-utils | ||
* renamed `constructor-utils` to `constructor-utils` package namespace. | ||
* renamed `constructor-utils/constructor-utils` to `constructor-utils` package namespace. | ||
@@ -13,0 +13,0 @@ **0.0.15** |
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
30250
0
475
17
- Removedes6-shim@^0.35.0
- Removedreflect-metadata@^0.1.3
- Removedes6-shim@0.35.8(transitive)
- Removedreflect-metadata@0.1.14(transitive)