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

feat(Option): ✨ Add new methods for Option #57

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/option/option.test.ts
Original file line number Diff line number Diff line change
@@ -49,4 +49,12 @@ describe('Option monad', () => {
])('$type should handle isSome operation correctly', ({ option, expected }) => {
expect(option.isSome()).toEqual(expected);
});

it('should create a Some', () => {
expect(Option.some(2)).toEqual(new Some(2));
});

it('should create a None', () => {
expect(Option.none()).toEqual(new None());
});
});
26 changes: 25 additions & 1 deletion src/option/option.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Nullable } from '../types';
import { Present, Nullable } from '../types';
import { Monad } from '../monad';
import { Matchable } from '../match';

@@ -26,6 +26,30 @@ abstract class Option<T> implements Monad<T>, Matchable<T, undefined> {
return new Some(value);
}

/**
* Creates an `Option` instance from a value.
* @template T The type of the value.
* @param {Present<T>} value The nullable value.
* @returns {Some<T>} A `Some` instance of the value
* @example
* const some = Option.some(5);
* some.match(console.log, () => console.log('none')); // 5
*/
static some<T>(value: Present<T>): Option<T> {
return new Some(value);
}

/**
* Creates a `None` instance.
* @returns {None} A `None` instance.
* @example
* const none = Option.none();
* none.match(console.log, () => console.log('none')); // none
*/
static none<T>(): Option<T> {
return new None();
}

/**
* Creates an `Option` instance from a `Matchable` instance.
* @template T The type of the value.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type Nullable<T> = T | null | undefined;
export type Present<T> = Exclude<T, null | undefined>;
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,9 @@
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictNullChecks": true,
},
"include": ["src"],
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]