Skip to content

Commit

Permalink
Merge pull request #20 from mrodrig/fix-j2c-184
Browse files Browse the repository at this point in the history
Add support for escaped paths.
  • Loading branch information
mrodrig committed May 26, 2021
2 parents a63e0ef + 321ca84 commit 6924ef0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
Expand Down Expand Up @@ -101,6 +101,7 @@
"template-tag-spacing": "error",
"unicode-bom": "error",
"wrap-regex": "error",
"no-invalid-regexp": "warn",

//ECMAScript6
"arrow-body-style": "error",
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ env:
- CC_TEST_REPORTER_ID=bf120a504e138055e0d71e23e1acd2d75d5dfe9fbcdaf0268ee9f4e3a23e5b2f
language: node_js
node_js:
- "16"
- "15"
- "14"
- "12"
- "10"
sudo: false
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let path = require('doc-path');

* `document` - `Object` - A JSON document that will be iterated over.
* `key` - `String` - A path to the existing key whose value will be returned.
* Note: If your key has a dot in it (eg. `a.b`) then be sure to escape the dot with a blackslash (eg. `a\\.b`).

If the key does not exist, `undefined` is returned.

Expand Down Expand Up @@ -81,6 +82,7 @@ console.log(path.evaluatePath(document, 'Features.packages.name'));

* `document` - `Object` - A JSON document that will be iterated over.
* `key` - `String` - A path to the existing key whose value will be set.
* Note: If your key has a dot in it (eg. `a.b`) then be sure to escape the dot with a blackslash (eg. `a\\.b`).
* `value` - `*` - The value that will be set at the given key.

If the key does not exist, then the object will be built up to have that path.
Expand Down Expand Up @@ -166,6 +168,7 @@ Lines : 100% ( 29/29 )

## Features

- Supports keys with escaped `.` characters (as of v3.0.0)
- Supports nested paths
- Including keys of objects inside arrays! (as of v2.0.0)
- Same common path specification as other programs such as MongoDB
2 changes: 1 addition & 1 deletion dist/path.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ function evaluatePath(obj, kp) {
} else if (Array.isArray(obj)) {
// If this object is actually an array, then iterate over those items evaluating the path
return obj.map((doc) => evaluatePath(doc, kp));
} else if (dotIndex >= 0 && kp !== key && obj[key]) {
// If there's a field with a non-nested dot, then recur into that sub-value
return evaluatePath(obj[key], remaining);
} else if (dotIndex === -1 && obj[key] && !obj[kp]) {
// If the field is here, but the key was escaped
return obj[key];
}

// Otherwise, we can just return value directly
Expand Down Expand Up @@ -82,7 +88,7 @@ function _sp(obj, kp, v) {
return obj.forEach((doc) => _sp(doc, remaining, v));
} else {
// Otherwise, we can set the path directly
obj[kp] = v;
obj[key] = v;
}

return obj;
Expand All @@ -95,11 +101,12 @@ function _sp(obj, kp, v) {
* @returns {{dotIndex: Number, key: String, remaining: String}}
*/
function state(kp) {
let dotIndex = kp.indexOf('.');
let match = (/(?<!\\)\./).exec(kp),
dotIndex = match ? match.index : -1;

return {
dotIndex,
key: kp.slice(0, dotIndex >= 0 ? dotIndex : undefined),
key: kp.slice(0, dotIndex >= 0 ? dotIndex : undefined).replace(/\\./g, '.'),
remaining: kp.slice(dotIndex + 1)
};
}
73 changes: 71 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "should" }]*/

let path = require('../dist/path'),
let path = require('../lib/path'),
should = require('should'),
assert = require('assert'),
doc = {};
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('doc-path Module', function() {
},
'testProperty.testProperty2': 'testVal2'
};
let returnVal = path.evaluatePath(doc, 'testProperty.testProperty2');
let returnVal = path.evaluatePath(doc, 'testProperty\\.testProperty2');
assert.equal(returnVal, 'testVal2');
done();
});
Expand Down Expand Up @@ -111,6 +111,32 @@ describe('doc-path Module', function() {
returnVal.should.deepEqual(['A/C', 'Radio']);
done();
});

it('should work with nested dots in the path when escaped properly', (done) => {
doc = {
'a.a': 'a',
'a.b': {
'c.d': '4',
c: '5',
'c.f': '6'
},
a: {
a: 1,
b: 2,
'b.c.d': 32
}
};
// Normal paths:
path.evaluatePath(doc, 'a.a').should.equal(1);
path.evaluatePath(doc, 'a.b').should.equal(2);
// Nested dot paths:
path.evaluatePath(doc, 'a\\.a').should.equal('a');
path.evaluatePath(doc, 'a\\.b.c\\.d').should.equal('4');
path.evaluatePath(doc, 'a\\.b.c').should.equal('5');
path.evaluatePath(doc, 'a\\.b.c\\.f').should.equal('6');
path.evaluatePath(doc, 'a.b\\.c\\.d').should.equal(32);
done();
});
});

describe('setPath', () => {
Expand Down Expand Up @@ -279,5 +305,48 @@ describe('doc-path Module', function() {
assert.equal(Object.polluted, undefined);
done();
});

it('should be able to set paths with nested dots correctly', (done) => {
doc = {
'a.a': 'a',
'a.b': {
'c.d': '4',
c: '5',
'c.f': '6'
},
a: {
a: 1,
b: 2,
'b.c.d': 32
}
};
// Normal paths:
path.setPath(doc, 'a.a', 'b');
doc.a.a.should.equal('b');
doc['a.a'].should.not.equal('b');

path.setPath(doc, 'a.b', 3);
doc.a.b.should.equal(3);
doc['a.b'].should.not.equal(3);

// Nested dot paths:
path.setPath(doc, 'a\\.a', 1);
doc['a.a'].should.equal(1);
doc.a.a.should.not.equal(1);

path.setPath(doc, 'a\\.b.c\\.d', 4);
doc['a.b']['c.d'].should.equal(4);

path.setPath(doc, 'a\\.b.c', 5);
doc['a.b'].c.should.equal(5);

path.setPath(doc, 'a\\.b.c\\.f', 6);
doc['a.b']['c.f'].should.equal(6);

path.setPath(doc, 'a.b\\.c\\.d', 32);
doc.a['b.c.d'].should.equal(32);

done();
});
});
});

0 comments on commit 6924ef0

Please sign in to comment.