class-transformer
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -5,3 +5,3 @@ (function (global, factory) { | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ClassTransformer = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
})(this, (function (exports) { 'use strict'; | ||
@@ -263,2 +263,11 @@ exports.TransformationType = void 0; | ||
var __spreadArray = (undefined && undefined.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
function instantiateArrayType(arrayType) { | ||
@@ -383,3 +392,7 @@ var array = new arrayType(); | ||
if (!targetType && value.constructor !== Object /* && TransformationType === TransformationType.CLASS_TO_PLAIN*/) | ||
targetType = value.constructor; | ||
if (!Array.isArray(value) && value.constructor === Array) ; | ||
else { | ||
// We are good we can use the built-in constructor | ||
targetType = value.constructor; | ||
} | ||
if (!targetType && source) | ||
@@ -430,10 +443,21 @@ targetType = source.constructor; | ||
var subValue = undefined; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
if (this_1.transformationType === exports.TransformationType.PLAIN_TO_CLASS) { | ||
/** | ||
* This section is added for the following report: | ||
* https://github.com/typestack/class-transformer/issues/596 | ||
* | ||
* We should not call functions or constructors when transforming to class. | ||
*/ | ||
subValue = value[valueKey]; | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
} | ||
} | ||
@@ -472,3 +496,5 @@ // determine a type | ||
if (this_1.transformationType === exports.TransformationType.CLASS_TO_PLAIN) { | ||
subValue[metadata_1.options.discriminator.property] = metadata_1.options.discriminator.subTypes.find(function (subType) { return subType.value === subValue.constructor; }).name; | ||
if (subValue) { | ||
subValue[metadata_1.options.discriminator.property] = metadata_1.options.discriminator.subTypes.find(function (subType) { return subType.value === subValue.constructor; }).name; | ||
} | ||
} | ||
@@ -639,2 +665,11 @@ } | ||
} | ||
/** | ||
* If decorators are ignored but we don't want the extraneous values, then we use the | ||
* metadata to decide which property is needed, but doesn't apply the decorator effect. | ||
*/ | ||
if (this.options.ignoreDecorators && this.options.excludeExtraneousValues && target) { | ||
var exposedProperties = defaultMetadataStorage.getExposedProperties(target, this.transformationType); | ||
var excludedProperties = defaultMetadataStorage.getExcludedProperties(target, this.transformationType); | ||
keys = __spreadArray(__spreadArray([], exposedProperties, true), excludedProperties, true); | ||
} | ||
if (!this.options.ignoreDecorators && target) { | ||
@@ -754,3 +789,3 @@ // add all exposed to list of keys | ||
} | ||
ClassTransformer.prototype.classToPlain = function (object, options) { | ||
ClassTransformer.prototype.instanceToPlain = function (object, options) { | ||
var executor = new TransformOperationExecutor(exports.TransformationType.CLASS_TO_PLAIN, __assign(__assign({}, defaultOptions), options)); | ||
@@ -763,3 +798,3 @@ return executor.transform(undefined, object, undefined, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.plainToClass = function (cls, plain, options) { | ||
ClassTransformer.prototype.plainToInstance = function (cls, plain, options) { | ||
var executor = new TransformOperationExecutor(exports.TransformationType.PLAIN_TO_CLASS, __assign(__assign({}, defaultOptions), options)); | ||
@@ -772,3 +807,3 @@ return executor.transform(undefined, plain, cls, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.classToClass = function (object, options) { | ||
ClassTransformer.prototype.instanceToInstance = function (object, options) { | ||
var executor = new TransformOperationExecutor(exports.TransformationType.CLASS_TO_CLASS, __assign(__assign({}, defaultOptions), options)); | ||
@@ -782,3 +817,3 @@ return executor.transform(undefined, object, undefined, undefined, undefined, undefined); | ||
ClassTransformer.prototype.serialize = function (object, options) { | ||
return JSON.stringify(this.classToPlain(object, options)); | ||
return JSON.stringify(this.instanceToPlain(object, options)); | ||
}; | ||
@@ -790,3 +825,3 @@ /** | ||
var jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
}; | ||
@@ -798,3 +833,3 @@ /** | ||
var jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
}; | ||
@@ -857,3 +892,3 @@ return ClassTransformer; | ||
*/ | ||
function TransformClassToClass(params) { | ||
function TransformInstanceToInstance(params) { | ||
return function (target, propertyKey, descriptor) { | ||
@@ -870,4 +905,4 @@ var classTransformer = new ClassTransformer(); | ||
return isPromise | ||
? result.then(function (data) { return classTransformer.classToClass(data, params); }) | ||
: classTransformer.classToClass(result, params); | ||
? result.then(function (data) { return classTransformer.instanceToInstance(data, params); }) | ||
: classTransformer.instanceToInstance(result, params); | ||
}; | ||
@@ -882,3 +917,3 @@ }; | ||
*/ | ||
function TransformClassToPlain(params) { | ||
function TransformInstanceToPlain(params) { | ||
return function (target, propertyKey, descriptor) { | ||
@@ -895,4 +930,4 @@ var classTransformer = new ClassTransformer(); | ||
return isPromise | ||
? result.then(function (data) { return classTransformer.classToPlain(data, params); }) | ||
: classTransformer.classToPlain(result, params); | ||
? result.then(function (data) { return classTransformer.instanceToPlain(data, params); }) | ||
: classTransformer.instanceToPlain(result, params); | ||
}; | ||
@@ -907,3 +942,3 @@ }; | ||
*/ | ||
function TransformPlainToClass(classType, params) { | ||
function TransformPlainToInstance(classType, params) { | ||
return function (target, propertyKey, descriptor) { | ||
@@ -920,4 +955,4 @@ var classTransformer = new ClassTransformer(); | ||
return isPromise | ||
? result.then(function (data) { return classTransformer.plainToClass(classType, data, params); }) | ||
: classTransformer.plainToClass(classType, result, params); | ||
? result.then(function (data) { return classTransformer.plainToInstance(classType, data, params); }) | ||
: classTransformer.plainToInstance(classType, result, params); | ||
}; | ||
@@ -965,4 +1000,4 @@ }; | ||
var classTransformer = new ClassTransformer(); | ||
function classToPlain(object, options) { | ||
return classTransformer.classToPlain(object, options); | ||
function instanceToPlain(object, options) { | ||
return classTransformer.instanceToPlain(object, options); | ||
} | ||
@@ -972,4 +1007,4 @@ function classToPlainFromExist(object, plainObject, options) { | ||
} | ||
function plainToClass(cls, plain, options) { | ||
return classTransformer.plainToClass(cls, plain, options); | ||
function plainToInstance(cls, plain, options) { | ||
return classTransformer.plainToInstance(cls, plain, options); | ||
} | ||
@@ -979,4 +1014,4 @@ function plainToClassFromExist(clsObject, plain, options) { | ||
} | ||
function classToClass(object, options) { | ||
return classTransformer.classToClass(object, options); | ||
function instanceToInstance(object, options) { | ||
return classTransformer.instanceToInstance(object, options); | ||
} | ||
@@ -991,2 +1026,7 @@ function classToClassFromExist(object, fromObject, options) { | ||
* Deserializes given JSON string to a object of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* instanceToClass(cls, JSON.parse(json), options) | ||
* ``` | ||
*/ | ||
@@ -998,2 +1038,8 @@ function deserialize(cls, json, options) { | ||
* Deserializes given JSON string to an array of objects of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* JSON.parse(json).map(value => instanceToClass(cls, value, options)) | ||
* ``` | ||
* | ||
*/ | ||
@@ -1008,14 +1054,14 @@ function deserializeArray(cls, json, options) { | ||
exports.Transform = Transform; | ||
exports.TransformClassToClass = TransformClassToClass; | ||
exports.TransformClassToPlain = TransformClassToPlain; | ||
exports.TransformPlainToClass = TransformPlainToClass; | ||
exports.TransformInstanceToInstance = TransformInstanceToInstance; | ||
exports.TransformInstanceToPlain = TransformInstanceToPlain; | ||
exports.TransformPlainToInstance = TransformPlainToInstance; | ||
exports.Type = Type; | ||
exports.classToClass = classToClass; | ||
exports.classToClassFromExist = classToClassFromExist; | ||
exports.classToPlain = classToPlain; | ||
exports.classToPlainFromExist = classToPlainFromExist; | ||
exports.deserialize = deserialize; | ||
exports.deserializeArray = deserializeArray; | ||
exports.plainToClass = plainToClass; | ||
exports.instanceToInstance = instanceToInstance; | ||
exports.instanceToPlain = instanceToPlain; | ||
exports.plainToClassFromExist = plainToClassFromExist; | ||
exports.plainToInstance = plainToInstance; | ||
exports.serialize = serialize; | ||
@@ -1025,3 +1071,3 @@ | ||
}))); | ||
})); | ||
//# sourceMappingURL=class-transformer.umd.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((t="undefined"!=typeof globalThis?globalThis:t||self).ClassTransformer={})}(this,(function(t){"use strict";var o;t.TransformationType=void 0,(o=t.TransformationType||(t.TransformationType={}))[o.PLAIN_TO_CLASS=0]="PLAIN_TO_CLASS",o[o.CLASS_TO_PLAIN=1]="CLASS_TO_PLAIN",o[o.CLASS_TO_CLASS=2]="CLASS_TO_CLASS";var e=new(function(){function o(){this._typeMetadatas=new Map,this._transformMetadatas=new Map,this._exposeMetadatas=new Map,this._excludeMetadatas=new Map,this._ancestorsMap=new Map}return o.prototype.addTypeMetadata=function(t){this._typeMetadatas.has(t.target)||this._typeMetadatas.set(t.target,new Map),this._typeMetadatas.get(t.target).set(t.propertyName,t)},o.prototype.addTransformMetadata=function(t){this._transformMetadatas.has(t.target)||this._transformMetadatas.set(t.target,new Map),this._transformMetadatas.get(t.target).has(t.propertyName)||this._transformMetadatas.get(t.target).set(t.propertyName,[]),this._transformMetadatas.get(t.target).get(t.propertyName).push(t)},o.prototype.addExposeMetadata=function(t){this._exposeMetadatas.has(t.target)||this._exposeMetadatas.set(t.target,new Map),this._exposeMetadatas.get(t.target).set(t.propertyName,t)},o.prototype.addExcludeMetadata=function(t){this._excludeMetadatas.has(t.target)||this._excludeMetadatas.set(t.target,new Map),this._excludeMetadatas.get(t.target).set(t.propertyName,t)},o.prototype.findTransformMetadatas=function(o,e,n){return this.findMetadatas(this._transformMetadatas,o,e).filter((function(o){return!o.options||(!0===o.options.toClassOnly&&!0===o.options.toPlainOnly||(!0===o.options.toClassOnly?n===t.TransformationType.CLASS_TO_CLASS||n===t.TransformationType.PLAIN_TO_CLASS:!0!==o.options.toPlainOnly||n===t.TransformationType.CLASS_TO_PLAIN))}))},o.prototype.findExcludeMetadata=function(t,o){return this.findMetadata(this._excludeMetadatas,t,o)},o.prototype.findExposeMetadata=function(t,o){return this.findMetadata(this._exposeMetadatas,t,o)},o.prototype.findExposeMetadataByCustomName=function(t,o){return this.getExposedMetadatas(t).find((function(t){return t.options&&t.options.name===o}))},o.prototype.findTypeMetadata=function(t,o){return this.findMetadata(this._typeMetadatas,t,o)},o.prototype.getStrategy=function(t){var o=this._excludeMetadatas.get(t),e=o&&o.get(void 0),n=this._exposeMetadatas.get(t),r=n&&n.get(void 0);return e&&r||!e&&!r?"none":e?"excludeAll":"exposeAll"},o.prototype.getExposedMetadatas=function(t){return this.getMetadata(this._exposeMetadatas,t)},o.prototype.getExcludedMetadatas=function(t){return this.getMetadata(this._excludeMetadatas,t)},o.prototype.getExposedProperties=function(o,e){return this.getExposedMetadatas(o).filter((function(o){return!o.options||(!0===o.options.toClassOnly&&!0===o.options.toPlainOnly||(!0===o.options.toClassOnly?e===t.TransformationType.CLASS_TO_CLASS||e===t.TransformationType.PLAIN_TO_CLASS:!0!==o.options.toPlainOnly||e===t.TransformationType.CLASS_TO_PLAIN))})).map((function(t){return t.propertyName}))},o.prototype.getExcludedProperties=function(o,e){return this.getExcludedMetadatas(o).filter((function(o){return!o.options||(!0===o.options.toClassOnly&&!0===o.options.toPlainOnly||(!0===o.options.toClassOnly?e===t.TransformationType.CLASS_TO_CLASS||e===t.TransformationType.PLAIN_TO_CLASS:!0!==o.options.toPlainOnly||e===t.TransformationType.CLASS_TO_PLAIN))})).map((function(t){return t.propertyName}))},o.prototype.clear=function(){this._typeMetadatas.clear(),this._exposeMetadatas.clear(),this._excludeMetadatas.clear(),this._ancestorsMap.clear()},o.prototype.getMetadata=function(t,o){var e,n=t.get(o);n&&(e=Array.from(n.values()).filter((function(t){return void 0!==t.propertyName})));for(var r=[],a=0,i=this.getAncestors(o);a<i.length;a++){var s=i[a],p=t.get(s);if(p){var f=Array.from(p.values()).filter((function(t){return void 0!==t.propertyName}));r.push.apply(r,f)}}return r.concat(e||[])},o.prototype.findMetadata=function(t,o,e){var n=t.get(o);if(n){var r=n.get(e);if(r)return r}for(var a=0,i=this.getAncestors(o);a<i.length;a++){var s=i[a],p=t.get(s);if(p){var f=p.get(e);if(f)return f}}},o.prototype.findMetadatas=function(t,o,e){var n,r=t.get(o);r&&(n=r.get(e));for(var a=[],i=0,s=this.getAncestors(o);i<s.length;i++){var p=s[i],f=t.get(p);f&&f.has(e)&&a.push.apply(a,f.get(e))}return a.slice().reverse().concat((n||[]).slice().reverse())},o.prototype.getAncestors=function(t){if(!t)return[];if(!this._ancestorsMap.has(t)){for(var o=[],e=Object.getPrototypeOf(t.prototype.constructor);void 0!==e.prototype;e=Object.getPrototypeOf(e.prototype.constructor))o.push(e);this._ancestorsMap.set(t,o)}return this._ancestorsMap.get(t)},o}());var n=function(){function o(t,o){this.transformationType=t,this.options=o,this.recursionStack=new Set}return o.prototype.transform=function(o,n,r,a,i,s){var p,f=this;if(void 0===s&&(s=0),Array.isArray(n)||n instanceof Set){var u=a&&this.transformationType===t.TransformationType.PLAIN_TO_CLASS?function(t){var o=new t;return o instanceof Set||"push"in o?o:[]}(a):[];return n.forEach((function(e,n){var a=o?o[n]:void 0;if(f.options.enableCircularCheck&&f.isCircular(e))f.transformationType===t.TransformationType.CLASS_TO_CLASS&&(u instanceof Set?u.add(e):u.push(e));else{var i=void 0;if("function"!=typeof r&&r&&r.options&&r.options.discriminator&&r.options.discriminator.property&&r.options.discriminator.subTypes){if(f.transformationType===t.TransformationType.PLAIN_TO_CLASS){i=r.options.discriminator.subTypes.find((function(t){return t.name===e[r.options.discriminator.property]}));var p={newObject:u,object:e,property:void 0},c=r.typeFunction(p);i=void 0===i?c:i.value,r.options.keepDiscriminatorProperty||delete e[r.options.discriminator.property]}f.transformationType===t.TransformationType.CLASS_TO_CLASS&&(i=e.constructor),f.transformationType===t.TransformationType.CLASS_TO_PLAIN&&(e[r.options.discriminator.property]=r.options.discriminator.subTypes.find((function(t){return t.value===e.constructor})).name)}else i=r;var l=f.transform(a,e,i,void 0,e instanceof Map,s+1);u instanceof Set?u.add(l):u.push(l)}})),u}if(r!==String||i){if(r!==Number||i){if(r!==Boolean||i){if((r===Date||n instanceof Date)&&!i)return n instanceof Date?new Date(n.valueOf()):null==n?n:new Date(n);if(("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:void 0).Buffer&&(r===Buffer||n instanceof Buffer)&&!i)return null==n?n:Buffer.from(n);if(null===(p=n)||"object"!=typeof p||"function"!=typeof p.then||i){if(i||null===n||"object"!=typeof n||"function"!=typeof n.then){if("object"==typeof n&&null!==n){r||n.constructor===Object||(r=n.constructor),!r&&o&&(r=o.constructor),this.options.enableCircularCheck&&this.recursionStack.add(n);var c=this.getKeys(r,n,i),l=o||{};o||this.transformationType!==t.TransformationType.PLAIN_TO_CLASS&&this.transformationType!==t.TransformationType.CLASS_TO_CLASS||(l=i?new Map:r?new r:{});for(var d=function(a){if("__proto__"===a||"constructor"===a)return"continue";var p=a,f=a,u=a;if(!y.options.ignoreDecorators&&r)if(y.transformationType===t.TransformationType.PLAIN_TO_CLASS)(c=e.findExposeMetadataByCustomName(r,a))&&(u=c.propertyName,f=c.propertyName);else if(y.transformationType===t.TransformationType.CLASS_TO_PLAIN||y.transformationType===t.TransformationType.CLASS_TO_CLASS){var c;(c=e.findExposeMetadata(r,a))&&c.options&&c.options.name&&(f=c.options.name)}var d=void 0;d=n instanceof Map?n.get(p):n[p]instanceof Function?n[p]():n[p];var T=void 0,m=d instanceof Map;if(r&&i)T=r;else if(r){var v=e.findTypeMetadata(r,u);if(v){var h={newObject:l,object:n,property:u},S=v.typeFunction?v.typeFunction(h):v.reflectedType;v.options&&v.options.discriminator&&v.options.discriminator.property&&v.options.discriminator.subTypes?n[p]instanceof Array?T=v:(y.transformationType===t.TransformationType.PLAIN_TO_CLASS&&(T=void 0===(T=v.options.discriminator.subTypes.find((function(t){if(d&&d instanceof Object&&v.options.discriminator.property in d)return t.name===d[v.options.discriminator.property]})))?S:T.value,v.options.keepDiscriminatorProperty||d&&d instanceof Object&&v.options.discriminator.property in d&&delete d[v.options.discriminator.property]),y.transformationType===t.TransformationType.CLASS_TO_CLASS&&(T=d.constructor),y.transformationType===t.TransformationType.CLASS_TO_PLAIN&&(d[v.options.discriminator.property]=v.options.discriminator.subTypes.find((function(t){return t.value===d.constructor})).name)):T=S,m=m||v.reflectedType===Map}else if(y.options.targetMaps)y.options.targetMaps.filter((function(t){return t.target===r&&!!t.properties[u]})).forEach((function(t){return T=t.properties[u]}));else if(y.options.enableImplicitConversion&&y.transformationType===t.TransformationType.PLAIN_TO_CLASS){var _=Reflect.getMetadata("design:type",r.prototype,u);_&&(T=_)}}var g=Array.isArray(n[p])?y.getReflectedType(r,u):void 0,C=o?o[p]:void 0;if(l.constructor.prototype){var A=Object.getOwnPropertyDescriptor(l.constructor.prototype,f);if((y.transformationType===t.TransformationType.PLAIN_TO_CLASS||y.transformationType===t.TransformationType.CLASS_TO_CLASS)&&(A&&!A.set||l[f]instanceof Function))return"continue"}if(y.options.enableCircularCheck&&y.isCircular(d)){if(y.transformationType===t.TransformationType.CLASS_TO_CLASS){L=d;(void 0!==(L=y.applyCustomTransformations(L,r,a,n,y.transformationType))||y.options.exposeUnsetFields)&&(l instanceof Map?l.set(f,L):l[f]=L)}}else{var M=y.transformationType===t.TransformationType.PLAIN_TO_CLASS?f:a,L=void 0;y.transformationType===t.TransformationType.CLASS_TO_PLAIN?(L=n[M],L=y.applyCustomTransformations(L,r,M,n,y.transformationType),L=n[M]===L?d:L,L=y.transform(C,L,T,g,m,s+1)):void 0===d&&y.options.exposeDefaultValues?L=l[f]:(L=y.transform(C,d,T,g,m,s+1),L=y.applyCustomTransformations(L,r,M,n,y.transformationType)),(void 0!==L||y.options.exposeUnsetFields)&&(l instanceof Map?l.set(f,L):l[f]=L)}},y=this,T=0,m=c;T<m.length;T++){d(m[T])}return this.options.enableCircularCheck&&this.recursionStack.delete(n),l}return n}return n}return new Promise((function(t,o){n.then((function(o){return t(f.transform(void 0,o,r,void 0,void 0,s+1))}),o)}))}return null==n?n:Boolean(n)}return null==n?n:Number(n)}return null==n?n:String(n)},o.prototype.applyCustomTransformations=function(t,o,n,r,a){var i=this,s=e.findTransformMetadatas(o,n,this.transformationType);return void 0!==this.options.version&&(s=s.filter((function(t){return!t.options||i.checkVersion(t.options.since,t.options.until)}))),(s=this.options.groups&&this.options.groups.length?s.filter((function(t){return!t.options||i.checkGroups(t.options.groups)})):s.filter((function(t){return!t.options||!t.options.groups||!t.options.groups.length}))).forEach((function(o){t=o.transformFn({value:t,key:n,obj:r,type:a,options:i.options})})),t},o.prototype.isCircular=function(t){return this.recursionStack.has(t)},o.prototype.getReflectedType=function(t,o){if(t){var n=e.findTypeMetadata(t,o);return n?n.reflectedType:void 0}},o.prototype.getKeys=function(o,n,r){var a=this,i=e.getStrategy(o);"none"===i&&(i=this.options.strategy||"exposeAll");var s=[];if(("exposeAll"===i||r)&&(s=n instanceof Map?Array.from(n.keys()):Object.keys(n)),r)return s;if(!this.options.ignoreDecorators&&o){var p=e.getExposedProperties(o,this.transformationType);this.transformationType===t.TransformationType.PLAIN_TO_CLASS&&(p=p.map((function(t){var n=e.findExposeMetadata(o,t);return n&&n.options&&n.options.name?n.options.name:t}))),s=this.options.excludeExtraneousValues?p:s.concat(p);var f=e.getExcludedProperties(o,this.transformationType);f.length>0&&(s=s.filter((function(t){return!f.includes(t)}))),void 0!==this.options.version&&(s=s.filter((function(t){var n=e.findExposeMetadata(o,t);return!n||!n.options||a.checkVersion(n.options.since,n.options.until)}))),s=this.options.groups&&this.options.groups.length?s.filter((function(t){var n=e.findExposeMetadata(o,t);return!n||!n.options||a.checkGroups(n.options.groups)})):s.filter((function(t){var n=e.findExposeMetadata(o,t);return!(n&&n.options&&n.options.groups&&n.options.groups.length)}))}return this.options.excludePrefixes&&this.options.excludePrefixes.length&&(s=s.filter((function(t){return a.options.excludePrefixes.every((function(o){return t.substr(0,o.length)!==o}))}))),s=s.filter((function(t,o,e){return e.indexOf(t)===o}))},o.prototype.checkVersion=function(t,o){var e=!0;return e&&t&&(e=this.options.version>=t),e&&o&&(e=this.options.version<o),e},o.prototype.checkGroups=function(t){return!t||this.options.groups.some((function(o){return t.includes(o)}))},o}(),r={enableCircularCheck:!1,enableImplicitConversion:!1,excludeExtraneousValues:!1,excludePrefixes:void 0,exposeDefaultValues:!1,exposeUnsetFields:!0,groups:void 0,ignoreDecorators:!1,strategy:void 0,targetMaps:void 0,version:void 0},a=function(){return(a=Object.assign||function(t){for(var o,e=1,n=arguments.length;e<n;e++)for(var r in o=arguments[e])Object.prototype.hasOwnProperty.call(o,r)&&(t[r]=o[r]);return t}).apply(this,arguments)},i=function(){function o(){}return o.prototype.classToPlain=function(o,e){return new n(t.TransformationType.CLASS_TO_PLAIN,a(a({},r),e)).transform(void 0,o,void 0,void 0,void 0,void 0)},o.prototype.classToPlainFromExist=function(o,e,i){return new n(t.TransformationType.CLASS_TO_PLAIN,a(a({},r),i)).transform(e,o,void 0,void 0,void 0,void 0)},o.prototype.plainToClass=function(o,e,i){return new n(t.TransformationType.PLAIN_TO_CLASS,a(a({},r),i)).transform(void 0,e,o,void 0,void 0,void 0)},o.prototype.plainToClassFromExist=function(o,e,i){return new n(t.TransformationType.PLAIN_TO_CLASS,a(a({},r),i)).transform(o,e,void 0,void 0,void 0,void 0)},o.prototype.classToClass=function(o,e){return new n(t.TransformationType.CLASS_TO_CLASS,a(a({},r),e)).transform(void 0,o,void 0,void 0,void 0,void 0)},o.prototype.classToClassFromExist=function(o,e,i){return new n(t.TransformationType.CLASS_TO_CLASS,a(a({},r),i)).transform(e,o,void 0,void 0,void 0,void 0)},o.prototype.serialize=function(t,o){return JSON.stringify(this.classToPlain(t,o))},o.prototype.deserialize=function(t,o,e){var n=JSON.parse(o);return this.plainToClass(t,n,e)},o.prototype.deserializeArray=function(t,o,e){var n=JSON.parse(o);return this.plainToClass(t,n,e)},o}();var s=new i;t.ClassTransformer=i,t.Exclude=function(t){return void 0===t&&(t={}),function(o,n){e.addExcludeMetadata({target:o instanceof Function?o:o.constructor,propertyName:n,options:t})}},t.Expose=function(t){return void 0===t&&(t={}),function(o,n){e.addExposeMetadata({target:o instanceof Function?o:o.constructor,propertyName:n,options:t})}},t.Transform=function(t,o){return void 0===o&&(o={}),function(n,r){e.addTransformMetadata({target:n.constructor,propertyName:r,transformFn:t,options:o})}},t.TransformClassToClass=function(t){return function(o,e,n){var r=new i,a=n.value;n.value=function(){for(var o=[],e=0;e<arguments.length;e++)o[e]=arguments[e];var n=a.apply(this,o),i=!!n&&("object"==typeof n||"function"==typeof n)&&"function"==typeof n.then;return i?n.then((function(o){return r.classToClass(o,t)})):r.classToClass(n,t)}}},t.TransformClassToPlain=function(t){return function(o,e,n){var r=new i,a=n.value;n.value=function(){for(var o=[],e=0;e<arguments.length;e++)o[e]=arguments[e];var n=a.apply(this,o),i=!!n&&("object"==typeof n||"function"==typeof n)&&"function"==typeof n.then;return i?n.then((function(o){return r.classToPlain(o,t)})):r.classToPlain(n,t)}}},t.TransformPlainToClass=function(t,o){return function(e,n,r){var a=new i,s=r.value;r.value=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=s.apply(this,e),i=!!r&&("object"==typeof r||"function"==typeof r)&&"function"==typeof r.then;return i?r.then((function(e){return a.plainToClass(t,e,o)})):a.plainToClass(t,r,o)}}},t.Type=function(t,o){return void 0===o&&(o={}),function(n,r){var a=Reflect.getMetadata("design:type",n,r);e.addTypeMetadata({target:n.constructor,propertyName:r,reflectedType:a,typeFunction:t,options:o})}},t.classToClass=function(t,o){return s.classToClass(t,o)},t.classToClassFromExist=function(t,o,e){return s.classToClassFromExist(t,o,e)},t.classToPlain=function(t,o){return s.classToPlain(t,o)},t.classToPlainFromExist=function(t,o,e){return s.classToPlainFromExist(t,o,e)},t.deserialize=function(t,o,e){return s.deserialize(t,o,e)},t.deserializeArray=function(t,o,e){return s.deserializeArray(t,o,e)},t.plainToClass=function(t,o,e){return s.plainToClass(t,o,e)},t.plainToClassFromExist=function(t,o,e){return s.plainToClassFromExist(t,o,e)},t.serialize=function(t,o){return s.serialize(t,o)},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((t="undefined"!=typeof globalThis?globalThis:t||self).ClassTransformer={})}(this,(function(t){"use strict";var o;t.TransformationType=void 0,(o=t.TransformationType||(t.TransformationType={}))[o.PLAIN_TO_CLASS=0]="PLAIN_TO_CLASS",o[o.CLASS_TO_PLAIN=1]="CLASS_TO_PLAIN",o[o.CLASS_TO_CLASS=2]="CLASS_TO_CLASS";var e=new(function(){function o(){this._typeMetadatas=new Map,this._transformMetadatas=new Map,this._exposeMetadatas=new Map,this._excludeMetadatas=new Map,this._ancestorsMap=new Map}return o.prototype.addTypeMetadata=function(t){this._typeMetadatas.has(t.target)||this._typeMetadatas.set(t.target,new Map),this._typeMetadatas.get(t.target).set(t.propertyName,t)},o.prototype.addTransformMetadata=function(t){this._transformMetadatas.has(t.target)||this._transformMetadatas.set(t.target,new Map),this._transformMetadatas.get(t.target).has(t.propertyName)||this._transformMetadatas.get(t.target).set(t.propertyName,[]),this._transformMetadatas.get(t.target).get(t.propertyName).push(t)},o.prototype.addExposeMetadata=function(t){this._exposeMetadatas.has(t.target)||this._exposeMetadatas.set(t.target,new Map),this._exposeMetadatas.get(t.target).set(t.propertyName,t)},o.prototype.addExcludeMetadata=function(t){this._excludeMetadatas.has(t.target)||this._excludeMetadatas.set(t.target,new Map),this._excludeMetadatas.get(t.target).set(t.propertyName,t)},o.prototype.findTransformMetadatas=function(o,e,n){return this.findMetadatas(this._transformMetadatas,o,e).filter((function(o){return!o.options||(!0===o.options.toClassOnly&&!0===o.options.toPlainOnly||(!0===o.options.toClassOnly?n===t.TransformationType.CLASS_TO_CLASS||n===t.TransformationType.PLAIN_TO_CLASS:!0!==o.options.toPlainOnly||n===t.TransformationType.CLASS_TO_PLAIN))}))},o.prototype.findExcludeMetadata=function(t,o){return this.findMetadata(this._excludeMetadatas,t,o)},o.prototype.findExposeMetadata=function(t,o){return this.findMetadata(this._exposeMetadatas,t,o)},o.prototype.findExposeMetadataByCustomName=function(t,o){return this.getExposedMetadatas(t).find((function(t){return t.options&&t.options.name===o}))},o.prototype.findTypeMetadata=function(t,o){return this.findMetadata(this._typeMetadatas,t,o)},o.prototype.getStrategy=function(t){var o=this._excludeMetadatas.get(t),e=o&&o.get(void 0),n=this._exposeMetadatas.get(t),r=n&&n.get(void 0);return e&&r||!e&&!r?"none":e?"excludeAll":"exposeAll"},o.prototype.getExposedMetadatas=function(t){return this.getMetadata(this._exposeMetadatas,t)},o.prototype.getExcludedMetadatas=function(t){return this.getMetadata(this._excludeMetadatas,t)},o.prototype.getExposedProperties=function(o,e){return this.getExposedMetadatas(o).filter((function(o){return!o.options||(!0===o.options.toClassOnly&&!0===o.options.toPlainOnly||(!0===o.options.toClassOnly?e===t.TransformationType.CLASS_TO_CLASS||e===t.TransformationType.PLAIN_TO_CLASS:!0!==o.options.toPlainOnly||e===t.TransformationType.CLASS_TO_PLAIN))})).map((function(t){return t.propertyName}))},o.prototype.getExcludedProperties=function(o,e){return this.getExcludedMetadatas(o).filter((function(o){return!o.options||(!0===o.options.toClassOnly&&!0===o.options.toPlainOnly||(!0===o.options.toClassOnly?e===t.TransformationType.CLASS_TO_CLASS||e===t.TransformationType.PLAIN_TO_CLASS:!0!==o.options.toPlainOnly||e===t.TransformationType.CLASS_TO_PLAIN))})).map((function(t){return t.propertyName}))},o.prototype.clear=function(){this._typeMetadatas.clear(),this._exposeMetadatas.clear(),this._excludeMetadatas.clear(),this._ancestorsMap.clear()},o.prototype.getMetadata=function(t,o){var e,n=t.get(o);n&&(e=Array.from(n.values()).filter((function(t){return void 0!==t.propertyName})));for(var r=[],a=0,i=this.getAncestors(o);a<i.length;a++){var s=i[a],p=t.get(s);if(p){var f=Array.from(p.values()).filter((function(t){return void 0!==t.propertyName}));r.push.apply(r,f)}}return r.concat(e||[])},o.prototype.findMetadata=function(t,o,e){var n=t.get(o);if(n){var r=n.get(e);if(r)return r}for(var a=0,i=this.getAncestors(o);a<i.length;a++){var s=i[a],p=t.get(s);if(p){var f=p.get(e);if(f)return f}}},o.prototype.findMetadatas=function(t,o,e){var n,r=t.get(o);r&&(n=r.get(e));for(var a=[],i=0,s=this.getAncestors(o);i<s.length;i++){var p=s[i],f=t.get(p);f&&f.has(e)&&a.push.apply(a,f.get(e))}return a.slice().reverse().concat((n||[]).slice().reverse())},o.prototype.getAncestors=function(t){if(!t)return[];if(!this._ancestorsMap.has(t)){for(var o=[],e=Object.getPrototypeOf(t.prototype.constructor);void 0!==e.prototype;e=Object.getPrototypeOf(e.prototype.constructor))o.push(e);this._ancestorsMap.set(t,o)}return this._ancestorsMap.get(t)},o}());var n=function(t,o,e){if(e||2===arguments.length)for(var n,r=0,a=o.length;r<a;r++)!n&&r in o||(n||(n=Array.prototype.slice.call(o,0,r)),n[r]=o[r]);return t.concat(n||Array.prototype.slice.call(o))};var r=function(){function o(t,o){this.transformationType=t,this.options=o,this.recursionStack=new Set}return o.prototype.transform=function(o,n,r,a,i,s){var p,f=this;if(void 0===s&&(s=0),Array.isArray(n)||n instanceof Set){var u=a&&this.transformationType===t.TransformationType.PLAIN_TO_CLASS?function(t){var o=new t;return o instanceof Set||"push"in o?o:[]}(a):[];return n.forEach((function(e,n){var a=o?o[n]:void 0;if(f.options.enableCircularCheck&&f.isCircular(e))f.transformationType===t.TransformationType.CLASS_TO_CLASS&&(u instanceof Set?u.add(e):u.push(e));else{var i=void 0;if("function"!=typeof r&&r&&r.options&&r.options.discriminator&&r.options.discriminator.property&&r.options.discriminator.subTypes){if(f.transformationType===t.TransformationType.PLAIN_TO_CLASS){i=r.options.discriminator.subTypes.find((function(t){return t.name===e[r.options.discriminator.property]}));var p={newObject:u,object:e,property:void 0},c=r.typeFunction(p);i=void 0===i?c:i.value,r.options.keepDiscriminatorProperty||delete e[r.options.discriminator.property]}f.transformationType===t.TransformationType.CLASS_TO_CLASS&&(i=e.constructor),f.transformationType===t.TransformationType.CLASS_TO_PLAIN&&(e[r.options.discriminator.property]=r.options.discriminator.subTypes.find((function(t){return t.value===e.constructor})).name)}else i=r;var d=f.transform(a,e,i,void 0,e instanceof Map,s+1);u instanceof Set?u.add(d):u.push(d)}})),u}if(r!==String||i){if(r!==Number||i){if(r!==Boolean||i){if((r===Date||n instanceof Date)&&!i)return n instanceof Date?new Date(n.valueOf()):null==n?n:new Date(n);if(("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:void 0).Buffer&&(r===Buffer||n instanceof Buffer)&&!i)return null==n?n:Buffer.from(n);if(null===(p=n)||"object"!=typeof p||"function"!=typeof p.then||i){if(i||null===n||"object"!=typeof n||"function"!=typeof n.then){if("object"==typeof n&&null!==n){r||n.constructor===Object||(Array.isArray(n)||n.constructor!==Array)&&(r=n.constructor),!r&&o&&(r=o.constructor),this.options.enableCircularCheck&&this.recursionStack.add(n);var c=this.getKeys(r,n,i),d=o||{};o||this.transformationType!==t.TransformationType.PLAIN_TO_CLASS&&this.transformationType!==t.TransformationType.CLASS_TO_CLASS||(d=i?new Map:r?new r:{});for(var l=function(a){if("__proto__"===a||"constructor"===a)return"continue";var p=a,f=a,u=a;if(!y.options.ignoreDecorators&&r)if(y.transformationType===t.TransformationType.PLAIN_TO_CLASS)(c=e.findExposeMetadataByCustomName(r,a))&&(u=c.propertyName,f=c.propertyName);else if(y.transformationType===t.TransformationType.CLASS_TO_PLAIN||y.transformationType===t.TransformationType.CLASS_TO_CLASS){var c;(c=e.findExposeMetadata(r,a))&&c.options&&c.options.name&&(f=c.options.name)}var l=void 0;l=y.transformationType===t.TransformationType.PLAIN_TO_CLASS?n[p]:n instanceof Map?n.get(p):n[p]instanceof Function?n[p]():n[p];var T=void 0,m=l instanceof Map;if(r&&i)T=r;else if(r){var h=e.findTypeMetadata(r,u);if(h){var v={newObject:d,object:n,property:u},S=h.typeFunction?h.typeFunction(v):h.reflectedType;h.options&&h.options.discriminator&&h.options.discriminator.property&&h.options.discriminator.subTypes?n[p]instanceof Array?T=h:(y.transformationType===t.TransformationType.PLAIN_TO_CLASS&&(T=void 0===(T=h.options.discriminator.subTypes.find((function(t){if(l&&l instanceof Object&&h.options.discriminator.property in l)return t.name===l[h.options.discriminator.property]})))?S:T.value,h.options.keepDiscriminatorProperty||l&&l instanceof Object&&h.options.discriminator.property in l&&delete l[h.options.discriminator.property]),y.transformationType===t.TransformationType.CLASS_TO_CLASS&&(T=l.constructor),y.transformationType===t.TransformationType.CLASS_TO_PLAIN&&l&&(l[h.options.discriminator.property]=h.options.discriminator.subTypes.find((function(t){return t.value===l.constructor})).name)):T=S,m=m||h.reflectedType===Map}else if(y.options.targetMaps)y.options.targetMaps.filter((function(t){return t.target===r&&!!t.properties[u]})).forEach((function(t){return T=t.properties[u]}));else if(y.options.enableImplicitConversion&&y.transformationType===t.TransformationType.PLAIN_TO_CLASS){var _=Reflect.getMetadata("design:type",r.prototype,u);_&&(T=_)}}var g=Array.isArray(n[p])?y.getReflectedType(r,u):void 0,A=o?o[p]:void 0;if(d.constructor.prototype){var M=Object.getOwnPropertyDescriptor(d.constructor.prototype,f);if((y.transformationType===t.TransformationType.PLAIN_TO_CLASS||y.transformationType===t.TransformationType.CLASS_TO_CLASS)&&(M&&!M.set||d[f]instanceof Function))return"continue"}if(y.options.enableCircularCheck&&y.isCircular(l)){if(y.transformationType===t.TransformationType.CLASS_TO_CLASS){L=l;(void 0!==(L=y.applyCustomTransformations(L,r,a,n,y.transformationType))||y.options.exposeUnsetFields)&&(d instanceof Map?d.set(f,L):d[f]=L)}}else{var C=y.transformationType===t.TransformationType.PLAIN_TO_CLASS?f:a,L=void 0;y.transformationType===t.TransformationType.CLASS_TO_PLAIN?(L=n[C],L=y.applyCustomTransformations(L,r,C,n,y.transformationType),L=n[C]===L?l:L,L=y.transform(A,L,T,g,m,s+1)):void 0===l&&y.options.exposeDefaultValues?L=d[f]:(L=y.transform(A,l,T,g,m,s+1),L=y.applyCustomTransformations(L,r,C,n,y.transformationType)),(void 0!==L||y.options.exposeUnsetFields)&&(d instanceof Map?d.set(f,L):d[f]=L)}},y=this,T=0,m=c;T<m.length;T++){l(m[T])}return this.options.enableCircularCheck&&this.recursionStack.delete(n),d}return n}return n}return new Promise((function(t,o){n.then((function(o){return t(f.transform(void 0,o,r,void 0,void 0,s+1))}),o)}))}return null==n?n:Boolean(n)}return null==n?n:Number(n)}return null==n?n:String(n)},o.prototype.applyCustomTransformations=function(t,o,n,r,a){var i=this,s=e.findTransformMetadatas(o,n,this.transformationType);return void 0!==this.options.version&&(s=s.filter((function(t){return!t.options||i.checkVersion(t.options.since,t.options.until)}))),(s=this.options.groups&&this.options.groups.length?s.filter((function(t){return!t.options||i.checkGroups(t.options.groups)})):s.filter((function(t){return!t.options||!t.options.groups||!t.options.groups.length}))).forEach((function(o){t=o.transformFn({value:t,key:n,obj:r,type:a,options:i.options})})),t},o.prototype.isCircular=function(t){return this.recursionStack.has(t)},o.prototype.getReflectedType=function(t,o){if(t){var n=e.findTypeMetadata(t,o);return n?n.reflectedType:void 0}},o.prototype.getKeys=function(o,r,a){var i=this,s=e.getStrategy(o);"none"===s&&(s=this.options.strategy||"exposeAll");var p=[];if(("exposeAll"===s||a)&&(p=r instanceof Map?Array.from(r.keys()):Object.keys(r)),a)return p;if(this.options.ignoreDecorators&&this.options.excludeExtraneousValues&&o){var f=e.getExposedProperties(o,this.transformationType),u=e.getExcludedProperties(o,this.transformationType);p=n(n([],f,!0),u,!0)}if(!this.options.ignoreDecorators&&o){f=e.getExposedProperties(o,this.transformationType);this.transformationType===t.TransformationType.PLAIN_TO_CLASS&&(f=f.map((function(t){var n=e.findExposeMetadata(o,t);return n&&n.options&&n.options.name?n.options.name:t}))),p=this.options.excludeExtraneousValues?f:p.concat(f);var c=e.getExcludedProperties(o,this.transformationType);c.length>0&&(p=p.filter((function(t){return!c.includes(t)}))),void 0!==this.options.version&&(p=p.filter((function(t){var n=e.findExposeMetadata(o,t);return!n||!n.options||i.checkVersion(n.options.since,n.options.until)}))),p=this.options.groups&&this.options.groups.length?p.filter((function(t){var n=e.findExposeMetadata(o,t);return!n||!n.options||i.checkGroups(n.options.groups)})):p.filter((function(t){var n=e.findExposeMetadata(o,t);return!(n&&n.options&&n.options.groups&&n.options.groups.length)}))}return this.options.excludePrefixes&&this.options.excludePrefixes.length&&(p=p.filter((function(t){return i.options.excludePrefixes.every((function(o){return t.substr(0,o.length)!==o}))}))),p=p.filter((function(t,o,e){return e.indexOf(t)===o}))},o.prototype.checkVersion=function(t,o){var e=!0;return e&&t&&(e=this.options.version>=t),e&&o&&(e=this.options.version<o),e},o.prototype.checkGroups=function(t){return!t||this.options.groups.some((function(o){return t.includes(o)}))},o}(),a={enableCircularCheck:!1,enableImplicitConversion:!1,excludeExtraneousValues:!1,excludePrefixes:void 0,exposeDefaultValues:!1,exposeUnsetFields:!0,groups:void 0,ignoreDecorators:!1,strategy:void 0,targetMaps:void 0,version:void 0},i=function(){return i=Object.assign||function(t){for(var o,e=1,n=arguments.length;e<n;e++)for(var r in o=arguments[e])Object.prototype.hasOwnProperty.call(o,r)&&(t[r]=o[r]);return t},i.apply(this,arguments)},s=function(){function o(){}return o.prototype.instanceToPlain=function(o,e){return new r(t.TransformationType.CLASS_TO_PLAIN,i(i({},a),e)).transform(void 0,o,void 0,void 0,void 0,void 0)},o.prototype.classToPlainFromExist=function(o,e,n){return new r(t.TransformationType.CLASS_TO_PLAIN,i(i({},a),n)).transform(e,o,void 0,void 0,void 0,void 0)},o.prototype.plainToInstance=function(o,e,n){return new r(t.TransformationType.PLAIN_TO_CLASS,i(i({},a),n)).transform(void 0,e,o,void 0,void 0,void 0)},o.prototype.plainToClassFromExist=function(o,e,n){return new r(t.TransformationType.PLAIN_TO_CLASS,i(i({},a),n)).transform(o,e,void 0,void 0,void 0,void 0)},o.prototype.instanceToInstance=function(o,e){return new r(t.TransformationType.CLASS_TO_CLASS,i(i({},a),e)).transform(void 0,o,void 0,void 0,void 0,void 0)},o.prototype.classToClassFromExist=function(o,e,n){return new r(t.TransformationType.CLASS_TO_CLASS,i(i({},a),n)).transform(e,o,void 0,void 0,void 0,void 0)},o.prototype.serialize=function(t,o){return JSON.stringify(this.instanceToPlain(t,o))},o.prototype.deserialize=function(t,o,e){var n=JSON.parse(o);return this.plainToInstance(t,n,e)},o.prototype.deserializeArray=function(t,o,e){var n=JSON.parse(o);return this.plainToInstance(t,n,e)},o}();var p=new s;t.ClassTransformer=s,t.Exclude=function(t){return void 0===t&&(t={}),function(o,n){e.addExcludeMetadata({target:o instanceof Function?o:o.constructor,propertyName:n,options:t})}},t.Expose=function(t){return void 0===t&&(t={}),function(o,n){e.addExposeMetadata({target:o instanceof Function?o:o.constructor,propertyName:n,options:t})}},t.Transform=function(t,o){return void 0===o&&(o={}),function(n,r){e.addTransformMetadata({target:n.constructor,propertyName:r,transformFn:t,options:o})}},t.TransformInstanceToInstance=function(t){return function(o,e,n){var r=new s,a=n.value;n.value=function(){for(var o=[],e=0;e<arguments.length;e++)o[e]=arguments[e];var n=a.apply(this,o),i=!!n&&("object"==typeof n||"function"==typeof n)&&"function"==typeof n.then;return i?n.then((function(o){return r.instanceToInstance(o,t)})):r.instanceToInstance(n,t)}}},t.TransformInstanceToPlain=function(t){return function(o,e,n){var r=new s,a=n.value;n.value=function(){for(var o=[],e=0;e<arguments.length;e++)o[e]=arguments[e];var n=a.apply(this,o),i=!!n&&("object"==typeof n||"function"==typeof n)&&"function"==typeof n.then;return i?n.then((function(o){return r.instanceToPlain(o,t)})):r.instanceToPlain(n,t)}}},t.TransformPlainToInstance=function(t,o){return function(e,n,r){var a=new s,i=r.value;r.value=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=i.apply(this,e),s=!!r&&("object"==typeof r||"function"==typeof r)&&"function"==typeof r.then;return s?r.then((function(e){return a.plainToInstance(t,e,o)})):a.plainToInstance(t,r,o)}}},t.Type=function(t,o){return void 0===o&&(o={}),function(n,r){var a=Reflect.getMetadata("design:type",n,r);e.addTypeMetadata({target:n.constructor,propertyName:r,reflectedType:a,typeFunction:t,options:o})}},t.classToClassFromExist=function(t,o,e){return p.classToClassFromExist(t,o,e)},t.classToPlainFromExist=function(t,o,e){return p.classToPlainFromExist(t,o,e)},t.deserialize=function(t,o,e){return p.deserialize(t,o,e)},t.deserializeArray=function(t,o,e){return p.deserializeArray(t,o,e)},t.instanceToInstance=function(t,o){return p.instanceToInstance(t,o)},t.instanceToPlain=function(t,o){return p.instanceToPlain(t,o)},t.plainToClassFromExist=function(t,o,e){return p.plainToClassFromExist(t,o,e)},t.plainToInstance=function(t,o,e){return p.plainToInstance(t,o,e)},t.serialize=function(t,o){return p.serialize(t,o)},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=class-transformer.umd.min.js.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
class ClassTransformer { | ||
classToPlain(object, options) { | ||
instanceToPlain(object, options) { | ||
const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.CLASS_TO_PLAIN, { | ||
@@ -23,3 +23,3 @@ ...default_options_constant_1.defaultOptions, | ||
} | ||
plainToClass(cls, plain, options) { | ||
plainToInstance(cls, plain, options) { | ||
const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.PLAIN_TO_CLASS, { | ||
@@ -38,3 +38,3 @@ ...default_options_constant_1.defaultOptions, | ||
} | ||
classToClass(object, options) { | ||
instanceToInstance(object, options) { | ||
const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.CLASS_TO_CLASS, { | ||
@@ -54,3 +54,3 @@ ...default_options_constant_1.defaultOptions, | ||
serialize(object, options) { | ||
return JSON.stringify(this.classToPlain(object, options)); | ||
return JSON.stringify(this.instanceToPlain(object, options)); | ||
} | ||
@@ -62,3 +62,3 @@ /** | ||
const jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
} | ||
@@ -70,3 +70,3 @@ /** | ||
const jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
} | ||
@@ -73,0 +73,0 @@ } |
@@ -15,7 +15,7 @@ "use strict"; | ||
__exportStar(require("./expose.decorator"), exports); | ||
__exportStar(require("./transform-class-to-class.decorator"), exports); | ||
__exportStar(require("./transform-class-to-plain.decorator"), exports); | ||
__exportStar(require("./transform-plain-to-class.decorator"), exports); | ||
__exportStar(require("./transform-instance-to-instance.decorator"), exports); | ||
__exportStar(require("./transform-instance-to-plain.decorator"), exports); | ||
__exportStar(require("./transform-plain-to-instance.decorator"), exports); | ||
__exportStar(require("./transform.decorator"), exports); | ||
__exportStar(require("./type.decorator"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deserializeArray = exports.deserialize = exports.serialize = exports.classToClassFromExist = exports.classToClass = exports.plainToClassFromExist = exports.plainToClass = exports.classToPlainFromExist = exports.classToPlain = exports.ClassTransformer = void 0; | ||
exports.deserializeArray = exports.deserialize = exports.serialize = exports.classToClassFromExist = exports.instanceToInstance = exports.plainToClassFromExist = exports.plainToInstance = exports.classToPlainFromExist = exports.instanceToPlain = exports.ClassTransformer = void 0; | ||
const ClassTransformer_1 = require("./ClassTransformer"); | ||
@@ -22,6 +22,6 @@ var ClassTransformer_2 = require("./ClassTransformer"); | ||
const classTransformer = new ClassTransformer_1.ClassTransformer(); | ||
function classToPlain(object, options) { | ||
return classTransformer.classToPlain(object, options); | ||
function instanceToPlain(object, options) { | ||
return classTransformer.instanceToPlain(object, options); | ||
} | ||
exports.classToPlain = classToPlain; | ||
exports.instanceToPlain = instanceToPlain; | ||
function classToPlainFromExist(object, plainObject, options) { | ||
@@ -31,6 +31,6 @@ return classTransformer.classToPlainFromExist(object, plainObject, options); | ||
exports.classToPlainFromExist = classToPlainFromExist; | ||
function plainToClass(cls, plain, options) { | ||
return classTransformer.plainToClass(cls, plain, options); | ||
function plainToInstance(cls, plain, options) { | ||
return classTransformer.plainToInstance(cls, plain, options); | ||
} | ||
exports.plainToClass = plainToClass; | ||
exports.plainToInstance = plainToInstance; | ||
function plainToClassFromExist(clsObject, plain, options) { | ||
@@ -40,6 +40,6 @@ return classTransformer.plainToClassFromExist(clsObject, plain, options); | ||
exports.plainToClassFromExist = plainToClassFromExist; | ||
function classToClass(object, options) { | ||
return classTransformer.classToClass(object, options); | ||
function instanceToInstance(object, options) { | ||
return classTransformer.instanceToInstance(object, options); | ||
} | ||
exports.classToClass = classToClass; | ||
exports.instanceToInstance = instanceToInstance; | ||
function classToClassFromExist(object, fromObject, options) { | ||
@@ -55,2 +55,7 @@ return classTransformer.classToClassFromExist(object, fromObject, options); | ||
* Deserializes given JSON string to a object of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* instanceToClass(cls, JSON.parse(json), options) | ||
* ``` | ||
*/ | ||
@@ -63,2 +68,8 @@ function deserialize(cls, json, options) { | ||
* Deserializes given JSON string to an array of objects of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* JSON.parse(json).map(value => instanceToClass(cls, value, options)) | ||
* ``` | ||
* | ||
*/ | ||
@@ -65,0 +76,0 @@ function deserializeArray(cls, json, options) { |
@@ -104,3 +104,3 @@ "use strict"; | ||
} | ||
else if (!!utils_1.getGlobal().Buffer && (targetType === Buffer || value instanceof Buffer) && !isMap) { | ||
else if (!!(0, utils_1.getGlobal)().Buffer && (targetType === Buffer || value instanceof Buffer) && !isMap) { | ||
if (value === null || value === undefined) | ||
@@ -110,3 +110,3 @@ return value; | ||
} | ||
else if (utils_1.isPromise(value) && !isMap) { | ||
else if ((0, utils_1.isPromise)(value) && !isMap) { | ||
return new Promise((resolve, reject) => { | ||
@@ -124,3 +124,12 @@ value.then((data) => resolve(this.transform(undefined, data, targetType, undefined, undefined, level + 1)), reject); | ||
if (!targetType && value.constructor !== Object /* && TransformationType === TransformationType.CLASS_TO_PLAIN*/) | ||
targetType = value.constructor; | ||
if (!Array.isArray(value) && value.constructor === Array) { | ||
// Somebody attempts to convert special Array like object to Array, eg: | ||
// const evilObject = { '100000000': '100000000', __proto__: [] }; | ||
// This could be used to cause Denial-of-service attack so we don't allow it. | ||
// See prevent-array-bomb.spec.ts for more details. | ||
} | ||
else { | ||
// We are good we can use the built-in constructor | ||
targetType = value.constructor; | ||
} | ||
if (!targetType && source) | ||
@@ -172,10 +181,21 @@ targetType = source.constructor; | ||
let subValue = undefined; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
if (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS) { | ||
/** | ||
* This section is added for the following report: | ||
* https://github.com/typestack/class-transformer/issues/596 | ||
* | ||
* We should not call functions or constructors when transforming to class. | ||
*/ | ||
subValue = value[valueKey]; | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
} | ||
} | ||
@@ -214,3 +234,5 @@ // determine a type | ||
if (this.transformationType === enums_1.TransformationType.CLASS_TO_PLAIN) { | ||
subValue[metadata.options.discriminator.property] = metadata.options.discriminator.subTypes.find(subType => subType.value === subValue.constructor).name; | ||
if (subValue) { | ||
subValue[metadata.options.discriminator.property] = metadata.options.discriminator.subTypes.find(subType => subType.value === subValue.constructor).name; | ||
} | ||
} | ||
@@ -374,2 +396,11 @@ } | ||
} | ||
/** | ||
* If decorators are ignored but we don't want the extraneous values, then we use the | ||
* metadata to decide which property is needed, but doesn't apply the decorator effect. | ||
*/ | ||
if (this.options.ignoreDecorators && this.options.excludeExtraneousValues && target) { | ||
const exposedProperties = storage_1.defaultMetadataStorage.getExposedProperties(target, this.transformationType); | ||
const excludedProperties = storage_1.defaultMetadataStorage.getExcludedProperties(target, this.transformationType); | ||
keys = [...exposedProperties, ...excludedProperties]; | ||
} | ||
if (!this.options.ignoreDecorators && target) { | ||
@@ -376,0 +407,0 @@ // add all exposed to list of keys |
@@ -6,3 +6,3 @@ "use strict"; | ||
it('should return true if Buffer is present in globalThis', () => { | ||
expect(_1.getGlobal().Buffer).toBe(true); | ||
expect((0, _1.getGlobal)().Buffer).toBe(true); | ||
}); | ||
@@ -12,3 +12,3 @@ it('should return false if Buffer is not present in globalThis', () => { | ||
delete global.Buffer; | ||
expect(_1.getGlobal().Buffer).toBe(false); | ||
expect((0, _1.getGlobal)().Buffer).toBe(false); | ||
global.Buffer = bufferImp; | ||
@@ -15,0 +15,0 @@ }); |
@@ -5,3 +5,3 @@ import { TransformOperationExecutor } from './TransformOperationExecutor'; | ||
export class ClassTransformer { | ||
classToPlain(object, options) { | ||
instanceToPlain(object, options) { | ||
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_PLAIN, { | ||
@@ -20,3 +20,3 @@ ...defaultOptions, | ||
} | ||
plainToClass(cls, plain, options) { | ||
plainToInstance(cls, plain, options) { | ||
const executor = new TransformOperationExecutor(TransformationType.PLAIN_TO_CLASS, { | ||
@@ -35,3 +35,3 @@ ...defaultOptions, | ||
} | ||
classToClass(object, options) { | ||
instanceToInstance(object, options) { | ||
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_CLASS, { | ||
@@ -51,3 +51,3 @@ ...defaultOptions, | ||
serialize(object, options) { | ||
return JSON.stringify(this.classToPlain(object, options)); | ||
return JSON.stringify(this.instanceToPlain(object, options)); | ||
} | ||
@@ -59,3 +59,3 @@ /** | ||
const jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
} | ||
@@ -67,5 +67,5 @@ /** | ||
const jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
} | ||
} | ||
//# sourceMappingURL=ClassTransformer.js.map |
export * from './exclude.decorator'; | ||
export * from './expose.decorator'; | ||
export * from './transform-class-to-class.decorator'; | ||
export * from './transform-class-to-plain.decorator'; | ||
export * from './transform-plain-to-class.decorator'; | ||
export * from './transform-instance-to-instance.decorator'; | ||
export * from './transform-instance-to-plain.decorator'; | ||
export * from './transform-plain-to-instance.decorator'; | ||
export * from './transform.decorator'; | ||
export * from './type.decorator'; | ||
//# sourceMappingURL=index.js.map |
@@ -7,4 +7,4 @@ import { ClassTransformer } from './ClassTransformer'; | ||
const classTransformer = new ClassTransformer(); | ||
export function classToPlain(object, options) { | ||
return classTransformer.classToPlain(object, options); | ||
export function instanceToPlain(object, options) { | ||
return classTransformer.instanceToPlain(object, options); | ||
} | ||
@@ -14,4 +14,4 @@ export function classToPlainFromExist(object, plainObject, options) { | ||
} | ||
export function plainToClass(cls, plain, options) { | ||
return classTransformer.plainToClass(cls, plain, options); | ||
export function plainToInstance(cls, plain, options) { | ||
return classTransformer.plainToInstance(cls, plain, options); | ||
} | ||
@@ -21,4 +21,4 @@ export function plainToClassFromExist(clsObject, plain, options) { | ||
} | ||
export function classToClass(object, options) { | ||
return classTransformer.classToClass(object, options); | ||
export function instanceToInstance(object, options) { | ||
return classTransformer.instanceToInstance(object, options); | ||
} | ||
@@ -33,2 +33,7 @@ export function classToClassFromExist(object, fromObject, options) { | ||
* Deserializes given JSON string to a object of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* instanceToClass(cls, JSON.parse(json), options) | ||
* ``` | ||
*/ | ||
@@ -40,2 +45,8 @@ export function deserialize(cls, json, options) { | ||
* Deserializes given JSON string to an array of objects of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* JSON.parse(json).map(value => instanceToClass(cls, value, options)) | ||
* ``` | ||
* | ||
*/ | ||
@@ -42,0 +53,0 @@ export function deserializeArray(cls, json, options) { |
@@ -119,3 +119,12 @@ import { defaultMetadataStorage } from './storage'; | ||
if (!targetType && value.constructor !== Object /* && TransformationType === TransformationType.CLASS_TO_PLAIN*/) | ||
targetType = value.constructor; | ||
if (!Array.isArray(value) && value.constructor === Array) { | ||
// Somebody attempts to convert special Array like object to Array, eg: | ||
// const evilObject = { '100000000': '100000000', __proto__: [] }; | ||
// This could be used to cause Denial-of-service attack so we don't allow it. | ||
// See prevent-array-bomb.spec.ts for more details. | ||
} | ||
else { | ||
// We are good we can use the built-in constructor | ||
targetType = value.constructor; | ||
} | ||
if (!targetType && source) | ||
@@ -167,10 +176,21 @@ targetType = source.constructor; | ||
let subValue = undefined; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
if (this.transformationType === TransformationType.PLAIN_TO_CLASS) { | ||
/** | ||
* This section is added for the following report: | ||
* https://github.com/typestack/class-transformer/issues/596 | ||
* | ||
* We should not call functions or constructors when transforming to class. | ||
*/ | ||
subValue = value[valueKey]; | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
} | ||
} | ||
@@ -209,3 +229,5 @@ // determine a type | ||
if (this.transformationType === TransformationType.CLASS_TO_PLAIN) { | ||
subValue[metadata.options.discriminator.property] = metadata.options.discriminator.subTypes.find(subType => subType.value === subValue.constructor).name; | ||
if (subValue) { | ||
subValue[metadata.options.discriminator.property] = metadata.options.discriminator.subTypes.find(subType => subType.value === subValue.constructor).name; | ||
} | ||
} | ||
@@ -369,2 +391,11 @@ } | ||
} | ||
/** | ||
* If decorators are ignored but we don't want the extraneous values, then we use the | ||
* metadata to decide which property is needed, but doesn't apply the decorator effect. | ||
*/ | ||
if (this.options.ignoreDecorators && this.options.excludeExtraneousValues && target) { | ||
const exposedProperties = defaultMetadataStorage.getExposedProperties(target, this.transformationType); | ||
const excludedProperties = defaultMetadataStorage.getExcludedProperties(target, this.transformationType); | ||
keys = [...exposedProperties, ...excludedProperties]; | ||
} | ||
if (!this.options.ignoreDecorators && target) { | ||
@@ -371,0 +402,0 @@ // add all exposed to list of keys |
@@ -18,3 +18,3 @@ var __assign = (this && this.__assign) || function () { | ||
} | ||
ClassTransformer.prototype.classToPlain = function (object, options) { | ||
ClassTransformer.prototype.instanceToPlain = function (object, options) { | ||
var executor = new TransformOperationExecutor(TransformationType.CLASS_TO_PLAIN, __assign(__assign({}, defaultOptions), options)); | ||
@@ -27,3 +27,3 @@ return executor.transform(undefined, object, undefined, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.plainToClass = function (cls, plain, options) { | ||
ClassTransformer.prototype.plainToInstance = function (cls, plain, options) { | ||
var executor = new TransformOperationExecutor(TransformationType.PLAIN_TO_CLASS, __assign(__assign({}, defaultOptions), options)); | ||
@@ -36,3 +36,3 @@ return executor.transform(undefined, plain, cls, undefined, undefined, undefined); | ||
}; | ||
ClassTransformer.prototype.classToClass = function (object, options) { | ||
ClassTransformer.prototype.instanceToInstance = function (object, options) { | ||
var executor = new TransformOperationExecutor(TransformationType.CLASS_TO_CLASS, __assign(__assign({}, defaultOptions), options)); | ||
@@ -46,3 +46,3 @@ return executor.transform(undefined, object, undefined, undefined, undefined, undefined); | ||
ClassTransformer.prototype.serialize = function (object, options) { | ||
return JSON.stringify(this.classToPlain(object, options)); | ||
return JSON.stringify(this.instanceToPlain(object, options)); | ||
}; | ||
@@ -54,3 +54,3 @@ /** | ||
var jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
}; | ||
@@ -62,3 +62,3 @@ /** | ||
var jsonObject = JSON.parse(json); | ||
return this.plainToClass(cls, jsonObject, options); | ||
return this.plainToInstance(cls, jsonObject, options); | ||
}; | ||
@@ -65,0 +65,0 @@ return ClassTransformer; |
export * from './exclude.decorator'; | ||
export * from './expose.decorator'; | ||
export * from './transform-class-to-class.decorator'; | ||
export * from './transform-class-to-plain.decorator'; | ||
export * from './transform-plain-to-class.decorator'; | ||
export * from './transform-instance-to-instance.decorator'; | ||
export * from './transform-instance-to-plain.decorator'; | ||
export * from './transform-plain-to-instance.decorator'; | ||
export * from './transform.decorator'; | ||
export * from './type.decorator'; | ||
//# sourceMappingURL=index.js.map |
@@ -7,4 +7,4 @@ import { ClassTransformer } from './ClassTransformer'; | ||
var classTransformer = new ClassTransformer(); | ||
export function classToPlain(object, options) { | ||
return classTransformer.classToPlain(object, options); | ||
export function instanceToPlain(object, options) { | ||
return classTransformer.instanceToPlain(object, options); | ||
} | ||
@@ -14,4 +14,4 @@ export function classToPlainFromExist(object, plainObject, options) { | ||
} | ||
export function plainToClass(cls, plain, options) { | ||
return classTransformer.plainToClass(cls, plain, options); | ||
export function plainToInstance(cls, plain, options) { | ||
return classTransformer.plainToInstance(cls, plain, options); | ||
} | ||
@@ -21,4 +21,4 @@ export function plainToClassFromExist(clsObject, plain, options) { | ||
} | ||
export function classToClass(object, options) { | ||
return classTransformer.classToClass(object, options); | ||
export function instanceToInstance(object, options) { | ||
return classTransformer.instanceToInstance(object, options); | ||
} | ||
@@ -33,2 +33,7 @@ export function classToClassFromExist(object, fromObject, options) { | ||
* Deserializes given JSON string to a object of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* instanceToClass(cls, JSON.parse(json), options) | ||
* ``` | ||
*/ | ||
@@ -40,2 +45,8 @@ export function deserialize(cls, json, options) { | ||
* Deserializes given JSON string to an array of objects of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* JSON.parse(json).map(value => instanceToClass(cls, value, options)) | ||
* ``` | ||
* | ||
*/ | ||
@@ -42,0 +53,0 @@ export function deserializeArray(cls, json, options) { |
@@ -0,1 +1,10 @@ | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
import { defaultMetadataStorage } from './storage'; | ||
@@ -123,3 +132,12 @@ import { TransformationType } from './enums'; | ||
if (!targetType && value.constructor !== Object /* && TransformationType === TransformationType.CLASS_TO_PLAIN*/) | ||
targetType = value.constructor; | ||
if (!Array.isArray(value) && value.constructor === Array) { | ||
// Somebody attempts to convert special Array like object to Array, eg: | ||
// const evilObject = { '100000000': '100000000', __proto__: [] }; | ||
// This could be used to cause Denial-of-service attack so we don't allow it. | ||
// See prevent-array-bomb.spec.ts for more details. | ||
} | ||
else { | ||
// We are good we can use the built-in constructor | ||
targetType = value.constructor; | ||
} | ||
if (!targetType && source) | ||
@@ -170,10 +188,21 @@ targetType = source.constructor; | ||
var subValue = undefined; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
if (this_1.transformationType === TransformationType.PLAIN_TO_CLASS) { | ||
/** | ||
* This section is added for the following report: | ||
* https://github.com/typestack/class-transformer/issues/596 | ||
* | ||
* We should not call functions or constructors when transforming to class. | ||
*/ | ||
subValue = value[valueKey]; | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
if (value instanceof Map) { | ||
subValue = value.get(valueKey); | ||
} | ||
else if (value[valueKey] instanceof Function) { | ||
subValue = value[valueKey](); | ||
} | ||
else { | ||
subValue = value[valueKey]; | ||
} | ||
} | ||
@@ -212,3 +241,5 @@ // determine a type | ||
if (this_1.transformationType === TransformationType.CLASS_TO_PLAIN) { | ||
subValue[metadata_1.options.discriminator.property] = metadata_1.options.discriminator.subTypes.find(function (subType) { return subType.value === subValue.constructor; }).name; | ||
if (subValue) { | ||
subValue[metadata_1.options.discriminator.property] = metadata_1.options.discriminator.subTypes.find(function (subType) { return subType.value === subValue.constructor; }).name; | ||
} | ||
} | ||
@@ -379,2 +410,11 @@ } | ||
} | ||
/** | ||
* If decorators are ignored but we don't want the extraneous values, then we use the | ||
* metadata to decide which property is needed, but doesn't apply the decorator effect. | ||
*/ | ||
if (this.options.ignoreDecorators && this.options.excludeExtraneousValues && target) { | ||
var exposedProperties = defaultMetadataStorage.getExposedProperties(target, this.transformationType); | ||
var excludedProperties = defaultMetadataStorage.getExcludedProperties(target, this.transformationType); | ||
keys = __spreadArray(__spreadArray([], exposedProperties, true), excludedProperties, true); | ||
} | ||
if (!this.options.ignoreDecorators && target) { | ||
@@ -381,0 +421,0 @@ // add all exposed to list of keys |
{ | ||
"name": "class-transformer", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors", | ||
@@ -5,0 +5,0 @@ "author": "TypeStack contributors", |
@@ -679,4 +679,2 @@ # class-transformer | ||
Note, that dates will be converted to strings when you'll try to convert class object to plain object. | ||
Same technique can be used with `Number`, `String`, `Boolean` | ||
@@ -683,0 +681,0 @@ primitive types when you want to convert your values into these types. |
@@ -7,4 +7,4 @@ import { ClassTransformOptions } from './interfaces'; | ||
*/ | ||
classToPlain<T extends Record<string, any>>(object: T, options?: ClassTransformOptions): Record<string, any>; | ||
classToPlain<T extends Record<string, any>>(object: T[], options?: ClassTransformOptions): Record<string, any>[]; | ||
instanceToPlain<T extends Record<string, any>>(object: T, options?: ClassTransformOptions): Record<string, any>; | ||
instanceToPlain<T extends Record<string, any>>(object: T[], options?: ClassTransformOptions): Record<string, any>[]; | ||
/** | ||
@@ -20,4 +20,4 @@ * Converts class (constructor) object to plain (literal) object. | ||
*/ | ||
plainToClass<T extends Record<string, any>, V extends Array<any>>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T[]; | ||
plainToClass<T extends Record<string, any>, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T; | ||
plainToInstance<T extends Record<string, any>, V extends Array<any>>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T[]; | ||
plainToInstance<T extends Record<string, any>, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T; | ||
/** | ||
@@ -33,4 +33,4 @@ * Converts plain (literal) object to class (constructor) object. | ||
*/ | ||
classToClass<T>(object: T, options?: ClassTransformOptions): T; | ||
classToClass<T>(object: T[], options?: ClassTransformOptions): T[]; | ||
instanceToInstance<T>(object: T, options?: ClassTransformOptions): T; | ||
instanceToInstance<T>(object: T[], options?: ClassTransformOptions): T[]; | ||
/** | ||
@@ -37,0 +37,0 @@ * Converts class (constructor) object to plain (literal) object. |
export * from './exclude.decorator'; | ||
export * from './expose.decorator'; | ||
export * from './transform-class-to-class.decorator'; | ||
export * from './transform-class-to-plain.decorator'; | ||
export * from './transform-plain-to-class.decorator'; | ||
export * from './transform-instance-to-instance.decorator'; | ||
export * from './transform-instance-to-plain.decorator'; | ||
export * from './transform-plain-to-instance.decorator'; | ||
export * from './transform.decorator'; | ||
export * from './type.decorator'; |
@@ -10,4 +10,4 @@ import { ClassTransformOptions } from './interfaces'; | ||
*/ | ||
export declare function classToPlain<T>(object: T, options?: ClassTransformOptions): Record<string, any>; | ||
export declare function classToPlain<T>(object: T[], options?: ClassTransformOptions): Record<string, any>[]; | ||
export declare function instanceToPlain<T>(object: T, options?: ClassTransformOptions): Record<string, any>; | ||
export declare function instanceToPlain<T>(object: T[], options?: ClassTransformOptions): Record<string, any>[]; | ||
/** | ||
@@ -17,2 +17,4 @@ * Converts class (constructor) object to plain (literal) object. | ||
* Also works with arrays. | ||
* | ||
* @deprecated This function is being removed. | ||
*/ | ||
@@ -24,4 +26,4 @@ export declare function classToPlainFromExist<T>(object: T, plainObject: Record<string, any>, options?: ClassTransformOptions): Record<string, any>; | ||
*/ | ||
export declare function plainToClass<T, V>(cls: ClassConstructor<T>, plain: V[], options?: ClassTransformOptions): T[]; | ||
export declare function plainToClass<T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T; | ||
export declare function plainToInstance<T, V>(cls: ClassConstructor<T>, plain: V[], options?: ClassTransformOptions): T[]; | ||
export declare function plainToInstance<T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T; | ||
/** | ||
@@ -31,2 +33,4 @@ * Converts plain (literal) object to class (constructor) object. | ||
* Also works with arrays. | ||
* | ||
* @deprecated This function is being removed. The current implementation is incorrect as it modifies the source object. | ||
*/ | ||
@@ -38,4 +42,4 @@ export declare function plainToClassFromExist<T, V>(clsObject: T[], plain: V[], options?: ClassTransformOptions): T[]; | ||
*/ | ||
export declare function classToClass<T>(object: T, options?: ClassTransformOptions): T; | ||
export declare function classToClass<T>(object: T[], options?: ClassTransformOptions): T[]; | ||
export declare function instanceToInstance<T>(object: T, options?: ClassTransformOptions): T; | ||
export declare function instanceToInstance<T>(object: T[], options?: ClassTransformOptions): T[]; | ||
/** | ||
@@ -45,2 +49,4 @@ * Converts class (constructor) object to plain (literal) object. | ||
* Also works with arrays. | ||
* | ||
* @deprecated This function is being removed. The current implementation is incorrect as it modifies the source object. | ||
*/ | ||
@@ -51,2 +57,7 @@ export declare function classToClassFromExist<T>(object: T, fromObject: T, options?: ClassTransformOptions): T; | ||
* Serializes given object to a JSON string. | ||
* | ||
* @deprecated This function is being removed. Please use | ||
* ``` | ||
* JSON.stringify(instanceToPlain(object, options)) | ||
* ``` | ||
*/ | ||
@@ -57,2 +68,7 @@ export declare function serialize<T>(object: T, options?: ClassTransformOptions): string; | ||
* Deserializes given JSON string to a object of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* instanceToClass(cls, JSON.parse(json), options) | ||
* ``` | ||
*/ | ||
@@ -62,3 +78,9 @@ export declare function deserialize<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T; | ||
* Deserializes given JSON string to an array of objects of the given class. | ||
* | ||
* @deprecated This function is being removed. Please use the following instead: | ||
* ``` | ||
* JSON.parse(json).map(value => instanceToClass(cls, value, options)) | ||
* ``` | ||
* | ||
*/ | ||
export declare function deserializeArray<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T[]; |
@@ -13,2 +13,5 @@ import { TargetMap } from './target-map.interface'; | ||
* Indicates if extraneous properties should be excluded from the value when converting a plain value to a class. | ||
* | ||
* This option requires that each property on the target class has at least one `@Expose` or `@Exclude` decorator | ||
* assigned from this library. | ||
*/ | ||
@@ -31,4 +34,6 @@ excludeExtraneousValues?: boolean; | ||
/** | ||
* If set to true then class transformer will ignore all @Expose and @Exclude decorators and what inside them. | ||
* If set to true then class transformer will ignore the effect of all @Expose and @Exclude decorators. | ||
* This option is useful if you want to kinda clone your object but do not apply decorators affects. | ||
* | ||
* __NOTE:__ You may still have to add the decorators to make other options work. | ||
*/ | ||
@@ -59,3 +64,4 @@ ignoreDecorators?: boolean; | ||
/** | ||
* When set to true `undefined` fields will be ignored during class to plain transformation. | ||
* When set to true, fields with `undefined` as value will be included in class to plain transformation. Otherwise | ||
* those fields will be omitted from the result. | ||
* | ||
@@ -62,0 +68,0 @@ * DEFAULT: `true` |
@@ -14,3 +14,3 @@ /** | ||
* ```ts | ||
* classToPlain(payload, { version: 1.0 }); | ||
* instanceToPlain(payload, { version: 1.0 }); | ||
* ``` | ||
@@ -24,3 +24,3 @@ */ | ||
* ```ts | ||
* classToPlain(payload, { version: 1.0 }); | ||
* instanceToPlain(payload, { version: 1.0 }); | ||
* ``` | ||
@@ -36,3 +36,3 @@ */ | ||
* ```ts | ||
* classToPlain(payload, { groups: ['user'] }); | ||
* instanceToPlain(payload, { groups: ['user'] }); | ||
* ``` | ||
@@ -39,0 +39,0 @@ */ |
@@ -10,3 +10,3 @@ /** | ||
* ```ts | ||
* classToPlain(payload, { version: 1.0 }); | ||
* instanceToPlain(payload, { version: 1.0 }); | ||
* ``` | ||
@@ -20,3 +20,3 @@ */ | ||
* ```ts | ||
* classToPlain(payload, { version: 1.0 }); | ||
* instanceToPlain(payload, { version: 1.0 }); | ||
* ``` | ||
@@ -32,3 +32,3 @@ */ | ||
* ```ts | ||
* classToPlain(payload, { groups: ['user'] }); | ||
* instanceToPlain(payload, { groups: ['user'] }); | ||
* ``` | ||
@@ -35,0 +35,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
769127
5008
1
902