Socket
Socket
Sign inDemoInstall

@stoplight/json

Package Overview
Dependencies
Maintainers
26
Versions
107
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/json - npm Package Compare versions

Comparing version 3.17.0 to 3.17.1

2

index.cjs.js

@@ -1,1 +0,1 @@

"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var lodash=require("lodash"),jsoncParser=require("jsonc-parser"),createOrderedObject=require("@stoplight/ordered-object-literal"),createOrderedObject__default=_interopDefault(createOrderedObject),types=require("@stoplight/types"),fastStringify=_interopDefault(require("safe-stable-stringify"));const hasRef=e=>lodash.isObject(e)&&"$ref"in e&&"string"==typeof e.$ref,isLocalRef=e=>e.length>0&&("#"===e||/^#\S*$/.test(e)),replaceInString=(e,t,r)=>{const n=e.toString();let o="",i=n,a=0,s=i.indexOf(t);for(;s>-1;)o+=n.substring(a,a+s)+r,i=i.substring(s+t.length,i.length),a+=s+t.length,s=i.indexOf(t);return i.length>0&&(o+=n.substring(n.length-i.length,n.length)),o},encodePointerFragment=e=>"number"==typeof e?e:replaceInString(replaceInString(e,"~","~0"),"/","~1"),pathToPointer=e=>encodeUriFragmentIdentifier(e),encodeUriFragmentIdentifier=e=>{if(e&&"object"!=typeof e)throw new TypeError("Invalid type: path must be an array of segments.");return 0===e.length?"#":`#/${e.map(encodePointerFragment).join("/")}`};function safeDecodeURIComponent(e){try{return decodeURIComponent(e)}catch(t){return e}}const PERCENT_ENCODING_OCTET=/%[0-9a-f]+/gi,decodePointer=e=>{let t;try{t=decodeURIComponent(e)}catch(r){t=e.replace(PERCENT_ENCODING_OCTET,safeDecodeURIComponent)}return replaceInString(replaceInString(t,"~1","/"),"~0","~")},pointerToPath=e=>decodeUriFragmentIdentifier(e),decodeFragmentSegments=e=>{const t=e.length,r=[];let n=-1;for(;++n<t;)r.push(decodePointer(e[n]));return r},decodeUriFragmentIdentifier=e=>{if("string"!=typeof e)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===e.length||"#"!==e[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===e.length)return[];if("/"!==e[1])throw new URIError("Invalid JSON Pointer syntax.");return decodeFragmentSegments(e.substring(2).split("/"))},extractSourceFromRef=e=>{if("string"!=typeof e||0===e.length||isLocalRef(e))return null;const t=e.indexOf("#");return-1===t?e:e.slice(0,t)};function isPlainObject(e){if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype||"function"==typeof e.constructor&&Function.toString.call(Object)===Function.toString.call(e.constructor)}function _resolveInlineRef(e,t,r){if(null!==extractSourceFromRef(t))throw new ReferenceError("Cannot resolve external references");const n=pointerToPath(t);let o=e;for(const i of n){if(!isPlainObject(o)&&!Array.isArray(o)||!(i in o))throw new ReferenceError(`Could not resolve '${t}'`);if(isPlainObject(o=o[i])&&"$ref"in o){if(r.includes(o))return r[r.length-1];if(r.push(o),"string"!=typeof o.$ref)throw new TypeError("$ref should be a string");o=_resolveInlineRef(e,o.$ref,r)}}if(0===r.length)return o;const i=r[r.length-1];return isPlainObject(i)&&("summary"in i||"description"in i)?Object.assign(Object.assign(Object.assign({},o),"description"in i?{description:i.description}:null),"summary"in i?{summary:i.summary}:null):o}function resolveInlineRef(e,t){return _resolveInlineRef(e,t,[])}const _traverse=(e,t,r)=>{const n={value:e,path:r};t.onEnter&&t.onEnter(n);for(const n of Object.keys(e)){const o=e[n];t.onProperty&&t.onProperty({parent:e,parentPath:r,property:n,propertyValue:o}),"object"==typeof o&&null!==o&&_traverse(o,t,r.concat(n))}t.onLeave&&t.onLeave(n)},traverse=(e,t)=>{"object"==typeof e&&null!==e&&_traverse(e,"function"==typeof t?{onProperty:t}:t,[])},BUNDLE_ROOT="#/__bundled__",ERRORS_ROOT="#/__errors__",bundleTarget=({document:e,path:t,bundleRoot:r=BUNDLE_ROOT,errorsRoot:n=ERRORS_ROOT,cloneDocument:o=!0},i)=>{if(t===r||t===n)throw new Error("Roots do not make any sense");const a=o?lodash.cloneDeep(e):e;return bundle(a,pointerToPath(r),pointerToPath(n))(t,{[t]:!0},i)},bundle=(e,t,r)=>{const n=new Set,o=(i,a,s,c={},l={},f={})=>{const p=pointerToPath(i),u=lodash.get(e,p);traverse(s||u,({parent:r})=>{if(hasRef(r)&&isLocalRef(r.$ref)){const s=r.$ref;if(f[s])return;if(c[s])return void(r.$ref=c[s]);let p,u,d,g,h;try{let r;if(p=pointerToPath(s),Array.isArray(lodash.get(e,p.slice(0,-1)))){r=`${p[p.length-2]}_${p[p.length-1]}`}else r=p[p.length-1];d=r;let o=1;for(;n.has(d);)if(d=`${r}_${++o}`,o>20)throw new Error(`Keys ${r}_2 through ${r}_20 already taken.`);n.add(d),u=[...t,d],g=pathToPointer(u)}catch(e){f[s]=e.message}if(!p||!u||!g)return;if("object"==typeof e&&null!==e)try{h=resolveInlineRef(Object(e),s)}catch(t){h=lodash.get(e,p)}if(void 0!==h&&(c[s]=g,r.$ref=g,!lodash.has(l,u))){const t=lodash.get(e,p);Array.isArray(t)?lodash.set(l,u,new Array(t.length).fill(null)):"object"==typeof t&&lodash.set(l,u,{}),lodash.set(l,u,h),a[s]||(a[s]=!0,o(i,a,h,c,l,f),a[s]=!1)}}});const d=lodash.get(l,t);return d&&Object.keys(d).length&&lodash.set(u,t,d),(Object.keys(f).length||lodash.has(e,r))&&lodash.set(u,r,lodash.has(e,r)?lodash.get(e,r):f),u};return o},decodePointerFragment=e=>replaceInString(replaceInString(e,"~1","/"),"~0","~"),decycle=(e,t)=>{const r=new WeakMap;return function e(n,o){let i;if(t&&(n=t(n)),isPlainObject(n)||Array.isArray(n)){const t=r.get(n);return t?{$ref:t}:(r.set(n,pathToPointer(o)),Array.isArray(n)?i=n.map((t,r)=>e(t,[...o,String(r)])):(i={},Object.keys(n).forEach(t=>{i[t]=e(n[t],[...o,t])})),r.delete(n),i)}return n}(e,[])},encodePointer=e=>replaceInString(replaceInString(e,"~","~0"),"//","/~1"),extractPointerFromRef=e=>{if("string"!=typeof e||0===e.length)return null;const t=e.indexOf("#");return-1===t?null:e.slice(t)},getFirstPrimitiveProperty=e=>{const t=jsoncParser.createScanner(e,!0);if(t.scan(),1!==t.getToken())return;if(t.scan(),2===t.getToken())return;if(10!==t.getToken())throw new SyntaxError("Unexpected character");const r=t.getTokenValue();if(t.scan(),6!==t.getToken())throw new SyntaxError("Colon expected");switch(t.scan(),t.getToken()){case 10:return[r,t.getTokenValue()];case 11:return[r,Number(t.getTokenValue())];case 8:return[r,!0];case 9:return[r,!1];case 7:return[r,null];case 16:throw new SyntaxError("Unexpected character");case 17:throw new SyntaxError("Unexpected end of file");default:return}},getJsonPathForPosition=({lineMap:e,ast:t},r)=>{const n=e[r.line],o=e[r.line+1];if(void 0===n)return;const i=jsoncParser.findNodeAtOffset(t,void 0===o?n+r.character:Math.min(o,n+r.character),!0);if(void 0===i)return;const a=jsoncParser.getNodePath(i);return 0!==a.length?a:void 0};function getLastPathSegment(e){return decodePointerFragment(e.split("/").pop()||"")}const getLocationForJsonPath=({lineMap:e,ast:t},r,n=!1)=>{const o=findNodeAtPath(t,r,n);if(void 0!==o&&void 0!==o.range)return{range:o.range}};function findNodeAtPath(e,t,r){e:for(const n of t){const t=Number.isInteger(Number(n))?Number(n):n;if("string"==typeof t||"number"==typeof t&&"array"!==e.type){if("object"!==e.type||!Array.isArray(e.children))return r?e:void 0;for(const r of e.children)if(Array.isArray(r.children)&&r.children[0].value===String(t)&&2===r.children.length){e=r.children[1];continue e}return r?e:void 0}if("array"!==e.type||t<0||!Array.isArray(e.children)||t>=e.children.length)return r?e:void 0;e=e.children[t]}return e}const parseWithPointers=(e,t={disallowComments:!0})=>{const r=[],{ast:n,data:o,lineMap:i}=parseTree(e,r,t);return{data:o,diagnostics:r,ast:n,lineMap:i}};function parseTree(e,t=[],r){const n=computeLineMap(e);let o={type:"array",offset:-1,length:-1,children:[],parent:void 0},i=null,a=[];const s=new WeakMap,c=[];function l(e){"property"===o.type&&(o.length=e-o.offset,o=o.parent)}function f(e,t,r){return{start:{line:e,character:t},end:{line:e,character:t+r}}}function p(e){return o.children.push(e),e}function u(e){Array.isArray(a)?a.push(e):null!==i&&(a[i]=e)}function d(e){u(e),c.push(a),a=e,i=null}function g(){a=c.pop()}const h={onObjectBegin:(e,t,n,i)=>{o=p({type:"object",offset:e,length:-1,parent:o,children:[],range:f(n,i,t)}),!1===r.ignoreDuplicateKeys&&s.set(o,[]),d(createObjectLiteral(!0===r.preserveKeyOrder))},onObjectProperty:(e,n,c,l,u)=>{if((o=p({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:c,parent:o}),!1===r.ignoreDuplicateKeys){const r=s.get(o.parent);r&&(0!==r.length&&r.includes(e)?t.push({range:f(l,u,c),message:"DuplicateKey",severity:types.DiagnosticSeverity.Error,path:getJsonPath(o),code:20}):r.push(e))}!0===r.preserveKeyOrder&&swapKey(a,e),i=e},onObjectEnd:(e,t,n,i)=>{!1===r.ignoreDuplicateKeys&&s.delete(o),o.length=e+t-o.offset,o.range&&(o.range.end.line=n,o.range.end.character=i+t),o=o.parent,l(e+t),g()},onArrayBegin:(e,t,r,n)=>{o=p({type:"array",offset:e,length:-1,parent:o,children:[],range:f(r,n,t)}),d([])},onArrayEnd:(e,t,r,n)=>{o.length=e+t-o.offset,o.range&&(o.range.end.line=r,o.range.end.character=n+t),o=o.parent,l(e+t),g()},onLiteralValue:(e,t,r,n,i)=>{p({type:getLiteralNodeType(e),offset:t,length:r,parent:o,value:e,range:f(n,i,r)}),l(t+r),u(e)},onSeparator:(e,t,r)=>{"property"===o.type&&(":"===e?o.colonOffset=t:","===e&&l(t))},onError:(e,r,n,o,i)=>{t.push({range:f(o,i,n),message:jsoncParser.printParseErrorCode(e),severity:types.DiagnosticSeverity.Error,code:e})}};jsoncParser.visit(e,h,r);const y=o.children[0];return y&&delete y.parent,{ast:y,data:a[0],lineMap:n}}function getLiteralNodeType(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const computeLineMap=e=>{const t=[0];let r=0;for(;r<e.length;r++)"\n"===e[r]&&t.push(r+1);return t.push(r+1),t};function getJsonPath(e,t=[]){return"property"===e.type&&t.unshift(e.children[0].value),void 0!==e.parent?("array"===e.parent.type&&void 0!==e.parent.parent&&t.unshift(e.parent.children.indexOf(e)),getJsonPath(e.parent,t)):t}function createObjectLiteral(e){return e?createOrderedObject__default({}):{}}function swapKey(e,t){if(!(t in e))return;const r=createOrderedObject.getOrder(e),n=r.indexOf(t);-1!==n&&(r.splice(n,1),r.push(t))}const renameObjectKey=(e,t,r)=>{if(!e||!Object.hasOwnProperty.call(e,t)||t===r)return e;const n={};for(const[o,i]of Object.entries(e))o===t?n[r]=i:o in n||(n[o]=i);return n};function isObject(e){return isPlainObject(e)||Array.isArray(e)}function reparentBundleTarget(e,t,r){if(r.length<=1||t.length<=1)throw Error("Source/target path must not be empty and point at root");if(0===t.indexOf(r))throw Error("Target path cannot be contained within source");const n=pointerToPath(t);let o=e;for(const e of n){if(!isObject(o))return;o=o[e]}if(!isObject(o))return;const i=pointerToPath(r);let a=e;for(const[e,t]of i.entries()){if(!isObject(a)||t in a)return;const r=e===i.length-1?o:{};a[t]=r,a=r}delete e[n[0]],_reparentBundleTarget(e,t,r)}function _reparentBundleTarget(e,t,r){for(const n of Object.keys(e)){const o=e[n];if("$ref"!==n)isObject(o)&&_reparentBundleTarget(o,t,r);else{if("string"!=typeof o||!isLocalRef(o))continue;0===o.indexOf(t)&&(e[n]=o.replace(t,r))}}}const safeParse=(e,t)=>{if("string"!=typeof e)return e;try{const r=parseNumber(e);return"string"==typeof r?r:JSON.parse(e,t)}catch(e){return}},parseNumber=e=>{const t=Number(e);return Number.isFinite(t)?String(t)===e?t:e:NaN},safeStringify=(e,t,r)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,t,r)}catch(n){return fastStringify(e,t,r)}},startsWith=(e,t)=>{if(e instanceof Array){if(t instanceof Array){if(t.length>e.length)return!1;for(const r in t){if(!t.hasOwnProperty(r))continue;const n=parseInt(e[r]),o=parseInt(t[r]);if(isNaN(n)&&isNaN(o)){if(e[r]!==t[r])return!1}else if(n!==o)return!1}}}else{if("string"!=typeof e)return!1;if("string"==typeof t)return e.startsWith(t)}return!0},stringify=(e,t,r)=>{const n=safeStringify(e,t,r);if(void 0===n)throw new Error("The value could not be stringified");return n};function toPropertyPath(e){return e.replace(/^(\/|#\/)/,"").split("/").map(decodePointerFragment).map(sanitize).join(".")}function sanitize(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}const KEYS=Symbol.for(createOrderedObject.ORDER_KEY_ID),traps={ownKeys:e=>KEYS in e?e[KEYS]:Reflect.ownKeys(e)},trapAccess=e=>new Proxy(e,traps);function trimStart(e,t){if("string"==typeof e&&"string"==typeof t)return lodash.trimStart(e,t);if(!(e&&Array.isArray(e)&&e.length&&t&&Array.isArray(t)&&t.length))return e;let r=0;for(const n in e)if(e.hasOwnProperty(n)){if(e[n]!==t[n])break;r++}return e.slice(r)}exports.BUNDLE_ROOT=BUNDLE_ROOT,exports.ERRORS_ROOT=ERRORS_ROOT,exports.KEYS=KEYS,exports.bundleTarget=bundleTarget,exports.decodePointer=decodePointer,exports.decodePointerFragment=decodePointerFragment,exports.decycle=decycle,exports.encodePointer=encodePointer,exports.encodePointerFragment=encodePointerFragment,exports.extractPointerFromRef=extractPointerFromRef,exports.extractSourceFromRef=extractSourceFromRef,exports.getFirstPrimitiveProperty=getFirstPrimitiveProperty,exports.getJsonPathForPosition=getJsonPathForPosition,exports.getLastPathSegment=getLastPathSegment,exports.getLocationForJsonPath=getLocationForJsonPath,exports.hasRef=hasRef,exports.isLocalRef=isLocalRef,exports.isPlainObject=isPlainObject,exports.parseTree=parseTree,exports.parseWithPointers=parseWithPointers,exports.pathToPointer=pathToPointer,exports.pointerToPath=pointerToPath,exports.renameObjectKey=renameObjectKey,exports.reparentBundleTarget=reparentBundleTarget,exports.resolveInlineRef=resolveInlineRef,exports.safeParse=safeParse,exports.safeStringify=safeStringify,exports.startsWith=startsWith,exports.stringify=stringify,exports.toPropertyPath=toPropertyPath,exports.trapAccess=trapAccess,exports.traverse=traverse,exports.trimStart=trimStart;
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var lodash=require("lodash"),jsoncParser=require("jsonc-parser"),createOrderedObject=require("@stoplight/ordered-object-literal"),createOrderedObject__default=_interopDefault(createOrderedObject),types=require("@stoplight/types"),fastStringify=_interopDefault(require("safe-stable-stringify"));const hasRef=e=>lodash.isObject(e)&&"$ref"in e&&"string"==typeof e.$ref,isLocalRef=e=>e.length>0&&("#"===e||/^#\S*$/.test(e)),replaceInString=(e,t,r)=>{const n=e.toString();let o="",i=n,a=0,s=i.indexOf(t);for(;s>-1;)o+=n.substring(a,a+s)+r,i=i.substring(s+t.length,i.length),a+=s+t.length,s=i.indexOf(t);return i.length>0&&(o+=n.substring(n.length-i.length,n.length)),o},encodePointerFragment=e=>"number"==typeof e?e:replaceInString(replaceInString(e,"~","~0"),"/","~1"),pathToPointer=e=>encodeUriFragmentIdentifier(e),encodeUriFragmentIdentifier=e=>{if(e&&"object"!=typeof e)throw new TypeError("Invalid type: path must be an array of segments.");return 0===e.length?"#":`#/${e.map(encodePointerFragment).join("/")}`};function safeDecodeURIComponent(e){try{return decodeURIComponent(e)}catch(t){return e}}const PERCENT_ENCODING_OCTET=/%[0-9a-f]+/gi,decodePointer=e=>{let t;try{t=decodeURIComponent(e)}catch(r){t=e.replace(PERCENT_ENCODING_OCTET,safeDecodeURIComponent)}return replaceInString(replaceInString(t,"~1","/"),"~0","~")},pointerToPath=e=>decodeUriFragmentIdentifier(e),decodeFragmentSegments=e=>{const t=e.length,r=[];let n=-1;for(;++n<t;)r.push(decodePointer(e[n]));return r},decodeUriFragmentIdentifier=e=>{if("string"!=typeof e)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===e.length||"#"!==e[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===e.length)return[];if("/"!==e[1])throw new URIError("Invalid JSON Pointer syntax.");return decodeFragmentSegments(e.substring(2).split("/"))},extractSourceFromRef=e=>{if("string"!=typeof e||0===e.length||isLocalRef(e))return null;const t=e.indexOf("#");return-1===t?e:e.slice(0,t)};function isPlainObject(e){if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype||"function"==typeof e.constructor&&Function.toString.call(Object)===Function.toString.call(e.constructor)}function _resolveInlineRef(e,t,r){if(null!==extractSourceFromRef(t))throw new ReferenceError("Cannot resolve external references");const n=pointerToPath(t);let o=e;for(const i of n){if(!isPlainObject(o)&&!Array.isArray(o)||!(i in o))throw new ReferenceError(`Could not resolve '${t}'`);if(isPlainObject(o=o[i])&&"$ref"in o){if(r.includes(o))return r[r.length-1];if(r.push(o),"string"!=typeof o.$ref)throw new TypeError("$ref should be a string");o=_resolveInlineRef(e,o.$ref,r)}}if(0===r.length)return o;const i=r[r.length-1];return isPlainObject(i)&&("summary"in i||"description"in i)?Object.assign(Object.assign(Object.assign({},o),"description"in i?{description:i.description}:null),"summary"in i?{summary:i.summary}:null):o}function resolveInlineRef(e,t){return _resolveInlineRef(e,t,[])}const _traverse=(e,t,r)=>{const n={value:e,path:r};t.onEnter&&t.onEnter(n);for(const n of Object.keys(e)){const o=e[n];t.onProperty&&t.onProperty({parent:e,parentPath:r,property:n,propertyValue:o}),"object"==typeof o&&null!==o&&_traverse(o,t,r.concat(n))}t.onLeave&&t.onLeave(n)},traverse=(e,t)=>{"object"==typeof e&&null!==e&&_traverse(e,"function"==typeof t?{onProperty:t}:t,[])},BUNDLE_ROOT="#/__bundled__",ERRORS_ROOT="#/__errors__",bundleTarget=({document:e,path:t,bundleRoot:r=BUNDLE_ROOT,errorsRoot:n=ERRORS_ROOT,cloneDocument:o=!0},i)=>{if(t===r||t===n)throw new Error("Roots do not make any sense");const a=o?lodash.cloneDeep(e):e;return bundle(a,pointerToPath(r),pointerToPath(n))(t,{[t]:!0},i)},bundle=(e,t,r)=>{const n=new Set,o=(i,a,s,c={},l={},f={})=>{const p=pointerToPath(i),u=lodash.get(e,p);traverse(s||u,({parent:r})=>{if(hasRef(r)&&isLocalRef(r.$ref)){const s=r.$ref;if(f[s])return;if(c[s])return void(r.$ref=c[s]);let p,u,d,g,h;try{let r;if(p=pointerToPath(s),Array.isArray(lodash.get(e,p.slice(0,-1)))){r=`${p[p.length-2]}_${p[p.length-1]}`}else r=p[p.length-1];d=r;let o=1;for(;n.has(d);)if(d=`${r}_${++o}`,o>20)throw new Error(`Keys ${r}_2 through ${r}_20 already taken.`);n.add(d),u=[...t,d],g=pathToPointer(u)}catch(e){f[s]=e.message}if(!p||!u||!g)return;if("object"==typeof e&&null!==e)try{h=resolveInlineRef(Object(e),s)}catch(t){h=lodash.get(e,p)}if(void 0!==h&&(c[s]=g,r.$ref=g,!lodash.has(l,u))){const t=lodash.get(e,p);Array.isArray(t)?lodash.set(l,u,new Array(t.length).fill(null)):"object"==typeof t&&lodash.setWith(l,u,{},Object),lodash.set(l,u,h),a[s]||(a[s]=!0,o(i,a,h,c,l,f),a[s]=!1)}}});const d=lodash.get(l,t);return d&&Object.keys(d).length&&lodash.set(u,t,d),(Object.keys(f).length||lodash.has(e,r))&&lodash.set(u,r,lodash.has(e,r)?lodash.get(e,r):f),u};return o},decodePointerFragment=e=>replaceInString(replaceInString(e,"~1","/"),"~0","~"),decycle=(e,t)=>{const r=new WeakMap;return function e(n,o){let i;if(t&&(n=t(n)),isPlainObject(n)||Array.isArray(n)){const t=r.get(n);return t?{$ref:t}:(r.set(n,pathToPointer(o)),Array.isArray(n)?i=n.map((t,r)=>e(t,[...o,String(r)])):(i={},Object.keys(n).forEach(t=>{i[t]=e(n[t],[...o,t])})),r.delete(n),i)}return n}(e,[])},encodePointer=e=>replaceInString(replaceInString(e,"~","~0"),"//","/~1"),extractPointerFromRef=e=>{if("string"!=typeof e||0===e.length)return null;const t=e.indexOf("#");return-1===t?null:e.slice(t)},getFirstPrimitiveProperty=e=>{const t=jsoncParser.createScanner(e,!0);if(t.scan(),1!==t.getToken())return;if(t.scan(),2===t.getToken())return;if(10!==t.getToken())throw new SyntaxError("Unexpected character");const r=t.getTokenValue();if(t.scan(),6!==t.getToken())throw new SyntaxError("Colon expected");switch(t.scan(),t.getToken()){case 10:return[r,t.getTokenValue()];case 11:return[r,Number(t.getTokenValue())];case 8:return[r,!0];case 9:return[r,!1];case 7:return[r,null];case 16:throw new SyntaxError("Unexpected character");case 17:throw new SyntaxError("Unexpected end of file");default:return}},getJsonPathForPosition=({lineMap:e,ast:t},r)=>{const n=e[r.line],o=e[r.line+1];if(void 0===n)return;const i=jsoncParser.findNodeAtOffset(t,void 0===o?n+r.character:Math.min(o,n+r.character),!0);if(void 0===i)return;const a=jsoncParser.getNodePath(i);return 0!==a.length?a:void 0};function getLastPathSegment(e){return decodePointerFragment(e.split("/").pop()||"")}const getLocationForJsonPath=({lineMap:e,ast:t},r,n=!1)=>{const o=findNodeAtPath(t,r,n);if(void 0!==o&&void 0!==o.range)return{range:o.range}};function findNodeAtPath(e,t,r){e:for(const n of t){const t=Number.isInteger(Number(n))?Number(n):n;if("string"==typeof t||"number"==typeof t&&"array"!==e.type){if("object"!==e.type||!Array.isArray(e.children))return r?e:void 0;for(const r of e.children)if(Array.isArray(r.children)&&r.children[0].value===String(t)&&2===r.children.length){e=r.children[1];continue e}return r?e:void 0}if("array"!==e.type||t<0||!Array.isArray(e.children)||t>=e.children.length)return r?e:void 0;e=e.children[t]}return e}const parseWithPointers=(e,t={disallowComments:!0})=>{const r=[],{ast:n,data:o,lineMap:i}=parseTree(e,r,t);return{data:o,diagnostics:r,ast:n,lineMap:i}};function parseTree(e,t=[],r){const n=computeLineMap(e);let o={type:"array",offset:-1,length:-1,children:[],parent:void 0},i=null,a=[];const s=new WeakMap,c=[];function l(e){"property"===o.type&&(o.length=e-o.offset,o=o.parent)}function f(e,t,r){return{start:{line:e,character:t},end:{line:e,character:t+r}}}function p(e){return o.children.push(e),e}function u(e){Array.isArray(a)?a.push(e):null!==i&&(a[i]=e)}function d(e){u(e),c.push(a),a=e,i=null}function g(){a=c.pop()}const h={onObjectBegin:(e,t,n,i)=>{o=p({type:"object",offset:e,length:-1,parent:o,children:[],range:f(n,i,t)}),!1===r.ignoreDuplicateKeys&&s.set(o,[]),d(createObjectLiteral(!0===r.preserveKeyOrder))},onObjectProperty:(e,n,c,l,u)=>{if((o=p({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:c,parent:o}),!1===r.ignoreDuplicateKeys){const r=s.get(o.parent);r&&(0!==r.length&&r.includes(e)?t.push({range:f(l,u,c),message:"DuplicateKey",severity:types.DiagnosticSeverity.Error,path:getJsonPath(o),code:20}):r.push(e))}!0===r.preserveKeyOrder&&swapKey(a,e),i=e},onObjectEnd:(e,t,n,i)=>{!1===r.ignoreDuplicateKeys&&s.delete(o),o.length=e+t-o.offset,o.range&&(o.range.end.line=n,o.range.end.character=i+t),o=o.parent,l(e+t),g()},onArrayBegin:(e,t,r,n)=>{o=p({type:"array",offset:e,length:-1,parent:o,children:[],range:f(r,n,t)}),d([])},onArrayEnd:(e,t,r,n)=>{o.length=e+t-o.offset,o.range&&(o.range.end.line=r,o.range.end.character=n+t),o=o.parent,l(e+t),g()},onLiteralValue:(e,t,r,n,i)=>{p({type:getLiteralNodeType(e),offset:t,length:r,parent:o,value:e,range:f(n,i,r)}),l(t+r),u(e)},onSeparator:(e,t,r)=>{"property"===o.type&&(":"===e?o.colonOffset=t:","===e&&l(t))},onError:(e,r,n,o,i)=>{t.push({range:f(o,i,n),message:jsoncParser.printParseErrorCode(e),severity:types.DiagnosticSeverity.Error,code:e})}};jsoncParser.visit(e,h,r);const y=o.children[0];return y&&delete y.parent,{ast:y,data:a[0],lineMap:n}}function getLiteralNodeType(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const computeLineMap=e=>{const t=[0];let r=0;for(;r<e.length;r++)"\n"===e[r]&&t.push(r+1);return t.push(r+1),t};function getJsonPath(e,t=[]){return"property"===e.type&&t.unshift(e.children[0].value),void 0!==e.parent?("array"===e.parent.type&&void 0!==e.parent.parent&&t.unshift(e.parent.children.indexOf(e)),getJsonPath(e.parent,t)):t}function createObjectLiteral(e){return e?createOrderedObject__default({}):{}}function swapKey(e,t){if(!(t in e))return;const r=createOrderedObject.getOrder(e),n=r.indexOf(t);-1!==n&&(r.splice(n,1),r.push(t))}const renameObjectKey=(e,t,r)=>{if(!e||!Object.hasOwnProperty.call(e,t)||t===r)return e;const n={};for(const[o,i]of Object.entries(e))o===t?n[r]=i:o in n||(n[o]=i);return n};function isObject(e){return isPlainObject(e)||Array.isArray(e)}function reparentBundleTarget(e,t,r){if(r.length<=1||t.length<=1)throw Error("Source/target path must not be empty and point at root");if(0===t.indexOf(r))throw Error("Target path cannot be contained within source");const n=pointerToPath(t);let o=e;for(const e of n){if(!isObject(o))return;o=o[e]}if(!isObject(o))return;const i=pointerToPath(r);let a=e;for(const[e,t]of i.entries()){if(!isObject(a)||t in a)return;const r=e===i.length-1?o:{};a[t]=r,a=r}delete e[n[0]],_reparentBundleTarget(e,t,r)}function _reparentBundleTarget(e,t,r){for(const n of Object.keys(e)){const o=e[n];if("$ref"!==n)isObject(o)&&_reparentBundleTarget(o,t,r);else{if("string"!=typeof o||!isLocalRef(o))continue;0===o.indexOf(t)&&(e[n]=o.replace(t,r))}}}const safeParse=(e,t)=>{if("string"!=typeof e)return e;try{const r=parseNumber(e);return"string"==typeof r?r:JSON.parse(e,t)}catch(e){return}},parseNumber=e=>{const t=Number(e);return Number.isFinite(t)?String(t)===e?t:e:NaN},safeStringify=(e,t,r)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,t,r)}catch(n){return fastStringify(e,t,r)}},startsWith=(e,t)=>{if(e instanceof Array){if(t instanceof Array){if(t.length>e.length)return!1;for(const r in t){if(!t.hasOwnProperty(r))continue;const n=parseInt(e[r]),o=parseInt(t[r]);if(isNaN(n)&&isNaN(o)){if(e[r]!==t[r])return!1}else if(n!==o)return!1}}}else{if("string"!=typeof e)return!1;if("string"==typeof t)return e.startsWith(t)}return!0},stringify=(e,t,r)=>{const n=safeStringify(e,t,r);if(void 0===n)throw new Error("The value could not be stringified");return n};function toPropertyPath(e){return e.replace(/^(\/|#\/)/,"").split("/").map(decodePointerFragment).map(sanitize).join(".")}function sanitize(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}const KEYS=Symbol.for(createOrderedObject.ORDER_KEY_ID),traps={ownKeys:e=>KEYS in e?e[KEYS]:Reflect.ownKeys(e)},trapAccess=e=>new Proxy(e,traps);function trimStart(e,t){if("string"==typeof e&&"string"==typeof t)return lodash.trimStart(e,t);if(!(e&&Array.isArray(e)&&e.length&&t&&Array.isArray(t)&&t.length))return e;let r=0;for(const n in e)if(e.hasOwnProperty(n)){if(e[n]!==t[n])break;r++}return e.slice(r)}exports.BUNDLE_ROOT=BUNDLE_ROOT,exports.ERRORS_ROOT=ERRORS_ROOT,exports.KEYS=KEYS,exports.bundleTarget=bundleTarget,exports.decodePointer=decodePointer,exports.decodePointerFragment=decodePointerFragment,exports.decycle=decycle,exports.encodePointer=encodePointer,exports.encodePointerFragment=encodePointerFragment,exports.extractPointerFromRef=extractPointerFromRef,exports.extractSourceFromRef=extractSourceFromRef,exports.getFirstPrimitiveProperty=getFirstPrimitiveProperty,exports.getJsonPathForPosition=getJsonPathForPosition,exports.getLastPathSegment=getLastPathSegment,exports.getLocationForJsonPath=getLocationForJsonPath,exports.hasRef=hasRef,exports.isLocalRef=isLocalRef,exports.isPlainObject=isPlainObject,exports.parseTree=parseTree,exports.parseWithPointers=parseWithPointers,exports.pathToPointer=pathToPointer,exports.pointerToPath=pointerToPath,exports.renameObjectKey=renameObjectKey,exports.reparentBundleTarget=reparentBundleTarget,exports.resolveInlineRef=resolveInlineRef,exports.safeParse=safeParse,exports.safeStringify=safeStringify,exports.startsWith=startsWith,exports.stringify=stringify,exports.toPropertyPath=toPropertyPath,exports.trapAccess=trapAccess,exports.traverse=traverse,exports.trimStart=trimStart;

@@ -1,1 +0,1 @@

import{isObject as e,cloneDeep as r,get as t,set as n,has as o,trimStart as i}from"lodash";import{createScanner as s,findNodeAtOffset as a,getNodePath as c,visit as f,printParseErrorCode as l}from"jsonc-parser";import u,{getOrder as p,ORDER_KEY_ID as h}from"@stoplight/ordered-object-literal";import{DiagnosticSeverity as y}from"@stoplight/types";import g from"safe-stable-stringify";const d=r=>e(r)&&"$ref"in r&&"string"==typeof r.$ref,b=e=>e.length>0&&("#"===e||/^#\S*$/.test(e)),m=(e,r,t)=>{const n=e.toString();let o="",i=n,s=0,a=i.indexOf(r);for(;a>-1;)o+=n.substring(s,s+a)+t,i=i.substring(a+r.length,i.length),s+=a+r.length,a=i.indexOf(r);return i.length>0&&(o+=n.substring(n.length-i.length,n.length)),o},w=e=>"number"==typeof e?e:m(m(e,"~","~0"),"/","~1"),O=e=>v(e),v=e=>{if(e&&"object"!=typeof e)throw new TypeError("Invalid type: path must be an array of segments.");return 0===e.length?"#":`#/${e.map(w).join("/")}`};function j(e){try{return decodeURIComponent(e)}catch(r){return e}}const A=/%[0-9a-f]+/gi,E=e=>{let r;try{r=decodeURIComponent(e)}catch(t){r=e.replace(A,j)}return m(m(r,"~1","/"),"~0","~")},$=e=>x(e),x=e=>{if("string"!=typeof e)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===e.length||"#"!==e[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===e.length)return[];if("/"!==e[1])throw new URIError("Invalid JSON Pointer syntax.");return(e=>{const r=e.length,t=[];let n=-1;for(;++n<r;)t.push(E(e[n]));return t})(e.substring(2).split("/"))},S=e=>{if("string"!=typeof e||0===e.length||b(e))return null;const r=e.indexOf("#");return-1===r?e:e.slice(0,r)};function _(e){if("object"!=typeof e||null===e)return!1;const r=Object.getPrototypeOf(e);return null===r||r===Object.prototype||"function"==typeof e.constructor&&Function.toString.call(Object)===Function.toString.call(e.constructor)}function k(e,r){return function e(r,t,n){if(null!==S(t))throw new ReferenceError("Cannot resolve external references");const o=$(t);let i=r;for(const s of o){if(!_(i)&&!Array.isArray(i)||!(s in i))throw new ReferenceError(`Could not resolve '${t}'`);if(_(i=i[s])&&"$ref"in i){if(n.includes(i))return n[n.length-1];if(n.push(i),"string"!=typeof i.$ref)throw new TypeError("$ref should be a string");i=e(r,i.$ref,n)}}if(0===n.length)return i;const s=n[n.length-1];return _(s)&&("summary"in s||"description"in s)?Object.assign(Object.assign(Object.assign({},i),"description"in s?{description:s.description}:null),"summary"in s?{summary:s.summary}:null):i}(e,r,[])}const N=(e,r,t)=>{const n={value:e,path:t};r.onEnter&&r.onEnter(n);for(const n of Object.keys(e)){const o=e[n];r.onProperty&&r.onProperty({parent:e,parentPath:t,property:n,propertyValue:o}),"object"==typeof o&&null!==o&&N(o,r,t.concat(n))}r.onLeave&&r.onLeave(n)},P=(e,r)=>{"object"==typeof e&&null!==e&&N(e,"function"==typeof r?{onProperty:r}:r,[])},T="#/__bundled__",I="#/__errors__",R=({document:e,path:t,bundleRoot:n="#/__bundled__",errorsRoot:o="#/__errors__",cloneDocument:i=!0},s)=>{if(t===n||t===o)throw new Error("Roots do not make any sense");const a=i?r(e):e;return K(a,$(n),$(o))(t,{[t]:!0},s)},K=(e,r,i)=>{const s=new Set,a=(c,f,l,u={},p={},h={})=>{const y=$(c),g=t(e,y);P(l||g,({parent:i})=>{if(d(i)&&b(i.$ref)){const l=i.$ref;if(h[l])return;if(u[l])return void(i.$ref=u[l]);let y,g,d,b,m;try{let n;if(y=$(l),Array.isArray(t(e,y.slice(0,-1)))){n=`${y[y.length-2]}_${y[y.length-1]}`}else n=y[y.length-1];d=n;let o=1;for(;s.has(d);)if(d=`${n}_${++o}`,o>20)throw new Error(`Keys ${n}_2 through ${n}_20 already taken.`);s.add(d),g=[...r,d],b=O(g)}catch(e){h[l]=e.message}if(!y||!g||!b)return;if("object"==typeof e&&null!==e)try{m=k(Object(e),l)}catch(r){m=t(e,y)}if(void 0!==m&&(u[l]=b,i.$ref=b,!o(p,g))){const r=t(e,y);Array.isArray(r)?n(p,g,new Array(r.length).fill(null)):"object"==typeof r&&n(p,g,{}),n(p,g,m),f[l]||(f[l]=!0,a(c,f,m,u,p,h),f[l]=!1)}}});const m=t(p,r);return m&&Object.keys(m).length&&n(g,r,m),(Object.keys(h).length||o(e,i))&&n(g,i,o(e,i)?t(e,i):h),g};return a},M=e=>m(m(e,"~1","/"),"~0","~"),U=(e,r)=>{const t=new WeakMap;return function e(n,o){let i;if(r&&(n=r(n)),_(n)||Array.isArray(n)){const r=t.get(n);return r?{$ref:r}:(t.set(n,O(o)),Array.isArray(n)?i=n.map((r,t)=>e(r,[...o,String(t)])):(i={},Object.keys(n).forEach(r=>{i[r]=e(n[r],[...o,r])})),t.delete(n),i)}return n}(e,[])},C=e=>m(m(e,"~","~0"),"//","/~1"),D=e=>{if("string"!=typeof e||0===e.length)return null;const r=e.indexOf("#");return-1===r?null:e.slice(r)},J=e=>{const r=s(e,!0);if(r.scan(),1!==r.getToken())return;if(r.scan(),2===r.getToken())return;if(10!==r.getToken())throw new SyntaxError("Unexpected character");const t=r.getTokenValue();if(r.scan(),6!==r.getToken())throw new SyntaxError("Colon expected");switch(r.scan(),r.getToken()){case 10:return[t,r.getTokenValue()];case 11:return[t,Number(r.getTokenValue())];case 8:return[t,!0];case 9:return[t,!1];case 7:return[t,null];case 16:throw new SyntaxError("Unexpected character");case 17:throw new SyntaxError("Unexpected end of file");default:return}},V=({lineMap:e,ast:r},t)=>{const n=e[t.line],o=e[t.line+1];if(void 0===n)return;const i=a(r,void 0===o?n+t.character:Math.min(o,n+t.character),!0);if(void 0===i)return;const s=c(i);return 0!==s.length?s:void 0};function F(e){return M(e.split("/").pop()||"")}const L=({lineMap:e,ast:r},t,n=!1)=>{const o=function(e,r,t){e:for(const n of r){const r=Number.isInteger(Number(n))?Number(n):n;if("string"==typeof r||"number"==typeof r&&"array"!==e.type){if("object"!==e.type||!Array.isArray(e.children))return t?e:void 0;for(const t of e.children)if(Array.isArray(t.children)&&t.children[0].value===String(r)&&2===t.children.length){e=t.children[1];continue e}return t?e:void 0}if("array"!==e.type||r<0||!Array.isArray(e.children)||r>=e.children.length)return t?e:void 0;e=e.children[r]}return e}(r,t,n);if(void 0!==o&&void 0!==o.range)return{range:o.range}};const W=(e,r={disallowComments:!0})=>{const t=[],{ast:n,data:o,lineMap:i}=B(e,t,r);return{data:o,diagnostics:t,ast:n,lineMap:i}};function B(e,r=[],t){const n=z(e);let o={type:"array",offset:-1,length:-1,children:[],parent:void 0},i=null,s=[];const a=new WeakMap,c=[];function h(e){"property"===o.type&&(o.length=e-o.offset,o=o.parent)}function g(e,r,t){return{start:{line:e,character:r},end:{line:e,character:r+t}}}function d(e){return o.children.push(e),e}function b(e){Array.isArray(s)?s.push(e):null!==i&&(s[i]=e)}function m(e){b(e),c.push(s),s=e,i=null}function w(){s=c.pop()}f(e,{onObjectBegin:(e,r,n,i)=>{o=d({type:"object",offset:e,length:-1,parent:o,children:[],range:g(n,i,r)}),!1===t.ignoreDuplicateKeys&&a.set(o,[]),m(function(e){return e?u({}):{}}(!0===t.preserveKeyOrder))},onObjectProperty:(e,n,c,f,l)=>{if((o=d({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:c,parent:o}),!1===t.ignoreDuplicateKeys){const t=a.get(o.parent);t&&(0!==t.length&&t.includes(e)?r.push({range:g(f,l,c),message:"DuplicateKey",severity:y.Error,path:G(o),code:20}):t.push(e))}!0===t.preserveKeyOrder&&function(e,r){if(!(r in e))return;const t=p(e),n=t.indexOf(r);-1!==n&&(t.splice(n,1),t.push(r))}(s,e),i=e},onObjectEnd:(e,r,n,i)=>{!1===t.ignoreDuplicateKeys&&a.delete(o),o.length=e+r-o.offset,o.range&&(o.range.end.line=n,o.range.end.character=i+r),o=o.parent,h(e+r),w()},onArrayBegin:(e,r,t,n)=>{o=d({type:"array",offset:e,length:-1,parent:o,children:[],range:g(t,n,r)}),m([])},onArrayEnd:(e,r,t,n)=>{o.length=e+r-o.offset,o.range&&(o.range.end.line=t,o.range.end.character=n+r),o=o.parent,h(e+r),w()},onLiteralValue:(e,r,t,n,i)=>{d({type:q(e),offset:r,length:t,parent:o,value:e,range:g(n,i,t)}),h(r+t),b(e)},onSeparator:(e,r,t)=>{"property"===o.type&&(":"===e?o.colonOffset=r:","===e&&h(r))},onError:(e,t,n,o,i)=>{r.push({range:g(o,i,n),message:l(e),severity:y.Error,code:e})}},t);const O=o.children[0];return O&&delete O.parent,{ast:O,data:s[0],lineMap:n}}function q(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const z=e=>{const r=[0];let t=0;for(;t<e.length;t++)"\n"===e[t]&&r.push(t+1);return r.push(t+1),r};function G(e,r=[]){return"property"===e.type&&r.unshift(e.children[0].value),void 0!==e.parent?("array"===e.parent.type&&void 0!==e.parent.parent&&r.unshift(e.parent.children.indexOf(e)),G(e.parent,r)):r}const H=(e,r,t)=>{if(!e||!Object.hasOwnProperty.call(e,r)||r===t)return e;const n={};for(const[o,i]of Object.entries(e))o===r?n[t]=i:o in n||(n[o]=i);return n};function Q(e){return _(e)||Array.isArray(e)}function X(e,r,t){if(t.length<=1||r.length<=1)throw Error("Source/target path must not be empty and point at root");if(0===r.indexOf(t))throw Error("Target path cannot be contained within source");const n=$(r);let o=e;for(const e of n){if(!Q(o))return;o=o[e]}if(!Q(o))return;const i=$(t);let s=e;for(const[e,r]of i.entries()){if(!Q(s)||r in s)return;const t=e===i.length-1?o:{};s[r]=t,s=t}delete e[n[0]],function e(r,t,n){for(const o of Object.keys(r)){const i=r[o];if("$ref"!==o)Q(i)&&e(i,t,n);else{if("string"!=typeof i||!b(i))continue;0===i.indexOf(t)&&(r[o]=i.replace(t,n))}}}(e,r,t)}const Y=(e,r)=>{if("string"!=typeof e)return e;try{const t=Z(e);return"string"==typeof t?t:JSON.parse(e,r)}catch(e){return}},Z=e=>{const r=Number(e);return Number.isFinite(r)?String(r)===e?r:e:NaN},ee=(e,r,t)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,r,t)}catch(n){return g(e,r,t)}},re=(e,r)=>{if(e instanceof Array){if(r instanceof Array){if(r.length>e.length)return!1;for(const t in r){if(!r.hasOwnProperty(t))continue;const n=parseInt(e[t]),o=parseInt(r[t]);if(isNaN(n)&&isNaN(o)){if(e[t]!==r[t])return!1}else if(n!==o)return!1}}}else{if("string"!=typeof e)return!1;if("string"==typeof r)return e.startsWith(r)}return!0},te=(e,r,t)=>{const n=ee(e,r,t);if(void 0===n)throw new Error("The value could not be stringified");return n};function ne(e){return e.replace(/^(\/|#\/)/,"").split("/").map(M).map(oe).join(".")}function oe(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}const ie=Symbol.for(h),se={ownKeys:e=>ie in e?e[ie]:Reflect.ownKeys(e)},ae=e=>new Proxy(e,se);function ce(e,r){if("string"==typeof e&&"string"==typeof r)return i(e,r);if(!(e&&Array.isArray(e)&&e.length&&r&&Array.isArray(r)&&r.length))return e;let t=0;for(const n in e)if(e.hasOwnProperty(n)){if(e[n]!==r[n])break;t++}return e.slice(t)}export{T as BUNDLE_ROOT,I as ERRORS_ROOT,ie as KEYS,R as bundleTarget,E as decodePointer,M as decodePointerFragment,U as decycle,C as encodePointer,w as encodePointerFragment,D as extractPointerFromRef,S as extractSourceFromRef,J as getFirstPrimitiveProperty,V as getJsonPathForPosition,F as getLastPathSegment,L as getLocationForJsonPath,d as hasRef,b as isLocalRef,_ as isPlainObject,B as parseTree,W as parseWithPointers,O as pathToPointer,$ as pointerToPath,H as renameObjectKey,X as reparentBundleTarget,k as resolveInlineRef,Y as safeParse,ee as safeStringify,re as startsWith,te as stringify,ne as toPropertyPath,ae as trapAccess,P as traverse,ce as trimStart};
import{isObject as e,cloneDeep as r,get as t,set as n,has as o,setWith as i,trimStart as s}from"lodash";import{createScanner as a,findNodeAtOffset as c,getNodePath as f,visit as l,printParseErrorCode as u}from"jsonc-parser";import p,{getOrder as h,ORDER_KEY_ID as y}from"@stoplight/ordered-object-literal";import{DiagnosticSeverity as g}from"@stoplight/types";import d from"safe-stable-stringify";const b=r=>e(r)&&"$ref"in r&&"string"==typeof r.$ref,m=e=>e.length>0&&("#"===e||/^#\S*$/.test(e)),w=(e,r,t)=>{const n=e.toString();let o="",i=n,s=0,a=i.indexOf(r);for(;a>-1;)o+=n.substring(s,s+a)+t,i=i.substring(a+r.length,i.length),s+=a+r.length,a=i.indexOf(r);return i.length>0&&(o+=n.substring(n.length-i.length,n.length)),o},O=e=>"number"==typeof e?e:w(w(e,"~","~0"),"/","~1"),v=e=>j(e),j=e=>{if(e&&"object"!=typeof e)throw new TypeError("Invalid type: path must be an array of segments.");return 0===e.length?"#":`#/${e.map(O).join("/")}`};function A(e){try{return decodeURIComponent(e)}catch(r){return e}}const E=/%[0-9a-f]+/gi,$=e=>{let r;try{r=decodeURIComponent(e)}catch(t){r=e.replace(E,A)}return w(w(r,"~1","/"),"~0","~")},x=e=>S(e),S=e=>{if("string"!=typeof e)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===e.length||"#"!==e[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===e.length)return[];if("/"!==e[1])throw new URIError("Invalid JSON Pointer syntax.");return(e=>{const r=e.length,t=[];let n=-1;for(;++n<r;)t.push($(e[n]));return t})(e.substring(2).split("/"))},_=e=>{if("string"!=typeof e||0===e.length||m(e))return null;const r=e.indexOf("#");return-1===r?e:e.slice(0,r)};function k(e){if("object"!=typeof e||null===e)return!1;const r=Object.getPrototypeOf(e);return null===r||r===Object.prototype||"function"==typeof e.constructor&&Function.toString.call(Object)===Function.toString.call(e.constructor)}function N(e,r){return function e(r,t,n){if(null!==_(t))throw new ReferenceError("Cannot resolve external references");const o=x(t);let i=r;for(const s of o){if(!k(i)&&!Array.isArray(i)||!(s in i))throw new ReferenceError(`Could not resolve '${t}'`);if(k(i=i[s])&&"$ref"in i){if(n.includes(i))return n[n.length-1];if(n.push(i),"string"!=typeof i.$ref)throw new TypeError("$ref should be a string");i=e(r,i.$ref,n)}}if(0===n.length)return i;const s=n[n.length-1];return k(s)&&("summary"in s||"description"in s)?Object.assign(Object.assign(Object.assign({},i),"description"in s?{description:s.description}:null),"summary"in s?{summary:s.summary}:null):i}(e,r,[])}const P=(e,r,t)=>{const n={value:e,path:t};r.onEnter&&r.onEnter(n);for(const n of Object.keys(e)){const o=e[n];r.onProperty&&r.onProperty({parent:e,parentPath:t,property:n,propertyValue:o}),"object"==typeof o&&null!==o&&P(o,r,t.concat(n))}r.onLeave&&r.onLeave(n)},T=(e,r)=>{"object"==typeof e&&null!==e&&P(e,"function"==typeof r?{onProperty:r}:r,[])},I="#/__bundled__",R="#/__errors__",K=({document:e,path:t,bundleRoot:n="#/__bundled__",errorsRoot:o="#/__errors__",cloneDocument:i=!0},s)=>{if(t===n||t===o)throw new Error("Roots do not make any sense");const a=i?r(e):e;return M(a,x(n),x(o))(t,{[t]:!0},s)},M=(e,r,s)=>{const a=new Set,c=(f,l,u,p={},h={},y={})=>{const g=x(f),d=t(e,g);T(u||d,({parent:s})=>{if(b(s)&&m(s.$ref)){const u=s.$ref;if(y[u])return;if(p[u])return void(s.$ref=p[u]);let g,d,b,m,w;try{let n;if(g=x(u),Array.isArray(t(e,g.slice(0,-1)))){n=`${g[g.length-2]}_${g[g.length-1]}`}else n=g[g.length-1];b=n;let o=1;for(;a.has(b);)if(b=`${n}_${++o}`,o>20)throw new Error(`Keys ${n}_2 through ${n}_20 already taken.`);a.add(b),d=[...r,b],m=v(d)}catch(e){y[u]=e.message}if(!g||!d||!m)return;if("object"==typeof e&&null!==e)try{w=N(Object(e),u)}catch(r){w=t(e,g)}if(void 0!==w&&(p[u]=m,s.$ref=m,!o(h,d))){const r=t(e,g);Array.isArray(r)?n(h,d,new Array(r.length).fill(null)):"object"==typeof r&&i(h,d,{},Object),n(h,d,w),l[u]||(l[u]=!0,c(f,l,w,p,h,y),l[u]=!1)}}});const w=t(h,r);return w&&Object.keys(w).length&&n(d,r,w),(Object.keys(y).length||o(e,s))&&n(d,s,o(e,s)?t(e,s):y),d};return c},U=e=>w(w(e,"~1","/"),"~0","~"),C=(e,r)=>{const t=new WeakMap;return function e(n,o){let i;if(r&&(n=r(n)),k(n)||Array.isArray(n)){const r=t.get(n);return r?{$ref:r}:(t.set(n,v(o)),Array.isArray(n)?i=n.map((r,t)=>e(r,[...o,String(t)])):(i={},Object.keys(n).forEach(r=>{i[r]=e(n[r],[...o,r])})),t.delete(n),i)}return n}(e,[])},D=e=>w(w(e,"~","~0"),"//","/~1"),J=e=>{if("string"!=typeof e||0===e.length)return null;const r=e.indexOf("#");return-1===r?null:e.slice(r)},V=e=>{const r=a(e,!0);if(r.scan(),1!==r.getToken())return;if(r.scan(),2===r.getToken())return;if(10!==r.getToken())throw new SyntaxError("Unexpected character");const t=r.getTokenValue();if(r.scan(),6!==r.getToken())throw new SyntaxError("Colon expected");switch(r.scan(),r.getToken()){case 10:return[t,r.getTokenValue()];case 11:return[t,Number(r.getTokenValue())];case 8:return[t,!0];case 9:return[t,!1];case 7:return[t,null];case 16:throw new SyntaxError("Unexpected character");case 17:throw new SyntaxError("Unexpected end of file");default:return}},F=({lineMap:e,ast:r},t)=>{const n=e[t.line],o=e[t.line+1];if(void 0===n)return;const i=c(r,void 0===o?n+t.character:Math.min(o,n+t.character),!0);if(void 0===i)return;const s=f(i);return 0!==s.length?s:void 0};function L(e){return U(e.split("/").pop()||"")}const W=({lineMap:e,ast:r},t,n=!1)=>{const o=function(e,r,t){e:for(const n of r){const r=Number.isInteger(Number(n))?Number(n):n;if("string"==typeof r||"number"==typeof r&&"array"!==e.type){if("object"!==e.type||!Array.isArray(e.children))return t?e:void 0;for(const t of e.children)if(Array.isArray(t.children)&&t.children[0].value===String(r)&&2===t.children.length){e=t.children[1];continue e}return t?e:void 0}if("array"!==e.type||r<0||!Array.isArray(e.children)||r>=e.children.length)return t?e:void 0;e=e.children[r]}return e}(r,t,n);if(void 0!==o&&void 0!==o.range)return{range:o.range}};const B=(e,r={disallowComments:!0})=>{const t=[],{ast:n,data:o,lineMap:i}=q(e,t,r);return{data:o,diagnostics:t,ast:n,lineMap:i}};function q(e,r=[],t){const n=G(e);let o={type:"array",offset:-1,length:-1,children:[],parent:void 0},i=null,s=[];const a=new WeakMap,c=[];function f(e){"property"===o.type&&(o.length=e-o.offset,o=o.parent)}function y(e,r,t){return{start:{line:e,character:r},end:{line:e,character:r+t}}}function d(e){return o.children.push(e),e}function b(e){Array.isArray(s)?s.push(e):null!==i&&(s[i]=e)}function m(e){b(e),c.push(s),s=e,i=null}function w(){s=c.pop()}l(e,{onObjectBegin:(e,r,n,i)=>{o=d({type:"object",offset:e,length:-1,parent:o,children:[],range:y(n,i,r)}),!1===t.ignoreDuplicateKeys&&a.set(o,[]),m(function(e){return e?p({}):{}}(!0===t.preserveKeyOrder))},onObjectProperty:(e,n,c,f,l)=>{if((o=d({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:c,parent:o}),!1===t.ignoreDuplicateKeys){const t=a.get(o.parent);t&&(0!==t.length&&t.includes(e)?r.push({range:y(f,l,c),message:"DuplicateKey",severity:g.Error,path:H(o),code:20}):t.push(e))}!0===t.preserveKeyOrder&&function(e,r){if(!(r in e))return;const t=h(e),n=t.indexOf(r);-1!==n&&(t.splice(n,1),t.push(r))}(s,e),i=e},onObjectEnd:(e,r,n,i)=>{!1===t.ignoreDuplicateKeys&&a.delete(o),o.length=e+r-o.offset,o.range&&(o.range.end.line=n,o.range.end.character=i+r),o=o.parent,f(e+r),w()},onArrayBegin:(e,r,t,n)=>{o=d({type:"array",offset:e,length:-1,parent:o,children:[],range:y(t,n,r)}),m([])},onArrayEnd:(e,r,t,n)=>{o.length=e+r-o.offset,o.range&&(o.range.end.line=t,o.range.end.character=n+r),o=o.parent,f(e+r),w()},onLiteralValue:(e,r,t,n,i)=>{d({type:z(e),offset:r,length:t,parent:o,value:e,range:y(n,i,t)}),f(r+t),b(e)},onSeparator:(e,r,t)=>{"property"===o.type&&(":"===e?o.colonOffset=r:","===e&&f(r))},onError:(e,t,n,o,i)=>{r.push({range:y(o,i,n),message:u(e),severity:g.Error,code:e})}},t);const O=o.children[0];return O&&delete O.parent,{ast:O,data:s[0],lineMap:n}}function z(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const G=e=>{const r=[0];let t=0;for(;t<e.length;t++)"\n"===e[t]&&r.push(t+1);return r.push(t+1),r};function H(e,r=[]){return"property"===e.type&&r.unshift(e.children[0].value),void 0!==e.parent?("array"===e.parent.type&&void 0!==e.parent.parent&&r.unshift(e.parent.children.indexOf(e)),H(e.parent,r)):r}const Q=(e,r,t)=>{if(!e||!Object.hasOwnProperty.call(e,r)||r===t)return e;const n={};for(const[o,i]of Object.entries(e))o===r?n[t]=i:o in n||(n[o]=i);return n};function X(e){return k(e)||Array.isArray(e)}function Y(e,r,t){if(t.length<=1||r.length<=1)throw Error("Source/target path must not be empty and point at root");if(0===r.indexOf(t))throw Error("Target path cannot be contained within source");const n=x(r);let o=e;for(const e of n){if(!X(o))return;o=o[e]}if(!X(o))return;const i=x(t);let s=e;for(const[e,r]of i.entries()){if(!X(s)||r in s)return;const t=e===i.length-1?o:{};s[r]=t,s=t}delete e[n[0]],function e(r,t,n){for(const o of Object.keys(r)){const i=r[o];if("$ref"!==o)X(i)&&e(i,t,n);else{if("string"!=typeof i||!m(i))continue;0===i.indexOf(t)&&(r[o]=i.replace(t,n))}}}(e,r,t)}const Z=(e,r)=>{if("string"!=typeof e)return e;try{const t=ee(e);return"string"==typeof t?t:JSON.parse(e,r)}catch(e){return}},ee=e=>{const r=Number(e);return Number.isFinite(r)?String(r)===e?r:e:NaN},re=(e,r,t)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,r,t)}catch(n){return d(e,r,t)}},te=(e,r)=>{if(e instanceof Array){if(r instanceof Array){if(r.length>e.length)return!1;for(const t in r){if(!r.hasOwnProperty(t))continue;const n=parseInt(e[t]),o=parseInt(r[t]);if(isNaN(n)&&isNaN(o)){if(e[t]!==r[t])return!1}else if(n!==o)return!1}}}else{if("string"!=typeof e)return!1;if("string"==typeof r)return e.startsWith(r)}return!0},ne=(e,r,t)=>{const n=re(e,r,t);if(void 0===n)throw new Error("The value could not be stringified");return n};function oe(e){return e.replace(/^(\/|#\/)/,"").split("/").map(U).map(ie).join(".")}function ie(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}const se=Symbol.for(y),ae={ownKeys:e=>se in e?e[se]:Reflect.ownKeys(e)},ce=e=>new Proxy(e,ae);function fe(e,r){if("string"==typeof e&&"string"==typeof r)return s(e,r);if(!(e&&Array.isArray(e)&&e.length&&r&&Array.isArray(r)&&r.length))return e;let t=0;for(const n in e)if(e.hasOwnProperty(n)){if(e[n]!==r[n])break;t++}return e.slice(t)}export{I as BUNDLE_ROOT,R as ERRORS_ROOT,se as KEYS,K as bundleTarget,$ as decodePointer,U as decodePointerFragment,C as decycle,D as encodePointer,O as encodePointerFragment,J as extractPointerFromRef,_ as extractSourceFromRef,V as getFirstPrimitiveProperty,F as getJsonPathForPosition,L as getLastPathSegment,W as getLocationForJsonPath,b as hasRef,m as isLocalRef,k as isPlainObject,q as parseTree,B as parseWithPointers,v as pathToPointer,x as pointerToPath,Q as renameObjectKey,Y as reparentBundleTarget,N as resolveInlineRef,Z as safeParse,re as safeStringify,te as startsWith,ne as stringify,oe as toPropertyPath,ce as trapAccess,T as traverse,fe as trimStart};
{
"name": "@stoplight/json",
"version": "3.17.0",
"version": "3.17.1",
"description": "Useful functions when working with JSON.",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc