Skip to content
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

Methods for option #49

Closed
wants to merge 28 commits into from
Closed

Conversation

Sstark97
Copy link
Contributor

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

In this PR I have added two methods for the Option cutie:

  • ofSome: Creates an Option with a safe value, so it will return the subtype Some
  • ofNone: Creates an Option with no value, so it will return the subtype None
const some = Option.ofSome(5);
some.match(console.log, () => console.log('none')); // 5

const none = Option.ofNone();
none.match(console.log, () => console.log('none')); // none

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

What gif best describes this PR or how it makes you feel?

happy

danielalvarezm and others added 11 commits October 18, 2024 12:40
Co-authored-by: Leto Rodríguez <leto4100@gmail.com>
Co-authored-by: Borja García <borjagarcia.dev@gmail.com>
Co-authored-by: Leto Rodríguez <leto4100@gmail.com>
Co-authored-by: Borja García <borjagarcia.dev@gmail.com>
Co-authored-by: Leto Rodríguez <leto4100@gmail.com>
Co-authored-by: Borja García <borjagarcia.dev@gmail.com>
Co-authored-by: Leto Rodríguez <leto4100@gmail.com>
Co-authored-by: Borja García <borjagarcia.dev@gmail.com>
Co-authored-by: Leto Rodríguez <leto4100@gmail.com>
Co-authored-by: Borja García <borjagarcia.dev@gmail.com>
Co-authored-by: Leto Rodríguez <leto4100@gmail.com>
Co-authored-by: Borja García <borjagarcia.dev@gmail.com>
@Sstark97
Copy link
Contributor Author

I don't know how the ofSome method should behave with null/undefined, what do you think the behavior should be?

const nullishSome = Option.ofSome(null) // ???
const undefinedSome = Option.ofSome(undefined) // ???

@Sstark97 Sstark97 mentioned this pull request Oct 27, 2024
2 tasks
@Sstark97 Sstark97 self-assigned this Oct 27, 2024
@Sstark97 Sstark97 requested review from Marius9595 and myugen October 27, 2024 19:35
@Marius9595 Marius9595 linked an issue Oct 27, 2024 that may be closed by this pull request
2 tasks
@Marius9595
Copy link
Collaborator

I don't know how the ofSome method should behave with null/undefined, what do you think the behavior should be?

const nullishSome = Option.ofSome(null) // ???
const undefinedSome = Option.ofSome(undefined) // ???

mmm maybe we could create this Type:

type NotNullable<T> = T extends null | undefined ? never : T

So, ofSome method should be something like:

  static ofSome<T>(value: NotNullable<T>): Some<T> {
    return new Some(value);
  }

In this way, the TS compiler raises an error if you try to pass an undefined or null as value with the static analysis

@Sstark97
Copy link
Contributor Author

Sstark97 commented Oct 27, 2024

Y try to do this, but not working well, not raise a compiler error. In the editor I see a warning, but the test pass:
image

I tried two options for NotNullable type:

//type NotNullable<T> = T extends null | undefined ? never : T
export type NotNullable<T> = Exclude<T, null | undefined>;

And I add this in tsconfig:

{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true,
  },

You can check all details in the last commit

@Marius9595
Copy link
Collaborator

From my point of view, this cannot be tested with Vitest or Jest with a test case. I mean, the test actually is the static analysis of code, if this finds an error it will notify the developer to fix it (as a red test case). In addition, You can try to run a build, this should fail. At the end, we can pass anything in runtime a function that allows statically a specific type as input parameter 😟

Could you verify my guess, running the build action?

@Sstark97
Copy link
Contributor Author

Yeah this worked well! 😄

In the next commit the code will be ready

myugen
myugen previously requested changes Oct 29, 2024
Copy link
Collaborator

@myugen myugen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like to see more people involved in the library.

I really appreciate your contribution @Sstark97 ❤️

src/option/option.ts Outdated Show resolved Hide resolved
@Marius9595
Copy link
Collaborator

Same, thanks for this contribution @Sstark97! ❤️

@myugen
Copy link
Collaborator

myugen commented Oct 29, 2024

@Sstark97 if you consider PR is ready, you can remove Draft state.

@Sstark97 Sstark97 marked this pull request as ready for review October 30, 2024 14:47
Copy link
Collaborator

@Marius9595 Marius9595 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job @Sstark97!

I put some comments to solve some doubts on my side

src/option/option.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
tsconfig.json Show resolved Hide resolved
@Marius9595 Marius9595 self-requested a review November 5, 2024 22:02
Marius9595
Marius9595 previously approved these changes Nov 5, 2024
@Sstark97 Sstark97 dismissed Marius9595’s stale review November 5, 2024 22:03

The merge-base changed after approval.

@Sstark97 Sstark97 closed this Nov 12, 2024
@Marius9595 Marius9595 removed a link to an issue Nov 12, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants