UNPKG

@winglet/json

Version:

TypeScript library for safe and efficient JSON data manipulation with RFC 6901 (JSON Pointer) and RFC 6902 (JSON Patch) compliance, featuring prototype pollution protection and immutable operations

25 lines (21 loc) 777 B
'use strict'; var filter = require('@winglet/common-utils/filter'); var _enum = require('../../../../enum.cjs'); const getArrayBasePath = (path) => { let lastSlash = -1; let segmentStart = 1; for (let i = 1; i < path.length; i++) { if (path[i] === _enum.JSONPointer.Separator) { const segment = path.slice(segmentStart, i); if (filter.isArrayIndex(segment)) return lastSlash !== -1 ? path.slice(0, lastSlash) : ''; lastSlash = i; segmentStart = i + 1; } } const lastSegment = path.slice(segmentStart); if (filter.isArrayIndex(lastSegment)) return lastSlash !== -1 ? path.slice(0, lastSlash) : ''; return null; }; exports.getArrayBasePath = getArrayBasePath;