Skip to content

Commit

Permalink
prepare version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalff committed Nov 2, 2019
1 parent 1647da9 commit 0b87c85
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# ChangeLog

## 2019-11-02 Version 1.10.0
## 2019-11-02 Version 2.0.0
* [[`2cb41bbd1b`](https://github.com/rhalff/dot-object/commit/2cb41bbd1b)] - Add useBrackets option for the .dot() method (by z1m1n #42)
* dot() now writes brackets by default (possibly breaking change).
Use Dot.useBrackets = false to keep the old behavior

## 2019-07-29 Version 1.9.0
* allows allow to process root level property using dot.object
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ Result:
"id": "my-id",
"nes.ted.value": true,
"other.nested.stuff": 5,
"some.array.0": "A",
"some.array.1": "B"
"some.array[0]": "A",
"some.array[1]": "B"
}
```

Expand Down
29 changes: 16 additions & 13 deletions dist/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@

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 = true
this.separator = separator || '.'
this.override = override
this.useArray = useArray
this.useArrayIndexBraces = useArrayIndexBraces
this.useBrackets = useBrackets
this.keepArray = false

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

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

function wrap(method) {
return function() {
Expand Down Expand Up @@ -493,12 +493,13 @@
* @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 @@ -508,14 +509,16 @@
)
)
) {
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 @@ -546,7 +549,7 @@
})

;
['useArray', 'keepArray', 'useArrayIndexBraces'].forEach(function(prop) {
['useArray', 'keepArray', 'useBrackets'].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.

29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ 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 = true
this.separator = separator || '.'
this.override = override
this.useArray = useArray
this.useArrayIndexBraces = useArrayIndexBraces
this.useBrackets = useBrackets
this.keepArray = false

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

var dotDefault = new DotObject('.', false, true, false)
var dotDefault = new DotObject('.', false, true, true)
function wrap (method) {
return function () {
return dotDefault[method].apply(dotDefault, arguments)
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

0 comments on commit 0b87c85

Please sign in to comment.