From 5b7ea154e649f269d128af61e7c47fbd5b5e9443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=EF=BC=88=E4=B9=A6=E7=94=9F=EF=BC=89?= Date: Sun, 27 Feb 2022 15:16:25 +0800 Subject: [PATCH] Add `.isBlob()` (#162) Co-authored-by: Sindre Sorhus --- readme.md | 1 + source/index.ts | 4 ++++ test/test.ts | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/readme.md b/readme.md index 12a56fd..05a291a 100644 --- a/readme.md +++ b/readme.md @@ -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). diff --git a/source/index.ts b/source/index.ts index 504e5e1..5ce333d 100644 --- a/source/index.ts +++ b/source/index.ts @@ -34,6 +34,7 @@ const objectTypeNames = [ 'Observable', 'Array', 'Buffer', + 'Blob', 'Object', 'RegExp', 'Date', @@ -178,6 +179,7 @@ is.array = (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')(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)); @@ -470,6 +472,7 @@ interface Assert { numericString: (value: unknown) => asserts value is string; array: (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: (value: unknown) => asserts value is Record; iterable: (value: unknown) => asserts value is Iterable; @@ -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: (value: unknown): asserts value is Iterable => assertType(is.iterable(value), AssertionTypeDescription.iterable, value), diff --git a/test/test.ts b/test/test.ts index 0556431..71a4a9d 100644 --- a/test/test.ts +++ b/test/test.ts @@ -170,6 +170,14 @@ const types = new Map([ ], typename: 'Buffer' }], + ['blob', { + is: is.blob, + assert: assert.blob, + fixtures: [ + new window.Blob() + ], + typename: 'Blob' + }], ['object', { is: is.object, assert: assert.object, @@ -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');