diff --git a/index.js b/index.js index d90122c..be19872 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ exports.all = require('./lib/all') exports.assign = require('./lib/assign') +exports.assignP = require('./lib/assignP') exports.backoff = require('./lib/backoff') exports.combine = require('./lib/combine') exports.combineAll = require('./lib/combineAll') diff --git a/src/assignP.js b/src/assignP.js new file mode 100644 index 0000000..b59dd83 --- /dev/null +++ b/src/assignP.js @@ -0,0 +1,9 @@ +const curry = require('ramda/src/curry') +const combineWithP = require('./combineWithP') +const put = require('./put') + +// assignP : String -> ({ k: v } -> Promise a) -> Promise { k: v } +const assignP = (key, fn) => + combineWithP(put(key), fn) + +module.exports = curry(assignP) diff --git a/test/assignP.js b/test/assignP.js new file mode 100644 index 0000000..570eaa9 --- /dev/null +++ b/test/assignP.js @@ -0,0 +1,19 @@ +const { expect } = require('chai') +const property = require('prop-factory') +const always = require('ramda/src/always') + +const { assignP } = require('..') + +const assigner = assignP('foo', always(Promise.resolve('bar'))) + +describe('assignP', () => { + const res = property() + + beforeEach(() => + assigner({}).then(res) + ) + + it('sets the foo property to the result of the function', () => + expect(res()).to.eql({ foo: 'bar' }) + ) +})