Skip to content

Commit

Permalink
Add "P" alias to "pattern"
Browse files Browse the repository at this point in the history
zoontek committed May 16, 2023
1 parent 2e99756 commit 9aa863a
Showing 8 changed files with 46 additions and 37 deletions.
6 changes: 3 additions & 3 deletions docs/docs/async-data.md
Original file line number Diff line number Diff line change
@@ -286,9 +286,9 @@ import { match, P } from "ts-pattern";
import { AsyncData } from "@swan-io/boxed";

match(asyncData)
.with(AsyncData.pattern.Done(P.select()), (value) => console.log(value))
.with(AsyncData.pattern.Loading, () => "Loading ...")
.with(AsyncData.pattern.NotAsked, () => "")
.with(AsyncData.P.Done(P.select()), (value) => console.log(value))
.with(AsyncData.P.Loading, () => "Loading ...")
.with(AsyncData.P.NotAsked, () => "")
.exhaustive();
```

4 changes: 2 additions & 2 deletions docs/docs/option.md
Original file line number Diff line number Diff line change
@@ -269,8 +269,8 @@ import { match, P } from "ts-pattern";
import { Option } from "@swan-io/boxed";

match(myOption)
.with(Option.pattern.Some(P.select()), (value) => console.log(value))
.with(Option.pattern.None, () => "No value")
.with(Option.P.Some(P.select()), (value) => console.log(value))
.with(Option.P.None, () => "No value")
.exhaustive();
```

4 changes: 2 additions & 2 deletions docs/docs/result.md
Original file line number Diff line number Diff line change
@@ -406,8 +406,8 @@ import { match, P } from "ts-pattern";
import { Result } from "@swan-io/boxed";

