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

View File

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

View File

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

View File

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

View File

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