-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spec): add option spec interface and impl
- Loading branch information
1 parent
7bc4975
commit 421d050
Showing
1 changed file
with
50 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** The option type. */ | ||
export enum OptionType { | ||
Some = "some", | ||
None = "none", | ||
} | ||
|
||
/** Common type. */ | ||
interface Container { | ||
/** Option type. */ | ||
type: OptionType; | ||
} | ||
|
||
/** The {@link None} API. */ | ||
export interface None extends Container { | ||
type: OptionType.None; | ||
} | ||
|
||
/** The {@link Some} API. */ | ||
export interface Some<T> extends Container { | ||
type: OptionType.Some; | ||
|
||
/** Return contained {@link T}. */ | ||
get get(): T; | ||
} | ||
|
||
export interface SomeConstructor { | ||
/** {@link Some} value of type {@link T } . */ | ||
of<const T>(value: T): Some<T>; | ||
} | ||
|
||
/** No value. */ | ||
export const None: None = { type: OptionType.None }; | ||
|
||
/** {@link Some} constructor. */ | ||
export const Some: SomeConstructor = { | ||
of<T>(value: T): Some<T> { | ||
return { | ||
type: OptionType.Some, | ||
get get(): T { | ||
return value; | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
/** Representation of {@link Some} or {@link None}. */ | ||
export type Option<T> = Some<T> | None; |