Skip to content

Commit

Permalink
Add is.formData and is.urlSearchParams (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaysonwu authored Sep 13, 2021
1 parent b007935 commit a8de3d6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
9 changes: 9 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const objectTypeNames = [
'DataView',
'Promise',
'URL',
'FormData',
'URLSearchParams',
'HTMLElement',
...typedArrayTypeNames
] as const;
Expand Down Expand Up @@ -362,6 +364,9 @@ is.nonEmptyMap = <Key = unknown, Value = unknown>(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>('FormData')(value);

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

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

Expand Down Expand Up @@ -523,6 +528,8 @@ interface Assert {
emptyMap: (value: unknown) => asserts value is Map<never, never>;
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 @@ -621,6 +628,8 @@ export const assert: Assert = {
emptyMap: (value: unknown): asserts value is Map<never, never> => assertType(is.emptyMap(value), AssertionTypeDescription.emptyMap, value),
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
42 changes: 42 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a8de3d6

Please sign in to comment.