Skip to content

Commit

Permalink
Add copyPath (#31)
Browse files Browse the repository at this point in the history
* Add copyPath

* 💅
  • Loading branch information
spencerfdavis committed Jun 3, 2019
1 parent 30d8842 commit 10d8b0e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions API.md
Expand Up @@ -17,6 +17,7 @@
| [`combineWithP`](#combinewithp) | `(c -> b -> d) (a -> Promise b) -> Promise c -> Promise d` |
| [`convergeP`](#convergep) | `(b -> c -> Promise d) -> [(a -> Promise b), (a -> Promise c)] -> a -> Promise d` |
| [`copyProp`](#copyprop) | `String -> String -> { k: v } -> { k: v }` |
| [`copyPath`](#copypath) | `[String] -> [String] -> { k: v } -> { k: v }` |
| [`evolveP`](#evolvep) | `{ k: (v -> Promise v) } -> { k: v } -> Promise { k: v }` |
| [`juxtP`](#juxtp) | `[a... -> Promise b] -> a... -> Promise [b]` |
| [`mapP`](#mapp) | `Functor f => (a -> Promise b) -> f a -> Promise f b` |
Expand Down Expand Up @@ -302,6 +303,24 @@ const getCourse = convergeP(assoc('course'), [ fetchByCourseId, identity ])
const addCourseLesson = composeP(addLesson, getCourse)
```

### copyPath

`@articulate/funky/lib/copyPath`

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

Quickly copy one path on an object to another path.

```js
copyPath(
['user', 'id'],
['payload', 'userId'],
{ user: { id: 'abc' }, payload: { name: 'Bob' } }
) //=> { user { id: 'abc' }, payload: { name: 'Bob', userId: 'abc' } }
```

### copyProp

`@articulate/funky/lib/copyProp`
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -12,6 +12,7 @@ exports.combineP = require('./lib/combineP')
exports.combineWith = require('./lib/combineWith')
exports.combineWithP = require('./lib/combineWithP')
exports.convergeP = require('./lib/convergeP')
exports.copyPath = require('./lib/copyPath')
exports.copyProp = require('./lib/copyProp')
exports.evolveP = require('./lib/evolveP')
exports.juxtP = require('./lib/juxtP')
Expand Down
9 changes: 9 additions & 0 deletions src/copyPath.js
@@ -0,0 +1,9 @@
const assocPath = require('ramda/src/assocPath')
const curry = require('ramda/src/curry')
const path = require('ramda/src/path')

// copyPath :: [String] -> [String] -> { k: v } -> { k: v }
const copyPath = (fromPath, toPath, obj) =>
assocPath(toPath, path(fromPath, obj), obj)

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

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

describe('copyPath', () => {
it('copies one path on an object to another path', () =>
expect(
copyPath(['foo', 'baz'], ['meta', 'bar'], {
foo: { baz: 'hey' },
meta: { secret: 'sec' }
})
).to.eql({ foo: { baz: 'hey' }, meta: { secret: 'sec', bar: 'hey' } }))
})

0 comments on commit 10d8b0e

Please sign in to comment.