diff --git a/README.md b/README.md index c67f302..637d744 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Standard Functions is a library aimed to fill in the gaps in the JS standard lib ## Functions
-
bimap(leftFn, rightFn, p)Promise.<(T|never)>
+
bimap(leftFn, rightFn, p)Promise.<(R|never)>

Map over the success or error of a promise.

fmap(fn, p)Promise.<(T|never)>
@@ -80,7 +80,7 @@ Works like the ! operator on the result of the function provided

-## bimap(leftFn, rightFn, p) ⇒ Promise.<(T\|never)> +## bimap(leftFn, rightFn, p) ⇒ Promise.<(R\|never)> Map over the success or error of a promise. **Kind**: global function @@ -233,7 +233,8 @@ Call instance method of an object. ## negate(fn) ⇒ function -Creates a function which negates the value of the result. Works like the ! operator on the result of the function provided +Creates a function which negates the value of the result. +Works like the ! operator on the result of the function provided **Kind**: global function diff --git a/async/bimap/bimap.js b/async/bimap/bimap.js index a24c449..923aac9 100644 --- a/async/bimap/bimap.js +++ b/async/bimap/bimap.js @@ -10,10 +10,10 @@ Object.defineProperty(exports, "__esModule", { * @param leftFn {Function} * @param rightFn {Function} * @param p {Promise} - * @returns {Promise} + * @returns {Promise} */ function bimap(leftFn, rightFn, p) { - return p.then(rightFn, leftFn); + return p.then(rightFn, e => Promise.reject(leftFn(e))); } exports.default = bimap; \ No newline at end of file diff --git a/async/bimap/bimap.js.flow b/async/bimap/bimap.js.flow index d515592..40b6500 100644 --- a/async/bimap/bimap.js.flow +++ b/async/bimap/bimap.js.flow @@ -5,14 +5,14 @@ * @param leftFn {Function} * @param rightFn {Function} * @param p {Promise} - * @returns {Promise} + * @returns {Promise} */ -function bimap( - leftFn: (Error) => any, - rightFn: (T) => any, +function bimap( + leftFn: (E) => any, + rightFn: (T) => R, p: Promise -): Promise { - return p.then(rightFn, leftFn); +): Promise { + return p.then(rightFn, (e: E): Promise => Promise.reject(leftFn(e))); } export default bimap; diff --git a/core/index.js b/core/index.js index 957a030..1ac414a 100644 --- a/core/index.js +++ b/core/index.js @@ -4,6 +4,24 @@ Object.defineProperty(exports, "__esModule", { value: true }); +var _always = require('./always/always'); + +Object.defineProperty(exports, 'always', { + enumerable: true, + get: function () { + return _interopRequireDefault(_always).default; + } +}); + +var _and = require('./and/and'); + +Object.defineProperty(exports, 'and', { + enumerable: true, + get: function () { + return _interopRequireDefault(_and).default; + } +}); + var _compose = require('./compose/compose'); Object.defineProperty(exports, 'compose', { @@ -49,6 +67,42 @@ Object.defineProperty(exports, 'identity', { } }); +var _inst = require('./inst/inst'); + +Object.defineProperty(exports, 'inst', { + enumerable: true, + get: function () { + return _interopRequireDefault(_inst).default; + } +}); + +var _negate = require('./negate/negate'); + +Object.defineProperty(exports, 'negate', { + enumerable: true, + get: function () { + return _interopRequireDefault(_negate).default; + } +}); + +var _not = require('./not/not'); + +Object.defineProperty(exports, 'not', { + enumerable: true, + get: function () { + return _interopRequireDefault(_not).default; + } +}); + +var _or = require('./or/or'); + +Object.defineProperty(exports, 'or', { + enumerable: true, + get: function () { + return _interopRequireDefault(_or).default; + } +}); + var _partial = require('./partial/partial'); Object.defineProperty(exports, 'partial', { @@ -67,4 +121,13 @@ Object.defineProperty(exports, 'partialRight', { } }); +var _trim = require('./trim/trim'); + +Object.defineProperty(exports, 'trim', { + enumerable: true, + get: function () { + return _interopRequireDefault(_trim).default; + } +}); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } \ No newline at end of file diff --git a/core/index.js.flow b/core/index.js.flow index 5629cca..e8a0d2e 100644 --- a/core/index.js.flow +++ b/core/index.js.flow @@ -1,9 +1,17 @@ // @flow +export { default as always } from './always/always'; +export { default as and } from './and/and'; export { default as compose } from './compose/compose'; export { default as composeRight } from './composeRight/composeRight'; export { default as curry } from './curry/curry'; export { default as curryRight } from './curryRight/curryRight'; export { default as identity } from './identity/identity'; +export { default as inst } from './inst/inst'; +export { default as negate } from './negate/negate'; +export { default as not } from './not/not'; +export { default as or } from './or/or'; export { default as partial } from './partial/partial'; export { default as partialRight } from './partialRight/partialRight'; +export { default as trim } from './trim/trim'; + diff --git a/package.json b/package.json index c255c31..7eb583a 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "test": "jest ./src", "check": "eslint ./src && flow ./src", - "build": "yarn check && babel src/ -d ./ --ignore **/*spec.js && yarn build:docs && yarn build:types", + "build": "npm run check && babel src/ -d ./ --ignore **/*spec.js && npm run build:docs && npm run build:types", "build:types": "flow-copy-source -v -i **/*spec.js src ./", "build:docs": "node ./scripts/docs" }, diff --git a/src/async/bimap/bimap.js b/src/async/bimap/bimap.js index d515592..40b6500 100644 --- a/src/async/bimap/bimap.js +++ b/src/async/bimap/bimap.js @@ -5,14 +5,14 @@ * @param leftFn {Function} * @param rightFn {Function} * @param p {Promise} - * @returns {Promise} + * @returns {Promise} */ -function bimap( - leftFn: (Error) => any, - rightFn: (T) => any, +function bimap( + leftFn: (E) => any, + rightFn: (T) => R, p: Promise -): Promise { - return p.then(rightFn, leftFn); +): Promise { + return p.then(rightFn, (e: E): Promise => Promise.reject(leftFn(e))); } export default bimap; diff --git a/src/async/bimap/bimap.spec.js b/src/async/bimap/bimap.spec.js index 049cdc5..3e22198 100644 --- a/src/async/bimap/bimap.spec.js +++ b/src/async/bimap/bimap.spec.js @@ -3,27 +3,37 @@ import bimap from './bimap'; describe('async/bimap', () => { - test('should only call left fn if promise rejects', async (): Promise => { - const leftFn: Function = jest.fn(); + test('should only call leftFn if promise rejects', async (done: Function): Promise => { + const expected: Error = new Error('error'); + const leftFn: Function = jest.fn().mockImplementation((v: number): Error => expected); const rightFn: Function = jest.fn(); - try { - await bimap(leftFn, rightFn, Promise.reject(1)); + expect.assertions(3); - expect(leftFn).not.toHaveBeenCalled(); - } catch (e) { - expect(e).not.toBeUndefined(); - expect(rightFn).not.toHaveBeenCalled(); - } + bimap(leftFn, rightFn, Promise.reject(3)) + .then(done) + .catch((actual: Error) => { + expect(actual).toBe(expected); + expect(leftFn).toHaveBeenCalled(); + expect(rightFn).not.toHaveBeenCalled(); + done(); + }); }); - test('should only call rightFn if promise resolves', async (): Promise => { + test('should only call rightFn if promise resolves', async (done: Function): Promise => { + const expected: number = 6; const leftFn: Function = jest.fn(); - const rightFn: Function = jest.fn(); + const rightFn: Function = jest.fn().mockImplementation((v: number): number => expected); - await bimap(leftFn, rightFn, Promise.resolve(1)); + expect.assertions(3); - expect(leftFn).not.toHaveBeenCalled(); - expect(rightFn).toHaveBeenCalled(); + bimap(leftFn, rightFn, Promise.resolve(1)) + .then((actual: number) => { + expect(actual).toBe(expected); + expect(leftFn).not.toHaveBeenCalled(); + expect(rightFn).toHaveBeenCalled(); + done(); + }) + .catch(done); }); -}); \ No newline at end of file +});