Skip to content

Commit 2cb41bb

Browse files
author
kzimin
committed
Implement useArrayIndexBraces option with tests
1 parent a6fc997 commit 2cb41bb

File tree

6 files changed

+71
-22
lines changed

6 files changed

+71
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ATTIC
22
.c9/
33
.idea
4+
yarn.lock
45
node_modules
56
*.log
67
*.swp

dist/dot-object.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,25 @@
5555

5656
var hasOwnProperty = Object.prototype.hasOwnProperty
5757

58-
function DotObject(separator, override, useArray) {
58+
function DotObject(separator, override, useArray, useArrayIndexBraces) {
5959
if (!(this instanceof DotObject)) {
60-
return new DotObject(separator, override, useArray)
60+
return new DotObject(separator, override, useArray, useArrayIndexBraces)
6161
}
6262

6363
if (typeof override === 'undefined') override = false
6464
if (typeof useArray === 'undefined') useArray = true
65+
if (typeof useArrayIndexBraces === 'undefined') useArrayIndexBraces = false
6566
this.separator = separator || '.'
6667
this.override = override
6768
this.useArray = useArray
69+
this.useArrayIndexBraces = useArrayIndexBraces
6870
this.keepArray = false
6971

7072
// contains touched arrays
7173
this.cleanup = []
7274
}
7375

74-
var dotDefault = new DotObject('.', false, true)
76+
var dotDefault = new DotObject('.', false, true, false)
7577

7678
function wrap(method) {
7779
return function() {
@@ -491,9 +493,11 @@
491493
* @param {Object} tgt target object
492494
* @param {Array} path path array (internal)
493495
*/
494-
DotObject.prototype.dot = function(obj, tgt, path) {
496+
DotObject.prototype.dot = function(obj, tgt, path, objIsArray) {
495497
tgt = tgt || {}
496498
path = path || []
499+
objIsArray = objIsArray || false
500+
497501
Object.keys(obj).forEach(function(key) {
498502
if (
499503
(
@@ -504,9 +508,15 @@
504508
)
505509
)
506510
) {
507-
return this.dot(obj[key], tgt, path.concat(key))
511+
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
508512
} else {
509-
tgt[path.concat(key).join(this.separator)] = obj[key]
513+
if (
514+
objIsArray && this.useArrayIndexBraces
515+
) {
516+
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
517+
} else {
518+
tgt[path.concat(key).join(this.separator)] = obj[key]
519+
}
510520
}
511521
}.bind(this))
512522
return tgt
@@ -536,7 +546,7 @@
536546
})
537547

538548
;
539-
['useArray', 'keepArray'].forEach(function(prop) {
549+
['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function(prop) {
540550
Object.defineProperty(DotObject, prop, {
541551
get: function() {
542552
return dotDefault[prop]

dist/dot-object.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,25 @@ function parsePath (path, sep) {
5454

5555
var hasOwnProperty = Object.prototype.hasOwnProperty
5656

57-
function DotObject (separator, override, useArray) {
57+
function DotObject (separator, override, useArray, useArrayIndexBraces) {
5858
if (!(this instanceof DotObject)) {
59-
return new DotObject(separator, override, useArray)
59+
return new DotObject(separator, override, useArray, useArrayIndexBraces)
6060
}
6161

6262
if (typeof override === 'undefined') override = false
6363
if (typeof useArray === 'undefined') useArray = true
64+
if (typeof useArrayIndexBraces === 'undefined') useArrayIndexBraces = false
6465
this.separator = separator || '.'
6566
this.override = override
6667
this.useArray = useArray
68+
this.useArrayIndexBraces = useArrayIndexBraces
6769
this.keepArray = false
6870

6971
// contains touched arrays
7072
this.cleanup = []
7173
}
7274

73-
var dotDefault = new DotObject('.', false, true)
75+
var dotDefault = new DotObject('.', false, true, false)
7476
function wrap (method) {
7577
return function () {
7678
return dotDefault[method].apply(dotDefault, arguments)
@@ -485,9 +487,11 @@ DotObject.prototype.transform = function (recipe, obj, tgt) {
485487
* @param {Object} tgt target object
486488
* @param {Array} path path array (internal)
487489
*/
488-
DotObject.prototype.dot = function (obj, tgt, path) {
490+
DotObject.prototype.dot = function (obj, tgt, path, objIsArray) {
489491
tgt = tgt || {}
490492
path = path || []
493+
objIsArray = objIsArray || false
494+
491495
Object.keys(obj).forEach(function (key) {
492496
if (
493497
(
@@ -498,9 +502,15 @@ DotObject.prototype.dot = function (obj, tgt, path) {
498502
)
499503
)
500504
) {
501-
return this.dot(obj[key], tgt, path.concat(key))
505+
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
502506
} else {
503-
tgt[path.concat(key).join(this.separator)] = obj[key]
507+
if (
508+
objIsArray && this.useArrayIndexBraces
509+
) {
510+
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
511+
} else {
512+
tgt[path.concat(key).join(this.separator)] = obj[key]
513+
}
504514
}
505515
}.bind(this))
506516
return tgt
@@ -528,7 +538,7 @@ DotObject.dot = wrap('dot')
528538
})
529539
})
530540

531-
;['useArray', 'keepArray'].forEach(function (prop) {
541+
;['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function (prop) {
532542
Object.defineProperty(DotObject, prop, {
533543
get: function () {
534544
return dotDefault[prop]

src/dot-object.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,25 @@ function parsePath (path, sep) {
5454

5555
var hasOwnProperty = Object.prototype.hasOwnProperty
5656

57-
function DotObject (separator, override, useArray) {
57+
function DotObject (separator, override, useArray, useArrayIndexBraces) {
5858
if (!(this instanceof DotObject)) {
59-
return new DotObject(separator, override, useArray)
59+
return new DotObject(separator, override, useArray, useArrayIndexBraces)
6060
}
6161

6262
if (typeof override === 'undefined') override = false
6363
if (typeof useArray === 'undefined') useArray = true
64+
if (typeof useArrayIndexBraces === 'undefined') useArrayIndexBraces = false
6465
this.separator = separator || '.'
6566
this.override = override
6667
this.useArray = useArray
68+
this.useArrayIndexBraces = useArrayIndexBraces
6769
this.keepArray = false
6870

6971
// contains touched arrays
7072
this.cleanup = []
7173
}
7274

73-
var dotDefault = new DotObject('.', false, true)
75+
var dotDefault = new DotObject('.', false, true, false)
7476
function wrap (method) {
7577
return function () {
7678
return dotDefault[method].apply(dotDefault, arguments)
@@ -485,9 +487,11 @@ DotObject.prototype.transform = function (recipe, obj, tgt) {
485487
* @param {Object} tgt target object
486488
* @param {Array} path path array (internal)
487489
*/
488-
DotObject.prototype.dot = function (obj, tgt, path) {
490+
DotObject.prototype.dot = function (obj, tgt, path, objIsArray) {
489491
tgt = tgt || {}
490492
path = path || []
493+
objIsArray = objIsArray || false
494+
491495
Object.keys(obj).forEach(function (key) {
492496
if (
493497
(
@@ -498,9 +502,15 @@ DotObject.prototype.dot = function (obj, tgt, path) {
498502
)
499503
)
500504
) {
501-
return this.dot(obj[key], tgt, path.concat(key))
505+
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
502506
} else {
503-
tgt[path.concat(key).join(this.separator)] = obj[key]
507+
if (
508+
objIsArray && this.useArrayIndexBraces
509+
) {
510+
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
511+
} else {
512+
tgt[path.concat(key).join(this.separator)] = obj[key]
513+
}
504514
}
505515
}.bind(this))
506516
return tgt
@@ -528,7 +538,7 @@ DotObject.dot = wrap('dot')
528538
})
529539
})
530540

531-
;['useArray', 'keepArray'].forEach(function (prop) {
541+
;['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function (prop) {
532542
Object.defineProperty(DotObject, prop, {
533543
get: function () {
534544
return dotDefault[prop]

test/dot.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ describe('dot():', function () {
6565
Dot.keepArray = false
6666
})
6767

68+
it('useArrayIndexBraces wrap indexes with braces', function () {
69+
var expected = {
70+
id: 'my-id',
71+
'nes.ted.value': true,
72+
'other.nested.stuff': 5,
73+
'some.array[0]': 'A',
74+
'some.array[1]': 'B',
75+
ehrm: 123,
76+
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)')
77+
}
78+
79+
Dot.useArrayIndexBraces = true
80+
81+
Dot.dot(obj).should.eql(expected)
82+
83+
Dot.useArrayIndexBraces = false
84+
})
85+
6886
it('Always keeps empty arrays', function () {
6987
Dot.dot({ hello: [] }).should.eql({ hello: [] })
7088
Dot.dot({ hello: { world: [] } }).should.eql({ 'hello.world': [] })

0 commit comments

Comments
 (0)