-
-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add P.object and P.object.empty #233
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
27f8749
docs: add P.object on README.md
gitsunmin ea7a937
Add object, object.empty pattern and tests
gitsunmin 1831e29
Fix multiple issues based on PR review feedback
gitsunmin f17c02c
Fix objectChainable type in patterns.ts and Pattern.ts
gitsunmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { Expect, Equal } from '../src/types/helpers'; | ||
import { P, match } from '../src'; | ||
|
||
describe('Object', () => { | ||
it('should match exact object', () => { | ||
const fn = () => 'hello'; | ||
|
||
const res = match({ str: fn() }) | ||
.with({ str: 'world' }, (obj) => { | ||
type t = Expect<Equal<typeof obj, { str: 'world' }>>; | ||
return obj.str; | ||
}) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, { | ||
readonly str: string; | ||
}>>; | ||
return 'not found'; | ||
}) | ||
.exhaustive(); | ||
expect(res).toEqual('not found'); | ||
}); | ||
|
||
it('when input is a Function, it should not match as an exact object', () => { | ||
const fn = () => () => {}; | ||
|
||
const res = match(fn()) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, () => void>>; | ||
return 'not found'; | ||
}) | ||
.otherwise(() => 'not found'); | ||
expect(res).toEqual('not found'); | ||
}) | ||
|
||
it('when input is a Number (a primitive value), it should not be matched as an exact object', () => { | ||
const fn = () => 1_000_000; | ||
|
||
const res = match(fn()) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, number>>; | ||
return 'not found'; | ||
}) | ||
.otherwise(() => 'not found'); | ||
expect(res).toEqual('not found'); | ||
}) | ||
|
||
it('when input is a String (a primitive value), it should not be matched as an exact object', () => { | ||
const fn = () => 'hello'; | ||
|
||
const res = match(fn()) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, string>>; | ||
return 'not found'; | ||
}) | ||
.otherwise(() => 'not found'); | ||
expect(res).toEqual('not found'); | ||
}) | ||
|
||
it('when input is a Boolean (a primitive value), it should not be matched as an exact object', () => { | ||
const fn = () => true; | ||
|
||
const res = match(fn()) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, boolean>>; | ||
return 'not found'; | ||
}) | ||
.otherwise(() => 'not found'); | ||
expect(res).toEqual('not found'); | ||
}) | ||
|
||
it('when input is Null, it should not be matched as an exact object', () => { | ||
const fn = () => null; | ||
|
||
const res = match(fn()) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, null>>; | ||
return 'not found'; | ||
}) | ||
.otherwise(() => 'not found'); | ||
expect(res).toEqual('not found'); | ||
}) | ||
|
||
it('should match object with nested objects', () => { | ||
const res = match({ x: { y: 1 } }) | ||
.with({ x: { y: 1 } }, (obj) => { | ||
type t = Expect<Equal<typeof obj, { readonly x: { readonly y: 1 } }>>; | ||
return 'yes'; | ||
}) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, never>>; | ||
return 'no'; | ||
}) | ||
.exhaustive(); | ||
expect(res).toEqual('yes'); | ||
}); | ||
|
||
it('should match object with nested objects and arrays', () => { | ||
const res = match({ x: { y: [1] } }) | ||
.with({ x: { y: [1] } }, (obj) => { | ||
type t = Expect<Equal<typeof obj, { x: { y: [1] } }>>; | ||
return 'yes'; | ||
}) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, { readonly x: { readonly y: readonly [1]}}>>; | ||
return 'no'; | ||
}) | ||
.exhaustive(); | ||
|
||
expect(res).toEqual('yes'); | ||
}); | ||
|
||
it('should match empty object', () => { | ||
const res = match({}) | ||
.with(P.object.empty(), (obj) => { | ||
type t = Expect<Equal<typeof obj, {}>>; | ||
|
||
return 'yes'; | ||
}) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, {}>>; | ||
|
||
return 'no'; | ||
}) | ||
.exhaustive(); | ||
expect(res).toEqual('yes'); | ||
}); | ||
|
||
it('should properly match an object against the P.object pattern, even with optional properties', () => { | ||
const res = match({ x: 1 }) | ||
.with(P.object.empty(), (obj) => { | ||
type t = Expect<Equal<typeof obj, { readonly x: 1; }>>; | ||
return 'no'; | ||
}) | ||
.with(P.object, (obj) => { | ||
type t = Expect<Equal<typeof obj, { | ||
readonly x: 1; | ||
}>>; | ||
return 'yes'; | ||
}) | ||
.exhaustive(); | ||
expect(res).toEqual('yes'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add test covering how P.object behaves with more inputs:
It should catch all values that are assignable to the
object
type, and type narrowing and exhaustive should both workThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added test. If you need more, please comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, functions and arrays are part of the
object
type in TypeScript: PlaygroundI think
P.object
should catch anything assignable toobject
to stay close to typescript semantics and play nicely with exhaustiveness checking.I'm going to update your PR shortly