Skip to content

Commit

Permalink
rename useArrayIndexBraces to useBrackets and fix nested arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalff committed Nov 2, 2019
1 parent e1cab3d commit 9554473
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
27 changes: 15 additions & 12 deletions src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ function parsePath (path, sep) {

var hasOwnProperty = Object.prototype.hasOwnProperty

function DotObject (separator, override, useArray, useArrayIndexBraces) {
function DotObject (separator, override, useArray, useBrackets) {
if (!(this instanceof DotObject)) {
return new DotObject(separator, override, useArray, useArrayIndexBraces)
return new DotObject(separator, override, useArray, useBrackets)
}

if (typeof override === 'undefined') override = false
if (typeof useArray === 'undefined') useArray = true
if (typeof useArrayIndexBraces === 'undefined') useArrayIndexBraces = false
if (typeof useBrackets === 'undefined') useBrackets = false
this.separator = separator || '.'
this.override = override
this.useArray = useArray
this.useArrayIndexBraces = useArrayIndexBraces
this.useBrackets = useBrackets
this.keepArray = false

// contains touched arrays
Expand Down Expand Up @@ -487,12 +487,13 @@ DotObject.prototype.transform = function (recipe, obj, tgt) {
* @param {Object} tgt target object
* @param {Array} path path array (internal)
*/
DotObject.prototype.dot = function (obj, tgt, path, objIsArray) {
DotObject.prototype.dot = function (obj, tgt, path) {
tgt = tgt || {}
path = path || []
objIsArray = objIsArray || false
var isArray = Array.isArray(obj)

Object.keys(obj).forEach(function (key) {
var index = isArray && this.useBrackets ? '[' + key + ']' : key
if (
(
isArrayOrObject(obj[key]) &&
Expand All @@ -502,14 +503,16 @@ DotObject.prototype.dot = function (obj, tgt, path, objIsArray) {
)
)
) {
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
if (isArray && this.useBrackets) {
return this.dot(obj[key], tgt, path.slice(0, -1).concat(path[path.length - 1] + index))
} else {
return this.dot(obj[key], tgt, path.concat(index))
}
} else {
if (
objIsArray && this.useArrayIndexBraces
) {
if (isArray && this.useBrackets) {
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
tgt[path.concat(index).join(this.separator)] = obj[key]
}
}
}.bind(this))
Expand Down Expand Up @@ -538,7 +541,7 @@ DotObject.dot = wrap('dot')
})
})

;['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function (prop) {
;['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) {
Object.defineProperty(DotObject, prop, {
get: function () {
return dotDefault[prop]
Expand Down
25 changes: 22 additions & 3 deletions test/dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('dot():', function () {
stuff: 5
}
},
nested: {
array: [
{
with: 'object1'
},
{
and: 'object2'
}
]
},
some: {
array: ['A', 'B']
},
Expand All @@ -35,6 +45,8 @@ describe('dot():', function () {
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,
Expand All @@ -53,6 +65,11 @@ describe('dot():', function () {
id: 'my-id',
'nes.ted.value': true,
'other.nested.stuff': 5,
'nested.array': [{
with: 'object1'
}, {
and: 'object2'
}],
'some.array': ['A', 'B'],
ehrm: 123,
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)')
Expand All @@ -65,22 +82,24 @@ describe('dot():', function () {
Dot.keepArray = false
})

it('useArrayIndexBraces wrap indexes with braces', function () {
it('useBrackets wrap indexes with 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)')
}

Dot.useArrayIndexBraces = true
Dot.useBrackets = true

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

Dot.useArrayIndexBraces = false
Dot.useBrackets = false
})

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

0 comments on commit 9554473

Please sign in to comment.