mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-22 08:53:48 +01:00
Introduce option type (#4150)
* Introduce option type * Improve test naming
This commit is contained in:
parent
9fcc80c07a
commit
06d678df6f
20
src/prelude/maybe.ts
Normal file
20
src/prelude/maybe.ts
Normal file
@ -0,0 +1,20 @@
|
||||
export interface Maybe<T> {
|
||||
isJust(): this is Just<T>;
|
||||
}
|
||||
|
||||
export type Just<T> = Maybe<T> & {
|
||||
get(): T
|
||||
};
|
||||
|
||||
export function just<T>(value: T): Just<T> {
|
||||
return {
|
||||
isJust: () => true,
|
||||
get: () => value
|
||||
};
|
||||
}
|
||||
|
||||
export function nothing<T>(): Maybe<T> {
|
||||
return {
|
||||
isJust: () => false,
|
||||
};
|
||||
}
|
28
test/prelude/maybe.ts
Normal file
28
test/prelude/maybe.ts
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Tests of Maybe
|
||||
*
|
||||
* How to run the tests:
|
||||
* > mocha test/prelude/maybe.ts --require ts-node/register
|
||||
*
|
||||
* To specify test:
|
||||
* > mocha test/prelude/maybe.ts --require ts-node/register -g 'test name'
|
||||
*/
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { just, nothing } from '../../src/prelude/maybe';
|
||||
|
||||
describe('just', () => {
|
||||
it('has a value', () => {
|
||||
assert.deepStrictEqual(just(3).isJust(), true);
|
||||
});
|
||||
|
||||
it('has the inverse called get', () => {
|
||||
assert.deepStrictEqual(just(3).get(), 3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('nothing', () => {
|
||||
it('has no value', () => {
|
||||
assert.deepStrictEqual(nothing().isJust(), false);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user