Skip to content

Commit 2a8866e

Browse files
refactor: rename caseOf() to match()
BREAKING CHANGE: rename caseOf() to match() for Either and Maybe monads
1 parent 5035988 commit 2a8866e

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ import { maybe } from 'typescript-monads'
4949
let maybeVisitedBeforeXTimes: number | undefined = 50
5050

5151
const priceWithDiscountForLoyalty = maybe(maybeVisitedBeforeXTimes)
52-
.caseOf({
52+
.match({
5353
some: visits => 15.00 - visits * 0.1,
5454
none: () => 15.00
5555
})
5656

5757
// handle multiple maybe conditionas together
5858
const canRideCoaster = getAge() // Maybe<number>
5959
.bind(age => getTicket(age)) // Maybe<Ticket>
60-
.caseOf({
60+
.match({
6161
some: ticket => ticket.canRide('coaster1'),
6262
none: () => false
6363
})

src/either.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface IEither<L, R> {
22
isLeft(): boolean
33
isRight(): boolean
4-
caseOf<T>(pattern: IEitherPattern<L, R, T>): T
4+
match<T>(pattern: IEitherPattern<L, R, T>): T
55
map<T>(f: (r: R) => T): IEither<L, T>
66
flatMap<T>(f: (r: R) => IEither<L, T>): IEither<L, T>
77
}
@@ -44,7 +44,7 @@ export function either<L, R>(left?: L, right?: R): IEither<L, R> {
4444
isRight() {
4545
return exists(right)
4646
},
47-
caseOf<T>(pattern: IEitherPattern<L, R, T>) {
47+
match<T>(pattern: IEitherPattern<L, R, T>) {
4848
return exists(right)
4949
? pattern.right(right as R)
5050
: pattern.left(left as L)

src/maybe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface IMaybe<T> extends IMonad<T> {
3737
/**
3838
* Unwrap and apply MaybePattern functions
3939
*/
40-
caseOf<R>(pattern: IMaybePattern<T, R>): R
40+
match<R>(pattern: IMaybePattern<T, R>): R
4141

4242
/**
4343
* Combine multiple maybe
@@ -59,7 +59,7 @@ export function maybe<T>(value?: T): IMaybe<T> {
5959
valueOr: (val: T) => value === null || value === undefined ? val : value,
6060
valueOrCompute: (f: () => T) => value === null || value === undefined ? f() : value,
6161
tap: (obj: IMaybePattern<T, void>) => value === null || value === undefined ? obj.none() : obj.some(value),
62-
caseOf: <R>(pattern: IMaybePattern<T, R>) => value === null || value === undefined ? pattern.none() : pattern.some(value),
62+
match: <R>(pattern: IMaybePattern<T, R>) => value === null || value === undefined ? pattern.none() : pattern.some(value),
6363
map: <R>(f: (t: T) => R) => value === null || value === undefined ? maybe<R>() : maybe<R>(f(value)),
6464
flatMap: <R>(f: (d: T) => IMaybe<R>) => value === null || value === undefined ? maybe<R>() : f(value)
6565
}

test/either.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ describe('Either', () => {
3434
expect(eitherThing.isLeft()).toBeFalsy()
3535
})
3636

37-
it('should map to caseOf right side', () => {
37+
it('should map to match right side', () => {
3838
const leftInput: number | undefined = undefined
3939
const rightInput = 'tester'
4040

4141
const eitherThing = either(leftInput, rightInput)
4242

43-
const mapped = eitherThing.caseOf({
43+
const mapped = eitherThing.match({
4444
left: num => '123',
4545
right: str => `${str}_right`
4646
})
4747

4848
expect(mapped).toEqual('tester_right')
4949
})
5050

51-
it('should map to caseOf left side', () => {
51+
it('should map to match left side', () => {
5252
const leftInput = 123
5353
const rightInput: string | undefined = undefined
5454

5555
const eitherThing = either(leftInput, rightInput)
5656

57-
const mapped = eitherThing.caseOf({
57+
const mapped = eitherThing.match({
5858
left: num => `${num}_left`,
5959
right: str => `${str}_right`
6060
})
@@ -71,7 +71,7 @@ describe('Either', () => {
7171

7272
const mapped = eitherThing
7373
.map(rightNum => rightNum + 12)
74-
.caseOf({
74+
.match({
7575
left: () => 3,
7676
right: num => num
7777
})
@@ -80,7 +80,7 @@ describe('Either', () => {
8080

8181
const mapped2 = eitherThing2
8282
.map(rightNum => rightNum + 12)
83-
.caseOf({
83+
.match({
8484
left: () => 3,
8585
right: num => num
8686
})
@@ -96,7 +96,7 @@ describe('Either', () => {
9696

9797
const mapped = eitherThing
9898
.flatMap(rightNum => either(rightNum, input2))
99-
.caseOf({
99+
.match({
100100
left: () => 3,
101101
right: num => num
102102
})
@@ -112,7 +112,7 @@ describe('Either', () => {
112112

113113
const mapped = eitherThing
114114
.flatMap(rightNum => either(rightNum, input2))
115-
.caseOf({
115+
.match({
116116
left: () => 3,
117117
right: num => num
118118
})

test/maybe.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ describe('Maybe', () => {
7575
})
7676
})
7777

78-
describe('when returning from a caseOf operation', () => {
78+
describe('when returning from a match operation', () => {
7979
it('should handle "none" case', () => {
8080
const sut: string | undefined = undefined
8181
const maybeAMappedString = maybe(sut)
82-
.caseOf({
82+
.match({
8383
none: () => 'fallback',
8484
some: _original => _original
8585
})
@@ -90,7 +90,7 @@ describe('Maybe', () => {
9090
it('should handle "some" case', () => {
9191
const sut: string | undefined = 'existing value'
9292
const maybeAMappedString = maybe(sut)
93-
.caseOf({
93+
.match({
9494
none: () => 'fallback',
9595
some: _original => _original
9696
})

0 commit comments

Comments
 (0)