-
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.
Merge pull request #63 from chomosuke/shangzhel/lifecycle
Implement app lifecycle
- Loading branch information
Showing
15 changed files
with
829 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,85 @@ | ||
import { ensureArray, ensureObject, ensureType } from '../ensure'; | ||
|
||
const map = { | ||
undefined, | ||
number: Number(), | ||
bigint: BigInt(Number()), | ||
boolean: Boolean(), | ||
string: String(), | ||
symbol: Symbol('symbol'), | ||
object: Object(), | ||
function() { }, | ||
}; | ||
|
||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
function asTypeTag(tag: string): any { | ||
return tag; | ||
} | ||
|
||
describe('ensure tests', () => { | ||
describe('ensureType tests', () => { | ||
describe('Success tests', () => { | ||
Object | ||
.entries(map) | ||
.forEach(([k, v]) => test(k, () => { | ||
expect(() => ensureType(v, asTypeTag(k))).not.toThrow(); | ||
})); | ||
|
||
test('null', () => { | ||
expect(() => ensureType(null, 'object')).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('Fail tests', () => { | ||
Object | ||
.entries(map) | ||
.forEach(([k0, v]) => describe(k0, () => { | ||
Object | ||
.keys(map) | ||
.filter((k1) => k0 !== k1) | ||
.map((k1) => test(k1, () => { | ||
expect(() => ensureType(v, asTypeTag(k1))).toThrow(); | ||
})); | ||
})); | ||
|
||
describe('null', () => { | ||
Object | ||
.keys(map) | ||
.filter((k) => k !== 'object') | ||
.map((k) => test(k, () => { | ||
expect(() => ensureType(null, asTypeTag(k))).toThrow(); | ||
})); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('ensureObject tests', () => { | ||
test('Success test', () => { | ||
expect(() => ensureObject(Object())).not.toThrow(); | ||
}); | ||
|
||
describe('Fail tests', () => { | ||
Object | ||
.entries(map) | ||
.filter(([k]) => k !== 'object') | ||
.map(([k, v]) => test(k, () => { | ||
expect(() => ensureObject(v)).toThrow(); | ||
})); | ||
}); | ||
}); | ||
|
||
describe('ensureArray tests', () => { | ||
test('Success test', () => { | ||
expect(() => ensureArray([])).not.toThrow(); | ||
}); | ||
|
||
describe('Fail tests', () => { | ||
Object | ||
.entries(map) | ||
.concat([['null', null]]) | ||
.map(([k, v]) => test(k, () => { | ||
expect(() => ensureArray(v)).toThrow(); | ||
})); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { TypeOfTag } from 'typescript'; | ||
|
||
export type AnyUnknown = { [k: string]: unknown }; | ||
|
||
export function ensureType(value: unknown, type: 'undefined'): undefined; | ||
export function ensureType(value: unknown, type: 'number'): number; | ||
export function ensureType(value: unknown, type: 'bigint'): bigint; | ||
export function ensureType(value: unknown, type: 'boolean'): boolean; | ||
export function ensureType(value: unknown, type: 'string'): string; | ||
export function ensureType(value: unknown, type: 'symbol'): symbol; | ||
export function ensureType(value: unknown, type: 'object'): AnyUnknown | null; | ||
export function ensureType(value: unknown, type: 'function'): CallableFunction; | ||
|
||
export function ensureType(value: unknown, type: TypeOfTag): unknown { | ||
if (typeof value !== type) throw new Error(`typeof value is not ${type}`); | ||
|
||
return value; | ||
} | ||
|
||
export function ensureObject(value: unknown): AnyUnknown { | ||
if (value == null) throw new Error('value is nullish'); | ||
|
||
const ensured = ensureType(value, 'object'); | ||
if (ensured == null) throw new Error('ensured is null'); | ||
|
||
return ensured; | ||
} | ||
|
||
export function ensureArray(value: unknown): unknown[] { | ||
if (!(value instanceof Array)) throw new Error(`value is not instanceof ${Array.name}`); | ||
|
||
return value; | ||
} |
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 +1,2 @@ | ||
export * from './api-types'; | ||
export * from './ensure'; |
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.