UNPKG

@stryke/json

Version:

A package containing JSON parsing/stringify utilities used by Storm Software.

2 lines (1 loc) 995 B
import{isNumber as o}from"@stryke/type-checks/is-number";import{isString as i}from"@stryke/type-checks/is-string";const s=/~1/g,u=/~0/g,c=/~/g,l=/\//g;export function escapePointerSegment(r){return!r.includes("/")&&!r.includes("~")?r:r.replace(c,"~0").replace(l,"~1")}export function unescapePointerSegment(r){return r.includes("~")?r.replace(s,"/").replace(u,"~"):r}export function parseJsonPointer(r){return r?r.slice(1).split("/").map(n=>unescapePointerSegment(n)):[]}export function formatJsonPointer(r){return isRoot(r)?"":`/${r.map(n=>escapePointerSegment(String(n))).join("/")}`}export const isRoot=r=>i(r)?r==="":o(r)?r===0:Array.isArray(r)&&r.length===0;export function parent(r){if(r.length===0)throw new Error("NO_PARENT");return r.slice(0,-1)}export function isValidIndex(r){if(o(r))return!0;const n=Number.parseInt(r,10);return String(n)===r&&n>=0}export const isInteger=r=>{const n=r.length;let t=0,e;for(;t<n;){if(e=r.codePointAt(t),e>=48&&e<=57){t++;continue}return!1}return!0};