Skip to content

Commit

Permalink
Add support for paths with escaped dots in path.
Browse files Browse the repository at this point in the history
There are valid JSON keys which can contain a nested '.' in them.
However, this module would not handle those properly, except in a
limited set of conditions. This commit adds support for these paths when
they are properly escaped using a '\' character. However, this does
slightly change the base functionality for the module where a value with
the same key path as a top-level key with the nested value's key path
will now read the nested value, unless the provided path has appropriate
escaping on the key path.

For example... with the example doc:
    { 'a.b' : 2, a : { b : 3 } }

evaluatePath(doc, 'a.b') will now return 3
    and
evaluatePath(doc, 'a\\.b') will now return 2

Related to mrodrig/json-2-csv#184
  • Loading branch information
mrodrig committed May 24, 2021
1 parent 4e0da2b commit 772f2ff
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
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.

20 changes: 17 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 All @@ -51,6 +57,10 @@ function setPath(obj, kp, v) {
return _sp(obj, kp, v);
}

function isObject(v) {
return typeof v === 'object' && v !== null;
}

/**
* Helper function that will set the value in the provided object/array.
* @param obj {Object|Array} object to set value in
Expand All @@ -75,14 +85,17 @@ function _sp(obj, kp, v) {
} else if (!obj[key]) {
// If the current key doesn't exist yet, populate it
obj[key] = {};
} else if (!isObject(obj[key])) {
obj[kp] = v;
return obj;
}
_sp(obj[key], remaining, v);
} else if (Array.isArray(obj)) {
// If this "obj" is actually an array, then we can loop over each of the values and set the path
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 +108,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)
};
}
71 changes: 70 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
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 772f2ff

Please sign in to comment.