forked from orbit-oss/is
Add is.enumCase and assert.enumCase (#150)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
parent
f5cc764e22
commit
a5b4017d5e
3 changed files with 39 additions and 0 deletions
19
readme.md
19
readme.md
|
|
@ -212,6 +212,25 @@ is.boundFunction(function () {});
|
||||||
##### .sharedArrayBuffer(value)
|
##### .sharedArrayBuffer(value)
|
||||||
##### .dataView(value)
|
##### .dataView(value)
|
||||||
|
|
||||||
|
##### .enumCase(value, enum)
|
||||||
|
|
||||||
|
**TypeScript-only**
|
||||||
|
|
||||||
|
Returns `true` if `value` is a member of `enum`.
|
||||||
|
|
||||||
|
```ts
|
||||||
|
enum Direction {
|
||||||
|
Ascending = 'ascending',
|
||||||
|
Descending = 'descending'
|
||||||
|
}
|
||||||
|
|
||||||
|
is.enumCase('ascending', Direction);
|
||||||
|
//=> true
|
||||||
|
|
||||||
|
is.enumCase('other', Direction);
|
||||||
|
//=> false
|
||||||
|
```
|
||||||
|
|
||||||
#### Emptiness
|
#### Emptiness
|
||||||
|
|
||||||
##### .emptyString(value)
|
##### .emptyString(value)
|
||||||
|
|
|
||||||
|
|
@ -230,6 +230,7 @@ is.bigUint64Array = isObjectOfType<BigUint64Array>('BigUint64Array');
|
||||||
is.arrayBuffer = isObjectOfType<ArrayBuffer>('ArrayBuffer');
|
is.arrayBuffer = isObjectOfType<ArrayBuffer>('ArrayBuffer');
|
||||||
is.sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>('SharedArrayBuffer');
|
is.sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>('SharedArrayBuffer');
|
||||||
is.dataView = isObjectOfType<DataView>('DataView');
|
is.dataView = isObjectOfType<DataView>('DataView');
|
||||||
|
is.enumCase = <T = unknown>(value: unknown, targetEnum: T) => Object.values(targetEnum).includes(value as string);
|
||||||
|
|
||||||
is.directInstanceOf = <T>(instance: unknown, class_: Class<T>): instance is T => Object.getPrototypeOf(instance) === class_.prototype;
|
is.directInstanceOf = <T>(instance: unknown, class_: Class<T>): instance is T => Object.getPrototypeOf(instance) === class_.prototype;
|
||||||
is.urlInstance = (value: unknown): value is URL => isObjectOfType<URL>('URL')(value);
|
is.urlInstance = (value: unknown): value is URL => isObjectOfType<URL>('URL')(value);
|
||||||
|
|
@ -501,6 +502,7 @@ interface Assert {
|
||||||
arrayBuffer: (value: unknown) => asserts value is ArrayBuffer;
|
arrayBuffer: (value: unknown) => asserts value is ArrayBuffer;
|
||||||
sharedArrayBuffer: (value: unknown) => asserts value is SharedArrayBuffer;
|
sharedArrayBuffer: (value: unknown) => asserts value is SharedArrayBuffer;
|
||||||
dataView: (value: unknown) => asserts value is DataView;
|
dataView: (value: unknown) => asserts value is DataView;
|
||||||
|
enumCase: <T = unknown>(value: unknown, targetEnum: T) => asserts value is T[keyof T];
|
||||||
urlInstance: (value: unknown) => asserts value is URL;
|
urlInstance: (value: unknown) => asserts value is URL;
|
||||||
urlString: (value: unknown) => asserts value is string;
|
urlString: (value: unknown) => asserts value is string;
|
||||||
truthy: (value: unknown) => asserts value is unknown;
|
truthy: (value: unknown) => asserts value is unknown;
|
||||||
|
|
@ -601,6 +603,7 @@ export const assert: Assert = {
|
||||||
arrayBuffer: (value: unknown): asserts value is ArrayBuffer => assertType(is.arrayBuffer(value), 'ArrayBuffer', value),
|
arrayBuffer: (value: unknown): asserts value is ArrayBuffer => assertType(is.arrayBuffer(value), 'ArrayBuffer', value),
|
||||||
sharedArrayBuffer: (value: unknown): asserts value is SharedArrayBuffer => assertType(is.sharedArrayBuffer(value), 'SharedArrayBuffer', value),
|
sharedArrayBuffer: (value: unknown): asserts value is SharedArrayBuffer => assertType(is.sharedArrayBuffer(value), 'SharedArrayBuffer', value),
|
||||||
dataView: (value: unknown): asserts value is DataView => assertType(is.dataView(value), 'DataView', value),
|
dataView: (value: unknown): asserts value is DataView => assertType(is.dataView(value), 'DataView', value),
|
||||||
|
enumCase: <T = unknown>(value: unknown, targetEnum: T): asserts value is T[keyof T] => assertType(is.enumCase(value, targetEnum), 'EnumCase', value),
|
||||||
urlInstance: (value: unknown): asserts value is URL => assertType(is.urlInstance(value), 'URL', value),
|
urlInstance: (value: unknown): asserts value is URL => assertType(is.urlInstance(value), 'URL', value),
|
||||||
urlString: (value: unknown): asserts value is string => assertType(is.urlString(value), AssertionTypeDescription.urlString, value),
|
urlString: (value: unknown): asserts value is string => assertType(is.urlString(value), AssertionTypeDescription.urlString, value),
|
||||||
truthy: (value: unknown): asserts value is unknown => assertType(is.truthy(value), AssertionTypeDescription.truthy, value),
|
truthy: (value: unknown): asserts value is unknown => assertType(is.truthy(value), AssertionTypeDescription.truthy, value),
|
||||||
|
|
|
||||||
17
test/test.ts
17
test/test.ts
|
|
@ -832,6 +832,23 @@ test('is.dataView', t => {
|
||||||
testType(t, 'dataView');
|
testType(t, 'dataView');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('is.enumCase', t => {
|
||||||
|
enum NonNumericalEnum {
|
||||||
|
Key1 = 'key1',
|
||||||
|
Key2 = 'key2',
|
||||||
|
}
|
||||||
|
|
||||||
|
t.true(is.enumCase('key1', NonNumericalEnum));
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.enumCase('key1', NonNumericalEnum);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.false(is.enumCase('invalid', NonNumericalEnum));
|
||||||
|
t.throws(() => {
|
||||||
|
assert.enumCase('invalid', NonNumericalEnum);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('is.directInstanceOf', t => {
|
test('is.directInstanceOf', t => {
|
||||||
const error = new Error();
|
const error = new Error();
|
||||||
const errorSubclass = new ErrorSubclassFixture();
|
const errorSubclass = new ErrorSubclassFixture();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue