Comparing version 2.0.0 to 2.1.0
@@ -1,2 +0,2 @@ | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.Morphism=r():e.Morphism=r()}(this,function(){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";function n(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,n)}return t}function o(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}t.r(r);var i=Symbol.for("SchemaOptions");function a(e){return h(e)&&e.hasOwnProperty("fn")&&e.hasOwnProperty("path")}function c(e){return l(e)}function u(e){return Array.isArray(e)&&e.every(c)}var s,p=(e,r)=>e.reduce((e,t)=>(b(e,t,g(r,t)),e),{});function f(e){return void 0===e}function h(e){var r=typeof e;return null!=e&&("object"===r||"function"===r)}function l(e){return"string"==typeof e||e instanceof String}function d(e){return"function"==typeof e}function y(e){if(Promise&&Promise.resolve)return Promise.resolve(e)==e;throw"Promise not supported in your environment"}function g(e,r){for(var t=(r=(r=r.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,o=t.length;n<o;++n){var i=t[n];if(!(h(e)&&i in e))return;e=e[i]}return e}function m(e,r){return e.reduce((e,t,i)=>(function(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?n(t,!0).forEach(function(r){o(e,r,t[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):n(t).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e})({},e,{[t]:r[i]}),{})}function v(e,r){return"number"==typeof r&&Array.isArray(e)||function(e,r){return null!=e&&Object.prototype.hasOwnProperty.call(e,r)}(e,r)}function b(e,r,t,n){if("number"==typeof r&&(r=[r]),!r||0===r.length)return e;if("string"==typeof r)return b(e,r.split(".").map(P),t,n);var o=r[0],i=function(e,r){if(v(e,r))return e[r]}(e,o);return 1===r.length?(void 0!==i&&n||(e[o]=t),i):(void 0===i&&("number"==typeof r[1]?e[o]=[]:e[o]={}),b(e[o],r.slice(1),t,n))}function P(e){var r=parseInt(e);return r.toString()===e?r:e}function O(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,n)}return t}function j(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?O(t,!0).forEach(function(r){w(e,r,t[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):O(t).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function w(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function S(e,r){return r&&!function(e){for(var r in e)if(Object.prototype.hasOwnProperty.call(e,r))return!1;return!0}(r)&&(e[i]=r),e}!function(e){e.Root="Root",e.Property="Property",e.ActionFunction="ActionFunction",e.ActionAggregator="ActionAggregator",e.ActionString="ActionString",e.ActionSelector="ActionSelector"}(s||(s={}));class A{constructor(e){this.schemaOptions=void 0,this.root=void 0,this.schema=void 0,this.schema=e,this.schemaOptions=A.getSchemaOptions(this.schema),this.root={data:{targetPropertyPath:"",propertyName:"MorphismTreeRoot",action:null,kind:s.Root},parent:null,children:[]},e&&this.parseSchema(e)}static getSchemaOptions(e){return j({},{class:{automapping:!0},undefinedValues:{strip:!1}},{},e?e[i]:void 0)}parseSchema(e,r,t){var n;(l(n=e)||d(n)||a(n)||u(n))&&r?(this.add({propertyName:r,action:e},t),t=t?"".concat(t,".").concat(r):r):(r&&(this.add({propertyName:r,action:null},t),t=t?"".concat(t,".").concat(r):r),Array.isArray(e)?e.forEach((e,r)=>{this.parseSchema(e,r.toString(),t)}):h(e)&&Object.keys(e).forEach(r=>{this.parseSchema(e[r],r,t)}))}*traverseBFS(){var e=[];for(e.push(this.root);e.length>0;){var r=e.shift();if(!r)return;for(var t=0,n=r.children.length;t<n;t++)e.push(r.children[t]);r.data.kind!==s.Root&&(yield r)}}add(e,r){var t=this.getActionKind(e.action);if(!t)throw new Error("The action specified for ".concat(e.propertyName," is not supported."));var n={data:j({},e,{kind:t,targetPropertyPath:""}),parent:null,children:[]};if(n.data.preparedAction=this.getPreparedAction(n.data),r)for(var o of this.traverseBFS())o.data.targetPropertyPath===r&&(n.parent=o,n.data.targetPropertyPath="".concat(o.data.targetPropertyPath,".").concat(n.data.propertyName),o.children.push(n));else n.parent=this.root,n.data.targetPropertyPath=n.data.propertyName,this.root.children.push(n)}getActionKind(e){return c(e)?s.ActionString:d(e)?s.ActionFunction:a(e)?s.ActionSelector:u(e)?s.ActionAggregator:null===e?s.Property:void 0}getPreparedAction(e){var{propertyName:r,action:t,kind:n}=e;if(c(t))return e=>{var{object:r}=e;return g(r,t)};if(d(t))return e=>{var{object:r,items:n,objectToCompute:o}=e;return t.call(void 0,r,n,o)};if(u(t))return e=>{var{object:r}=e;return p(t,r)};if(a(t))return e=>{var n,{object:o,items:i,objectToCompute:a}=e;try{var c;Array.isArray(t.path)?c=p(t.path,o):l(t.path)&&(c=g(o,t.path)),n=t.fn.call(void 0,c,o,i,a)}catch(e){throw e.message="Unable to set target property [".concat(r,"].\n \n An error occured when applying [").concat(t.fn.name,"] on property [").concat(t.path,"]\n \n Internal error: ").concat(e.message),e}return n};if(n===s.Property)return null;throw new Error("The action specified for ".concat(r," is not supported."))}}function M(e){return(r,t,n)=>{var o=n.value;return"function"==typeof o&&(n.value=function(){for(var r=arguments.length,t=new Array(r),n=0;n<r;n++)t[n]=arguments[n];var i=o.apply(this,t);return y(i)?Promise.resolve(i).then(r=>e(r)):e(i)}),n}}function _(e,r,t,n){var o=r.schemaOptions,i=[];for(var a of r.traverseBFS()){var{preparedAction:c,targetPropertyPath:u}=a.data;c&&i.push({targetPropertyPath:u,preparedAction:c({object:e,objectToCompute:n,items:t})})}return i.reduce((e,r)=>{var t=((e,r)=>f(r)?f(e)?void 0:e:r)(g(e,r.targetPropertyPath),r.preparedAction);return void 0===t?(o&&o.undefinedValues&&o.undefinedValues.strip?o.undefinedValues.default&&b(e,r.targetPropertyPath,o.undefinedValues.default(e,r.targetPropertyPath)):b(e,r.targetPropertyPath,t),e):(b(e,r.targetPropertyPath,t),e)},n)}function E(e,r){var t,n=A.getSchemaOptions(e);if(r&&n.class&&n.class.automapping){var o=function(e,r){var t=Object.keys(new e),n=m(t,t);return Object.assign(n,r)}(r,e);t=new A(o)}else t=new A(e);return function(e){if(!e)return e;if(Array.isArray(e))return e.map(n=>{if(r){var o=new r;return _(n,t,e,o)}return _(n,t,e,{})});var n=e;if(r){var o=new r;return _(n,t,[n],o)}return _(n,t,[n],{})}}function x(e,r,t){switch(arguments.length){case 1:return E(e);case 2:return E(e)(r);case 3:if(t)return null!==r?E(e,t)(r):E(e,t);throw new Error("When using morphism(schema, items, type), type should be defined but value received is ".concat(t))}}function T(e,r){return M(E(e,r))}function k(e){return M(E(e))}function D(e,r){return M(E(e,r))}t.d(r,"morph",function(){return T}),t.d(r,"toJSObject",function(){return k}),t.d(r,"toClassObject",function(){return D}),t.d(r,"morphism",function(){return x}),t.d(r,"createSchema",function(){return S}),t.d(r,"Schema",function(){}),t.d(r,"StrictSchema",function(){}),t.d(r,"SchemaOptions",function(){}),t.d(r,"Mapper",function(){}),t.d(r,"SCHEMA_OPTIONS_SYMBOL",function(){return i});var N=new class{constructor(e){this._registry=null,this._registry=e||{cache:new Map}}register(e,r){if(!e&&!r)throw new Error("type paramater is required when you register a mapping");if(this.exists(e))throw new Error("A mapper for ".concat(e.name," has already been registered"));var t;return t=x(r||{},null,e),this._registry.cache.set(e,t),t}map(e,r){if(!this.exists(e)){var t=this.register(e);if(void 0===r)return t}return this.getMapper(e)(r)}getMapper(e){return this._registry.cache.get(e)}setMapper(e,r){if(r){if(this.exists(e)){var t=x(r,null,e);return this._registry.cache.set(e,t),t}throw new Error("The type ".concat(e.name," is not registered. Register it using `Mophism.register(").concat(e.name,", schema)`"))}throw new Error("The schema must be an Object. Found ".concat(r))}deleteMapper(e){return this._registry.cache.delete(e)}exists(e){return this._registry.cache.has(e)}get mappers(){return this._registry.cache}},F=x;F.register=(e,r)=>N.register(e,r),F.map=(e,r)=>N.map(e,r),F.getMapper=e=>N.getMapper(e),F.setMapper=(e,r)=>N.setMapper(e,r),F.deleteMapper=e=>N.deleteMapper(e),F.mappers=N.mappers;var R=F;r.default=R}])}); | ||
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Morphism=t():r.Morphism=t()}(this,function(){return function(r){var t={};function e(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=r,e.c=t,e.d=function(r,t,n){e.o(r,t)||Object.defineProperty(r,t,{enumerable:!0,get:n})},e.r=function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},e.t=function(r,t){if(1&t&&(r=e(r)),8&t)return r;if(4&t&&"object"==typeof r&&r&&r.__esModule)return r;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:r}),2&t&&"string"!=typeof r)for(var o in r)e.d(n,o,function(t){return r[t]}.bind(null,o));return n},e.n=function(r){var t=r&&r.__esModule?function(){return r.default}:function(){return r};return e.d(t,"a",t),t},e.o=function(r,t){return Object.prototype.hasOwnProperty.call(r,t)},e.p="",e(e.s=0)}([function(r,t,e){"use strict";function n(r,t){var e=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable})),e.push.apply(e,n)}return e}function o(r,t,e){return t in r?Object.defineProperty(r,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):r[t]=e,r}e.r(t);var i=Symbol("SchemaOptions");function a(r){return f(r)&&(r.hasOwnProperty("fn")||r.hasOwnProperty("path"))}function c(r){return h(r)}function s(r){return Array.isArray(r)&&r.every(c)}var u=(r,t)=>r.reduce((r,e)=>(m(r,e,y(t,e)),r),{});function p(r){return void 0===r}function f(r){var t=typeof r;return null!=r&&("object"===t||"function"===t)}function h(r){return"string"==typeof r||r instanceof String}function l(r){return"function"==typeof r}function d(r){if(Promise&&Promise.resolve)return Promise.resolve(r)==r;throw new Error("Promise not supported in your environment")}function y(r,t){for(var e=(t=(t=t.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,o=e.length;n<o;++n){var i=e[n];if(!(f(r)&&i in r))return;r=r[i]}return r}function v(r,t){return r.reduce((r,e,i)=>(function(r){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?n(e,!0).forEach(function(t){o(r,t,e[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(e)):n(e).forEach(function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(e,t))})}return r})({},r,{[e]:t[i]}),{})}function g(r,t){return"number"==typeof t&&Array.isArray(r)||function(r,t){return null!=r&&Object.prototype.hasOwnProperty.call(r,t)}(r,t)}function m(r,t,e,n){if("number"==typeof t&&(t=[t]),!t||0===t.length)return r;if("string"==typeof t)return m(r,t.split(".").map(b),e,n);var o=t[0],i=function(r,t){if(g(r,t))return r[t]}(r,o);return 1===t.length?(void 0!==i&&n||(r[o]=e),i):(void 0===i&&("number"==typeof t[1]?r[o]=[]:r[o]={}),m(r[o],t.slice(1),e,n))}function b(r){var t=parseInt(r);return t.toString()===r?t:r}function P(r){for(var t in r)if(Object.prototype.hasOwnProperty.call(r,t))return!1;return!0}var O=Symbol("errors");class w extends Error{constructor(r){super("Invalid value ".concat(r.value," supplied at property ").concat(r.targetProperty,". Expecting: ").concat(r.expect)),this.targetProperty=void 0,this.value=void 0,this.expect=void 0,this.targetProperty=r.targetProperty,this.value=r.value,this.expect=r.expect}}class j extends Error{constructor(r,t){super(),this.errors=void 0,this.reporter=void 0,this.target=void 0,this.errors=new Set,this.reporter=r,this.target=t}addError(r){this.errors.add(r);var t=this.reporter.report(this.target);t&&(this.message=t.join("\n"))}}function S(r){return r&&r[O]&&r[O].errors.size>0}function A(r){var{message:t}=r;return t}class x{constructor(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:A;this.formatter=r}report(r){var t=this.extractErrors(r);return t?[...t.errors.values()].map(this.formatter):null}extractErrors(r){return S(r)?r[O]:null}}var E,M=new x;class _ extends Error{constructor(r){super(r.expect),this.value=void 0,this.expect=void 0,this.value=r.value,this.expect=r.expect}}function T(r,t){var e=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable})),e.push.apply(e,n)}return e}function N(r){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?T(e,!0).forEach(function(t){k(r,t,e[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(e)):T(e).forEach(function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(e,t))})}return r}function k(r,t,e){return t in r?Object.defineProperty(r,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):r[t]=e,r}function D(r,t){return t&&!P(t)&&(r[i]=t),r}!function(r){r.Root="Root",r.Property="Property",r.ActionFunction="ActionFunction",r.ActionAggregator="ActionAggregator",r.ActionString="ActionString",r.ActionSelector="ActionSelector"}(E||(E={}));class F{constructor(r){this.schemaOptions=void 0,this.root=void 0,this.schema=void 0,this.schema=r,this.schemaOptions=F.getSchemaOptions(this.schema),this.root={data:{targetPropertyPath:"",propertyName:"MorphismTreeRoot",action:null,kind:E.Root},parent:null,children:[]},r&&this.parseSchema(r)}static getSchemaOptions(r){return N({},{class:{automapping:!0},undefinedValues:{strip:!1}},{},r?r[i]:void 0)}parseSchema(r,t,e){if((h(n=r)||l(n)||a(n)||s(n))&&t)this.add({propertyName:t,action:r},e),e=e?"".concat(e,".").concat(t):t;else{if(t){if(f(r)&&P(r))throw new Error("A value of a schema property can't be an empty object. Value ".concat(JSON.stringify(r)," found for property ").concat(t));this.add({propertyName:t,action:r},e),e=e?"".concat(e,".").concat(t):t}Array.isArray(r)?r.forEach((r,t)=>{this.parseSchema(r,t.toString(),e)}):f(r)&&Object.keys(r).forEach(t=>{this.parseSchema(r[t],t,e)})}var n}*traverseBFS(){var r=[];for(r.push(this.root);r.length>0;){var t=r.shift();if(t){for(var e=0,n=t.children.length;e<n;e++)r.push(t.children[e]);t.data.kind!==E.Root&&(yield t)}}}add(r,t){var e={data:N({},r,{kind:this.getActionKind(r),targetPropertyPath:""}),parent:null,children:[]};if(e.data.preparedAction=this.getPreparedAction(e.data),t)for(var n of this.traverseBFS())n.data.targetPropertyPath===t&&(e.parent=n,e.data.targetPropertyPath="".concat(n.data.targetPropertyPath,".").concat(e.data.propertyName),n.children.push(e));else e.parent=this.root,e.data.targetPropertyPath=e.data.propertyName,this.root.children.push(e)}getActionKind(r){if(c(r.action))return E.ActionString;if(l(r.action))return E.ActionFunction;if(a(r.action))return E.ActionSelector;if(s(r.action))return E.ActionAggregator;if(f(r.action))return E.Property;throw new Error("The action specified for ".concat(r.propertyName," is not supported."))}getPreparedAction(r){var{propertyName:t,action:e,kind:n}=r;return c(e)?r=>{var{object:t}=r;return y(t,e)}:l(e)?r=>{var{object:t,items:n,objectToCompute:o}=r;return e.call(void 0,t,n,o)}:s(e)?r=>{var{object:t}=r;return u(e,t)}:a(e)?r=>{var n,{object:o,items:i,objectToCompute:a}=r;if(e.path?Array.isArray(e.path)?n=u(e.path,o):h(e.path)&&(n=y(o,e.path)):n=o,e.fn)try{n=e.fn.call(void 0,n,o,i,a)}catch(r){throw r.message="Unable to set target property [".concat(t,"].\n \n An error occured when applying [").concat(e.fn.name,"] on property [").concat(e.path,"]\n \n Internal error: ").concat(r.message),r}if(e.validation)try{n=e.validation.validate(n)}catch(r){if(!(r instanceof _))throw r;var c=new w({targetProperty:t,expect:r.expect,value:r.value});S(a)?a[O].addError(c):(this.schemaOptions.validation&&this.schemaOptions.validation.reporter?a[O]=new j(this.schemaOptions.validation.reporter,a):a[O]=new j(M,a),a[O].addError(c))}return n}:n===E.Property?null:void 0}}function R(r){return(t,e,n)=>{var o=n.value;return"function"==typeof o&&(n.value=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];var i=o.apply(this,e);return d(i)?Promise.resolve(i).then(t=>r(t)):r(i)}),n}}function V(r,t,e,n){var o=t.schemaOptions,i=[];for(var a of t.traverseBFS()){var{preparedAction:c,targetPropertyPath:s}=a.data;c&&i.push({targetPropertyPath:s,preparedAction:c({object:r,objectToCompute:n,items:e})})}return i.reduce((r,t)=>{var e=((r,t)=>p(t)?p(r)?void 0:r:t)(y(r,t.targetPropertyPath),t.preparedAction);return void 0===e?(o&&o.undefinedValues&&o.undefinedValues.strip?o.undefinedValues.default&&m(r,t.targetPropertyPath,o.undefinedValues.default(r,t.targetPropertyPath)):m(r,t.targetPropertyPath,e),C(o,r),r):(m(r,t.targetPropertyPath,e),C(o,r),r)},n)}function C(r,t){if(r&&r.validation&&r.validation.throw&&S(t)){var e;if(r.validation.reporter)e=r.validation.reporter.extractErrors(t);else e=M.extractErrors(t);if(e)throw e}}function B(r,t){var e,n=F.getSchemaOptions(r);if(t&&n.class&&n.class.automapping){var o=function(r,t){var e=Object.keys(new r),n=v(e,e);return Object.assign(n,t)}(t,r);e=new F(o)}else e=new F(r);return r=>{if(!r)return r;if(Array.isArray(r))return r.map(n=>{if(t){var o=new t;return V(n,e,r,o)}return V(n,e,r,{})});var n=r;if(t){var o=new t;return V(n,e,[n],o)}return V(n,e,[n],{})}}function I(r,t,e){switch(arguments.length){case 1:return B(r);case 2:return B(r)(t);case 3:if(e)return null!==t?B(r,e)(t):B(r,e);throw new Error("When using morphism(schema, items, type), type should be defined but value received is ".concat(e))}}function J(r,t){return R(B(r,t))}function K(r){return R(B(r))}function q(r,t){return R(B(r,t))}e.d(t,"morph",function(){return J}),e.d(t,"toJSObject",function(){return K}),e.d(t,"toClassObject",function(){return q}),e.d(t,"morphism",function(){return I}),e.d(t,"createSchema",function(){return D}),e.d(t,"Schema",function(){}),e.d(t,"StrictSchema",function(){}),e.d(t,"SchemaOptions",function(){}),e.d(t,"Mapper",function(){}),e.d(t,"SCHEMA_OPTIONS_SYMBOL",function(){return i}),e.d(t,"Reporter",function(){return x}),e.d(t,"reporter",function(){return M}),e.d(t,"Formatter",function(){});var z=new class{constructor(){this._registry=null,this._registry={cache:new Map}}register(r,t){if(!r&&!t)throw new Error("type paramater is required when you register a mapping");if(this.exists(r))throw new Error("A mapper for ".concat(r.name," has already been registered"));var e;return e=I(t||{},null,r),this._registry.cache.set(r,e),e}map(r,t){if(!this.exists(r)){var e=this.register(r);if(void 0===t)return e}return this.getMapper(r)(t)}getMapper(r){return this._registry.cache.get(r)}setMapper(r,t){if(t){if(this.exists(r)){var e=I(t,null,r);return this._registry.cache.set(r,e),e}throw new Error("The type ".concat(r.name," is not registered. Register it using `Mophism.register(").concat(r.name,", schema)`"))}throw new Error("The schema must be an Object. Found ".concat(t))}deleteMapper(r){return this._registry.cache.delete(r)}exists(r){return this._registry.cache.has(r)}get mappers(){return this._registry.cache}},H=I;H.register=(r,t)=>z.register(r,t),H.map=(r,t)=>z.map(r,t),H.getMapper=r=>z.getMapper(r),H.setMapper=(r,t)=>z.setMapper(r,t),H.deleteMapper=r=>z.deleteMapper(r),H.mappers=z.mappers;var L=H;t.default=L}])}); | ||
//# sourceMappingURL=morphism.map |
import { ActionSelector, ActionAggregator, ActionFunction } from './types'; | ||
/** | ||
* Symbol identifier used to store options on a Morphism schema. Using the `createSchema` helper to avoid using the symbol directly. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { SCHEMA_OPTIONS_SYMBOL } from 'morphism'; | ||
* | ||
* const options: SchemaOptions = { class: { automapping: true }, undefinedValues: { strip: true } }; | ||
* const schema: Schema = { targetProperty: 'sourceProperty', [SCHEMA_OPTIONS_SYMBOL]: options } | ||
* ``` | ||
*/ | ||
export declare const SCHEMA_OPTIONS_SYMBOL: unique symbol; | ||
@@ -3,0 +15,0 @@ export declare function isActionSelector<S, R>(value: any): value is ActionSelector<S, R>; |
@@ -8,2 +8,3 @@ /** | ||
import { IMorphismRegistry } from './MorphismRegistry'; | ||
import { Reporter, reporter as defaultReporter, Formatter } from './validation/reporter'; | ||
/** | ||
@@ -57,3 +58,3 @@ * Currying function that either outputs a mapping function or the transformed data. | ||
declare const Morphism: typeof morphism & IMorphismRegistry; | ||
export { morphism, createSchema, Schema, StrictSchema, SchemaOptions, Mapper, SCHEMA_OPTIONS_SYMBOL }; | ||
export { morphism, createSchema, Schema, StrictSchema, SchemaOptions, Mapper, SCHEMA_OPTIONS_SYMBOL, Reporter, defaultReporter as reporter, Formatter }; | ||
export default Morphism; |
@@ -64,3 +64,3 @@ import { Constructable, Schema, Mapper } from './types'; | ||
*/ | ||
constructor(cache?: Map<any, any> | WeakMap<any, any>); | ||
constructor(); | ||
/** | ||
@@ -67,0 +67,0 @@ * Register a mapping schema for a Class. |
import { Actions, Schema, StrictSchema } from './types'; | ||
import { Reporter } from './validation/reporter'; | ||
export declare enum NodeKind { | ||
@@ -35,10 +36,53 @@ Root = "Root", | ||
}>; | ||
/** | ||
* Options attached to a `Schema` or `StrictSchema` | ||
*/ | ||
export interface SchemaOptions<Target = any> { | ||
/** | ||
* Specify how to handle ES6 Class | ||
* @memberof SchemaOptions | ||
*/ | ||
class?: { | ||
/** | ||
* Specify wether ES6 Class fields should be automapped if names on source and target match | ||
* @default true | ||
* @type {boolean} | ||
*/ | ||
automapping: boolean; | ||
}; | ||
/** | ||
* Specify how to handle undefined values mapped during the transformations | ||
* @memberof SchemaOptions | ||
*/ | ||
undefinedValues?: { | ||
/** | ||
* Undefined values should be removed from the target | ||
* @default false | ||
* @type {boolean} | ||
*/ | ||
strip: boolean; | ||
/** | ||
* Optional callback to be executed for every undefined property on the Target | ||
* @function default | ||
*/ | ||
default?: (target: Target, propertyPath: string) => any; | ||
}; | ||
/** | ||
* Schema validation options | ||
* @memberof SchemaOptions | ||
*/ | ||
validation?: { | ||
/** | ||
* Should throw when property validation fails | ||
* @default false | ||
* @type {boolean} | ||
*/ | ||
throw: boolean; | ||
/** | ||
* Custom reporter to use when throw option is set to true | ||
* @default false | ||
* @type {boolean} | ||
*/ | ||
reporter?: Reporter; | ||
}; | ||
} | ||
@@ -61,5 +105,5 @@ /** | ||
add(data: AddNode<Target, Source>, targetPropertyPath?: string): void; | ||
getActionKind(action: Actions<Target, Source> | null): NodeKind.Property | NodeKind.ActionFunction | NodeKind.ActionAggregator | NodeKind.ActionString | NodeKind.ActionSelector | undefined; | ||
getPreparedAction(nodeData: SchemaNodeData<Target, Source>): PreparedAction | null; | ||
getActionKind(data: AddNode<Target, Source>): NodeKind.Property | NodeKind.ActionFunction | NodeKind.ActionAggregator | NodeKind.ActionString | NodeKind.ActionSelector; | ||
getPreparedAction(nodeData: SchemaNodeData<Target, Source>): PreparedAction | null | undefined; | ||
} | ||
export {}; |
import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from './morphism'; | ||
import { BaseValidator } from './validation/validators/BaseValidator'; | ||
/** | ||
@@ -34,3 +35,3 @@ * A structure-preserving object from a source data towards a target data. | ||
export declare type StrictSchema<Target = any, Source = any> = { | ||
[destinationProperty in keyof Target]: ActionString<Source> | ActionFunction<Target, Source, Target[destinationProperty]> | ActionAggregator<Source> | ActionSelector<Source, Target[destinationProperty]> | StrictSchema<Target[destinationProperty], Source>; | ||
[destinationProperty in keyof Target]: ActionString<Source> | ActionFunction<Target, Source, Target[destinationProperty]> | ActionAggregator<Source> | ActionSelector<Source, Target> | StrictSchema<Target[destinationProperty], Source>; | ||
} & { | ||
@@ -40,3 +41,3 @@ [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target>; | ||
export declare type Schema<Target = any, Source = any> = { | ||
[destinationProperty in keyof Target]?: ActionString<Source> | ActionFunction<Target, Source, Target[destinationProperty]> | ActionAggregator<Source> | ActionSelector<Source, Target[destinationProperty]> | Schema<Target[destinationProperty], Source>; | ||
[destinationProperty in keyof Target]?: ActionString<Source> | ActionFunction<Target, Source, Target[destinationProperty]> | ActionAggregator<Source> | ActionSelector<Source, Target> | Schema<Target[destinationProperty], Source>; | ||
} & { | ||
@@ -102,3 +103,3 @@ [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target | any>; | ||
*/ | ||
export declare type ActionString<Source> = string; | ||
export declare type ActionString<Source> = string | keyof Source; | ||
/** | ||
@@ -150,5 +151,6 @@ * An Array of String that allows to perform a function over source property | ||
*/ | ||
export interface ActionSelector<Source = object, R = any> { | ||
path: ActionString<Source> | ActionAggregator<Source>; | ||
fn: (fieldValue: any, object: Source, items: Source, objectToCompute: R) => R; | ||
export interface ActionSelector<Source = object, Target = any> { | ||
path?: ActionString<Source> | ActionAggregator<Source>; | ||
fn?: (fieldValue: any, object: Source, items: Source, objectToCompute: Target) => Target; | ||
validation?: BaseValidator<ReturnType<undefined extends ActionSelector['fn'] ? any : ActionSelector['fn']>>; | ||
} | ||
@@ -161,5 +163,11 @@ export interface Constructable<T> { | ||
export declare type ResultItem<TSchema extends Schema> = DestinationFromSchema<TSchema>; | ||
/** | ||
* Function to map an Input source towards a Target. Input can be a single object, or a collection of objects | ||
* @function | ||
* @typeparam TSchema Schema | ||
* @typeparam TResult Result Type | ||
*/ | ||
export interface Mapper<TSchema extends Schema | StrictSchema, TResult = ResultItem<TSchema>> { | ||
(data: SourceFromSchema<TSchema>[]): TResult[]; | ||
(data: SourceFromSchema<TSchema>): TResult; | ||
(data?: SourceFromSchema<TSchema>[] | null): TResult[]; | ||
(data?: SourceFromSchema<TSchema> | null): TResult; | ||
} |
{ | ||
"name": "morphism", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Do not repeat anymore your objects transformations.", | ||
@@ -77,3 +77,3 @@ "homepage": "https://github.com/nobrainr/morphism", | ||
"typedoc-plugin-internal-external": "^2.0.1", | ||
"typescript": "^3.4.2", | ||
"typescript": "^3.5.3", | ||
"webpack": "4.38.0", | ||
@@ -80,0 +80,0 @@ "webpack-cli": "^3.3.0" |
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
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
105904
20
666