Skip to content

chore: update isNull and isUndefined and add more predicate utils #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/reference/predicate/isBoolean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# isBoolean

The `isBoolean` function checks if a value is a boolean.

## Signature

```typescript
function isBoolean(val: any): val is boolean
```

## Parameters

- **`val`**: `unknown` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is boolean, otherwise `false`.

## Examples

```typescript
import { isBoolean } from 'js-utils-es/predicate';

isBoolean(1); // false
isBoolean(true); // true
```
27 changes: 27 additions & 0 deletions docs/reference/predicate/isDate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# isDate

The `isDate` function checks if a value is a `Date` object.

## Signature

```typescript
function isDate(val: any): val is Date
```

## Parameters

- **`val`**: `unknown` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a `Date`, otherwise `false`.

## Examples

```typescript
import { isDate } from 'js-utils-es/predicate';

isDate(1); // false
isDate(true); // false
isDate(new Date()); // true
```
28 changes: 28 additions & 0 deletions docs/reference/predicate/isFunction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# isFunction

The `isFunction` function checks if a value is a `Function` object.

## Signature

```typescript
function isFunction(val: any)
```

## Parameters

- **`val`**: `any` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a `Function`, otherwise `false`.

## Examples

```typescript
import { isFunction } from 'js-utils-es/predicate';

isFunction(1); // false
isFunction(true); // false
isFunction(new Date()); // false
isFunction(() => {}); // true
```
2 changes: 1 addition & 1 deletion docs/reference/predicate/isNull.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This function can also serve as a type predicate in TypeScript, narrowing the ty
## Signature

```typescript
function isNull(x: unknown): x is null;
function isNull(val: any): val is null
```

### Parameters
Expand Down
28 changes: 28 additions & 0 deletions docs/reference/predicate/isNumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# isNumber

The `isNumber` function checks if a value is a `number`.

## Signature

```typescript
function isNumber(val: any): val is number
```

## Parameters

- **`val`**: `any` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a `number`, otherwise `false`.

## Examples

```typescript
import { isNumber } from 'js-utils-es/predicate';

isNumber(1); // true
isNumber(true); // false
isNumber(new Date()); // false
isNumber(() => {}); // false
```
30 changes: 30 additions & 0 deletions docs/reference/predicate/isObject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# isObject

The `isObject` function checks if a value is a plain object.

## Signature

```typescript
function isObject(val: any): val is object
```

## Parameters

- **`val`**: `any` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a plain object, otherwise `false`.

## Examples

```typescript
import { isObject } from 'js-utils-es/predicate';

isObject({}); // true

isObject(1); // false
isObject(true); // false
isObject(new Date()); // false
isObject(() => {}); // false
```
30 changes: 30 additions & 0 deletions docs/reference/predicate/isRegExp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# isRegExp

The `isRegExp` function checks if a value is a `RegExp`.

## Signature

```typescript
function isRegExp(val: any): val is RegExp
```

## Parameters

- **`val`**: `any` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a `RegExp`, otherwise `false`.

## Examples

```typescript
import { isRegExp } from 'js-utils-es/predicate';

isRegExp(/[az]/g); // true

isRegExp(1); // false
isRegExp(true); // false
isRegExp(new Date()); // false
isRegExp(() => {}); // false
```
30 changes: 30 additions & 0 deletions docs/reference/predicate/isString.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# isString

The `isString` function checks if a value is a `string`.

## Signature

```typescript
function isString(val: unknown): val is string
```

## Parameters

- **`val`**: `any` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a `string`, otherwise `false`.

## Examples

```typescript
import { isString } from 'js-utils-es/predicate';

isString('1'); // true

isString(1); // false
isString(true); // false
isString(new Date()); // false
isString(() => {}); // false
```
4 changes: 2 additions & 2 deletions docs/reference/predicate/isUndefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ This function can also serve as a type predicate in TypeScript, narrowing the ty
## Signature

```typescript
function isUndefined(x: unknown): x is undefined;
function isUndefined(val: any): val is undefined
```

### Parameters

- `x` (`unknown`): The value to test if it is `undefined`.
- `x` (`any`): The value to test if it is `undefined`.

