Skip to content

Commit

Permalink
Add .isBlob() (#162)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
bigbigDreamer and sindresorhus authored Feb 27, 2022
1 parent 6cbefb9 commit 5b7ea15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ is.array(value, is.number); // Validate `value` is an array and all of its items
**Note:** TypeScript users must use `.function_()` because of a TypeScript naming limitation.

##### .buffer(value)
##### .blob(value)
##### .object(value)

Keep in mind that [functions are objects too](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions).
Expand Down
4 changes: 4 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const objectTypeNames = [
'Observable',
'Array',
'Buffer',
'Blob',
'Object',
'RegExp',
'Date',
Expand Down Expand Up @@ -178,6 +179,7 @@ is.array = <T = unknown>(value: unknown, assertion?: (value: T) => value is T):
};

is.buffer = (value: unknown): value is Buffer => (value as any)?.constructor?.isBuffer?.(value) ?? false;
is.blob = (value: unknown): value is Blob => isObjectOfType<Blob>('Blob')(value);

is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value);
is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value));
Expand Down Expand Up @@ -470,6 +472,7 @@ interface Assert {
numericString: (value: unknown) => asserts value is string;
array: <T = unknown>(value: unknown, assertion?: (element: unknown) => asserts element is T) => asserts value is T[];
buffer: (value: unknown) => asserts value is Buffer;
blob: (value: unknown) => asserts value is Blob;
nullOrUndefined: (value: unknown) => asserts value is null | undefined;
object: <Key extends keyof any = string, Value = unknown>(value: unknown) => asserts value is Record<Key, Value>;
iterable: <T = unknown>(value: unknown) => asserts value is Iterable<T>;
Expand Down Expand Up @@ -572,6 +575,7 @@ export const assert: Assert = {
}
},
buffer: (value: unknown): asserts value is Buffer => assertType(is.buffer(value), 'Buffer', value),
blob: (value: unknown): asserts value is Blob => assertType(is.blob(value), 'Blob', value),
nullOrUndefined: (value: unknown): asserts value is null | undefined => assertType(is.nullOrUndefined(value), AssertionTypeDescription.nullOrUndefined, value),
object: (value: unknown): asserts value is object => assertType(is.object(value), 'Object', value),
iterable: <T = unknown>(value: unknown): asserts value is Iterable<T> => assertType(is.iterable(value), AssertionTypeDescription.iterable, value),
Expand Down
12 changes: 12 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ const types = new Map<string, Test>([
],
typename: 'Buffer'
}],
['blob', {
is: is.blob,
assert: assert.blob,
fixtures: [
new window.Blob()
],
typename: 'Blob'
}],
['object', {
is: is.object,
assert: assert.object,
Expand Down Expand Up @@ -683,6 +691,10 @@ test('is.buffer', t => {
testType(t, 'buffer');
});

test('is.blob', t => {
testType(t, 'blob');
});

test('is.object', t => {
const testData = types.get('object');

Expand Down

0 comments on commit 5b7ea15

Please sign in to comment.