Skip to content

Commit

Permalink
Make simple String/Array polyfills for includes and utilize
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 4, 2016
1 parent d636ca5 commit ee3764d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ var isNode = module && !!module.exports;

var allowedResultTypes = ['value', 'path', 'pointer', 'parent', 'parentProperty', 'all'];

if (!Array.prototype.includes) {
Array.prototype.includes = function (item) { // eslint-disable-line no-extend-native
return this.indexOf(item) > -1;
};
}
if (!String.prototype.includes) {
String.prototype.includes = function (item) { // eslint-disable-line no-extend-native
return this.indexOf(item) > -1;
};
}

var moveToAnotherArray = function (source, target, conditionCb) {
for (var i = 0, kl = source.length; i < kl; i++) {
var key = source[i];
Expand Down Expand Up @@ -140,7 +151,7 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
if (Array.isArray(expr)) {
expr = JSONPath.toPathString(expr);
}
if (!expr || !json || allowedResultTypes.indexOf(this.currResultType) === -1) {
if (!expr || !json || !allowedResultTypes.includes(this.currResultType)) {
return;
}
this._obj = json;
Expand Down Expand Up @@ -270,7 +281,7 @@ JSONPath.prototype._trace = function (expr, val, path, parent, parentPropName, c
}
});
}
else if (loc.indexOf(',') > -1) { // [name1,name2,...]
else if (loc.includes(',')) { // [name1,name2,...]
var parts, i;
for (parts = loc.split(','), i = 0; i < parts.length; i++) {
addRet(this._trace(unshift(parts[i], x), val, path, parent, parentPropName, callback));
Expand All @@ -281,7 +292,7 @@ JSONPath.prototype._trace = function (expr, val, path, parent, parentPropName, c
var valueType = loc.slice(1, -2);
switch (valueType) {
case 'scalar':
if (!val || (['object', 'function'].indexOf(typeof val) === -1)) {
if (!val || !(['object', 'function'].includes(typeof val))) {
addType = true;
}
break;
Expand Down Expand Up @@ -399,19 +410,19 @@ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropNa

JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
if (!this._obj || !_v) {return false;}
if (code.indexOf('@parentProperty') > -1) {
if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/g, '_$_parentProperty');
}
if (code.indexOf('@parent') > -1) {
if (code.includes('@parent')) {
this.currSandbox._$_parent = parent;
code = code.replace(/@parent/g, '_$_parent');
}
if (code.indexOf('@property') > -1) {
if (code.includes('@property')) {
this.currSandbox._$_property = _vname;
code = code.replace(/@property/g, '_$_property');
}
if (code.indexOf('@path') > -1) {
if (code.includes('@path')) {
this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
code = code.replace(/@path/g, '_$_path');
}
Expand Down

0 comments on commit ee3764d

Please sign in to comment.