Skip to content

Commit

Permalink
refactor: separate promise handler function
Browse files Browse the repository at this point in the history
  • Loading branch information
emilianobovetti committed Dec 6, 2019
1 parent 1676841 commit 42c5a84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
18 changes: 9 additions & 9 deletions __tests__/Result.js
Expand Up @@ -9,7 +9,7 @@ import { Result, Ok, Err } from '../src';
*/

test('Result(0) throws an error', () =>
expect(() => Result(0)).toThrow('Expected function or promise, got number')
expect(() => Result(0)).toThrow('Expected function, got number')
);

test('Result(fn) returns an `Ok` instance by default', () => {
Expand All @@ -26,26 +26,26 @@ test('Result(errFn) returns an `Err` instance', () => {
expect(res).toBeInstanceOf(Err);
});

test('Result(Promise.resolve()) returns an `Ok` instance in a Promise', () => {
const prom = Result(Promise.resolve('Ok'));
test('Result.promise(Promise.resolve()) makes an `Ok` instance', () => {
const prom = Result.promise(Promise.resolve('Ok'));

return prom.then(res => expect(res).toBeInstanceOf(Result));
});

test('Result(Promise.reject()) returns an `Err` instance in a Promise', () => {
const prom = Result(Promise.reject('Oh no!'));
test('Result.promise(Promise.reject()) makes an `Err` instance', () => {
const prom = Result.promise(Promise.reject('Oh no!'));

return prom.then(res => expect(res).toBeInstanceOf(Result));
});

test('Result(succPromise) wraps the value correctly', () => {
const prom = Result(Promise.resolve('Ok'));
test('Result.promise(succPromise) wraps the value correctly', () => {
const prom = Result.promise(Promise.resolve('Ok'));

return prom.then(res => expect(res.get()).toBe('Ok'));
});

test('Result(errPromise) wraps the value correctly', () => {
const prom = Result(Promise.reject('Oh no!'));
test('Result.promise(errPromise) wraps the value correctly', () => {
const prom = Result.promise(Promise.reject('Oh no!'));

return prom.then(res => expect(res.getErr()).toBe('Oh no!'));
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "@fpc/result",
"description": "Result type for modern javascript",
"version": "0.0.5",
"version": "0.0.6",
"author": "Emiliano Bovetti <emiliano.bovetti@gmail.com>",
"license": "GPL-3.0",
"keywords": [
Expand Down
26 changes: 12 additions & 14 deletions src/result.js
@@ -1,4 +1,4 @@
import { isFunction, isPromise, typeOf } from '@fpc/types';
import { expectFunction } from '@fpc/types';

/* global Symbol */
/* eslint-disable func-style, no-use-before-define, no-sequences */
Expand Down Expand Up @@ -48,22 +48,20 @@ Ctor.prototype = {
},
};

export const Result = (src, ...args) => {
if (isFunction(src)) {
try {
const ret = src(...args);

return ret instanceof Result ? ret : new Ok(ret);
} catch (e) {
return e instanceof Err ? e : new Err(e);
}
} else if (isPromise(src)) {
return src.then(Ok, Err);
} else {
throw new TypeError(`Expected function or promise, got ${typeOf(src)}`);
export const Result = (fn, ...args) => {
expectFunction(fn);

try {
const ret = fn(...args);

return ret instanceof Result ? ret : new Ok(ret);
} catch (e) {
return e instanceof Err ? e : new Err(e);
}
};

Result.promise = promise => promise.then(Ok, Err);

Result.prototype = Ctor.prototype;

export function Ok (val) {
Expand Down

0 comments on commit 42c5a84

Please sign in to comment.