Skip to content

Commit 3a88b97

Browse files
committed
feat(predicate): add is utils
1 parent f21a86f commit 3a88b97

26 files changed

+444
-0
lines changed

docs/reference/predicate/isBoolean.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# isBoolean
2+
3+
The `isBoolean` function checks if a value is a boolean.
4+
5+
## Signature
6+
7+
```typescript
8+
function isBoolean(val: any): val is boolean
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `unknown` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is boolean, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isBoolean } from 'js-utils-es/predicate';
23+
24+
isBoolean(1); // false
25+
isBoolean(true); // true
26+
```

docs/reference/predicate/isDate.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# isDate
2+
3+
The `isDate` function checks if a value is a `Date` object.
4+
5+
## Signature
6+
7+
```typescript
8+
function isDate(val: any): val is Date
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `unknown` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a `Date`, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isDate } from 'js-utils-es/predicate';
23+
24+
isDate(1); // false
25+
isDate(true); // false
26+
isDate(new Date()); // true
27+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# isFunction
2+
3+
The `isFunction` function checks if a value is a `Function` object.
4+
5+
## Signature
6+
7+
```typescript
8+
function isFunction(val: any)
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `any` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a `Function`, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isFunction } from 'js-utils-es/predicate';
23+
24+
isFunction(1); // false
25+
isFunction(true); // false
26+
isFunction(new Date()); // false
27+
isFunction(() => {}); // true
28+
```

docs/reference/predicate/isNumber.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# isNumber
2+
3+
The `isNumber` function checks if a value is a `number`.
4+
5+
## Signature
6+
7+
```typescript
8+
function isNumber(val: any): val is number
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `any` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a `number`, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isNumber } from 'js-utils-es/predicate';
23+
24+
isNumber(1); // true
25+
isNumber(true); // false
26+
isNumber(new Date()); // false
27+
isNumber(() => {}); // false
28+
```

docs/reference/predicate/isObject.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# isObject
2+
3+
The `isObject` function checks if a value is a plain object.
4+
5+
## Signature
6+
7+
```typescript
8+
function isObject(val: any): val is object
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `any` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a plain object, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isObject } from 'js-utils-es/predicate';
23+
24+
isObject({}); // true
25+
26+
isObject(1); // false
27+
isObject(true); // false
28+
isObject(new Date()); // false
29+
isObject(() => {}); // false
30+
```

docs/reference/predicate/isRegExp.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# isRegExp
2+
3+
The `isRegExp` function checks if a value is a `RegExp`.
4+
5+
## Signature
6+
7+
```typescript
8+
function isRegExp(val: any): val is RegExp
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `any` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a `RegExp`, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isRegExp } from 'js-utils-es/predicate';
23+
24+
isRegExp(/[az]/g); // true
25+
26+
isRegExp(1); // false
27+
isRegExp(true); // false
28+
isRegExp(new Date()); // false
29+
isRegExp(() => {}); // false
30+
```

docs/reference/predicate/isString.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# isString
2+
3+
The `isString` function checks if a value is a `string`.
4+
5+
## Signature
6+
7+
```typescript
8+
function isString(val: unknown): val is string
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `any` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a `string`, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isString } from 'js-utils-es/predicate';
23+
24+
isString('1'); // true
25+
26+
isString(1); // false
27+
isString(true); // false
28+
isString(new Date()); // false
29+
isString(() => {}); // false
30+
```

docs/reference/predicate/isWindow.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# isWindow
2+
3+
The `isWindow` function checks if a value is a `window` object.
4+
5+
## Signature
6+
7+
```typescript
8+
function isWindow(val: any): boolean
9+
```
10+
11+
## Parameters
12+
13+
- **`val`**: `any` - The value to check.
14+
15+
## Returns
16+
17+
- **`boolean`** - Returns `true` if the value is a `window` object, otherwise `false`.
18+
19+
## Examples
20+
21+
```typescript
22+
import { isWindow } from 'js-utils-es/predicate';
23+
24+
isWindow(window); // true
25+
isWindow(true); // false
26+
isWindow(new Date()); // false
27+
isWindow(() => {}); // false
28+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable prefer-regex-literals */
2+
import { describe, expect, it } from 'vitest';
3+
import { toString } from './toString';
4+
5+
describe('toString', () => {
6+
it('case1', async () => {
7+
expect(toString(1)).toEqual('[object Number]');
8+
});
9+
10+
it('case2', async () => {
11+
expect(toString('1')).toEqual('[object String]');
12+
});
13+
14+
it('case3', async () => {
15+
expect(toString(true)).toEqual('[object Boolean]');
16+
});
17+
18+
it('case4', async () => {
19+
expect(toString([])).toEqual('[object Array]');
20+
});
21+
22+
it('case5', async () => {
23+
expect(toString({})).toEqual('[object Object]');
24+
});
25+
26+
it('case6', async () => {
27+
expect(toString(new Date())).toEqual('[object Date]');
28+
});
29+
30+
it('case7', async () => {
31+
expect(toString(new RegExp(/[a-z]/g))).toEqual('[object RegExp]');
32+
});
33+
});

src/predicate/_internal/toString.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Convert value to string.
3+
* @param {any} v - The value to convert to string.
4+
* @returns {string} - return string
5+
*/
6+
7+
export const toString = (v: any) => Object.prototype.toString.call(v);

0 commit comments

Comments
 (0)