json-reference-parser
Version:
A parser to dereference references given in json
1 lines • 1.19 kB
JavaScript
module.exports=(()=>{var e={45:e=>{function r(){}r.prototype.dereference=function(e){try{return this.json=e,"string"==typeof e&&(this.json=JSON.parse(e)),this.dereferenceCache={},this.iterateJSON(this.json,this.json,[],[]).value}catch(e){console.error("Failed to derefer ",e.message)}},r.prototype.iterateJSON=function(e,r,t,n){let s,o={value:e};if(-1==n.indexOf(e))if(e&&"object"==typeof e&&!ArrayBuffer.isView(e)&&(t.push[e],n.push(e)),this.isReference(e))s=this.dereferenceReference(e,r),o.value=s.value;else for(let o of Object.keys(e)){let f=e[o];this.isReference(f)?(s=this.dereferenceReference(f,r),e[o]=s.value):"object"==typeof f&&-1==t.indexOf(f)&&(s=this.iterateJSON(f,r,t,n),e[o]!=s.value&&(e[o]=s.value))}return t.pop(),o},r.prototype.isReference=function(e){return e&&"object"==typeof e&&"string"==typeof e.$ref&&e.$ref.length>0&&"#/"===e.$ref.substr(0,2)},r.prototype.dereferenceReference=function(e,r){let t=e.$ref.split("/").slice(1);return this.dereferenceCache[t]?{value:this.dereferenceCache[t]}:{value:t.reduce(((e,r)=>e[r]),r)}},e.exports=r}},r={};return function t(n){if(r[n])return r[n].exports;var s=r[n]={exports:{}};return e[n](s,s.exports,t),s.exports}(45)})();