Skip to content

Commit

Permalink
Implement useArrayIndexBraces option with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
z1m1n committed Oct 31, 2019
1 parent a6fc997 commit 2cb41bb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ATTIC
.c9/
.idea
yarn.lock
node_modules
*.log
*.swp
24 changes: 17 additions & 7 deletions dist/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,25 @@

var hasOwnProperty = Object.prototype.hasOwnProperty

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

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

// contains touched arrays
this.cleanup = []
}

var dotDefault = new DotObject('.', false, true)
var dotDefault = new DotObject('.', false, true, false)

function wrap(method) {
return function() {
Expand Down Expand Up @@ -491,9 +493,11 @@
* @param {Object} tgt target object
* @param {Array} path path array (internal)
*/
DotObject.prototype.dot = function(obj, tgt, path) {
DotObject.prototype.dot = function(obj, tgt, path, objIsArray) {
tgt = tgt || {}
path = path || []
objIsArray = objIsArray || false

Object.keys(obj).forEach(function(key) {
if (
(
Expand All @@ -504,9 +508,15 @@
)
)
) {
return this.dot(obj[key], tgt, path.concat(key))
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
if (
objIsArray && this.useArrayIndexBraces
) {
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
}
}
}.bind(this))
return tgt
Expand Down Expand Up @@ -536,7 +546,7 @@
})

;
['useArray', 'keepArray'].forEach(function(prop) {
['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function(prop) {
Object.defineProperty(DotObject, prop, {
get: function() {
return dotDefault[prop]
Expand Down
2 changes: 1 addition & 1 deletion dist/dot-object.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,25 @@ function parsePath (path, sep) {

var hasOwnProperty = Object.prototype.hasOwnProperty

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

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

// contains touched arrays
this.cleanup = []
}

var dotDefault = new DotObject('.', false, true)
var dotDefault = new DotObject('.', false, true, false)
function wrap (method) {
return function () {
return dotDefault[method].apply(dotDefault, arguments)
Expand Down Expand Up @@ -485,9 +487,11 @@ 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) {
DotObject.prototype.dot = function (obj, tgt, path, objIsArray) {
tgt = tgt || {}
path = path || []
objIsArray = objIsArray || false

Object.keys(obj).forEach(function (key) {
if (
(
Expand All @@ -498,9 +502,15 @@ DotObject.prototype.dot = function (obj, tgt, path) {
)
)
) {
return this.dot(obj[key], tgt, path.concat(key))
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
if (
objIsArray && this.useArrayIndexBraces
) {
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
}
}
}.bind(this))
return tgt
Expand Down Expand Up @@ -528,7 +538,7 @@ DotObject.dot = wrap('dot')
})
})

;['useArray', 'keepArray'].forEach(function (prop) {
;['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function (prop) {
Object.defineProperty(DotObject, prop, {
get: function () {
return dotDefault[prop]
Expand Down
24 changes: 17 additions & 7 deletions src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,25 @@ function parsePath (path, sep) {

var hasOwnProperty = Object.prototype.hasOwnProperty

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

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

// contains touched arrays
this.cleanup = []
}

var dotDefault = new DotObject('.', false, true)
var dotDefault = new DotObject('.', false, true, false)
function wrap (method) {
return function () {
return dotDefault[method].apply(dotDefault, arguments)
Expand Down Expand Up @@ -485,9 +487,11 @@ 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) {
DotObject.prototype.dot = function (obj, tgt, path, objIsArray) {
tgt = tgt || {}
path = path || []
objIsArray = objIsArray || false

Object.keys(obj).forEach(function (key) {
if (
(
Expand All @@ -498,9 +502,15 @@ DotObject.prototype.dot = function (obj, tgt, path) {
)
)
) {
return this.dot(obj[key], tgt, path.concat(key))
return this.dot(obj[key], tgt, path.concat(key), Array.isArray(obj[key]))
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
if (
objIsArray && this.useArrayIndexBraces
) {
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key]
} else {
tgt[path.concat(key).join(this.separator)] = obj[key]
}
}
}.bind(this))
return tgt
Expand Down Expand Up @@ -528,7 +538,7 @@ DotObject.dot = wrap('dot')
})
})

;['useArray', 'keepArray'].forEach(function (prop) {
;['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function (prop) {
Object.defineProperty(DotObject, prop, {
get: function () {
return dotDefault[prop]
Expand Down
18 changes: 18 additions & 0 deletions test/dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ describe('dot():', function () {
Dot.keepArray = false
})

it('useArrayIndexBraces wrap indexes with braces', function () {
var expected = {
id: 'my-id',
'nes.ted.value': true,
'other.nested.stuff': 5,
'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.dot(obj).should.eql(expected)

Dot.useArrayIndexBraces = false
})

it('Always keeps empty arrays', function () {
Dot.dot({ hello: [] }).should.eql({ hello: [] })
Dot.dot({ hello: { world: [] } }).should.eql({ 'hello.world': [] })
Expand Down

0 comments on commit 2cb41bb

Please sign in to comment.