From 26d668c9f1bc6e047ef2903fdbe01c89b2d7da6f Mon Sep 17 00:00:00 2001 From: kaysonwu <772663139@qq.com> Date: Sat, 11 Sep 2021 20:47:00 +0800 Subject: [PATCH] add is.urlSearchParams --- readme.md | 11 +++++++++++ source/index.ts | 5 +++++ test/test.ts | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/readme.md b/readme.md index a93800b..bbc0d9d 100644 --- a/readme.md +++ b/readme.md @@ -443,6 +443,17 @@ 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 data = new URLSearchParams(); + +is.urlSearchParams(data); +//=> 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 5ccd3c6..86499f1 100644 --- a/source/index.ts +++ b/source/index.ts @@ -48,6 +48,7 @@ const objectTypeNames = [ 'Promise', 'URL', 'FormData', + 'URLSearchParams', 'HTMLElement', ...typedArrayTypeNames ] as const; @@ -365,6 +366,8 @@ is.nonEmptyMap = (value: unknown): value is Map< 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; type ArrayMethod = (fn: (value: unknown, index: number, array: unknown[]) => boolean, thisArg?: unknown) => boolean; @@ -526,6 +529,7 @@ interface Assert { 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; @@ -625,6 +629,7 @@ export const assert: Assert = { 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 678f17e..accab31 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1642,6 +1642,27 @@ test('is.formData', t => { }); }); +test('is.urlSearchParams', t => { + const params = new window.URLSearchParams(); + t.true(is.urlSearchParams(params)); + t.false(is.urlSearchParams({})); + t.false(is.urlSearchParams(undefined)); + t.false(is.urlSearchParams(null)); + + t.notThrows(() => { + assert.urlSearchParams(params); + }); + 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.