UNPKG

type-transformer

Version:

Transformation / serialization / deserialization of plain JavaScript objects to typed objects and vice versa

2 lines (1 loc) 13.3 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e["type-transformer"]={})}(this,function(e){"use strict";var t,r,n=function(e,t){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function i(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function o(e,t){var r,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(o){return function(c){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=(i=a.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}}function a(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}}function c(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}(t=e.Direction||(e.Direction={})).PLAIN_TO_TYPED="PLAIN_TO_TYPED",t.TYPED_TO_PLAIN="TYPED_TO_PLAIN",(r=e.Visibility||(e.Visibility={})).EXPOSE="EXPOSE",r.EXCLUDE="EXCLUDE";var s,l=function(){function t(){this.selectors=[]}return t.prototype.addSelector=function(e,t){this.selectors.push([e,t])},t.prototype.selected=function(t,r){var n,i,o=this.defaultVisibility;try{for(var s=a(this.selectors),l=s.next();!l.done;l=s.next()){var u=c(l.value,2),f=u[0],p=u[1];if(!p||this.doesSelectorApply(t,r,p)){if(!f(t,r,p))return e.Visibility.EXCLUDE;o=e.Visibility.EXPOSE}}}catch(e){n={error:e}}finally{try{l&&!l.done&&(i=s.return)&&i.call(s)}finally{if(n)throw n.error}}return o},t.prototype.doesSelectorApply=function(t,r,n){return(!n.toPlainOnly||r===e.Direction.TYPED_TO_PLAIN)&&(!n.toTypedOnly||r===e.Direction.PLAIN_TO_TYPED)},t}(),u=function(){return!0},f=function(){return!1},p=function(){function t(e,t){this.registry=e,this.type=t,this.inherits=!1,this._fields=new Map,this._postProcessors=[]}return t.prototype.getField=function(e){var t=this._fields.get(e);return t||(t=new y(e),this._fields.set(e,t)),t},Object.defineProperty(t.prototype,"defaultVisibility",{get:function(){if(this._defaultVisibility)return this._defaultVisibility;if(this.inherits){var e=this.getAncestor();if(e)return e.defaultVisibility}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"fields",{get:function(){var e,t,r,n;if(!this.inherits)return this._fields.values();var i=new Map;try{for(var o=a(this.inheritanceChain()),c=o.next();!c.done;c=o.next()){var s=c.value;try{for(var l=a(s._fields.values()),u=l.next();!u.done;u=l.next()){var f=u.value;i.has(f.name)||i.set(f.name,f)}}catch(e){r={error:e}}finally{try{u&&!u.done&&(n=l.return)&&n.call(l)}finally{if(r)throw r.error}}}}catch(t){e={error:t}}finally{try{c&&!c.done&&(t=o.return)&&t.call(o)}finally{if(e)throw e.error}}return i.values()},enumerable:!0,configurable:!0}),t.prototype.excludeAll=function(){return this._defaultVisibility=e.Visibility.EXCLUDE,this},t.prototype.exposeAll=function(){return this._defaultVisibility=e.Visibility.EXPOSE,this},t.prototype.inherit=function(){return this.inherits=!0,this},t.prototype.postProcess=function(e,t){return this._postProcessors.push(new v(e,t)),this},t.prototype.getPostProcessors=function(t,r){return this._postProcessors.filter(function(n){var i=n.selected(t,r);return!i||i===e.Visibility.EXPOSE})},t.prototype.inheritanceChain=function(){var e;return o(this,function(t){switch(t.label){case 0:e=this,t.label=1;case 1:return[4,e];case 2:t.sent(),t.label=3;case 3:if(e=e.getAncestor())return[3,1];t.label=4;case 4:return[2]}})},t.prototype.getAncestor=function(){for(var e,t=this.type;Object.prototype!==(t=Object.getPrototypeOf(t));)if(e=this.registry.getSchema(t))return e},t}(),y=function(t){function r(e){var r=t.call(this)||this;return r.name=e,r._transformers=[],r}return i(r,t),r.prototype.select=function(e){return this.addSelector(e),this},r.prototype.exclude=function(e){return this.addSelector(f,e),this},r.prototype.expose=function(e){return this.addSelector(u,e),e&&(this._propName=e.propName),this},r.prototype.nested=function(e){return this.nestedType=e,this},r.prototype.transform=function(e,t){var r=new d(e,t);return this._transformers.push(r),this},r.prototype.getValueTransformers=function(t,r){return this._transformers.filter(function(n){var i=n.selected(t,r);return!i||i===e.Visibility.EXPOSE})},Object.defineProperty(r.prototype,"propName",{get:function(){return this._propName||this.name},enumerable:!0,configurable:!0}),r}(l),h=function(t){function r(r,n){var i=t.call(this)||this;return i.transformFn=r,n&&(i.addSelector(u,n),i.defaultVisibility=e.Visibility.EXCLUDE),i}return i(r,t),r.prototype.transform=function(e,t,r){return this.transformFn(e,t,r)},r}(l),d=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t}(h),v=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t}(h),g=function(){function e(){this.schemasByType=new Map}return e.prototype.getOrCreateSchema=function(e){var t=this.schemasByType.get(e);return t||(t=new p(this,e),this.schemasByType.set(e,t)),t},e.prototype.getSchema=function(e){return this.schemasByType.get(e)},e.prototype.clear=function(){this.schemasByType.clear()},e}(),b=new g;(s=e.CircularRefStrategy||(e.CircularRefStrategy={})).NONE="NONE",s.BREAK="BREAK",s.RESOLVE="RESOLVE",s.ERROR="ERROR";var P=function(){function e(e,t){this.object=e,this.referencedBy=new Map,t&&this.referencedBy.set(t.object,t)}return e.prototype.isReferencedBy=function(e){var t,r;if(this.referencedBy.has(e))return!0;try{for(var n=a(this.referencedBy.values()),i=n.next();!i.done;i=n.next()){if(i.value.isReferencedBy(e))return!0}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}return!1},e.prototype.getReferencing=function(e){var t,r;if(this.referencedBy.has(e))return this.referencedBy.get(e).targetObject;try{for(var n=a(this.referencedBy.values()),i=n.next();!i.done;i=n.next()){var o=i.value.getReferencing(e);if(o)return o}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}},e.prototype.addReferencer=function(e){this.referencedBy.set(e.object,e)},e}(),E=function(){function t(t){this.nodeStack=[],this.allNodes=new Map,this.circularStrategy=e.CircularRefStrategy.BREAK,t&&(this.circularStrategy=t)}return Object.defineProperty(t.prototype,"current",{get:function(){if(this.nodeStack.length)return this.nodeStack[this.nodeStack.length-1]},enumerable:!0,configurable:!0}),t.prototype.preventCircularDependencies=function(t,r,n){if(this.circularStrategy!==e.CircularRefStrategy.NONE&&this.isCircularReference(t))return this.handleCircularReference(t);this.descend(t).targetObject=r;var i=n();return this.ascend(),i},t.prototype.descend=function(e){var t=this.current,r=this.allNodes.get(e);return r&&t?r.addReferencer(t):(r=new P(e,t),this.allNodes.set(e,r)),this.nodeStack.push(r),r},t.prototype.ascend=function(){this.nodeStack.pop()},t.prototype.isCircularReference=function(e){return!!this.allNodes.has(e)&&this.current.isReferencedBy(e)},t.prototype.handleCircularReference=function(t){switch(this.circularStrategy){case e.CircularRefStrategy.BREAK:return;case e.CircularRefStrategy.ERROR:throw new Error("A circular reference was detected.");case e.CircularRefStrategy.RESOLVE:return this.current.getReferencing(t);default:throw new Error("Unkown strategy for handling circular references: "+this.circularStrategy)}},t}(),O=function(){function t(e){this.options=e,this.referenceGraph=new E(e.circularRefStrategy)}return t.prototype.toPlain=function(t,r,n){var i=this,o=t;if("object"==typeof o&&null!==o){o.toJS&&(o=o.toJS());var s=c(this.getTargetObject(o,void 0,n),2),l=s[0],u=s[1];r||o.constructor===Object||(r=o.constructor),o=this.referenceGraph.preventCircularDependencies(o,u,function(){var t,n,s,f;if(l)try{for(var p=a(o),y=p.next();!y.done;y=p.next()){var h=y.value;h=i.toPlain(h),u.push(h)}}catch(e){t={error:e}}finally{try{y&&!y.done&&(n=p.return)&&n.call(p)}finally{if(t)throw t.error}}else{var d=i.getFields(e.Direction.TYPED_TO_PLAIN,o,r);try{for(var v=a(d),g=v.next();!g.done;g=v.next()){var b=c(g.value,2),P=b[0],E=b[1];i.applyFieldToPlain(o,u,P,E)}}catch(e){s={error:e}}finally{try{g&&!g.done&&(f=v.return)&&f.call(v)}finally{if(s)throw s.error}}}return u})}var f=this.getSchema(r);return o=this.postProcessObject(t,o,e.Direction.TYPED_TO_PLAIN,f)},t.prototype.applyFieldToPlain=function(t,r,n,i){var o,c,s=n,l=t[n];if(void 0!==l||!this.options.skipUndefinedProperties){if("function"==typeof l&&(l=t[n]()),i){s=i.propName instanceof Function?i.propName(t,e.Direction.TYPED_TO_PLAIN):i.propName;var u=i.getValueTransformers(t,e.Direction.TYPED_TO_PLAIN);try{for(var f=a(u),p=f.next();!p.done;p=f.next()){l=p.value.transform(l,t,e.Direction.TYPED_TO_PLAIN)}}catch(e){o={error:e}}finally{try{p&&!p.done&&(c=f.return)&&c.call(f)}finally{if(o)throw o.error}}}return l=this.toPlain(l),r[s]=l,r}},t.prototype.toTyped=function(t,r,n){var i=this,o=t;if("object"==typeof o&&null!==o){var s=c(this.getTargetObject(o,r,n),2),l=s[0],u=s[1];return this.referenceGraph.preventCircularDependencies(o,u,function(){var t,n,s,f;if(l)try{for(var p=a(o),y=p.next();!y.done;y=p.next()){var h=y.value;h=i.toTyped(h,r),u.push(h)}}catch(e){t={error:e}}finally{try{y&&!y.done&&(n=p.return)&&n.call(p)}finally{if(t)throw t.error}}else{var d=i.getFields(e.Direction.PLAIN_TO_TYPED,o,r);try{for(var v=a(d),g=v.next();!g.done;g=v.next()){var b=c(g.value,2),P=b[0],E=b[1];u=i.applyFieldToTyped(o,u,P,E)}}catch(e){s={error:e}}finally{try{g&&!g.done&&(f=v.return)&&f.call(v)}finally{if(s)throw s.error}}}return u})}var f=this.getSchema(r);return o=this.postProcessObject(t,o,e.Direction.PLAIN_TO_TYPED,f)},t.prototype.applyFieldToTyped=function(t,r,n,i){var o,c,s,l=n;i&&(l=i.propName instanceof Function?i.propName(t,e.Direction.TYPED_TO_PLAIN):i.propName,s=i.nestedType);var u=t[l];if(i){var f=i.getValueTransformers(t,e.Direction.PLAIN_TO_TYPED);try{for(var p=a(f),y=p.next();!y.done;y=p.next()){u=y.value.transform(u,t,e.Direction.PLAIN_TO_TYPED)}}catch(e){o={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(o)throw o.error}}}return u=this.toTyped(u,s),r.set&&"Map"===r.constructor.name?r=r.set(n,u):r[n]=u,r},t.prototype.postProcessObject=function(e,t,r,n){var i,o;if(n){var c=n.getPostProcessors(e,r);try{for(var s=a(c),l=s.next();!l.done;l=s.next()){t=l.value.transform(e,t,r)}}catch(e){i={error:e}}finally{try{l&&!l.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}return t},t.prototype.getFields=function(t,r,n){var i,o,c=e.Visibility.EXPOSE,s=this.getSchema(n);s&&s.defaultVisibility&&(c=s.defaultVisibility);var l=new Map;if(c===e.Visibility.EXPOSE&&Object.keys(r).forEach(function(e){return l.set(e,void 0)}),s)try{for(var u=a(s.fields),f=u.next();!f.done;f=u.next()){var p=f.value,y=p.selected(r,t)||c;y===e.Visibility.EXPOSE?l.set(p.name,p):y===e.Visibility.EXCLUDE&&l.delete(p.name)}}catch(e){i={error:e}}finally{try{f&&!f.done&&(o=u.return)&&o.call(u)}finally{if(i)throw i.error}}return l},t.prototype.getSchema=function(e){return e?this.options.registry.getSchema(e):void 0},t.prototype.getTargetObject=function(e,t,r){var n=e instanceof Array||e instanceof Set;return[n,r||(n?[]:t?new t:{})]},t}();function m(e){return(e=e||{}).registry||(e.registry=b),e}e.createRegistry=function(){return new g},e.createSchema=function(e,t){return(t=t||b).getOrCreateSchema(e)},e.typedToPlain=function(e,t,r){return new O(m(t)).toPlain(e,void 0,r)},e.plainToTyped=function(e,t,r,n){return new O(m(r)).toTyped(e,t,n)},e.expose=function(e){return function(t,r){t instanceof Function?b.getOrCreateSchema(t).exposeAll():r&&b.getOrCreateSchema(t.constructor).getField(r).expose(e)}},e.exclude=function(){return function(e,t){e instanceof Function?b.getOrCreateSchema(e).excludeAll():t&&b.getOrCreateSchema(e.constructor).getField(t).exclude()}},e.select=function(e){return function(t,r){if(!r)throw'Cannot use "@select" on a class.';b.getOrCreateSchema(t.constructor).getField(r).select(e)}},e.inherit=function(){return function(e){b.getOrCreateSchema(e).inherit()}},e.Selectable=l,Object.defineProperty(e,"__esModule",{value:!0})});