Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalff committed Nov 2, 2019
2 parents ea358be + a292262 commit b18aaac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ DotObject.prototype.dot = function (obj, tgt, path) {
)
) {
if (isArray && this.useBrackets) {
return this.dot(obj[key], tgt, path.slice(0, -1).concat(path[path.length - 1] + index))
var previousKey = path[path.length - 1] || ''
return this.dot(obj[key], tgt, path.slice(0, -1).concat(previousKey + index))
} else {
return this.dot(obj[key], tgt, path.concat(index))
}
Expand Down
53 changes: 49 additions & 4 deletions test/dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var pkg = require('./fixtures/package.json')
describe('dot():', function () {
var obj

// Dot.useBrackets = false;

beforeEach(function () {
obj = {
id: 'my-id',
Expand Down Expand Up @@ -36,7 +38,25 @@ describe('dot():', function () {
ehrm: 123,
dates: {
first: new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)')
}
},
arrays: [
[
[
{
all: [
[
{
the: [
'way',
['down']
]
}
]
]
}
]
]
]
}
})

Expand All @@ -50,7 +70,9 @@ describe('dot():', function () {
'some.array[0]': 'A',
'some.array[1]': 'B',
ehrm: 123,
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)')
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)'),
'arrays[0][0][0].all[0][0].the[0]': 'way',
'arrays[0][0][0].all[0][0].the[1][0]': 'down'
}

Dot.dot(obj).should.eql(expected)
Expand All @@ -72,7 +94,8 @@ describe('dot():', function () {
}],
'some.array': ['A', 'B'],
ehrm: 123,
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)')
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)'),
arrays: JSON.parse(JSON.stringify(obj.arrays))
}

Dot.keepArray = true
Expand All @@ -92,10 +115,32 @@ describe('dot():', function () {
'some.array[0]': 'A',
'some.array[1]': 'B',
ehrm: 123,
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)')
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)'),
'arrays[0][0][0].all[0][0].the[0]': 'way',
'arrays[0][0][0].all[0][0].the[1][0]': 'down'
}

Dot.dot(obj).should.eql(expected)
})

it('useBrackets wrap indexes without brackets', function () {
var expected = {
id: 'my-id',
'nes.ted.value': true,
'other.nested.stuff': 5,
'nested.array.0.with': 'object1',
'nested.array.1.and': 'object2',
'some.array.0': 'A',
'some.array.1': 'B',
ehrm: 123,
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)'),
'arrays.0.0.0.all.0.0.the.0': 'way',
'arrays.0.0.0.all.0.0.the.1.0': 'down'
}

Dot.useBrackets = false
Dot.dot(obj).should.eql(expected)
Dot.useBrackets = true
})

it('Always keeps empty arrays', function () {
Expand Down

0 comments on commit b18aaac

Please sign in to comment.