UNPKG

test-openapi

Version:
109 lines (70 loc) 2.03 kB
"use strict";require("core-js/modules/es.string.replace");Object.defineProperty(exports,"__esModule",{value:true});exports.tryGet=exports.get=void 0; const get=function(value,path){ const pathA=removeBrackets({path}); return getProperty(value,pathA); };exports.get=get; const getProperty=function(value,path){ if(!isComplex(value)){ return; } if({}.propertyIsEnumerable.call(value,path)){ return value[path]; } const property=Object.keys(value). filter(key=>path.startsWith(`${key}.`)). reduce(getLargestString,""); if(property===""){ return; } const child=value[property]; const childPath=path.replace(`${property}.`,""); return getProperty(child,childPath); }; const isComplex=function(value){ return typeof value==="object"&&value!==null; }; const getLargestString=function(memo,string){ if(string.length>=memo.length){ return string; } return memo; }; const tryGet=function(value,path){ const pathA=splitPath({path}); const wrongIndex=pathA.findIndex((valueA,index)=> isWrongPath({path:pathA,value,index})); if(wrongIndex===0){ return{wrongPath:pathA[0],value}; } const wrongPath=getWrongPath({path:pathA,index:wrongIndex}); const parentPath=getParentPath({path:pathA,index:wrongIndex}); const childValue=get(value,parentPath); const valueB={[parentPath]:childValue}; return{wrongPath,value:valueB}; };exports.tryGet=tryGet; const splitPath=function({path}){ const pathA=removeBrackets({path}); const pathB=pathA.split("."); return pathB; }; const isWrongPath=function({path,value,index}){ const pathB=path.slice(0,index+1).join("."); return get(value,pathB)===undefined; }; const getWrongPath=function({path,index}){ if(index===-1){ return; } return path.slice(0,index+1).join("."); }; const getParentPath=function({path,index}){ if(index===-1){ return path.join("."); } return path.slice(0,index).join("."); }; const removeBrackets=function({path}){ return path.replace(BRACKETS_REGEXP,".$1").replace(/^\./u,""); }; const BRACKETS_REGEXP=/\[([0-9]+)\]/gu; //# sourceMappingURL=get.js.map