UNPKG

@cmath10/js-mapper

Version:
2 lines (1 loc) 3.89 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).JsMapper={})}(this,(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 i=(t,r)=>{if("object"!=typeof t||null===t)return e(r)},a=(t,r,s)=>{if(0===r.length)return t;const n="Path "+[...s,...r].join(".")+" is not reachable in the source";var o,c,l;o=t,c=r[0],l=n,Object.prototype.hasOwnProperty.call(o,c)||e(l||"Object has no property "+c);const h=t[r[0]];return 1===r.length?h:(i(h,n),a(h,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 i(t,"Path extracting not available for scalar types"),a(t,this._path,[])}catch(t){if(void 0!==this._fallback)return"function"==typeof this._fallback?this._fallback():this._fallback;throw t}}}class o{set mapper(t){this._mapper=t}filter(t){return t}}class c extends o{constructor(t){super(),this._callback=t}filter(t){return this._callback(t)}}class l extends o{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 h=t=>"string"==typeof t?new l(t):"function"==typeof t?new c(t):t;class p extends o{constructor(t){super(),this._filters=[],this._filters=t.map((t=>Array.isArray(t)?new p(t):h(t)))}filter(t){return this._filters.forEach((e=>{void 0!==this._mapper&&(e.mapper=this._mapper),t=e.filter(t)})),t}}class f{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,h(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 f(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 u{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 o,d=new u,y=(t,e,r,s)=>{const i=void 0!==s?s:e.createDestination();return e.extractors.forEach(((s,a)=>{const n=e.injectors.get(a)||d,o=e.filters.get(a)||_;o.mapper=t;const c=o.filter(s.extract(r));n.inject(i,a,c)})),i};t.JsMap=f,t.JsMapCallbackExtractor=s,t.JsMapCallbackFilter=c,t.JsMapExtractor=r,t.JsMapFilter=o,t.JsMapFilterChain=p,t.JsMapInjector=u,t.JsMapMappingFilter=l,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&&((i=t).length>2&&"["===i[0]&&"]"===i[i.length-1]))return((t,r,s)=>{const i=t.get(r.substr(1,r.length-2));return Array.isArray(s)?s.map((e=>y(t,i,e,void 0))):e("For array maps source must be an array")})(this,t,r);var i;const a=t instanceof f?t:this.get(t,!1);return y(this,a,r,s)}},Object.defineProperty(t,"__esModule",{value:!0})}));