UNPKG

jiff

Version:

JSON diff and patch based on rfc6902

46 lines (36 loc) 1.18 kB
/** @license MIT License (c) copyright 2010-2014 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ module.exports = jsonPointerParse; var parseRx = /\/|~1|~0/g; var separator = '/'; var escapeChar = '~'; var encodedSeparator = '~1'; /** * Parse through an encoded JSON Pointer string, decoding each path segment * and passing it to an onSegment callback function. * @see https://tools.ietf.org/html/rfc6901#section-4 * @param {string} path encoded JSON Pointer string * @param {{function(segment:string):boolean}} onSegment callback function * @returns {string} original path */ function jsonPointerParse(path, onSegment) { var pos, accum, matches, match; pos = path.charAt(0) === separator ? 1 : 0; accum = ''; parseRx.lastIndex = pos; while(matches = parseRx.exec(path)) { match = matches[0]; accum += path.slice(pos, parseRx.lastIndex - match.length); pos = parseRx.lastIndex; if(match === separator) { if (onSegment(accum) === false) return path; accum = ''; } else { accum += match === encodedSeparator ? separator : escapeChar; } } accum += path.slice(pos); onSegment(accum); return path; }