Skip to content

Commit 152ccac

Browse files
fix(maybe): use functions instead of properties (#3)
1 parent 09b0088 commit 152ccac

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { maybe, Maybe } from './maybe'
1+
export { maybe, IMaybe, IMaybePattern } from './maybe'

src/maybe.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
1-
interface CaseOf<TIn, TOut> {
2-
readonly some: (val: TIn) => TOut
3-
readonly none: () => TOut
1+
/**
2+
* Define a contract to unwrap Maybe object
3+
*/
4+
export interface IMaybePattern<TIn, TOut> {
5+
/**
6+
* Function to handle when a value exists.
7+
*/
8+
some(val: TIn): TOut
9+
10+
/**
11+
* Function to handle when a value is undefined.
12+
*/
13+
none(): TOut
414
}
515

6-
export interface Maybe<T> {
16+
/**
17+
* Abstraction for handling possibility of undefined values
18+
*/
19+
export interface IMaybe<T> {
720
/**
821
* Unwrap a Maybe with a default value
922
*/
10-
readonly valueOr: (val: T) => T,
23+
valueOr(val: T): T
1124

1225
/**
1326
* Unwrap a Maybe with a default computed value
1427
*/
15-
readonly valueOrCompute: (f: () => T) => T,
16-
readonly map: <R>(val: CaseOf<T, R>) => R
17-
readonly do: (val: CaseOf<T, void>) => void,
18-
readonly bind: <R>(f: (maybe: T) => Maybe<R>) => Maybe<R>
28+
valueOrCompute(f: () => T): T
29+
30+
/**
31+
* Unwrap and apply MaybePattern functions
32+
*/
33+
map<R>(val: IMaybePattern<T, R>): R
34+
35+
/**
36+
* Execute functions with side-effects.
37+
*/
38+
do(val: IMaybePattern<T, void>): void
39+
40+
/**
41+
* Combine multiple maybe
42+
*/
43+
bind<R>(f: (t: T) => IMaybe<R>): IMaybe<R>
1944
}
2045

21-
export function maybe<T>(value?: T): Maybe<T> {
46+
export function maybe<T>(value?: T): IMaybe<T> {
2247
return {
2348
valueOr: (val: T) => value || val,
2449
valueOrCompute: (f: () => T) => value || f(),
25-
map: <R>(obj: CaseOf<T, R>) => value ? obj.some(value) : obj.none(),
26-
do: (obj: CaseOf<T, void>) => value ? obj.some(value) : obj.none(),
27-
bind: <R>(f: (d: T) => Maybe<R>) => value ? f(value) : maybe<R>()
50+
map: <R>(obj: IMaybePattern<T, R>) => value ? obj.some(value) : obj.none(),
51+
do: (obj: IMaybePattern<T, void>) => value ? obj.some(value) : obj.none(),
52+
bind: <R>(f: (d: T) => IMaybe<R>) => value ? f(value) : maybe<R>()
2853
}
2954
}

test/maybe.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('Maybe', () => {
9595
it('should handle "none" case', () => {
9696
const sut: string | undefined = undefined
9797
const nsut: number | undefined = undefined
98-
98+
9999
const maybeSomeNumber = maybe(sut)
100100
.bind(() => maybe(nsut))
101101
.valueOr(1)
@@ -106,7 +106,7 @@ describe('Maybe', () => {
106106
it('should handle "some" case', () => {
107107
const sut: string | undefined = 'initial'
108108
const nsut: number | undefined = 20
109-
109+
110110
const maybeSomeNumber = maybe(sut)
111111
.bind(() => maybe(nsut))
112112
.valueOr(0)

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"no-let": true,
2222
"no-object-mutation": true,
2323
"no-delete": true,
24-
"no-method-signature": true,
24+
"no-method-signature": false,
2525
"no-this": true,
2626
"no-class": true,
2727
"no-expression-statement": false,

0 commit comments

Comments
 (0)