-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
259 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,57 +1,74 @@ | ||
import type { Nullable } from './utils/types' | ||
// Maybe monad implementation, API inspired by https://github.com/gigobyte/purify | ||
// Reference documentation https://gigobyte.github.io/purify/adts/Maybe | ||
// ISC licensed https://github.com/gigobyte/purify/blob/0840eb69b97617b09aca098f73948c61de563194/LICENSE | ||
|
||
export interface Maybe<T extends {}> { | ||
// isJust: () => boolean | ||
// isNothing: () => boolean | ||
map: <U extends {}>(f: (value: T) => U) => Maybe<U> | ||
chain: <U extends {}>(f: (value: T) => Maybe<U>) => Maybe<U> | ||
export interface IMaybe<T extends {}> { | ||
isJust: () => boolean | ||
isNothing: () => boolean | ||
map: <U extends {}>(f: (value: T) => U) => IMaybe<U> | ||
alt: (other: IMaybe<T>) => IMaybe<T> | ||
altLazy: (other: () => IMaybe<T>) => IMaybe<T> | ||
chain: <U extends {}>(f: (value: T) => IMaybe<U>) => IMaybe<U> | ||
orDefault: <U>(defaultValue: U) => T | U | ||
orDefaultLazy: <U>(getDefaultValue: () => U) => T | U | ||
filter: (pred: (value: T) => boolean) => Maybe<T> | ||
filter: (pred: (value: T) => boolean) => IMaybe<T> | ||
extract: () => T | undefined | ||
// extractNullable: () => T | null | ||
extractNullable: () => T | null | ||
ifJust: (f: (value: T) => void) => this | ||
// ifNothing: (f: () => void) => this | ||
ifNothing: (f: () => void) => this | ||
} | ||
|
||
export const just = <T extends {}>(value: T): Maybe<T> => { | ||
const instance: Maybe<T> = { | ||
// isJust: () => true, | ||
// isNothing: () => false, | ||
map: (f) => just(f(value)), | ||
export type Maybe<T extends {}> = IMaybe<T> | ||
|
||
export const Just = <T extends {}>(value: T): IMaybe<T> => { | ||
const instance: IMaybe<T> = { | ||
isJust: () => true, | ||
isNothing: () => false, | ||
map: (f) => Just(f(value)), | ||
alt: () => instance, | ||
altLazy: () => instance, | ||
chain: (f) => f(value), | ||
orDefault: () => value, | ||
orDefaultLazy: () => value, | ||
filter: (pred) => (pred(value) ? instance : nothing()), | ||
filter: (pred) => (pred(value) ? instance : Nothing), | ||
extract: () => value, | ||
// extractNullable: () => value, | ||
extractNullable: () => value, | ||
ifJust: (f) => (f(value), instance), | ||
// ifNothing: () => instance, | ||
ifNothing: () => instance, | ||
} | ||
if (import.meta.env.DEV) { | ||
Object.freeze(instance) | ||
} | ||
return instance | ||
} | ||
|
||
export const nothing = <T extends {}>(): Maybe<T> => { | ||
const instance: Maybe<T> = { | ||
// isJust: () => false, | ||
// isNothing: () => true, | ||
map: () => nothing(), | ||
chain: () => nothing(), | ||
orDefault: (defaultValue) => defaultValue, | ||
orDefaultLazy: (getDefaultValue) => getDefaultValue(), | ||
filter: () => instance, | ||
extract: () => undefined, | ||
// extractNullable: () => null, | ||
ifJust: () => instance, | ||
// ifNothing: (f) => (f(), instance), | ||
} | ||
return instance | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const Nothing: IMaybe<any> = { | ||
isJust: () => false, | ||
isNothing: () => true, | ||
map: () => Nothing, | ||
alt: (other) => other, | ||
altLazy: (other) => other(), | ||
chain: () => Nothing, | ||
orDefault: (defaultValue) => defaultValue, | ||
orDefaultLazy: (getDefaultValue) => getDefaultValue(), | ||
filter: () => Nothing, | ||
extract: () => undefined, | ||
extractNullable: () => null, | ||
ifJust: () => Nothing, | ||
ifNothing: (f) => (f(), Nothing), | ||
} | ||
|
||
if (import.meta.env.DEV) { | ||
Object.freeze(Nothing) | ||
} | ||
|
||
type MaybeFromNullable = <T extends {}>(value: Nullable<T>) => Maybe<T> | ||
type Nullish = null | undefined | ||
type MaybeFromNullable = <T extends {}>(value: T | Nullish) => IMaybe<T> | ||
|
||
export const fromNullable: MaybeFromNullable = (value) => (value != null ? just(value) : nothing()) | ||
export const fromNullable: MaybeFromNullable = (value) => (value != null ? Just(value) : Nothing) | ||
|
||
type MaybeFromFalsy = <T extends {}>(value: Nullable<T> | false | 0 | 0n | '') => Maybe<T> | ||
type Falsy = Nullish | false | 0 | 0n | '' | ||
type MaybeFromFalsy = <T extends {}>(value: T | Falsy) => IMaybe<T> | ||
|
||
export const fromFalsy: MaybeFromFalsy = (value) => (value ? just(value) : nothing()) | ||
export const fromFalsy: MaybeFromFalsy = (value) => (value ? Just(value) : Nothing) |
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
Oops, something went wrong.