diff --git a/readme.md b/readme.md index 76f167d..c9b5463 100644 --- a/readme.md +++ b/readme.md @@ -432,6 +432,28 @@ Returns `true` if `value` is an odd integer. Returns `true` if `value` can be used as an object property key (either `string`, `number`, or `symbol`). +##### .formData(value) + +Returns `true` if `value` is an instance of the [`FormData` class](https://developer.mozilla.org/en-US/docs/Web/API/FormData). + +```js +const data = new FormData(); + +is.formData(data); +//=> true +``` + +##### .urlSearchParams(value) + +Returns `true` if `value` is an instance of the [`URLSearchParams` class](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). + +```js +const searchParams = new URLSearchParams(); + +is.urlSearchParams(searchParams); +//=> true +``` + ##### .any(predicate | predicate[], ...values) Using a single `predicate` argument, returns `true` if **any** of the input `values` returns true in the `predicate`: diff --git a/source/index.ts b/source/index.ts index 875794a..86499f1 100644 --- a/source/index.ts +++ b/source/index.ts @@ -47,6 +47,8 @@ const objectTypeNames = [ 'DataView', 'Promise', 'URL', + 'FormData', + 'URLSearchParams', 'HTMLElement', ...typedArrayTypeNames ] as const; @@ -362,6 +364,9 @@ is.nonEmptyMap = (value: unknown): value is Map< // `PropertyKey` is any value that can be used as an object key (string, number, or symbol) is.propertyKey = (value: unknown): value is PropertyKey => is.any([is.string, is.number, is.symbol], value); +is.formData = (value: unknown): value is FormData => isObjectOfType('FormData')(value); + +is.urlSearchParams = (value: unknown): value is URLSearchParams => isObjectOfType('URLSearchParams')(value); export type Predicate = (value: unknown) => boolean; @@ -523,6 +528,8 @@ interface Assert { emptyMap: (value: unknown) => asserts value is Map; nonEmptyMap: (value: unknown) => asserts value is Map; propertyKey: (value: unknown) => asserts value is PropertyKey; + formData: (value: unknown) => asserts value is FormData; + urlSearchParams: (value: unknown) => asserts value is URLSearchParams; // Numbers. evenInteger: (value: number) => asserts value is number; @@ -621,6 +628,8 @@ export const assert: Assert = { emptyMap: (value: unknown): asserts value is Map => assertType(is.emptyMap(value), AssertionTypeDescription.emptyMap, value), nonEmptyMap: (value: unknown): asserts value is Map => assertType(is.nonEmptyMap(value), AssertionTypeDescription.nonEmptyMap, value), propertyKey: (value: unknown): asserts value is number => assertType(is.propertyKey(value), 'PropertyKey', value), + formData: (value: unknown): asserts value is FormData => assertType(is.formData(value), 'FormData', value), + urlSearchParams: (value: unknown): asserts value is URLSearchParams => assertType(is.urlSearchParams(value), 'URLSearchParams', value), // Numbers. evenInteger: (value: number): asserts value is number => assertType(is.evenInteger(value), AssertionTypeDescription.evenInteger, value), diff --git a/test/test.ts b/test/test.ts index 6c2ae57..87455db 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1621,6 +1621,48 @@ test('is.all', t => { }); }); +test('is.formData', t => { + const data = new window.FormData(); + t.true(is.formData(data)); + t.false(is.formData({})); + t.false(is.formData(undefined)); + t.false(is.formData(null)); + + t.notThrows(() => { + assert.formData(data); + }); + t.throws(() => { + assert.formData({}); + }); + t.throws(() => { + assert.formData(undefined); + }); + t.throws(() => { + assert.formData(null); + }); +}); + +test('is.urlSearchParams', t => { + const searchParams = new URLSearchParams(); + t.true(is.urlSearchParams(searchParams)); + t.false(is.urlSearchParams({})); + t.false(is.urlSearchParams(undefined)); + t.false(is.urlSearchParams(null)); + + t.notThrows(() => { + assert.urlSearchParams(searchParams); + }); + t.throws(() => { + assert.urlSearchParams({}); + }); + t.throws(() => { + assert.urlSearchParams(undefined); + }); + t.throws(() => { + assert.urlSearchParams(null); + }); +}); + test('assert', t => { // Contrived test showing that TypeScript acknowledges the type assertion in `assert.number()`. // Real--world usage includes asserting user input, but here we use a random number/string generator.