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

@stoplight/json

Package Overview
Dependencies
Maintainers
18
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.1.1 to 3.1.2

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"),types=require("@stoplight/types"),fastStringify=_interopDefault(require("safe-stable-stringify")),lodash=require("lodash");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"),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 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=[""],p=[];function c(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 g(e){return o.children.push(e),e}function l(e){Array.isArray(a)?a.push(e):i&&(a[i]=e)}function u(e){l(e),p.push(a),a=e,i=null}function d(){a=p.pop()}const h={onObjectBegin:(e,t,n,i)=>{o=g({type:"object",offset:e,length:-1,parent:o,children:[],range:f(n,i,t)}),!1===r.ignoreDuplicateKeys&&(s.length=0),u({})},onObjectProperty:(e,n,a,p,c)=>{(o=g({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:a,parent:o}),!1===r.ignoreDuplicateKeys&&(0!==s.length&&s.includes(e)?t.push({range:f(p,c,a),message:"DuplicateKey",severity:types.DiagnosticSeverity.Error,path:getJsonPath(o),code:20}):s.push(e)),i=e},onObjectEnd:(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,c(e+t),d()},onArrayBegin:(e,t,r,n)=>{o=g({type:"array",offset:e,length:-1,parent:o,children:[],range:f(r,n,t)}),u([])},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,c(e+t),d()},onLiteralValue:(e,t,r,n,i)=>{g({type:getLiteralNodeType(e),offset:t,length:r,parent:o,value:e,range:f(n,i,r)}),c(t+r),l(e)},onSeparator:(e,t,r)=>{"property"===o.type&&(":"===e?o.colonOffset=t:","===e&&c(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}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||!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.getJsonPathForPosition=getJsonPathForPosition,exports.getLastPathSegment=getLastPathSegment,exports.getLocationForJsonPath=getLocationForJsonPath,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 jsoncParser=require("jsonc-parser"),types=require("@stoplight/types"),fastStringify=_interopDefault(require("safe-stable-stringify")),lodash=require("lodash");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"),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 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=[""],p=[];function c(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 g(e){return o.children.push(e),e}function l(e){Array.isArray(a)?a.push(e):null!==i&&(a[i]=e)}function u(e){l(e),p.push(a),a=e,i=null}function d(){a=p.pop()}const h={onObjectBegin:(e,t,n,i)=>{o=g({type:"object",offset:e,length:-1,parent:o,children:[],range:f(n,i,t)}),!1===r.ignoreDuplicateKeys&&(s.length=0),u({})},onObjectProperty:(e,n,a,p,c)=>{(o=g({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:a,parent:o}),!1===r.ignoreDuplicateKeys&&(0!==s.length&&s.includes(e)?t.push({range:f(p,c,a),message:"DuplicateKey",severity:types.DiagnosticSeverity.Error,path:getJsonPath(o),code:20}):s.push(e)),i=e},onObjectEnd:(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,c(e+t),d()},onArrayBegin:(e,t,r,n)=>{o=g({type:"array",offset:e,length:-1,parent:o,children:[],range:f(r,n,t)}),u([])},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,c(e+t),d()},onLiteralValue:(e,t,r,n,i)=>{g({type:getLiteralNodeType(e),offset:t,length:r,parent:o,value:e,range:f(n,i,r)}),c(t+r),l(e)},onSeparator:(e,t,r)=>{"property"===o.type&&(":"===e?o.colonOffset=t:","===e&&c(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}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||!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.getJsonPathForPosition=getJsonPathForPosition,exports.getLastPathSegment=getLastPathSegment,exports.getLocationForJsonPath=getLocationForJsonPath,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;

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

import{findNodeAtOffset as r,getNodePath as e,visit as t,printParseErrorCode as n}from"jsonc-parser";import{DiagnosticSeverity as i}from"@stoplight/types";import o from"safe-stable-stringify";import{trimStart as a}from"lodash";const s=(r,e,t)=>{const n=r.toString();let i="",o=n,a=0,s=o.indexOf(e);for(;s>-1;)i+=n.substring(a,a+s)+t,o=o.substring(s+e.length,o.length),a+=s+e.length,s=o.indexOf(e);return o.length>0&&(i+=n.substring(n.length-o.length,n.length)),i},f=r=>s(s(decodeURIComponent(""+r),"~1","/"),"~0","~"),p=r=>s(s(r,"~1","/"),"~0","~"),c=r=>s(s(r,"~","~0"),"//","/~1"),l=r=>"number"==typeof r?r:s(s(r,"~","~0"),"/","~1"),u=({lineMap:t,ast:n},i)=>{const o=t[i.line],a=t[i.line+1];if(void 0===o)return;const s=r(n,void 0===a?o+i.character:Math.min(a,o+i.character),!0);if(void 0===s)return;const f=e(s);return 0!==f.length?f:void 0};function h(r){return p(r.split("/").pop()||"")}const g=({lineMap:r,ast:e},t,n=!1)=>{const i=function(r,e,t){r:for(const n of e){const e=Number.isInteger(Number(n))?Number(n):n;if("string"==typeof e||"number"==typeof e&&"array"!==r.type){if("object"!==r.type||!Array.isArray(r.children))return t?r:void 0;for(const t of r.children)if(Array.isArray(t.children)&&t.children[0].value===String(e)){r=t.children[1];continue r}return t?r:void 0}if("array"!==r.type||e<0||!Array.isArray(r.children)||e>=r.children.length)return t?r:void 0;r=r.children[e]}return r}(e,t,n);if(void 0!==i&&void 0!==i.range)return{range:i.range}};const y=(r,e={disallowComments:!0})=>{const t=[],{ast:n,data:i,lineMap:o}=d(r,t,e);return{data:i,diagnostics:t,ast:n,lineMap:o}};function d(r,e=[],o){const a=b(r);let s={type:"array",offset:-1,length:-1,children:[],parent:void 0},f=null,p=[];const c=[""],l=[];function u(r){"property"===s.type&&(s.length=r-s.offset,s=s.parent)}function h(r,e,t){return{start:{line:r,character:e},end:{line:r,character:e+t}}}function g(r){return s.children.push(r),r}function y(r){Array.isArray(p)?p.push(r):f&&(p[f]=r)}function d(r){y(r),l.push(p),p=r,f=null}function A(){p=l.pop()}t(r,{onObjectBegin:(r,e,t,n)=>{s=g({type:"object",offset:r,length:-1,parent:s,children:[],range:h(t,n,e)}),!1===o.ignoreDuplicateKeys&&(c.length=0),d({})},onObjectProperty:(r,t,n,a,p)=>{(s=g({type:"property",offset:t,length:-1,parent:s,children:[]})).children.push({type:"string",value:r,offset:t,length:n,parent:s}),!1===o.ignoreDuplicateKeys&&(0!==c.length&&c.includes(r)?e.push({range:h(a,p,n),message:"DuplicateKey",severity:i.Error,path:v(s),code:20}):c.push(r)),f=r},onObjectEnd:(r,e,t,n)=>{s.length=r+e-s.offset,s.range&&(s.range.end.line=t,s.range.end.character=n+e),s=s.parent,u(r+e),A()},onArrayBegin:(r,e,t,n)=>{s=g({type:"array",offset:r,length:-1,parent:s,children:[],range:h(t,n,e)}),d([])},onArrayEnd:(r,e,t,n)=>{s.length=r+e-s.offset,s.range&&(s.range.end.line=t,s.range.end.character=n+e),s=s.parent,u(r+e),A()},onLiteralValue:(r,e,t,n,i)=>{g({type:m(r),offset:e,length:t,parent:s,value:r,range:h(n,i,t)}),u(e+t),y(r)},onSeparator:(r,e,t)=>{"property"===s.type&&(":"===r?s.colonOffset=e:","===r&&u(e))},onError:(r,t,o,a,s)=>{e.push({range:h(a,s,o),message:n(r),severity:i.Error,code:r})}},o);const N=s.children[0];return N&&delete N.parent,{ast:N,data:p[0],lineMap:a}}function m(r){switch(typeof r){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const b=r=>{const e=[0];let t=0;for(;t<r.length;t++)"\n"===r[t]&&e.push(t+1);return e.push(t+1),e};function v(r,e=[]){return"property"===r.type&&e.unshift(r.children[0].value),void 0!==r.parent?("array"===r.parent.type&&void 0!==r.parent.parent&&e.unshift(r.parent.children.indexOf(r)),v(r.parent,e)):e}const A=r=>N(r),N=r=>{if(r&&"object"!=typeof r)throw new TypeError("Invalid type: path must be an array of segments.");return 0===r.length?"#":`#/${r.map(l).join("/")}`},O=r=>w(r),w=r=>{if("string"!=typeof r)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===r.length||"#"!==r[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===r.length)return[];if("/"!==r[1])throw new URIError("Invalid JSON Pointer syntax.");return(r=>{const e=r.length,t=[];let n=-1;for(;++n<e;)t.push(f(r[n]));return t})(r.substring(2).split("/"))},I=(r,e,t)=>{if(!r||!r[e]||e===t)return r;const n={};for(const[i,o]of Object.entries(r))i===e?n[t]=o:i in n||(n[i]=o);return n},j=(r,e)=>{if("string"!=typeof r)return r;try{const t=S(r);return"string"==typeof t?t:JSON.parse(r,e)}catch(r){return}},S=r=>{const e=Number(r);return Number.isFinite(e)?String(e)===r?e:r:NaN},E=(r,e,t)=>{if("string"==typeof r)return r;try{return JSON.stringify(r,e,t)}catch(n){return o(r,e,t)}},x=(r,e)=>{if(r instanceof Array){if(e instanceof Array){if(e.length>r.length)return!1;for(const t in e){if(!e.hasOwnProperty(t))continue;const n=parseInt(r[t]),i=parseInt(e[t]);if(isNaN(n)&&isNaN(i)){if(r[t]!==e[t])return!1}else if(n!==i)return!1}}}else{if("string"!=typeof r)return!1;if("string"==typeof e)return r.startsWith(e)}return!0};function M(r){return r.replace(/^(\/|#\/)/,"").split("/").map(p).map(P).join(".")}function P(r){return r.includes(".")?`["${r.replace(/"/g,'\\"')}"]`:r}function J(r,e){if("string"==typeof r&&"string"==typeof e)return a(r,e);if(!(r&&Array.isArray(r)&&r.length&&e&&Array.isArray(e)&&e.length))return r;let t=0;for(const n in r)if(r.hasOwnProperty(n)){if(r[n]!==e[n])break;t++}return r.slice(t)}export{f as decodePointer,p as decodePointerFragment,c as encodePointer,l as encodePointerFragment,u as getJsonPathForPosition,h as getLastPathSegment,g as getLocationForJsonPath,d as parseTree,y as parseWithPointers,A as pathToPointer,O as pointerToPath,I as renameObjectKey,j as safeParse,E as safeStringify,x as startsWith,M as toPropertyPath,J as trimStart};
import{findNodeAtOffset as r,getNodePath as e,visit as n,printParseErrorCode as t}from"jsonc-parser";import{DiagnosticSeverity as i}from"@stoplight/types";import o from"safe-stable-stringify";import{trimStart as a}from"lodash";const s=(r,e,n)=>{const t=r.toString();let i="",o=t,a=0,s=o.indexOf(e);for(;s>-1;)i+=t.substring(a,a+s)+n,o=o.substring(s+e.length,o.length),a+=s+e.length,s=o.indexOf(e);return o.length>0&&(i+=t.substring(t.length-o.length,t.length)),i},f=r=>s(s(decodeURIComponent(""+r),"~1","/"),"~0","~"),l=r=>s(s(r,"~1","/"),"~0","~"),p=r=>s(s(r,"~","~0"),"//","/~1"),c=r=>"number"==typeof r?r:s(s(r,"~","~0"),"/","~1"),u=({lineMap:n,ast:t},i)=>{const o=n[i.line],a=n[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 f=e(s);return 0!==f.length?f:void 0};function h(r){return l(r.split("/").pop()||"")}const g=({lineMap:r,ast:e},n,t=!1)=>{const i=function(r,e,n){r:for(const t of e){const e=Number.isInteger(Number(t))?Number(t):t;if("string"==typeof e||"number"==typeof e&&"array"!==r.type){if("object"!==r.type||!Array.isArray(r.children))return n?r:void 0;for(const n of r.children)if(Array.isArray(n.children)&&n.children[0].value===String(e)){r=n.children[1];continue r}return n?r:void 0}if("array"!==r.type||e<0||!Array.isArray(r.children)||e>=r.children.length)return n?r:void 0;r=r.children[e]}return r}(e,n,t);if(void 0!==i&&void 0!==i.range)return{range:i.range}};const y=(r,e={disallowComments:!0})=>{const n=[],{ast:t,data:i,lineMap:o}=d(r,n,e);return{data:i,diagnostics:n,ast:t,lineMap:o}};function d(r,e=[],o){const a=b(r);let s={type:"array",offset:-1,length:-1,children:[],parent:void 0},f=null,l=[];const p=[""],c=[];function u(r){"property"===s.type&&(s.length=r-s.offset,s=s.parent)}function h(r,e,n){return{start:{line:r,character:e},end:{line:r,character:e+n}}}function g(r){return s.children.push(r),r}function y(r){Array.isArray(l)?l.push(r):null!==f&&(l[f]=r)}function d(r){y(r),c.push(l),l=r,f=null}function A(){l=c.pop()}n(r,{onObjectBegin:(r,e,n,t)=>{s=g({type:"object",offset:r,length:-1,parent:s,children:[],range:h(n,t,e)}),!1===o.ignoreDuplicateKeys&&(p.length=0),d({})},onObjectProperty:(r,n,t,a,l)=>{(s=g({type:"property",offset:n,length:-1,parent:s,children:[]})).children.push({type:"string",value:r,offset:n,length:t,parent:s}),!1===o.ignoreDuplicateKeys&&(0!==p.length&&p.includes(r)?e.push({range:h(a,l,t),message:"DuplicateKey",severity:i.Error,path:v(s),code:20}):p.push(r)),f=r},onObjectEnd:(r,e,n,t)=>{s.length=r+e-s.offset,s.range&&(s.range.end.line=n,s.range.end.character=t+e),s=s.parent,u(r+e),A()},onArrayBegin:(r,e,n,t)=>{s=g({type:"array",offset:r,length:-1,parent:s,children:[],range:h(n,t,e)}),d([])},onArrayEnd:(r,e,n,t)=>{s.length=r+e-s.offset,s.range&&(s.range.end.line=n,s.range.end.character=t+e),s=s.parent,u(r+e),A()},onLiteralValue:(r,e,n,t,i)=>{g({type:m(r),offset:e,length:n,parent:s,value:r,range:h(t,i,n)}),u(e+n),y(r)},onSeparator:(r,e,n)=>{"property"===s.type&&(":"===r?s.colonOffset=e:","===r&&u(e))},onError:(r,n,o,a,s)=>{e.push({range:h(a,s,o),message:t(r),severity:i.Error,code:r})}},o);const N=s.children[0];return N&&delete N.parent,{ast:N,data:l[0],lineMap:a}}function m(r){switch(typeof r){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const b=r=>{const e=[0];let n=0;for(;n<r.length;n++)"\n"===r[n]&&e.push(n+1);return e.push(n+1),e};function v(r,e=[]){return"property"===r.type&&e.unshift(r.children[0].value),void 0!==r.parent?("array"===r.parent.type&&void 0!==r.parent.parent&&e.unshift(r.parent.children.indexOf(r)),v(r.parent,e)):e}const A=r=>N(r),N=r=>{if(r&&"object"!=typeof r)throw new TypeError("Invalid type: path must be an array of segments.");return 0===r.length?"#":`#/${r.map(c).join("/")}`},O=r=>w(r),w=r=>{if("string"!=typeof r)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===r.length||"#"!==r[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===r.length)return[];if("/"!==r[1])throw new URIError("Invalid JSON Pointer syntax.");return(r=>{const e=r.length,n=[];let t=-1;for(;++t<e;)n.push(f(r[t]));return n})(r.substring(2).split("/"))},I=(r,e,n)=>{if(!r||!r[e]||e===n)return r;const t={};for(const[i,o]of Object.entries(r))i===e?t[n]=o:i in t||(t[i]=o);return t},j=(r,e)=>{if("string"!=typeof r)return r;try{const n=S(r);return"string"==typeof n?n:JSON.parse(r,e)}catch(r){return}},S=r=>{const e=Number(r);return Number.isFinite(e)?String(e)===r?e:r:NaN},E=(r,e,n)=>{if("string"==typeof r)return r;try{return JSON.stringify(r,e,n)}catch(t){return o(r,e,n)}},x=(r,e)=>{if(r instanceof Array){if(e instanceof Array){if(e.length>r.length)return!1;for(const n in e){if(!e.hasOwnProperty(n))continue;const t=parseInt(r[n]),i=parseInt(e[n]);if(isNaN(t)&&isNaN(i)){if(r[n]!==e[n])return!1}else if(t!==i)return!1}}}else{if("string"!=typeof r)return!1;if("string"==typeof e)return r.startsWith(e)}return!0};function M(r){return r.replace(/^(\/|#\/)/,"").split("/").map(l).map(P).join(".")}function P(r){return r.includes(".")?`["${r.replace(/"/g,'\\"')}"]`:r}function J(r,e){if("string"==typeof r&&"string"==typeof e)return a(r,e);if(!(r&&Array.isArray(r)&&r.length&&e&&Array.isArray(e)&&e.length))return r;let n=0;for(const t in r)if(r.hasOwnProperty(t)){if(r[t]!==e[t])break;n++}return r.slice(n)}export{f as decodePointer,l as decodePointerFragment,p as encodePointer,c as encodePointerFragment,u as getJsonPathForPosition,h as getLastPathSegment,g as getLocationForJsonPath,d as parseTree,y as parseWithPointers,A as pathToPointer,O as pointerToPath,I as renameObjectKey,j as safeParse,E as safeStringify,x as startsWith,M as toPropertyPath,J as trimStart};
{
"name": "@stoplight/json",
"version": "3.1.1",
"version": "3.1.2",
"description": "Useful functions when working with JSON.",

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

@@ -21,4 +21,4 @@ # @stoplight/json

- **[parseWithPointers](https://stoplightio.github.io/json/globals.html#parsewithpointers)**: Like `JSON.parse(val)` but also returns parsing errors as well as full ast with line information.
- **[pathToPointer](https://stoplightio.github.io/json/globals.html#pathtopointer)**: Turns an array of path segments into a json pointer IE `['paths', '/user', 'get']` -> `#/paths/~1/user/get`.
- **[pointerToPath](https://stoplightio.github.io/json/globals.html#pointertopath)**: Turns a json pointer into an array of path segments IE `#/paths/~1/user/get` -> `['paths', '/user', 'get']`.
- **[pathToPointer](https://stoplightio.github.io/json/globals.html#pathtopointer)**: Turns an array of path segments into a json pointer IE `['paths', '/user', 'get']` -> `#/paths/~1user/get`.
- **[pointerToPath](https://stoplightio.github.io/json/globals.html#pointertopath)**: Turns a json pointer into an array of path segments IE `#/paths/~1user/get` -> `['paths', '/user', 'get']`.
- **[safeParse](https://stoplightio.github.io/json/globals.html#safeparse)**: Like `JSON.parse(val)` but does not throw on invalid JSON.

@@ -25,0 +25,0 @@ - **[safeStringify](https://stoplightio.github.io/json/globals.html#safestringify)**: Like `JSON.stringify(val)` but handles circular references.

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