Make `getChildPaths` json pointer compliant

This commit is contained in:
jos 2019-06-19 12:46:50 +02:00
parent d712e642cc
commit 797541cd6b
5 changed files with 23 additions and 22 deletions

View File

@ -3701,11 +3701,15 @@ Node.prototype._onChangeType = function (newType) {
/**
* Sort the child's of the node. Only applicable when the node has type 'object'
* or 'array'.
* @param {String[]} path Path of the child value to be compared
* @param {String} direction Sorting direction. Available values: "asc", "desc"
* @param {String[] | string} path Path of the child value to be compared
* @param {String} direction Sorting direction. Available values: "asc", "desc"
* @private
*/
Node.prototype.sort = function (path, direction) {
if (typeof path === 'string') {
path = util.parsePath(path);
}
if (!this._hasChilds()) {
return;
}
@ -4427,7 +4431,7 @@ Node.prototype.showSortModal = function () {
function onSort (sortedBy) {
var path = sortedBy.path;
var pathArray = (path === '.') ? [] : path.split('.').slice(1);
var pathArray = util.parsePath(path);
node.sortedBy = sortedBy
node.sort(pathArray, sortedBy.direction)

View File

@ -18,7 +18,7 @@ var util = require('./util');
function showSortModal (container, json, onSort, options) {
var paths = Array.isArray(json)
? util.getChildPaths(json)
: ['.'];
: [''];
var selectedPath = options && options.path && util.contains(paths, options.path)
? options.path
: paths[0]
@ -79,7 +79,7 @@ function showSortModal (container, json, onSort, options) {
paths.forEach(function (path) {
var option = document.createElement('option');
option.text = path;
option.text = path || '.';
option.value = path;
field.appendChild(option);
});

View File

@ -137,7 +137,7 @@ function showTransformModal (container, json, onTransform) {
});
var selectablePaths = util.getChildPaths(json, true).filter(function(path) {
return path !== '.';
return path !== '';
});
if (selectablePaths.length > 0) {
selectablePaths.forEach(function (path) {
@ -179,14 +179,11 @@ function showTransformModal (container, json, onTransform) {
query.value = Array.isArray(value) ? '[*]' : '@';
function preprocessPath(path) {
if (path[0] === '.') {
return (path === '.')
? '@'
: path.slice(1);
}
else {
return path;
}
return (path === '')
? '@'
: (path[0] === '.')
? path.slice(1)
: path;
}
function generateQueryFromWizard () {
@ -198,7 +195,7 @@ function showTransformModal (container, json, onTransform) {
var exampleValue = util.get(value, examplePath)
var value1 = typeof exampleValue === 'string'
? filterValue.value
: parseString(filterValue.value);
: util.parseString(filterValue.value);
query.value = '[? ' +
field1 + ' ' +

View File

@ -1202,7 +1202,7 @@ exports.getChildPaths = function (json, includeObjects) {
var isValue = !Array.isArray(json) && !exports.isObject(json)
if (isValue || includeObjects) {
pathsMap[rootPath || '.'] = true;
pathsMap[rootPath || ''] = true;
}
if (exports.isObject(json)) {
@ -1218,7 +1218,7 @@ exports.getChildPaths = function (json, includeObjects) {
});
}
else {
pathsMap['.'] = true;
pathsMap[''] = true;
}
return Object.keys(pathsMap).sort();

View File

@ -274,7 +274,7 @@ describe('util', function () {
];
assert.deepStrictEqual(util.getChildPaths(json, true), [
'.',
'',
'.location',
'.location.latitude',
'.location.longitude',
@ -287,14 +287,14 @@ describe('util', function () {
var json = [ 1, 2, 3 ];
assert.deepStrictEqual(util.getChildPaths(json), [
'.'
''
])
});
it('should extract all child paths of a non-array', function () {
assert.deepStrictEqual(util.getChildPaths({a: 2, b: {c: 3}}), ['.'])
assert.deepStrictEqual(util.getChildPaths('foo'), ['.'])
assert.deepStrictEqual(util.getChildPaths(123), ['.'])
assert.deepStrictEqual(util.getChildPaths({a: 2, b: {c: 3}}), [''])
assert.deepStrictEqual(util.getChildPaths('foo'), [''])
assert.deepStrictEqual(util.getChildPaths(123), [''])
});
})