Skip to content
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

[docs ] Util page documentation imrpovement #11

Merged
merged 1 commit into from
Sep 4, 2021
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
70 changes: 69 additions & 1 deletion packages/shared/utils/docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,72 @@ description: Common utils and helpers.

# Utils

Common utils and helpers.
Common utils and helpers, as functions and types.

## Functions

- `isClient` - Check if we're on the server or client side
- `unRef` - Accepts either a ref object or a dom node and returns a dom node
- `isRef` - Check if object is a react ref
- `isWindow` - is the object window
- `noop` - no operation
- `rand` - random number between a min and max number
- `isBoolean`
- `isFunction`
- `isNumber`
- `isString`
- `isObject`

## Types

- `Fn` - A function type
- `MaybeRef<T>` - Union type of react ref and dom node.


# Type Declarations

```typescript
/**
* Any function
*/
declare type Fn = () => void

/**
* Maybe it's a react ref, or a dom node.
*/
declare type MaybeRef<T> = T | MutableRefObject<T>

/**
* Accepts either a ref object or a dom node and returns a dom node
*
* @param target - ref or a dom node
* @returns dom node
*/
declare function unRef<T extends unknown = HTMLElement>(target: MaybeRef<T>): T

/**
* Check if we're on the server or client side
*/
declare const isClient: boolean

/**
* Check if object is a react ref
*/
declare const isRef: (obj: unknown) => boolean

declare const isBoolean: (val: any) => val is boolean

declare const isFunction: <T extends Function>(val: any) => val is T

declare const isNumber: (val: any) => val is number

declare const isString: (val: unknown) => val is string

declare const isObject: (val: any) => val is object

declare const isWindow: (val: any) => val is Window

declare const noop: () => void

declare const rand: (min: number, max: number) => number
```
6 changes: 6 additions & 0 deletions packages/shared/utils/is.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable @typescript-eslint/ban-types */

/**
* Check if we're on the server or client side
*/
export const isClient = typeof window !== 'undefined'

/**
* Check if object is a react ref
*/
export const isRef = (obj: unknown): boolean =>
obj !== null &&
typeof obj === 'object' &&
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { MutableRefObject } from 'react'
export type Fn = () => void

/**
* Maybe it's a ref, or not.
* Maybe it's a react ref, or a dom node.
*
* ```ts
* type MaybeRef<T> = T | MutableRefObject<T>
Expand Down