-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
211 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,270 @@ | ||
# Utils | ||
|
||
`Utils` module provides generic helper functions. | ||
`Utils` module provides generic helper functions. It includes functions to check the type of a value. These functions work cross-realm (e.g., across vm contexts, iframes, etc.). | ||
|
||
## `isNullish` | ||
## `isBlob` | ||
|
||
Checks if the given value is `null` or `undefined`. | ||
Checks if a value is a `Blob`. | ||
|
||
```ts | ||
function isNullish(value: unknown): value is undefined | null | ||
function isBlob(value: unknown): value is Blob | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `Blob`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isNullish } from '@globalbrain/sefirot/lib/support/Utils' | ||
import { isBlob } from '@globalbrain/sefirot/lib/support/Utils' | ||
isNullish(undefined) // <- true | ||
isNullish(null) // <- true | ||
isNullish('') // <- false | ||
const blob = new Blob() | ||
console.log(isBlob(blob)) // true | ||
console.log(isBlob({})) // false | ||
``` | ||
|
||
## `isString` | ||
## `isDate` | ||
|
||
Checks if the given value is `string`. | ||
Checks if a value is a `Date`. | ||
|
||
```ts | ||
function isString(value: unknown): value is string | ||
function isDate(value: unknown): value is Date | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `Date`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isString } from '@globalbrain/sefirot/lib/support/Utils' | ||
import { isDate } from '@globalbrain/sefirot/lib/support/Utils' | ||
const date = new Date() | ||
console.log(isDate(date)) // true | ||
console.log(isDate('2023-01-01')) // false | ||
``` | ||
|
||
## `isError` | ||
|
||
Checks if a value is an `Error`. | ||
|
||
```ts | ||
function isError(value: unknown): value is Error | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is an `Error`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isError } from '@globalbrain/sefirot/lib/support/Utils' | ||
const error = new Error('Something went wrong') | ||
console.log(isError(error)) // true | ||
console.log(isError({})) // false | ||
``` | ||
|
||
## `isFile` | ||
|
||
Checks if a value is a `File`. | ||
|
||
```ts | ||
function isFile(value: unknown): value is File | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `File`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isFile } from '@globalbrain/sefirot/lib/support/Utils' | ||
const file = new File(['content'], 'file.txt') | ||
console.log(isFile(file)) // true | ||
console.log(isFile({})) // false | ||
``` | ||
|
||
## `isFormData` | ||
|
||
Checks if a value is `FormData`. | ||
|
||
```ts | ||
function isFormData(value: unknown): value is FormData | ||
``` | ||
|
||
isString('abc') // <- true | ||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is `FormData`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isFormData } from '@globalbrain/sefirot/lib/support/Utils' | ||
const formData = new FormData() | ||
console.log(isFormData(formData)) // true | ||
console.log(isFormData({})) // false | ||
``` | ||
|
||
## `isNumber` | ||
|
||
Checks if the given value is `number`. | ||
Checks if a value is a `number`. | ||
|
||
```ts | ||
function isNumber(value: unknown): value is number | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `number`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isNumber } from '@globalbrain/sefirot/lib/support/Utils' | ||
isNumber(123) // <- true | ||
console.log(isNumber(123)) // true | ||
console.log(isNumber('123')) // false | ||
``` | ||
|
||
## `isArray` | ||
## `isObject` | ||
|
||
Checks if the given value is `array`. | ||
Checks if a value is a plain object. | ||
|
||
```ts | ||
function isArray(value: unknown): value is unknown[] | ||
function isObject(value: unknown): value is Record<string, unknown> | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a plain object. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isArray } from '@globalbrain/sefirot/lib/support/Utils' | ||
import { isObject } from '@globalbrain/sefirot/lib/support/Utils' | ||
isArray([1, 2, 3]) // <- true | ||
console.log(isObject({})) // true | ||
console.log(isObject(new Date())) // false | ||
``` | ||
|
||
## `isObject` | ||
## `isRequest` | ||
|
||
Checks if the given value is `object`. this function will return `false` for `null` and `array`. | ||
Checks if a value is a `Request`. | ||
|
||
```ts | ||
function isObject(value: unknown): value is Record<string, any> | ||
function isRequest(value: unknown): value is Request | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `Request`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isObject } from '@globalbrain/sefirot/lib/support/Utils' | ||
import { isRequest } from '@globalbrain/sefirot/lib/support/Utils' | ||
isObject({ foo: 'bar' }) // <- true | ||
isObject([1, 2, 3]) // <- false | ||
isObject(null) // <- false | ||
const request = new Request('https://example.com') | ||
console.log(isRequest(request)) // true | ||
console.log(isRequest({})) // false | ||
``` | ||
|
||
## `isFile` | ||
## `isResponse` | ||
|
||
Checks if the given value is `File`. | ||
Checks if a value is a `Response`. | ||
|
||
```ts | ||
function isFile(value: unknown): value is File | ||
function isResponse(value: unknown): value is Response | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `Response`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isFile } from '@globalbrain/sefirot/lib/support/Utils' | ||
import { isResponse } from '@globalbrain/sefirot/lib/support/Utils' | ||
const response = new Response() | ||
console.log(isResponse(response)) // true | ||
console.log(isResponse({})) // false | ||
``` | ||
|
||
## `isString` | ||
|
||
Checks if a value is a `string`. | ||
|
||
```ts | ||
function isString(value: unknown): value is string | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `value`: The value to check. | ||
|
||
**Returns:** | ||
|
||
- `true` if the value is a `string`. | ||
- `false` otherwise. | ||
|
||
**Example:** | ||
|
||
```ts | ||
import { isString } from '@globalbrain/sefirot/lib/support/Utils' | ||
isFile(file) | ||
console.log(isString('Hello')) // true | ||
console.log(isString(123)) // false | ||
``` |