Skip to content

Commit

Permalink
Fix parsing of array paths for non standard separators
Browse files Browse the repository at this point in the history
  • Loading branch information
boidolr committed Aug 1, 2020
1 parent 3dcb2de commit 94b9eb8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var blacklistFilter = function (part) { return blacklist.indexOf(part) === -1 }

function parsePath (path, sep) {
if (path.indexOf('[') >= 0) {
path = path.replace(/\[/g, '.').replace(/]/g, '')
path = path.replace(/\[/g, sep).replace(/]/g, '')
}

var parts = path.split(sep)
Expand Down
2 changes: 1 addition & 1 deletion src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var blacklistFilter = function (part) { return blacklist.indexOf(part) === -1 }

function parsePath (path, sep) {
if (path.indexOf('[') >= 0) {
path = path.replace(/\[/g, '.').replace(/]/g, '')
path = path.replace(/\[/g, sep).replace(/]/g, '')
}

var parts = path.split(sep)
Expand Down
22 changes: 22 additions & 0 deletions test/dot-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ describe('Object test:', function () {
})
})

it('Should expand dotted keys with array notation with different separator', function () {
var row = {
id: 2,
my_arr_0: 'one',
my_arr_1: 'two',
my_arr_2: 'three',
'my_arr2[0]': 'one',
'my_arr2[1]': 'two',
'my_arr2[2]': 'three'
}

new Dot('_').object(row)

row.should.eql({
id: 2,
my: {
arr: ['one', 'two', 'three'],
arr2: ['one', 'two', 'three']
}
})
})

it('Should allow keys with numbers', function () {
var row = {
id: 2,
Expand Down

0 comments on commit 94b9eb8

Please sign in to comment.