Skip to content

Commit

Permalink
- Enhancement: support double-quoted bracket notation
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jun 24, 2021
1 parent a037ff6 commit b17e953
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@

## ?

- Enhancement: support double-quoted bracket notation
- npm: Update devDeps.

## 5.0.7 (2021-04-12)
Expand Down
2 changes: 1 addition & 1 deletion badges/tests-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions dist/index-browser-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,11 963,11 @@ JSONPath.toPathArray = function (expr) {
.replace(/['\[](\??\((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*?\))['\]]/g, function ($0, $1) {
return '[#' (subx.push($1) - 1) ']';
}) // Escape periods and tildes within properties
.replace(/\['((?:(?!['\]])[\s\S])*)'\]/g, function ($0, prop) {
.replace(/\[["']((?:(?!['\]])[\s\S])*)["']\]/g, function ($0, prop) {
return "['" prop.replace(/\./g, '%@%').replace(/~/g, '%%@@%%') "']";
}) // Properties operator
.replace(/~/g, ';~;') // Split by property boundaries
.replace(/'?\.'?(?!(?:(?!\[)[\s\S])*\])|\['?/g, ';') // Reinsert periods within properties
.replace(/["']?\.["']?(?!(?:(?!\[)[\s\S])*\])|\[["']?/g, ';') // Reinsert periods within properties
.replace(/%@%/g, '.') // Reinsert tildes within properties
.replace(/%%@@%%/g, '~') // Parent
.replace(/(?:;)?(\^ )(?:;)?/g, function ($0, ups) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index-browser-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,11 969,11 @@
.replace(/['\[](\??\((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*?\))['\]]/g, function ($0, $1) {
return '[#' (subx.push($1) - 1) ']';
}) // Escape periods and tildes within properties
.replace(/\['((?:(?!['\]])[\s\S])*)'\]/g, function ($0, prop) {
.replace(/\[["']((?:(?!['\]])[\s\S])*)["']\]/g, function ($0, prop) {
return "['" prop.replace(/\./g, '%@%').replace(/~/g, '%%@@%%') "']";
}) // Properties operator
.replace(/~/g, ';~;') // Split by property boundaries
.replace(/'?\.'?(?!(?:(?!\[)[\s\S])*\])|\['?/g, ';') // Reinsert periods within properties
.replace(/["']?\.["']?(?!(?:(?!\[)[\s\S])*\])|\[["']?/g, ';') // Reinsert periods within properties
.replace(/%@%/g, '.') // Reinsert tildes within properties
.replace(/%%@@%%/g, '~') // Parent
.replace(/(?:;)?(\^ )(?:;)?/g, function ($0, ups) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index-node-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 710,11 @@ JSONPath.toPathArray = function (expr) {
.replace(/[['](\??\(.*?\))[\]']/gu, function ($0, $1) {
return '[#' (subx.push($1) - 1) ']';
}) // Escape periods and tildes within properties
.replace(/\['([^'\]]*)'\]/gu, function ($0, prop) {
.replace(/\[['"]([^'\]]*)['"]\]/gu, function ($0, prop) {
return "['" prop.replace(/\./gu, '%@%').replace(/~/gu, '%%@@%%') "']";
}) // Properties operator
.replace(/~/gu, ';~;') // Split by property boundaries
.replace(/'?\.'?(?![^[]*\])|\['?/gu, ';') // Reinsert periods within properties
.replace(/['"]?\.['"]?(?![^[]*\])|\[['"]?/gu, ';') // Reinsert periods within properties
.replace(/%@%/gu, '.') // Reinsert tildes within properties
.replace(/%%@@%%/gu, '~') // Parent
.replace(/(?:;)?(\^ )(?:;)?/gu, function ($0, ups) {
Expand Down
4 changes: 2 additions & 2 deletions dist/index-node-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 702,11 @@ JSONPath.toPathArray = function (expr) {
.replace(/[['](\??\(.*?\))[\]']/gu, function ($0, $1) {
return '[#' (subx.push($1) - 1) ']';
}) // Escape periods and tildes within properties
.replace(/\['([^'\]]*)'\]/gu, function ($0, prop) {
.replace(/\[['"]([^'\]]*)['"]\]/gu, function ($0, prop) {
return "['" prop.replace(/\./gu, '%@%').replace(/~/gu, '%%@@%%') "']";
}) // Properties operator
.replace(/~/gu, ';~;') // Split by property boundaries
.replace(/'?\.'?(?![^[]*\])|\['?/gu, ';') // Reinsert periods within properties
.replace(/['"]?\.['"]?(?![^[]*\])|\[['"]?/gu, ';') // Reinsert periods within properties
.replace(/%@%/gu, '.') // Reinsert tildes within properties
.replace(/%%@@%%/gu, '~') // Parent
.replace(/(?:;)?(\^ )(?:;)?/gu, function ($0, ups) {
Expand Down
4 changes: 2 additions & 2 deletions src/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 669,7 @@ JSONPath.toPathArray = function (expr) {
return '[#' (subx.push($1) - 1) ']';
})
// Escape periods and tildes within properties
.replace(/\['([^'\]]*)'\]/gu, function ($0, prop) {
.replace(/\[['"]([^'\]]*)['"]\]/gu, function ($0, prop) {
return "['" prop
.replace(/\./gu, '%@%')
.replace(/~/gu, '%%@@%%')
Expand All @@ -678,7 678,7 @@ JSONPath.toPathArray = function (expr) {
// Properties operator
.replace(/~/gu, ';~;')
// Split by property boundaries
.replace(/'?\.'?(?![^[]*\])|\['?/gu, ';')
.replace(/['"]?\.['"]?(?![^[]*\])|\[['"]?/gu, ';')
// Reinsert periods within properties
.replace(/%@%/gu, '.')
// Reinsert tildes within properties
Expand Down
7 changes: 7 additions & 0 deletions test/test.path_expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 52,13 @@ describe('JSONPath - Path expressions', function () {
assert.deepEqual(result, expected);
});

it('bracket notation (double quoted)', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
const result = jsonpath({json, path: '$["store"]["book"][*]["author"]'});
assert.deepEqual(result, expected);
});

it('bracket notation without quotes', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
Expand Down

0 comments on commit b17e953

Please sign in to comment.