UNPKG

@cmath10/js-mapper

Version:
2 lines (1 loc) 3.7 kB
var JsMapper=function(t){"use strict";const e=t=>{throw new Error("[cmath10/js-mapper] "+t)};class r{extract(t){return e("JMapExtractor::extract() not implemented yet for "+this.constructor.name)}}class s extends r{constructor(t){super(),this._callback=t}extract(t){return this._callback(t)}}const a=(t,r)=>{if("object"!=typeof t||null===t)return e(r)},i=(t,r,s)=>{if(0===r.length)return t;const n="Path "+[...s,...r].join(".")+" is not reachable in the source";var c,o,h;c=t,o=r[0],h=n,Object.prototype.hasOwnProperty.call(c,o)||e(h||"Object has no property "+o);const l=t[r[0]];return 1===r.length?l:(a(l,n),i(l,r.slice(1),[...s,r[0]]))};class n extends r{constructor(t,e){super(),this._path=t.split("."),this._fallback=e}extract(t){try{return a(t,"Path extracting not available for scalar types"),i(t,this._path,[])}catch(t){if(void 0!==this._fallback)return"function"==typeof this._fallback?this._fallback():this._fallback;throw t}}}class c{set mapper(t){this._mapper=t}filter(t){return t}}class o extends c{constructor(t){super(),this._callback=t}filter(t){return this._callback(t)}}class h extends c{constructor(t){super(),this._type=t}filter(t){if(void 0!==this._mapper)return this._mapper.map(this._type,t);e("Mapper was not set for filter")}}var l=t=>"string"==typeof t?new h(t):"function"==typeof t?new o(t):t;class p extends c{constructor(t){super(),this._filters=[],this._filters=t.map((t=>Array.isArray(t)?new p(t):l(t)))}filter(t){return this._filters.forEach((e=>{void 0!==this._mapper&&(e.mapper=this._mapper),t=e.filter(t)})),t}}class u{constructor(t=(()=>({}))){this._destination=t,this._extractors=new Map,this._filters=new Map,this._injectors=new Map}get extractors(){return this._extractors}get filters(){return this._filters}get injectors(){return this._injectors}destination(t){return this._destination=t,this}route(t,e,r){return this.forMember(t,new n(e,r))}forMember(t,e){return"function"==typeof e?this._extractors.set(t,new s(e)):this._extractors.set(t,e),this}filter(t,e){return Array.isArray(e)?this._filters.set(t,new p(e)):this._filters.set(t,l(e)),this}inject(t,e){return this._injectors.set(t,e),this}exclude(t){return[this._extractors,this._filters,this._injectors].forEach((e=>{e.has(t)&&e.delete(t)})),this}clone(){const t=new u(this._destination);return this._extractors.forEach(((e,r)=>{t.extractors.set(r,e)})),this._filters.forEach(((e,r)=>{t.filters.set(r,e)})),t}createDestination(){return this._destination()}}class f{inject(t,r,s){((t,r)=>{if("object"!=typeof t||null===t)e(r)})(t,"Scalar destinations not supported by default injector"),((t,e,r)=>{"function"==typeof t[e]?t[e].call(t,r):t[e]=r})(t,r,s)}}const _=new c,m=new f,y=(t,e,r,s)=>{const a=void 0!==s?s:e.createDestination();return e.extractors.forEach(((s,i)=>{const n=e.injectors.get(i)||m,c=e.filters.get(i)||_;c.mapper=t;const o=c.filter(s.extract(r));n.inject(a,i,o)})),a};return t.JsMap=u,t.JsMapCallbackExtractor=s,t.JsMapCallbackFilter=o,t.JsMapExtractor=r,t.JsMapFilter=c,t.JsMapFilterChain=p,t.JsMapInjector=f,t.JsMapMappingFilter=h,t.JsMapPathExtractor=n,t.default=class{constructor(){this._maps=new Map}get(t,r=!0){this._maps.has(t)||e("No map for type: "+t);const s=this._maps.get(t);return r?s.clone():s}set(t,e){return this._maps.set(t,e),this}unset(t){return this._maps.has(t)&&this._maps.delete(t),this}map(t,r,s){if("string"==typeof t&&((a=t).length>2&&"["===a[0]&&"]"===a[a.length-1]))return((t,r,s)=>{const a=t.get(r.substr(1,r.length-2));return Array.isArray(s)?s.map((e=>y(t,a,e,void 0))):e("For array maps source must be an array")})(this,t,r);var a;const i=t instanceof u?t:this.get(t,!1);return y(this,i,r,s)}},Object.defineProperty(t,"__esModule",{value:!0}),t}({});