Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@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.5.0 to 3.5.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 jsoncParser=require("jsonc-parser"),lodash=require("lodash"),types=require("@stoplight/types"),fastStringify=_interopDefault(require("safe-stable-stringify"));const 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},decodePointer=e=>replaceInString(replaceInString(decodeURIComponent(""+e),"~1","/"),"~0","~"),decodePointerFragment=e=>replaceInString(replaceInString(e,"~1","/"),"~0","~"),encodePointer=e=>replaceInString(replaceInString(e,"~","~0"),"//","/~1"),encodePointerFragment=e=>"number"==typeof e?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)},isLocalRef=e=>e.length>0&&"#"===e[0],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)},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)){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 hasRef=e=>lodash.isObject(e)&&"$ref"in e&&"string"==typeof e.$ref,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}},KEYS=Symbol("object_keys"),traps={ownKeys:e=>e[KEYS]};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 p(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 l(e){return o.children.push(e),e}function u(e){Array.isArray(a)?a.push(e):null!==i&&(a[i]=e)}function g(e){u(e),c.push(a),a=e,i=null}function h(){KEYS in a&&a[KEYS].push(KEYS),a=c.pop()}const d={onObjectBegin:(e,t,n,i)=>{o=l({type:"object",offset:e,length:-1,parent:o,children:[],range:f(n,i,t)}),!1===r.ignoreDuplicateKeys&&s.set(o,[]),g(createObjectLiteral(!0===r.preserveKeyOrder))},onObjectProperty:(e,n,c,p,u)=>{if((o=l({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(p,u,c),message:"DuplicateKey",severity:types.DiagnosticSeverity.Error,path:getJsonPath(o),code:20}):r.push(e))}!0===r.preserveKeyOrder&&KEYS in a&&pushKey(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,p(e+t),h()},onArrayBegin:(e,t,r,n)=>{o=l({type:"array",offset:e,length:-1,parent:o,children:[],range:f(r,n,t)}),g([])},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,p(e+t),h()},onLiteralValue:(e,t,r,n,i)=>{l({type:getLiteralNodeType(e),offset:t,length:r,parent:o,value:e,range:f(n,i,r)}),p(t+r),u(e)},onSeparator:(e,t,r)=>{"property"===o.type&&(":"===e?o.colonOffset=t:","===e&&p(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,d,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){if(e){const e=new Proxy({},traps);return Reflect.defineProperty(e,KEYS,{value:[]}),e}return{}}function pushKey(e,t){const r=t in e?e[KEYS].indexOf(t):-1;-1!==r&&e[KEYS].splice(r,1),e[KEYS].push(t)}const 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("/")}`},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("/"))},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},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};function toPropertyPath(e){return e.replace(/^(\/|#\/)/,"").split("/").map(decodePointerFragment).map(sanitize).join(".")}function sanitize(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}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.decodePointer=decodePointer,exports.decodePointerFragment=decodePointerFragment,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.parseTree=parseTree,exports.parseWithPointers=parseWithPointers,exports.pathToPointer=pathToPointer,exports.pointerToPath=pointerToPath,exports.renameObjectKey=renameObjectKey,exports.safeParse=safeParse,exports.safeStringify=safeStringify,exports.startsWith=startsWith,exports.toPropertyPath=toPropertyPath,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"),types=require("@stoplight/types"),fastStringify=_interopDefault(require("safe-stable-stringify"));const 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},decodePointer=e=>replaceInString(replaceInString(decodeURIComponent(""+e),"~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("/"))},traverse=(e,t,r=[])=>{if(e&&"object"==typeof e)for(const n in e)e.hasOwnProperty(n)&&(t({parentPath:r,property:n,propertyValue:e[n]}),e[n]&&"object"==typeof e[n]&&traverse(e[n],t,r.concat(n)))},bundleTarget=({document:e,path:t},r)=>_bundle(lodash.cloneDeep(e),t,r),_bundle=(e,t,r)=>{const n=lodash.get(e,pointerToPath(t));return traverse(r||n,({property:r,propertyValue:o})=>{if("$ref"===r&&"string"==typeof o){const r=pointerToPath(o),i=lodash.get(e,r),a=!!lodash.get(n,r);i&&!a&&(lodash.set(n,r,i),_bundle(e,t,i))}}),n},decodePointerFragment=e=>replaceInString(replaceInString(e,"~1","/"),"~0","~"),encodePointer=e=>replaceInString(replaceInString(e,"~","~0"),"//","/~1"),encodePointerFragment=e=>"number"==typeof e?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)},isLocalRef=e=>e.length>0&&"#"===e[0],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)},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)){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 hasRef=e=>lodash.isObject(e)&&"$ref"in e&&"string"==typeof e.$ref,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}},KEYS=Symbol("object_keys"),traps={ownKeys:e=>e[KEYS]};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 p(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 l(e){return o.children.push(e),e}function u(e){Array.isArray(a)?a.push(e):null!==i&&(a[i]=e)}function g(e){u(e),c.push(a),a=e,i=null}function h(){KEYS in a&&a[KEYS].push(KEYS),a=c.pop()}const d={onObjectBegin:(e,t,n,i)=>{o=l({type:"object",offset:e,length:-1,parent:o,children:[],range:f(n,i,t)}),!1===r.ignoreDuplicateKeys&&s.set(o,[]),g(createObjectLiteral(!0===r.preserveKeyOrder))},onObjectProperty:(e,n,c,p,u)=>{if((o=l({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(p,u,c),message:"DuplicateKey",severity:types.DiagnosticSeverity.Error,path:getJsonPath(o),code:20}):r.push(e))}!0===r.preserveKeyOrder&&KEYS in a&&pushKey(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,p(e+t),h()},onArrayBegin:(e,t,r,n)=>{o=l({type:"array",offset:e,length:-1,parent:o,children:[],range:f(r,n,t)}),g([])},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,p(e+t),h()},onLiteralValue:(e,t,r,n,i)=>{l({type:getLiteralNodeType(e),offset:t,length:r,parent:o,value:e,range:f(n,i,r)}),p(t+r),u(e)},onSeparator:(e,t,r)=>{"property"===o.type&&(":"===e?o.colonOffset=t:","===e&&p(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,d,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){if(e){const e=new Proxy({},traps);return Reflect.defineProperty(e,KEYS,{value:[]}),e}return{}}function pushKey(e,t){const r=t in e?e[KEYS].indexOf(t):-1;-1!==r&&e[KEYS].splice(r,1),e[KEYS].push(t)}const 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("/")}`},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},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};function toPropertyPath(e){return e.replace(/^(\/|#\/)/,"").split("/").map(decodePointerFragment).map(sanitize).join(".")}function sanitize(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}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.bundleTarget=bundleTarget,exports.decodePointer=decodePointer,exports.decodePointerFragment=decodePointerFragment,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.parseTree=parseTree,exports.parseWithPointers=parseWithPointers,exports.pathToPointer=pathToPointer,exports.pointerToPath=pointerToPath,exports.renameObjectKey=renameObjectKey,exports.safeParse=safeParse,exports.safeStringify=safeStringify,exports.startsWith=startsWith,exports.toPropertyPath=toPropertyPath,exports.traverse=traverse,exports.trimStart=trimStart;

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

export * from './bundle';
export * from './decodePointer';

@@ -21,3 +22,4 @@ export * from './decodePointerFragment';

export * from './toPropertyPath';
export * from './traverse';
export * from './trimStart';
export * from './types';

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

import{createScanner as e,findNodeAtOffset as r,getNodePath as n,visit as t,printParseErrorCode as i}from"jsonc-parser";import{isObject as o,trimStart as a}from"lodash";import{DiagnosticSeverity as s}from"@stoplight/types";import c from"safe-stable-stringify";const u=(e,r,n)=>{const t=e.toString();let i="",o=t,a=0,s=o.indexOf(r);for(;s>-1;)i+=t.substring(a,a+s)+n,o=o.substring(s+r.length,o.length),a+=s+r.length,s=o.indexOf(r);return o.length>0&&(i+=t.substring(t.length-o.length,t.length)),i},l=e=>u(u(decodeURIComponent(""+e),"~1","/"),"~0","~"),f=e=>u(u(e,"~1","/"),"~0","~"),p=e=>u(u(e,"~","~0"),"//","/~1"),h=e=>"number"==typeof e?e:u(u(e,"~","~0"),"/","~1"),g=e=>{if("string"!=typeof e||0===e.length)return null;const r=e.indexOf("#");return-1===r?null:e.slice(r)},y=e=>e.length>0&&"#"===e[0],d=e=>{if("string"!=typeof e||0===e.length||y(e))return null;const r=e.indexOf("#");return-1===r?e:e.slice(0,r)},m=r=>{const n=e(r,!0);if(n.scan(),1!==n.getToken())return;if(n.scan(),2===n.getToken())return;if(10!==n.getToken())throw new SyntaxError("Unexpected character");const t=n.getTokenValue();if(n.scan(),6!==n.getToken())throw new SyntaxError("Colon expected");switch(n.scan(),n.getToken()){case 10:return[t,n.getTokenValue()];case 11:return[t,Number(n.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}},b=({lineMap:e,ast:t},i)=>{const o=e[i.line],a=e[i.line+1];if(void 0===o)return;const s=r(t,void 0===a?o+i.character:Math.min(a,o+i.character),!0);if(void 0===s)return;const c=n(s);return 0!==c.length?c:void 0};function w(e){return f(e.split("/").pop()||"")}const v=({lineMap:e,ast:r},n,t=!1)=>{const i=function(e,r,n){e:for(const t of r){const r=Number.isInteger(Number(t))?Number(t):t;if("string"==typeof r||"number"==typeof r&&"array"!==e.type){if("object"!==e.type||!Array.isArray(e.children))return n?e:void 0;for(const n of e.children)if(Array.isArray(n.children)&&n.children[0].value===String(r)){e=n.children[1];continue e}return n?e:void 0}if("array"!==e.type||r<0||!Array.isArray(e.children)||r>=e.children.length)return n?e:void 0;e=e.children[r]}return e}(r,n,t);if(void 0!==i&&void 0!==i.range)return{range:i.range}};const O=e=>o(e)&&"$ref"in e&&"string"==typeof e.$ref,x=(e,r={disallowComments:!0})=>{const n=[],{ast:t,data:i,lineMap:o}=S(e,n,r);return{data:i,diagnostics:n,ast:t,lineMap:o}},N=Symbol("object_keys"),A={ownKeys:e=>e[N]};function S(e,r=[],n){const o=j(e);let a={type:"array",offset:-1,length:-1,children:[],parent:void 0},c=null,u=[];const l=new WeakMap,f=[];function p(e){"property"===a.type&&(a.length=e-a.offset,a=a.parent)}function h(e,r,n){return{start:{line:e,character:r},end:{line:e,character:r+n}}}function g(e){return a.children.push(e),e}function y(e){Array.isArray(u)?u.push(e):null!==c&&(u[c]=e)}function d(e){y(e),f.push(u),u=e,c=null}function m(){N in u&&u[N].push(N),u=f.pop()}t(e,{onObjectBegin:(e,r,t,i)=>{a=g({type:"object",offset:e,length:-1,parent:a,children:[],range:h(t,i,r)}),!1===n.ignoreDuplicateKeys&&l.set(a,[]),d(function(e){if(e){const e=new Proxy({},A);return Reflect.defineProperty(e,N,{value:[]}),e}return{}}(!0===n.preserveKeyOrder))},onObjectProperty:(e,t,i,o,f)=>{if((a=g({type:"property",offset:t,length:-1,parent:a,children:[]})).children.push({type:"string",value:e,offset:t,length:i,parent:a}),!1===n.ignoreDuplicateKeys){const n=l.get(a.parent);n&&(0!==n.length&&n.includes(e)?r.push({range:h(o,f,i),message:"DuplicateKey",severity:s.Error,path:k(a),code:20}):n.push(e))}!0===n.preserveKeyOrder&&N in u&&function(e,r){const n=r in e?e[N].indexOf(r):-1;-1!==n&&e[N].splice(n,1);e[N].push(r)}(u,e),c=e},onObjectEnd:(e,r,t,i)=>{!1===n.ignoreDuplicateKeys&&l.delete(a),a.length=e+r-a.offset,a.range&&(a.range.end.line=t,a.range.end.character=i+r),a=a.parent,p(e+r),m()},onArrayBegin:(e,r,n,t)=>{a=g({type:"array",offset:e,length:-1,parent:a,children:[],range:h(n,t,r)}),d([])},onArrayEnd:(e,r,n,t)=>{a.length=e+r-a.offset,a.range&&(a.range.end.line=n,a.range.end.character=t+r),a=a.parent,p(e+r),m()},onLiteralValue:(e,r,n,t,i)=>{g({type:E(e),offset:r,length:n,parent:a,value:e,range:h(t,i,n)}),p(r+n),y(e)},onSeparator:(e,r,n)=>{"property"===a.type&&(":"===e?a.colonOffset=r:","===e&&p(r))},onError:(e,n,t,o,a)=>{r.push({range:h(o,a,t),message:i(e),severity:s.Error,code:e})}},n);const b=a.children[0];return b&&delete b.parent,{ast:b,data:u[0],lineMap:o}}function E(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const j=e=>{const r=[0];let n=0;for(;n<e.length;n++)"\n"===e[n]&&r.push(n+1);return r.push(n+1),r};function k(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)),k(e.parent,r)):r}const I=e=>T(e),T=e=>{if(e&&"object"!=typeof e)throw new TypeError("Invalid type: path must be an array of segments.");return 0===e.length?"#":`#/${e.map(h).join("/")}`},P=e=>K(e),K=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,n=[];let t=-1;for(;++t<r;)n.push(l(e[t]));return n})(e.substring(2).split("/"))},M=(e,r,n)=>{if(!e||!Object.hasOwnProperty.call(e,r)||r===n)return e;const t={};for(const[i,o]of Object.entries(e))i===r?t[n]=o:i in t||(t[i]=o);return t},U=(e,r)=>{if("string"!=typeof e)return e;try{const n=J(e);return"string"==typeof n?n:JSON.parse(e,r)}catch(e){return}},J=e=>{const r=Number(e);return Number.isFinite(r)?String(r)===e?r:e:NaN},R=(e,r,n)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,r,n)}catch(t){return c(e,r,n)}},D=(e,r)=>{if(e instanceof Array){if(r instanceof Array){if(r.length>e.length)return!1;for(const n in r){if(!r.hasOwnProperty(n))continue;const t=parseInt(e[n]),i=parseInt(r[n]);if(isNaN(t)&&isNaN(i)){if(e[n]!==r[n])return!1}else if(t!==i)return!1}}}else{if("string"!=typeof e)return!1;if("string"==typeof r)return e.startsWith(r)}return!0};function V(e){return e.replace(/^(\/|#\/)/,"").split("/").map(f).map($).join(".")}function $(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}function C(e,r){if("string"==typeof e&&"string"==typeof r)return a(e,r);if(!(e&&Array.isArray(e)&&e.length&&r&&Array.isArray(r)&&r.length))return e;let n=0;for(const t in e)if(e.hasOwnProperty(t)){if(e[t]!==r[t])break;n++}return e.slice(n)}export{l as decodePointer,f as decodePointerFragment,p as encodePointer,h as encodePointerFragment,g as extractPointerFromRef,d as extractSourceFromRef,m as getFirstPrimitiveProperty,b as getJsonPathForPosition,w as getLastPathSegment,v as getLocationForJsonPath,O as hasRef,y as isLocalRef,S as parseTree,x as parseWithPointers,I as pathToPointer,P as pointerToPath,M as renameObjectKey,U as safeParse,R as safeStringify,D as startsWith,V as toPropertyPath,C as trimStart};
import{cloneDeep as e,get as r,set as t,isObject as n,trimStart as o}from"lodash";import{createScanner as i,findNodeAtOffset as a,getNodePath as s,visit as c,printParseErrorCode as u}from"jsonc-parser";import{DiagnosticSeverity as f}from"@stoplight/types";import p from"safe-stable-stringify";const l=(e,r,t)=>{const n=e.toString();let o="",i=n,a=0,s=i.indexOf(r);for(;s>-1;)o+=n.substring(a,a+s)+t,i=i.substring(s+r.length,i.length),a+=s+r.length,s=i.indexOf(r);return i.length>0&&(o+=n.substring(n.length-i.length,n.length)),o},h=e=>l(l(decodeURIComponent(""+e),"~1","/"),"~0","~"),y=e=>g(e),g=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(h(e[n]));return t})(e.substring(2).split("/"))},d=(e,r,t=[])=>{if(e&&"object"==typeof e)for(const n in e)e.hasOwnProperty(n)&&(r({parentPath:t,property:n,propertyValue:e[n]}),e[n]&&"object"==typeof e[n]&&d(e[n],r,t.concat(n)))},m=({document:r,path:t},n)=>b(e(r),t,n),b=(e,n,o)=>{const i=r(e,y(n));return d(o||i,({property:o,propertyValue:a})=>{if("$ref"===o&&"string"==typeof a){const o=y(a),s=r(e,o),c=!!r(i,o);s&&!c&&(t(i,o,s),b(e,n,s))}}),i},w=e=>l(l(e,"~1","/"),"~0","~"),v=e=>l(l(e,"~","~0"),"//","/~1"),O=e=>"number"==typeof e?e:l(l(e,"~","~0"),"/","~1"),x=e=>{if("string"!=typeof e||0===e.length)return null;const r=e.indexOf("#");return-1===r?null:e.slice(r)},N=e=>e.length>0&&"#"===e[0],A=e=>{if("string"!=typeof e||0===e.length||N(e))return null;const r=e.indexOf("#");return-1===r?e:e.slice(0,r)},S=e=>{const r=i(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}},j=({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 c=s(i);return 0!==c.length?c:void 0};function E(e){return w(e.split("/").pop()||"")}const k=({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)){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 I=e=>n(e)&&"$ref"in e&&"string"==typeof e.$ref,P=(e,r={disallowComments:!0})=>{const t=[],{ast:n,data:o,lineMap:i}=M(e,t,r);return{data:o,diagnostics:t,ast:n,lineMap:i}},T=Symbol("object_keys"),K={ownKeys:e=>e[T]};function M(e,r=[],t){const n=V(e);let o={type:"array",offset:-1,length:-1,children:[],parent:void 0},i=null,a=[];const s=new WeakMap,p=[];function l(e){"property"===o.type&&(o.length=e-o.offset,o=o.parent)}function h(e,r,t){return{start:{line:e,character:r},end:{line:e,character:r+t}}}function y(e){return o.children.push(e),e}function g(e){Array.isArray(a)?a.push(e):null!==i&&(a[i]=e)}function d(e){g(e),p.push(a),a=e,i=null}function m(){T in a&&a[T].push(T),a=p.pop()}c(e,{onObjectBegin:(e,r,n,i)=>{o=y({type:"object",offset:e,length:-1,parent:o,children:[],range:h(n,i,r)}),!1===t.ignoreDuplicateKeys&&s.set(o,[]),d(function(e){if(e){const e=new Proxy({},K);return Reflect.defineProperty(e,T,{value:[]}),e}return{}}(!0===t.preserveKeyOrder))},onObjectProperty:(e,n,c,u,p)=>{if((o=y({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=s.get(o.parent);t&&(0!==t.length&&t.includes(e)?r.push({range:h(u,p,c),message:"DuplicateKey",severity:f.Error,path:J(o),code:20}):t.push(e))}!0===t.preserveKeyOrder&&T in a&&function(e,r){const t=r in e?e[T].indexOf(r):-1;-1!==t&&e[T].splice(t,1);e[T].push(r)}(a,e),i=e},onObjectEnd:(e,r,n,i)=>{!1===t.ignoreDuplicateKeys&&s.delete(o),o.length=e+r-o.offset,o.range&&(o.range.end.line=n,o.range.end.character=i+r),o=o.parent,l(e+r),m()},onArrayBegin:(e,r,t,n)=>{o=y({type:"array",offset:e,length:-1,parent:o,children:[],range:h(t,n,r)}),d([])},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,l(e+r),m()},onLiteralValue:(e,r,t,n,i)=>{y({type:U(e),offset:r,length:t,parent:o,value:e,range:h(n,i,t)}),l(r+t),g(e)},onSeparator:(e,r,t)=>{"property"===o.type&&(":"===e?o.colonOffset=r:","===e&&l(r))},onError:(e,t,n,o,i)=>{r.push({range:h(o,i,n),message:u(e),severity:f.Error,code:e})}},t);const b=o.children[0];return b&&delete b.parent,{ast:b,data:a[0],lineMap:n}}function U(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const V=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 J(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)),J(e.parent,r)):r}const R=e=>$(e),$=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("/")}`},D=(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},C=(e,r)=>{if("string"!=typeof e)return e;try{const t=B(e);return"string"==typeof t?t:JSON.parse(e,r)}catch(e){return}},B=e=>{const r=Number(e);return Number.isFinite(r)?String(r)===e?r:e:NaN},W=(e,r,t)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,r,t)}catch(n){return p(e,r,t)}},F=(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};function L(e){return e.replace(/^(\/|#\/)/,"").split("/").map(w).map(_).join(".")}function _(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}function q(e,r){if("string"==typeof e&&"string"==typeof r)return o(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{m as bundleTarget,h as decodePointer,w as decodePointerFragment,v as encodePointer,O as encodePointerFragment,x as extractPointerFromRef,A as extractSourceFromRef,S as getFirstPrimitiveProperty,j as getJsonPathForPosition,E as getLastPathSegment,k as getLocationForJsonPath,I as hasRef,N as isLocalRef,M as parseTree,P as parseWithPointers,R as pathToPointer,y as pointerToPath,D as renameObjectKey,C as safeParse,W as safeStringify,F as startsWith,L as toPropertyPath,d as traverse,q as trimStart};
{
"name": "@stoplight/json",
"version": "3.5.0",
"version": "3.5.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