Skip to content

Commit

Permalink
feat elastic#41: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alemagio committed Oct 22, 2019
1 parent f430ad3 commit 8bd2e5d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
21 changes: 6 additions & 15 deletions lib/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,12 @@ function truncateCustomKeys (value, max, keywords) {
let result = value
const keys = Object.keys(result)
const truncatedKeys = keys.map(k => keywords.includes(k) ? k : truncate(k, max))
const noDuplicates = incrementalDuplicates(truncatedKeys)

keys.forEach((k, index) => {
const {[k]: kValue, ...rest} = result
result = {[noDuplicates[index]]: truncateCustomKeys(kValue, max, keywords), ...rest}
})
return result
}

function incrementalDuplicates (arr) {
let result = arr
result.forEach((el, index) => {
if (result.indexOf(el) !== index) {
result.splice(index, 1, el.slice(0, -1) + index)
}
})
for (const [index, k] of keys.entries()) {
const value = result[k]
delete result[k]
const newKey = truncatedKeys[index]
result[newKey] = truncateCustomKeys(value, max, keywords)
}
return result
}
2 changes: 0 additions & 2 deletions test/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ options.forEach(function (opts) {
context: {
custom: {
[genStr('a', customKeyLen)]: 'truncate my key',
[genStr('a', customKeyLen - 1) + 1]: 'truncate my key too',
},
db: {
statement: 'SELECT * FROM USERS',
Expand Down Expand Up @@ -205,7 +204,6 @@ options.forEach(function (opts) {
context: {
custom: {
[genStr('a', veryLong)]: 'truncate my key',
[genStr('a', veryLong - 10)]: 'truncate my key too',
},
db: {
statement: 'SELECT * FROM USERS',
Expand Down

0 comments on commit 8bd2e5d

Please sign in to comment.