Skip to content

Commit

Permalink
Add is.enumCase and assert.enumCase (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
olivierbeaulieu and sindresorhus authored Jan 17, 2022
1 parent f5cc764 commit a5b4017
Showing 3 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -212,6 +212,25 @@ is.boundFunction(function () {});
##### .sharedArrayBuffer(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

##### .emptyString(value)
3 changes: 3 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -230,6 +230,7 @@ is.bigUint64Array = isObjectOfType<BigUint64Array>('BigUint64Array');
is.arrayBuffer = isObjectOfType<ArrayBuffer>('ArrayBuffer');
is.sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>('SharedArrayBuffer');
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.urlInstance = (value: unknown): value is URL => isObjectOfType<URL>('URL')(value);
@@ -501,6 +502,7 @@ interface Assert {
arrayBuffer: (value: unknown) => asserts value is ArrayBuffer;
sharedArrayBuffer: (value: unknown) => asserts value is SharedArrayBuffer;
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;
urlString: (value: unknown) => asserts value is string;
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),
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),
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),
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),
17 changes: 17 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -832,6 +832,23 @@ test('is.dataView', t => {
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 => {
const error = new Error();
const errorSubclass = new ErrorSubclassFixture();

0 comments on commit a5b4017

Please sign in to comment.