Skip to content

Commit

Permalink
refactor: simplify some cases of common.formatQueryValue (#1598)
Browse files Browse the repository at this point in the history
* refactor(common): replace lodash.forOwn with Array.reduce

This commit replaces usage of lodash.forOwn with a combination of Object.keys() & Array.reduce.

Related #1285

* refactor(commong): use Array.map

This commit simplifies array formatting by using Array.map instead of a plain for loop.

* test(common): add unit test coverage for #formatQueryValue

This commit adds unit test coverage for #formatQueryValue.

Related #1404

* chore: run prettier to fix lint errors

* fix: use Object.entries() in favor of Object.keys()

This commit replaces the usage of Object.keys() with Object.entries() to address [this](#1598 (comment)) comment.

* test(common): break down tests

This commit addresses the following comments:
1- #1598 (comment)
2- #1598 (comment)

* chore: run prettier to fix lint errors

* refactor(common): replace lodash.forOwn with Array.reduce

This commit replaces usage of lodash.forOwn with a combination of Object.keys() & Array.reduce.

Related #1285

* refactor(commong): use Array.map

This commit simplifies array formatting by using Array.map instead of a plain for loop.

* test(common): add unit test coverage for #formatQueryValue

This commit adds unit test coverage for #formatQueryValue.

Related #1404

* chore: run prettier to fix lint errors

* fix: use Object.entries() in favor of Object.keys()

This commit replaces the usage of Object.keys() with Object.entries() to address [this](#1598 (comment)) comment.

* test(common): break down tests

This commit addresses the following comments:
1- #1598 (comment)
2- #1598 (comment)

* chore: run prettier to fix lint errors

* chore: remove unnecessary formatting changes
  • Loading branch information
laumair authored and gr2m committed Jul 12, 2019
1 parent b9431a7 commit 1e0acad
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/common.js
Expand Up @@ -453,20 +453,18 @@ function formatQueryValue(key, value, stringFormattingFn) {
case value instanceof RegExp:
break
case _.isArray(value): {
const tmpArray = new Array(value.length)
for (let i = 0; i < value.length; ++i) {
tmpArray[i] = formatQueryValue(i, value[i], stringFormattingFn)[1]
}
value = tmpArray
value = value.map(function(val, idx) {
return formatQueryValue(idx, val, stringFormattingFn)[1]
})
break
}
case _.isObject(value): {
const tmpObj = {}
_.forOwn(value, function(subVal, subKey) {
value = Object.entries(value).reduce(function(acc, [subKey, subVal]) {
const subPair = formatQueryValue(subKey, subVal, stringFormattingFn)
tmpObj[subPair[0]] = subPair[1]
})
value = tmpObj
acc[subPair[0]] = subPair[1]

return acc
}, {})
break
}
}
Expand Down

0 comments on commit 1e0acad

Please sign in to comment.