### Returns

Expand Down
28 changes: 28 additions & 0 deletions docs/reference/predicate/isWindow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# isWindow

The `isWindow` function checks if a value is a `window` object.

## Signature

```typescript
function isWindow(val: any): boolean
```

## Parameters

- **`val`**: `any` - The value to check.

## Returns

- **`boolean`** - Returns `true` if the value is a `window` object, otherwise `false`.

## Examples

```typescript
import { isWindow } from 'js-utils-es/predicate';

isWindow(window); // true
isWindow(true); // false
isWindow(new Date()); // false
isWindow(() => {}); // false
```
33 changes: 33 additions & 0 deletions src/predicate/_internal/toString.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable prefer-regex-literals */
import { describe, expect, it } from 'vitest';
import { toString } from './toString';

describe('toString', () => {
it('case1', async () => {
expect(toString(1)).toEqual('[object Number]');
});

it('case2', async () => {
expect(toString('1')).toEqual('[object String]');
});

it('case3', async () => {
expect(toString(true)).toEqual('[object Boolean]');
});

it('case4', async () => {
expect(toString([])).toEqual('[object Array]');
});

it('case5', async () => {
expect(toString({})).toEqual('[object Object]');
});

it('case6', async () => {
expect(toString(new Date())).toEqual('[object Date]');
});

it('case7', async () => {
expect(toString(new RegExp(/[a-z]/g))).toEqual('[object RegExp]');
});
});
7 changes: 7 additions & 0 deletions src/predicate/_internal/toString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Convert value to string.
* @param {any} v - The value to convert to string.
* @returns {string} - return string
*/

export const toString = (v: any) => Object.prototype.toString.call(v);
14 changes: 14 additions & 0 deletions src/predicate/isBoolean.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest';
import { isBoolean } from './isBoolean';

describe('isBoolean', () => {
it('case1', () => {
expect(isBoolean(null)).toBe(false);
expect(isBoolean(undefined)).toBe(false);
expect(isBoolean({})).toBe(false);
expect(isBoolean('1')).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);
});
});
3 changes: 3 additions & 0 deletions src/predicate/isBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isBoolean(val: any): val is boolean {
return typeof val === 'boolean';
}
15 changes: 15 additions & 0 deletions src/predicate/isDate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import { isDate } from './isDate';

describe('isDate', () => {
it('case1', () => {
expect(isDate(null)).toBe(false);
expect(isDate(undefined)).toBe(false);
expect(isDate({})).toBe(false);
expect(isDate('1')).toBe(false);
expect(isDate(1)).toBe(false);
expect(isDate(true)).toBe(false);
expect(isDate(false)).toBe(false);
expect(isDate(new Date())).toBe(true);
});
});
5 changes: 5 additions & 0 deletions src/predicate/isDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { toString } from './_internal/toString';

export function isDate(val: any): val is Date {
return toString(val) === '[object Date]';
}
18 changes: 18 additions & 0 deletions src/predicate/isFunction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest';
import { isFunction } from './isFunction';

describe('isFunction', () => {
it('case1', () => {
expect(isFunction(null)).toBe(false);
expect(isFunction(undefined)).toBe(false);
expect(isFunction({})).toBe(false);
expect(isFunction('1')).toBe(false);
expect(isFunction(1)).toBe(false);
expect(isFunction(true)).toBe(false);
expect(isFunction(false)).toBe(false);
expect(isFunction(new Date())).toBe(false);
expect(isFunction(() => {
return 2;
})).toBe(true);
});
});
4 changes: 4 additions & 0 deletions src/predicate/isFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line ts/no-unsafe-function-type
export function isFunction<T extends Function>(val: any): val is T {
return typeof val === 'function';
}
7 changes: 5 additions & 2 deletions src/predicate/isNull.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toString } from './_internal/toString';

/**
* Checks if the given value is null.
*
Expand All @@ -18,6 +20,7 @@
* console.log(isNull(value2)); // false
* console.log(isNull(value3)); // false
*/
export function isNull(x: unknown): x is null {
return x === null;

export function isNull(val: any): val is null {
return toString(val) === '[object Null]';
}
Loading
Loading