Skip to content

Commit

Permalink
add is.urlSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
kaysonwu committed Sep 11, 2021
1 parent 2823166 commit 26d668c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
5 changes: 5 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const objectTypeNames = [
'Promise',
'URL',
'FormData',
'URLSearchParams',
'HTMLElement',
...typedArrayTypeNames
] as const;
Expand Down Expand Up @@ -365,6 +366,8 @@ is.nonEmptyMap = <Key = unknown, Value = unknown>(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>('FormData')(value);

is.urlSearchParams = (value: unknown): value is URLSearchParams => isObjectOfType<URLSearchParams>('URLSearchParams')(value);

export type Predicate = (value: unknown) => boolean;

type ArrayMethod = (fn: (value: unknown, index: number, array: unknown[]) => boolean, thisArg?: unknown) => boolean;
Expand Down Expand Up @@ -526,6 +529,7 @@ interface Assert {
nonEmptyMap: <Key = unknown, Value = unknown>(value: unknown) => asserts value is Map<Key, Value>;
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;
Expand Down Expand Up @@ -625,6 +629,7 @@ export const assert: Assert = {
nonEmptyMap: <Key = unknown, Value = unknown>(value: unknown): asserts value is Map<Key, Value> => 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),
Expand Down
21 changes: 21 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 26d668c

Please sign in to comment.