Skip to content

Commit

Permalink
Merge 9474df3 into 040773c
Browse files Browse the repository at this point in the history
  • Loading branch information
jesspoemape committed Sep 4, 2020
2 parents 040773c + 9474df3 commit 3ca8e98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 7 additions & 13 deletions src/renameAll.js
Expand Up @@ -2,22 +2,16 @@ const curry = require('ramda/src/curry')

// renameAll :: { k: v } -> { k: v } -> { k: v }
const renameAll = (renames, obj) => {
obj = Object.assign({}, obj)

for (let frum in renames) {
if (!(frum in obj)) continue

const to = renames[frum]

if (typeof to === 'object') {
obj[frum] = renameAll(to, obj[frum])
const newObj = {}
for (let prevKey in obj) {
const nextKey = renames[prevKey] || prevKey
if (typeof nextKey === 'object') {
newObj[prevKey] = renameAll(renames[prevKey], obj[prevKey])
} else {
obj[to] = obj[frum]
delete obj[frum]
newObj[nextKey] = obj[prevKey]
}
}

return obj
return newObj
}

module.exports = curry(renameAll)
12 changes: 9 additions & 3 deletions test/renameAll.js
Expand Up @@ -8,23 +8,29 @@ describe('renameAll', () => {
name: 'bird',
sounds: {
call: 'chirp'
}
},
title: 'My title',
latestTitle: 'haha (business)',
}

const renames = {
color: 'appearance',
count: 'number',
sounds: {
call: 'say'
}
},
latestTitle: 'title',
title: 'updatedTitle',
}

const expected = {
appearance: 'red',
name: 'bird',
sounds: {
say: 'chirp'
}
},
title: 'haha (business)',
updatedTitle: 'My title',
}

it('renames multiple nested properties on an object using a name-map', () =>
Expand Down

0 comments on commit 3ca8e98

Please sign in to comment.