constructor-utils
Advanced tools
Comparing version 0.0.19 to 0.0.20
@@ -15,2 +15,3 @@ export declare type ConstructorFunction<T> = { | ||
plainToConstructorArray<T>(cls: ConstructorFunction<T>, json: Object[], options?: PlainToConstructorOptions): T[]; | ||
private getMethods(obj); | ||
private convert(cls, object, operationType, options?); | ||
@@ -17,0 +18,0 @@ private isSkipped(target, propertyName, operationType); |
@@ -34,2 +34,20 @@ "use strict"; | ||
// ------------------------------------------------------------------------- | ||
ConstructorUtils.prototype.getMethods = function (obj) { | ||
var ret = Object.getOwnPropertyNames(obj); | ||
while (true) { | ||
obj = Object.getPrototypeOf(obj); | ||
var arr = void 0; | ||
try { | ||
arr = Object.getOwnPropertyNames(obj); | ||
} | ||
catch (e) { | ||
break; | ||
} | ||
for (var i = 0; i < arr.length; i++) { | ||
if (ret.indexOf(arr[i]) == -1) | ||
ret.push(arr[i]); | ||
} | ||
} | ||
return ret; | ||
}; | ||
ConstructorUtils.prototype.convert = function (cls, object, operationType, options) { | ||
@@ -49,3 +67,19 @@ var _this = this; | ||
} | ||
var keys = Object.keys(object).concat(Object.getOwnPropertyNames(cls.prototype)); | ||
var keys = Object.keys(object); | ||
if (operationType === "constructorToPlain") { | ||
keys = keys.concat(this.getMethods(cls.prototype)).filter(function (key) { return [ | ||
'constructor', | ||
'toString', | ||
'toLocaleString', | ||
'valueOf', | ||
'hasOwnProperty', | ||
'isPrototypeOf', | ||
'propertyIsEnumerable', | ||
'__defineGetter__', | ||
'__lookupGetter__', | ||
'__defineSetter__', | ||
'__lookupSetter__', | ||
'__proto__' | ||
].indexOf(key) === -1; }); | ||
} | ||
var _loop_1 = function(key) { | ||
@@ -60,13 +94,3 @@ if (this_1.isSkipped(cls, key, operationType)) | ||
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) { | ||
if (!(object[key] instanceof Array) && object[key] instanceof Object && object[key].constructor) { | ||
type_1 = object[key].constructor; | ||
@@ -80,12 +104,19 @@ } | ||
newObject[key] = reflectedType && operationType === "plainToConstructor" ? new reflectedType() : []; | ||
if (object[key].length > 0 && type_1) { | ||
if (object[key].length > 0) { | ||
object[key].forEach(function (arrayItem) { | ||
newObject[key].push(_this.convert(type_1, arrayItem, operationType)); | ||
if (!type_1 && operationType === "constructorToPlain" && arrayItem instanceof Object && arrayItem.constructor) { | ||
type_1 = arrayItem.constructor; | ||
} | ||
if (type_1) { | ||
newObject[key].push(_this.convert(type_1, arrayItem, operationType)); | ||
} | ||
else { | ||
newObject[key].push(arrayItem); | ||
} | ||
}); | ||
} | ||
else { | ||
object[key].forEach(function (arrayItem) { | ||
} /*else { | ||
object[key].forEach((arrayItem: any) => { | ||
newObject[key].push(arrayItem); | ||
}); | ||
} | ||
}*/ | ||
} | ||
@@ -92,0 +123,0 @@ else if (object[key] instanceof Date && type_1 === Date && operationType === "constructorToPlain") { |
{ | ||
"name": "constructor-utils", | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"description": "Constructor utilities to simplify work with classes and constructors. Provides plain javascript objects to classes transformation utilities.", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -8,3 +8,3 @@ # constructor-utils | ||
**0.0.18 >> 0.0.19** | ||
**0.0.18 >> 0.0.20** | ||
@@ -11,0 +11,0 @@ * fixed bug when getters are not converted with es6 target |
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
31425
509