Skip to content

Instantly share code, notes, and snippets.

@emyann
Created March 5, 2019 19:55
Show Gist options
  • Save emyann/505eba233c179d50b8c3103e98c3dda1 to your computer and use it in GitHub Desktop.
Save emyann/505eba233c179d50b8c3103e98c3dda1 to your computer and use it in GitHub Desktop.
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.morphism=e():t.morphism=e()}(this,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";function n(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){o(t,e,r[e])})}return t}function o(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}r.r(e);const i=(t,e)=>t.reduce((t,r)=>(function(t,e,r){const o=(e=(e=e.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),i=o.pop(),c=o.reduceRight((t,e)=>({[e]:t}),{[i]:r});return n({},t,c)})(t,r,p(e,r)),{});function c(t){return void 0===t}function u(t){const e=typeof t;return null!=t&&("object"===e||"function"===e)}function a(t){return"string"==typeof t||t instanceof String}function s(t){return"function"==typeof t}function p(t,e){const r=(e=(e=e.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split(".");for(let e=0,n=r.length;e<n;++e){const n=r[e];if(!(u(t)&&n in t))return;t=t[n]}return t}function f(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){l(t,e,r[e])})}return t}function l(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function h(t){return u(t)&&t.hasOwnProperty("fn")&&t.hasOwnProperty("path")}function d(t){return Array.isArray(t)}function y(t){return a(t)}var g,m;r.d(e,"isActionSelector",function(){return h}),r.d(e,"morphism",function(){return A}),r.d(e,"MorphismRegistry",function(){return P}),r.d(e,"morph",function(){return M}),r.d(e,"toJSObject",function(){return $}),r.d(e,"toClassObject",function(){return _}),function(t){t.ActionFunction="ActionFunction",t.ActionAggregator="ActionAggregator",t.ActionString="ActionString",t.ActionSelector="ActionSelector"}(g||(g={})),function(t){t.Root="Root"}(m||(m={}));class b{constructor(){this.root=void 0,this.root={data:{targetPropertyPath:"",propertyName:"MorphismTreeRoot",action:null,kind:m.Root},parent:null,children:[]}}*traverseBFS(){const t=[];t.push(this.root);let e=t.shift();for(;e;){for(let r=0,n=e.children.length;r<n;r++)t.push(e.children[r]);e.data.kind!==m.Root&&(yield e),e=t.shift()}}add(t){if(!t.action)throw new Error(`Action of ${t.propertyName} can't be ${t.action}.`);const e=this.getActionKind(t.action);if(!e)throw new Error(`The action specified for ${t.propertyName} is not supported.`);const r={data:f({},t,{kind:e,targetPropertyPath:""}),parent:null,children:[]};r.data.preparedAction=this.getPreparedAction(r.data),r.parent=this.root,r.data.targetPropertyPath=r.data.propertyName,this.root.children.push(r)}getActionKind(t){return y(t)?g.ActionString:s(t)?g.ActionFunction:h(t)?g.ActionSelector:d(t)?g.ActionAggregator:void 0}getPreparedAction(t){const{propertyName:e,action:r}=t;if(y(r))return({object:t})=>({[e]:p(t,r)});if(s(r))return({object:t,items:n,objectToCompute:o})=>({[e]:r.call(void 0,t,n,o)});if(d(r))return({object:t})=>({[e]:i(r,t)});if(h(r))return({object:t,items:n,objectToCompute:o})=>{let c;try{let u;Array.isArray(r.path)?u=i(r.path,t):a(r.path)&&(u=p(t,r.path)),c=r.fn.call(void 0,u,t,n,o)}catch(t){throw t.message=`Unable to set target property [${e}].\n \n An error occured when applying [${r.fn.name}] on property [${r.path}]\n \n Internal error: ${t.message}`,t}return{[e]:c}};throw new Error(`The action specified for ${e} is not supported.`)}}function w(t){const e=new b;return function t(e,r,n){if(o=r,(a(o)||s(o)||h(o)||d(o))&&n)return void e.add({propertyName:n,action:r});var o;Object.keys(r).forEach(n=>{t(e,r[n],n)})}(e,t),e}function j(t,e,r,n){const o=[];for(const i of e.traverseBFS()){const{preparedAction:e}=i.data;e&&o.push(e({object:t,objectToCompute:n,items:r}))}return o.reduce((t,e)=>{return function(t,e,r){return Object.entries(e).forEach(([e,n])=>{t[e]=r?r(t[e],n):n}),t}(t,e,(t,e)=>c(e)?c(t)?void 0:t:e)},n)}function v(t,e){const r=w(t);return function(t){if(!t)return t;if(Array.isArray(t))return t.map(n=>{if(e){const o=new e;return j(n,r,t,o)}return j(n,r,t,{})});{const n=t;if(e){const t=new e;return j(n,r,[n],t)}return j(n,r,[n],{})}}}function O(t,e){let r=Object.keys(new t),o=(i=r,r.reduce((t,e,r)=>n({},t,{[e]:i[r]}),{}));var i;return Object.assign(o,e)}function A(t,e,r){if(void 0===e&&void 0===r)return v(t);if(t&&e&&r){return v(O(r,t),r)(e)}if(t&&e)return v(t)(e);if(r&&e){return v(O(r,t),r)(e)}if(r){let e=O(r,t);return t=>v(e,r)(t)}}class P{constructor(t){this._registry=null,this._registry=t||{cache:new Map}}register(t,e={}){if(!t&&!e)throw new Error("type paramater is required when register a mapping");if(this.exists(t))throw new Error(`A mapper for ${t.name} has already been registered`);const r=A(e,null,t);return this._registry.cache.set(t,r),r}map(t,e){if(!this.exists(t)){const r=this.register(t);if(void 0===e)return r}return this.getMapper(t)(e)}getMapper(t){return this._registry.cache.get(t)}setMapper(t,e){if(e){if(this.exists(t)){let r=A(e,null,t);return this._registry.cache.set(t,r),r}throw new Error(`The type ${t.name} is not registered. Register it using \`Mophism.register(${t.name}, schema)\``)}throw new Error(`The schema must be an Object. Found ${e}`)}deleteMapper(t){return this._registry.cache.delete(t)}exists(t){return this._registry.cache.has(t)}get mappers(){return this._registry.cache}}function S(t){return(e,r,n)=>{const o=n.value;return"function"==typeof o&&(n.value=function(...e){const r=o.apply(this,e);return function(t){if(Promise&&Promise.resolve)return Promise.resolve(t)==t;throw"Promise not supported in your environment"}(r)?Promise.resolve(r).then(e=>t(e)):t(r)}),n}}function M(t,e){return S(v(t,e))}function $(t){return S(v(t))}function _(t,e){return S(v(t,e))}const x=new P,E=A;E.register=((t,e)=>x.register(t,e)),E.map=((t,e)=>x.map(t,e)),E.getMapper=(t=>x.getMapper(t)),E.setMapper=((t,e)=>x.setMapper(t,e)),E.deleteMapper=(t=>x.deleteMapper(t)),E.mappers=x.mappers;const T=E;e.default=T}])});
//# sourceMappingURL=morphism.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment