UNPKG

@cmath10/js-mapper

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