Skip to content

Commit

Permalink
feat(docs): defered api and function page
Browse files Browse the repository at this point in the history
  • Loading branch information
Hfutsora committed May 11, 2023
1 parent 5656a48 commit c13e4ed
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"max-len": ["error", { "code": 200 }],
"prefer-rest-params": "off",
"prefer-const": "off",
"@typescript-eslint/no-empty-interface": "off"
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-empty-function": "off"
}
}
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const Modules = [
},

// utils
{
text: 'Function',
link: '/modules/Function'
},
{
text: 'Async',
link: '/modules/Async'
Expand Down
35 changes: 35 additions & 0 deletions docs/modules/Async.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,39 @@ Attempts to get a successful response from task no more than times times before
```ts
await retry(() => new Promise(r => r(1)), 3) ➔ 1
await retry(() => 2), { times: 3, interval: 300 }) ➔ 2
```

### deferred

```ts
type Thenable<Params extends any[], Data> = (
(...args: Params) => Promise<Data>) | ((...args: Params) => Data);
interface DeferredOptions<D = any> {
/**
* Optional delay in milliseconds before executing the function.
*
* @default 0
*/
delay?: number;
onError?: (e: unknown) => void;
onSuccess?: (data: D) => void;
}
interface DeferredReturn<Data, Params extends any[]> {
execute: (delay?: number, ...args: Params) => Promise<Data>;
}
/**
* @param promise The promise or thenable function to be executed.
* @param options Optional configuration for the deferred function.
* @returns An object with the execute method that can be called to execute the deferred function.
*/
<Data, Params extends any[] = []>(
promise: Promise<Data> | Thenable<Params, Data>,
options?: DeferredOptions<Data> | undefined) => DeferredReturn<Data, Params>;
```

Creates a deferred function that wraps a promise or a thenable function, allowing delayed execution and handling of success and error cases.

```ts
deferred(() => 1).execute().then(r => expect(r).toBe(1))
deferred(() => { throw 0 }).execute().catch(err => expect(err).toBe(0))
```
143 changes: 143 additions & 0 deletions docs/modules/Function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Function

Some useful utils.

## API

### lazy

```ts
Lazy<A> = () => A
```

Creates a lazy function.

```ts
lazy(1) ➔ () => 1
```

### noop

```ts
() => void
```

A no-operation funciton.

```ts
noop() ➔ undefined
```

### identity

```ts
<A>(a: A) => A
```

Identity.

```ts
identity(1) ➔ 1
```

### constant

```ts
<A>(a: A) => Lazy<A>
```

A thunk returns self.

```ts
constant(1)() ➔ 1
```

### constTrue

```ts
Lazy<boolean>
```

A thunk returns always `true`.

```ts
constTrue() ➔ true
```

### constFalse

```ts
Lazy<boolean>
```

A thunk returns always `false`.

```ts
constFalse() ➔ false
```

### constNull

```ts
Lazy<null>
```

A thunk returns always `null`.

```ts
constNull() ➔ null
```

### constUndefined

```ts
Lazy<undefined>
```

A thunk returns always `undefined`.

```ts
constUndefined() ➔ undefined
```

### constVoid

```ts
Lazy<void>
```

A thunk returns always `void`.

```ts
constVoid() ➔ undefined
```

### isNullable

```ts
(a: unknown) => a is null | undefined
```

Returns whether the value is null or undefined.

```ts
isNullable(null) ➔ true
isNullable(undefined) ➔ true
isNullable(0) ➔ false
```

### isNonNullable

```ts
// Exclude null and undefined from T
type NonNullable<T> = T & {}
<A>(a: A) => a is NonNullable<A>
```
Returns whether the value is `NonNullable`.
```ts
isNonNullable(null) ➔ false
isNonNullable(undefined) ➔ false
isNonNullable(0) ➔ true
```
2 changes: 1 addition & 1 deletion src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const constUndefined: Lazy<undefined> = constant(void 0)
/**
* A thunk returns always `void`.
*/
export const constVoid = constUndefined
export const constVoid: () => void = constUndefined

/**
* Returns whether the value is null or undefined.
Expand Down

0 comments on commit c13e4ed

Please sign in to comment.