Comparing version 0.4.3 to 0.4.4
147
jpath.js
'use strict'; | ||
/** | ||
* escapes JSON Pointer using ~0 for ~ and ~1 for / | ||
* @param s the string to escape | ||
* @return the escaped string | ||
*/ | ||
function jpescape(s) { | ||
s = s.replaceAll('~','~0'); | ||
s = s.replaceAll('/','~1'); | ||
return s; | ||
return s.replace(/\~/g, '~0').replace(/\//g, '~1'); | ||
} | ||
/** | ||
* unescapes JSON Pointer using ~0 for ~ and ~1 for / | ||
* @param s the string to unescape | ||
* @return the unescaped string | ||
*/ | ||
function jpunescape(s) { | ||
return s.replace(/\~1/g, '/').replace(/~0/g, '~'); | ||
} | ||
// JSON Pointer specification: http://tools.ietf.org/html/rfc6901 | ||
/** | ||
* from obj, return the property with a JSON Pointer prop, optionally setting it | ||
* to newValue | ||
* @param obj the object to point into | ||
* @param prop the JSON Pointer or JSON Reference | ||
* @param newValue optional value to set the property to | ||
* @return the found property, or false | ||
*/ | ||
function jptr(obj, prop, newValue) { | ||
//property not found | ||
if (typeof obj === 'undefined') return false; | ||
if ((!prop) || (prop == '#')) return obj; | ||
if (!prop || typeof prop !== 'string' || (prop === '#')) return (typeof newValue !== 'undefined' ? newValue : obj); | ||
if (prop.startsWith('#')) prop = prop.slice(1); | ||
if (prop.startsWith('/')) prop = prop.slice(1); | ||
var props = prop.split('/'); | ||
if (prop.indexOf('#')>=0) { | ||
let parts = prop.split('#'); | ||
let uri = parts[0]; | ||
if (uri) return false; // we do internal resolution only | ||
prop = parts[1]; | ||
prop = decodeURIComponent(prop.slice(1).split('+').join(' ')); | ||
} | ||
if (prop.startsWith('/')) prop = prop.slice(1); | ||
var current = props[0]; | ||
current = current.replaceAll('~1','/'); | ||
current = current.replaceAll('~0','~'); | ||
let components = prop.split('/'); | ||
for (let i=0;i<components.length;i++) { | ||
components[i] = jpunescape(components[i]); | ||
var index = -1; | ||
if ((props.length>1) && (Array.isArray(obj[current]))) { | ||
var next = props[1]; | ||
var value = parseInt(next,10); | ||
if (next == '-') { | ||
index = obj[current].length; | ||
} | ||
else { | ||
if (!isNaN(value)) index = value; | ||
} | ||
if (index>=0) { | ||
props.splice(1,1); | ||
prop = props.join('/'); | ||
} | ||
} | ||
let setAndLast = (typeof newValue !== 'undefined') && (i == components.length-1); | ||
//property split found; recursive call | ||
if (props.length>1) { | ||
var pos = prop.indexOf('/'); | ||
//get object at property (before split), pass on remainder | ||
if (index>=0) { | ||
return jptr(obj[current][index], prop.substr(pos+1),newValue); //was props | ||
} | ||
else { | ||
return jptr(obj[current], prop.substr(pos+1),newValue); | ||
} | ||
} | ||
//no split; get property[index] or property | ||
var source = obj; | ||
if (current) source = obj[current]; | ||
if (index>=0) { | ||
if (index>=source.length) { | ||
if (typeof newValue != 'undefined') { | ||
source.push(newValue); | ||
return newValue; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
else { | ||
if (typeof newValue != 'undefined') { | ||
source[index] = newValue; | ||
} | ||
return source[index]; | ||
} | ||
} | ||
else { | ||
if (typeof newValue != 'undefined') { | ||
obj[prop] = newValue; | ||
source = obj[prop]; | ||
} | ||
return source; | ||
} | ||
let index = parseInt(components[i],10); | ||
if (!Array.isArray(obj) || isNaN(index) || (index.toString() !== components[i])) { | ||
index = (Array.isArray(obj) && components[i] === '-') ? -2 : -1; | ||
} | ||
else { | ||
components[i] = (i > 0) ? components[i-1] : ''; // backtrack to indexed property name | ||
} | ||
if ((index != -1) || obj.hasOwnProperty(components[i])) { | ||
if (index >= 0) { | ||
if (setAndLast) { | ||
obj[index] = newValue; | ||
} | ||
obj = obj[index]; | ||
} | ||
else if (index === -2) { | ||
if (setAndLast) { | ||
if (Array.isArray(obj)) { | ||
obj.push(newValue); | ||
} | ||
return newValue; | ||
} | ||
else return undefined; | ||
} | ||
else { | ||
if (setAndLast) { | ||
obj[components[i]] = newValue; | ||
} | ||
obj = obj[components[i]]; | ||
} | ||
} | ||
else { | ||
if ((typeof newValue !== 'undefined') && (typeof obj === 'object') && | ||
(!Array.isArray(obj))) { | ||
obj[components[i]] = (setAndLast ? newValue : ((components[i+1] === '0' || components[i+1] === '-') ? [] : {})); | ||
obj = obj[components[i]]; | ||
} | ||
else return false; | ||
} | ||
} | ||
return obj; | ||
} | ||
@@ -208,3 +222,3 @@ | ||
// [*] | ||
target = target.replaceAll('[*]','[]'); | ||
target = target.split('[*]').join('[]'); | ||
// .. | ||
@@ -214,4 +228,3 @@ if ((target.indexOf('..') > 0) && (target != '$..*')) { | ||
target = x[x.length-1]; | ||
//target = target.replaceAll('..','.'); | ||
target = target.replaceAll('$',''); | ||
target = target.split('$').join(''); | ||
checkEnd = true; | ||
@@ -218,0 +231,0 @@ } |
{ | ||
"name": "jgexml", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "The Just-Good-Enough XML Toolkit", | ||
@@ -5,0 +5,0 @@ "main": "jgeXml.js", |
# jgeXml - The Just-Good-Enough XML Toolkit | ||
[![Build status](https://travis-ci.org/Mermade/jgeXml.svg?branch=master)](https://travis-ci.org/Mermade/jgeXml) | ||
[![Join the Mermade Slack](https://img.shields.io/badge/Slack-Mermade-brightgreen)](https://join.slack.com/t/mermade/shared_invite/zt-g78g7xir-MLE_CTCcXCdfJfG3CJe9qA) | ||
@@ -5,0 +6,0 @@ [![Share on Twitter][twitter-image]][twitter-link] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75116
2223
78