UNPKG

smart-path-resolver

Version:

A lightweight and fast JavaScript/TypeScript library for resolving object paths with support for arrays, Maps, Sets, and function calls.

2 lines 5.38 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("smart-path-resolver",[],e):"object"==typeof exports?exports["smart-path-resolver"]=e():t["smart-path-resolver"]=e()}(this,(()=>(()=>{"use strict";var t={99:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.PathResolver=void 0;const n=r(41);class s{context;options;tokenCache;static DEFAULT_OPTIONS={useCache:!0,maxCacheSize:1e3};constructor(t,e){this.context=t,this.options={...s.DEFAULT_OPTIONS,...e},this.tokenCache=new Map}tokenise(t){if(this.options.useCache){const e=this.tokenCache.get(t);if(e)return e}const e=t.split(".").map((t=>{if(n.Utils.isArraySegment(t))return this.parseArrayOrMapAndSet(t);if(n.Utils.isFunctionSegment(t))return this.parseFunction(t);if(n.Utils.isIdentifierSegment(t))return{segment:t,type:"property"};throw new Error("Invalid segment format")}));if(this.options.useCache){if(this.tokenCache.size>=this.options.maxCacheSize){const t=this.tokenCache.keys().next().value;this.tokenCache.delete(t)}this.tokenCache.set(t,e)}return e}async resolve(t){if(!t)throw new Error("Path cannot be empty");const e=this.tokenise(t);return this._resolve(e,this.context,0)}resolveSync(t){if(!t)throw new Error("Path cannot be empty");const e=this.tokenise(t);let r=this.context;for(const t of e){if(!n.Utils.isObject(r))throw new Error(`Cannot access property "${t.segment}" of ${r}`);switch(t.type){case"array":if(!n.Utils.isArray(r[t.segment]))throw new Error(`Property "${t.segment}" is not an array`);if(r=r[t.segment][t.arrayIndex],void 0===r)throw new Error("Invalid Array Index");break;case"mapOrSet":const e=r[t.segment];if(!n.Utils.isMap(e)&&!n.Utils.isSet(e))throw new Error(`Property "${t.segment}" is not a map or set`);if(n.Utils.isMap(e)&&(r=e.get(t.mapOrSetKey)),n.Utils.isSet(e)&&(r=e.has(t.mapOrSetKey)),void 0===r)throw new Error("Invalid Map or Set key");break;case"function":const s=r[t.segment];if(!n.Utils.isFunction(s))throw new Error(`Property "${t.segment}" is not a function`);if(n.Utils.isAsyncFunction(s))throw new Error(`Cannot call async function "${t.segment}" in sync mode`);if(r=s.apply(r,t.functionArgs),void 0===r)throw new Error("Function return undefined value");break;default:r=r[t.segment]}}if(void 0===r)throw new Error("Invalid Path");return r}async _resolve(t,e,r){if(r>=t.length)return e;if(!n.Utils.isObject(e))throw new Error("Cannot access properties of "+e);const s=t[r];let i;switch(s.type){case"array":const t=e[s.segment];if(!n.Utils.isArray(t))throw new Error(`Property "${s.segment}" is not an array`);if(i=t[s.arrayIndex],void 0===i)throw new Error("Invalid Array Index");break;case"mapOrSet":const r=e[s.segment];if(!n.Utils.isMap(r)&&!n.Utils.isSet(r))throw new Error(`Property "${s.segment}" is not a map or set`);if(n.Utils.isMap(r)&&(i=r.get(s.mapOrSetKey)),n.Utils.isSet(r)&&(i=r.has(s.mapOrSetKey)),void 0===i)throw new Error("Invalid Map or Set key");break;case"function":const o=e[s.segment];if(!n.Utils.isFunction(o))throw new Error(`Property "${s.segment}" is not a function`);if(i=n.Utils.isAsyncFunction(o)?await o.apply(e,s.functionArgs):o.apply(e,s.functionArgs),void 0===i)throw new Error("Function return undefined value");break;default:i=e[s.segment]}return this._resolve(t,i,r+1)}parseArrayOrMapAndSet(t){const e=t.match(n.Utils.ARRAY_REGEX);if(!e)throw new Error(`Invalid segment: ${t}`);const[,r,s]=e,i=!isNaN(Number(s));return{segment:r,type:i?"array":"mapOrSet",arrayIndex:i?Number(s):void 0,mapOrSetKey:i?void 0:s}}parseFunction(t){const e=t.match(n.Utils.FUNCTION_REGEX);if(!e)throw new Error(`Invalid function segment: ${t}`);const[,r,s]=e;return{segment:r,type:"function",functionArgs:s?s.split(",").map((t=>{const e=t.trim();return isNaN(Number(e))?e:Number(e)})):[]}}clearCache(){this.tokenCache.clear()}}e.PathResolver=s},41:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Utils=void 0;class r{static FUNCTION_REGEX=/([\w]+)\((([\w]+)(,[\w]+)*)?\)/;static ARRAY_REGEX=/([\w]+)\[(\w+)\]/;static IDENTIFIER_REGEX=/^[a-zA-Z_$][a-zA-Z0-9_$]*$/;static isAsyncFunction(t){return"AsyncFunction"===t.constructor.name}static isFunctionSegment(t){return r.FUNCTION_REGEX.test(t)}static isArraySegment(t){return r.ARRAY_REGEX.test(t)}static isIdentifierSegment(t){return r.IDENTIFIER_REGEX.test(t)}static isObject(t){return null!==t&&"object"==typeof t}static isFunction(t){return"function"==typeof t}static isArray(t){return Array.isArray(t)}static isMap(t){return t instanceof Map}static isSet(t){return t instanceof Set}}e.Utils=r},156:function(t,e,r){var n=this&&this.__createBinding||(Object.create?function(t,e,r,n){void 0===n&&(n=r);var s=Object.getOwnPropertyDescriptor(e,r);s&&!("get"in s?!e.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,n,s)}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]}),s=this&&this.__exportStar||function(t,e){for(var r in t)"default"===r||Object.prototype.hasOwnProperty.call(e,r)||n(e,t,r)};Object.defineProperty(e,"__esModule",{value:!0}),s(r(99),e),s(r(41),e),s(r(613),e)},613:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0})}},e={};var r=function r(n){var s=e[n];if(void 0!==s)return s.exports;var i=e[n]={exports:{}};return t[n].call(i.exports,i,i.exports,r),i.exports}(156);return r})())); //# sourceMappingURL=index.js.map