Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

constructor-utils

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

constructor-utils - npm Package Compare versions

Comparing version 0.0.19 to 0.0.20

1

ConstructorUtils.d.ts

@@ -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);

67

ConstructorUtils.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc