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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5280cff
feat(try): doc try monad
danielalvarezm Oct 4, 2024
fd16bdd
feat(option): doc option monad
danielalvarezm Oct 11, 2024
30f0f23
feat(io): doc io monad
danielalvarezm Oct 11, 2024
e9208b4
feat(either): doc either monad
danielalvarezm Oct 11, 2024
a77b7f8
feat(future): doc future monad
danielalvarezm Oct 11, 2024
14a6349
refactor: apply suggested changes
danielalvarezm Oct 18, 2024
5126ce2
feat(Option): :rotating_light: Option monad should create a Some
Oct 27, 2024
c19cf75
feat(Option): :white_check_mark: Option monad should create a Some
Oct 27, 2024
2f22faf
feat(Option): :rotating_light: Option monad should create a None
Oct 27, 2024
929b72a
feat(Option): :rotating_light: Option monad should create a None
Oct 27, 2024
37c5e25
feat(Option): :white_check_mark: Option monad should create a None
Oct 27, 2024
1f6461d
feat(Option): :recycle: Add NotNullable type
Oct 27, 2024
7d7dda6
feat(Option): :fire: Remove unnecessary test
Oct 28, 2024
2cd9034
feat(Option): :recycle: Rename options methods
Oct 29, 2024
e1b99b9
feat(Option): :recycle: Rename NotNullabe type to Present
Oct 31, 2024
0aa808a
fix(Try): fix Failure subclass generic typing and remove static variable
Marius9595 Nov 1, 2024
fcd2839
fix(monads): fix compilation to enable commonjs and esm entrypoint to…
Marius9595 Nov 1, 2024
f024f32
docs(monads): add tag
Marius9595 Nov 1, 2024
ffcd133
feat(Option): :rotating_light: Option monad should create a Some
Oct 27, 2024
83e23a1
feat(Option): :white_check_mark: Option monad should create a Some
Oct 27, 2024
bc32c8a
feat(Option): :rotating_light: Option monad should create a None
Oct 27, 2024
0953d2a
feat(Option): :rotating_light: Option monad should create a None
Oct 27, 2024
a41d200
feat(Option): :white_check_mark: Option monad should create a None
Oct 27, 2024
777a3a1
feat(Option): :recycle: Add NotNullable type
Oct 27, 2024
f9f5fa8
feat(Option): :fire: Remove unnecessary test
Oct 28, 2024
506e7f3
feat(Option): :recycle: Rename options methods
Oct 29, 2024
cbb5cc0
feat(Option): :recycle: Rename NotNullabe type to Present
Oct 31, 2024
0be8476
Merge remote-tracking branch 'origin/methods-for-option' into methods…
Nov 5, 2024
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
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{
"name": "@leanmind/monads",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"version": "0.0.0-semantically-released",
"description": "A collection of monads implemented in TypeScript using object-oriented programming.",
"keywords": [
"functional-programming",
"monads",
"typescript",
"oop"
"oop",
"error-handling"
],
"scripts": {
"analize": "npm run lint:fix && npm run compile",
"build": "npm run lint:fix && tsc",
"build": "npm run lint:fix && npm run compile:commonjs && npm run compile:esm",
"compile:commonjs": "tsc --outDir dist/cjs --module commonjs",
"compile:esm": "tsc --outDir dist/esm --module esnext",
"compile": "tsc --noEmit",
"compile:watch": "npm run compile -- --watch",
"compile:build": "tsc -b",
Expand Down
8 changes: 8 additions & 0 deletions src/option/option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/try/try.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Try monad', () => {
typeMatchable: 'None',
tryType: 'Failure',
matchable: Option.of<number>(undefined),
expected: Failure.NO_ERROR_PROVIDED,
expected: new Failure(new Error('No error provided')),
},
])('$tryType should be created from $typeMatchable', ({ matchable, expected }) => {
expect(Try.from(matchable)).toEqual(expected);
Expand Down
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>;
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist",
"outDir": "./dist",
"module": "ESNext",
"target": "ESNext",
"lib": ["ES2023"],
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictNullChecks": true,
Marius9595 marked this conversation as resolved.
Show resolved Hide resolved
},
"include": ["src"],
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
Expand Down