diff --git a/src/dot-object.js b/src/dot-object.js index c41633b..d7e9507 100644 --- a/src/dot-object.js +++ b/src/dot-object.js @@ -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 @@ -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]) && @@ -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)) @@ -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] diff --git a/test/dot.js b/test/dot.js index 0383ec8..11606d6 100644 --- a/test/dot.js +++ b/test/dot.js @@ -20,6 +20,16 @@ describe('dot():', function () { stuff: 5 } }, + nested: { + array: [ + { + with: 'object1' + }, + { + and: 'object2' + } + ] + }, some: { array: ['A', 'B'] }, @@ -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, @@ -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)') @@ -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 () {