Skip to content

Commit

Permalink
Add renamePath
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Jun 15, 2018
1 parent a020645 commit 35741de
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
23 changes: 20 additions & 3 deletions API.md
Expand Up @@ -27,6 +27,7 @@
| [`reject`](#reject) | `a -> Promise Error` |
| [`rename`](#rename) | `String -> String -> { k: v } -> { k: v }` |
| [`renameAll`](#renameall) | `{ k: v } -> { k: v } -> { k: v }` |
| [`renamePath`](#renamepath) | `[String] -> String -> { k: v } -> { k: v }` |
| [`resolve`](#resolve) | `a -> Promise a` |
| [`tapP`](#tapp) | `(a -> Promise b) -> a -> Promise a` |
| [`useWithP`](#usewithp) | `(a -> b -> Promise c) -> [(d -> Promise a), (e -> Promise b)] -> (d -> e -> Promise c)` |
Expand Down Expand Up @@ -440,9 +441,25 @@ renameAll :: { k: v } -> { k: v } -> { k: v }
Rename multiple properties on an object to have different keys.

```js
const orig = { first_name: 'Miles', last_name: 'Callisto' }
renameAll({ first_name: 'firstName', last_name: 'lastName' }, orig)
//=> { firstName: 'Miles', lastName: 'Callisto' }
const orig = { user: { first_name: 'Miles', last_name: 'Callisto' } }
renameAll({ user: { first_name: 'firstName' } }, orig)
//=> { user: { firstName: 'Miles', last_name: 'Callisto' } }
```

### renamePath

`@articulate/funky/lib/renamePath`

```haskell
renamePath :: [String] -> String -> { k: v } -> { k: v }
```

Rename a deeply nested path on an object to have a different key.

```js
const orig = { user: { first_name: 'Miles', last_name: 'Callisto' } }
renamePath(['user', 'first_name'], 'firstName', orig)
//=> { user: { firstName: 'Miles', last_name: 'Callisto' } }
```

### resolve
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -23,6 +23,7 @@ exports.promisify = require('./lib/promisify')
exports.reject = require('./lib/reject')
exports.rename = require('./lib/rename')
exports.renameAll = require('./lib/renameAll')
exports.renamePath = require('./lib/renamePath')
exports.resolve = require('./lib/resolve')
exports.tapP = require('./lib/tapP')
exports.useWithP = require('./lib/useWithP')
Expand Down
12 changes: 12 additions & 0 deletions src/renamePath.js
@@ -0,0 +1,12 @@
const assoc = require('ramda/src/assoc')
const curry = require('ramda/src/curry')

const rename = require('./rename')

// renamePath :: [String] -> String -> { k: v } -> { k: v }
const renamePath = ([ head, ...tail ], to, obj) =>
tail.length
? assoc(head, renamePath(tail, to, obj[head]), obj)
: rename(head, to, obj)

module.exports = curry(renamePath)
18 changes: 18 additions & 0 deletions test/renamePath.js
@@ -0,0 +1,18 @@
const { expect } = require('chai')

const { renamePath } = require('..')

describe('renamePath', () => {
const orig = { a: { b: 'b' }, c: 'c' }
const expected = { a: { B: 'b' }, c: 'c' }

it('renames a deeply nested path on an object', () =>
expect(renamePath(['a', 'b'], 'B', orig)).to.eql(expected)
)

it('is curried', () => {
expect(renamePath(['a', 'b'])('B', orig)).to.eql(expected)
expect(renamePath(['a', 'b'], 'B')(orig)).to.eql(expected)
expect(renamePath(['a', 'b'])('B')(orig)).to.eql(expected)
})
})

0 comments on commit 35741de

Please sign in to comment.