match(myResult)
.with(Result.pattern.Ok(P.select()), (value) => console.log(value))
.with(Result.pattern.Error(P.select()), (error) => {
.with(Result.P.Ok(P.select()), (value) => console.log(value))
.with(Result.P.Error(P.select()), (error) => {
console.error(error);
return "fallback";
})
13 changes: 8 additions & 5 deletions src/AsyncData.ts
Original file line number Diff line number Diff line change
@@ -341,6 +341,12 @@ const NOT_ASKED = (() => {
const Loading = <A = never>(): AsyncData<A> => LOADING as Loading<A>;
const NotAsked = <A = never>(): AsyncData<A> => NOT_ASKED as NotAsked<A>;

const asyncDataPattern = {
Done: <A>(value: A) => ({ tag: "Done", value } as const),
NotAsked: { tag: "NotAsked" } as const,
Loading: { tag: "Loading" } as const,
};

export const AsyncData = {
/**
* Create an AsyncData.Done value
@@ -410,9 +416,6 @@ export const AsyncData = {
: a.tag === b.tag;
},

pattern: {
Done: <T>(x: T) => ({ tag: "Done", value: x } as const),
NotAsked: { tag: "NotAsked" } as const,
Loading: { tag: "Loading" } as const,
},
P: asyncDataPattern,
pattern: asyncDataPattern,
};
22 changes: 14 additions & 8 deletions src/OptionResult.ts
Original file line number Diff line number Diff line change
@@ -155,6 +155,11 @@ const NONE = (() => {

const None = <A = never>(): Option<A> => NONE as None<A>;

const optionPattern = {
Some: <A>(value: A) => ({ tag: "Some", value } as const),
None: { tag: "None" } as const,
};

export const Option = {
/**
* Create an Option.Some value
@@ -242,10 +247,8 @@ export const Option = {
: a.tag === b.tag;
},

pattern: {
Some: <T>(x: T) => ({ tag: "Some", value: x } as const),
None: { tag: "None" } as const,
},
P: optionPattern,
pattern: optionPattern,
};

interface IResult<A, E> {
@@ -457,6 +460,11 @@ const Error = <A = never, E = never>(value: E): Result<A, E> => {
return result;
};

const resultPattern = {
Ok: <A>(value: A) => ({ tag: "Ok", value } as const),
Error: <E>(value: E) => ({ tag: "Error", value } as const),
};

export const Result = {
/**
* Create an Result.Ok value
@@ -573,8 +581,6 @@ export const Result = {
return equals(a.value as unknown as A, b.value as unknown as A);
},

pattern: {
Ok: <T>(x: T) => ({ tag: "Ok", value: x } as const),
Error: <T>(x: T) => ({ tag: "Error", value: x } as const),
},
P: resultPattern,
pattern: resultPattern,
};
18 changes: 9 additions & 9 deletions test/AsyncData.test.ts
Original file line number Diff line number Diff line change
@@ -227,23 +227,23 @@ test("AsyncData.allFromDict", () => {
test("ts-pattern", () => {
expect(
match(AsyncData.Done(1))
.with(AsyncData.pattern.Done(P.select()), (value) => value)
.with(AsyncData.pattern.Loading, () => 2)
.with(AsyncData.pattern.NotAsked, () => 3)
.with(AsyncData.P.Done(P.select()), (value) => value)
.with(AsyncData.P.Loading, () => 2)
.with(AsyncData.P.NotAsked, () => 3)
.exhaustive(),
).toEqual(1);
expect(
match(AsyncData.Loading())
.with(AsyncData.pattern.Done(P.any), (value) => value)
.with(AsyncData.pattern.Loading, () => 2)
.with(AsyncData.pattern.NotAsked, () => 3)
.with(AsyncData.P.Done(P.any), (value) => value)
.with(AsyncData.P.Loading, () => 2)
.with(AsyncData.P.NotAsked, () => 3)
.exhaustive(),
).toEqual(2);
expect(
match(AsyncData.NotAsked())
.with(AsyncData.pattern.Done(P.any), (value) => value)
.with(AsyncData.pattern.Loading, () => 2)
.with(AsyncData.pattern.NotAsked, () => 3)
.with(AsyncData.P.Done(P.any), (value) => value)
.with(AsyncData.P.Loading, () => 2)
.with(AsyncData.P.NotAsked, () => 3)
.exhaustive(),
).toEqual(3);
});
8 changes: 4 additions & 4 deletions test/Option.test.ts
Original file line number Diff line number Diff line change
@@ -158,15 +158,15 @@ test("Option.allFromDict", () => {
test("ts-pattern", () => {
expect(
match(Option.Some(1))
.with(Option.pattern.Some(P.select()), (value) => value)
.with(Option.pattern.None, () => 2)
.with(Option.P.Some(P.select()), (value) => value)
.with(Option.P.None, () => 2)
.exhaustive(),
).toEqual(1);

expect(
match(Option.None())
.with(Option.pattern.Some(P.any), (value) => value)
.with(Option.pattern.None, () => 2)
.with(Option.P.Some(P.any), (value) => value)
.with(Option.P.None, () => 2)
.exhaustive(),
).toEqual(2);
});
8 changes: 4 additions & 4 deletions test/Result.test.ts
Original file line number Diff line number Diff line change
@@ -207,15 +207,15 @@ test("Result.allFromDict", () => {
test("ts-pattern", () => {
expect(
match(Result.Ok(1))
.with(Result.pattern.Ok(P.select()), (value) => value)
.with(Result.pattern.Error(P.any), () => 2)
.with(Result.P.Ok(P.select()), (value) => value)
.with(Result.P.Error(P.any), () => 2)
.exhaustive(),
).toEqual(1);

expect(
match(Result.Error(2))
.with(Result.pattern.Ok(P.any), (value) => value)
.with(Result.pattern.Error(P.select()), (value) => value)
.with(Result.P.Ok(P.any), (value) => value)
.with(Result.P.Error(P.select()), (value) => value)
.exhaustive(),
).toEqual(2);
});

0 comments on commit 9aa863a

Please sign in to comment.