Skip to content

Commit

Permalink
feat(resolveRef): new function (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 6, 2022
1 parent dd94d76 commit 29fd5ef
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 10 deletions.
6 changes: 5 additions & 1 deletion packages/contributors.json
Expand Up @@ -166,6 +166,7 @@
"phaust",
"marktnoonan",
"dm4t2",
"melishev",
"mauriciabad",
"mxmvshnvsk",
"AldeonMoriak",
Expand All @@ -185,6 +186,7 @@
"a1xon",
"octref",
"praburangki",
"preeteshjain",
"QiroNT",
"ramonakira",
"Redemption198",
Expand Down Expand Up @@ -221,12 +223,14 @@
"iGalaxyz",
"winter-ice",
"monkeywithacupcake",
"katsuyaU",
"kagurazaka-0",
"koheing",
"kongmoumou",
"laozei6401",
"leovoon",
"likeswinds",
"lxhyl",
"lxnelyclxud",
"lzdFeiFei",
"meteorlxy",
"odex21",
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/index.ts
Expand Up @@ -21,6 +21,8 @@ export * from './refDebounced'
export * from './refDefault'
export * from './refThrottled'
export * from './refWithControl'
export * from './resolveRef'
export * from './resolveUnref'
export * from './set'
export * from './syncRef'
export * from './syncRefs'
Expand Down
20 changes: 20 additions & 0 deletions packages/shared/resolveRef/index.md
@@ -0,0 +1,20 @@
---
category: Utilities
related: resolveUnref
---

# resolveRef

Normalize value/ref/getter to `ref` or `computed`.

## Usage

```ts
import { resolveRef } from '@vueuse/core'

const foo = ref('hi')

const a = resolveRef(0) // Ref<number>
const b = resolveRef(foo) // Ref<string>
const c = resolveRef(() => 'hi') // ComputedRef<string>
```
15 changes: 15 additions & 0 deletions packages/shared/resolveRef/index.ts
@@ -0,0 +1,15 @@
import type { ComputedRef, Ref } from 'vue-demi'
import { computed, ref } from 'vue-demi'
import type { MaybeComputedRef, MaybeRef } from '../utils'

/**
* Normalize value/ref/getter to `ref` or `computed`.
*/
export function resolveRef<T>(r: MaybeComputedRef<T>): ComputedRef<T>
export function resolveRef<T>(r: MaybeRef<T>): Ref<T>
export function resolveRef<T>(r: MaybeComputedRef<T>) {
return typeof r === 'function'
? computed<T>(r as any)
: ref(r)
}

20 changes: 20 additions & 0 deletions packages/shared/resolveUnref/index.md
@@ -0,0 +1,20 @@
---
category: Utilities
related: resolveRef
---

# resolveUnref

Get the value of value/ref/getter.

## Usage

```ts
import { resolveUnref } from '@vueuse/core'

const foo = ref('hi')

const a = resolveUnref(0) // 0
const b = resolveUnref(foo) // 'hi'
const c = resolveUnref(() => 'hi') // 'hi'
```
11 changes: 11 additions & 0 deletions packages/shared/resolveUnref/index.ts
@@ -0,0 +1,11 @@
import { unref } from 'vue-demi'
import type { MaybeComputedRef } from '../utils'

/**
* Normalize value/ref/getter to `ref` or `computed`.
*/
export function resolveUnref<T>(r: MaybeComputedRef<T>): T {
return typeof r === 'function'
? (r as any)()
: unref(r)
}
29 changes: 20 additions & 9 deletions packages/shared/utils/types.ts
Expand Up @@ -5,15 +5,6 @@ import type { Ref, WatchOptions, WatchSource } from 'vue-demi'
*/
export type Fn = () => void

/**
* Maybe it's a ref, or not.
*
* ```ts
* type MaybeRef<T> = T | Ref<T>
* ```
*/
export type MaybeRef<T> = T | Ref<T>

/**
* A ref that allow to set null or undefined
*/
Expand All @@ -27,6 +18,26 @@ export type RemovableRef<T> = Omit<Ref<T>, 'value'> & {
*/
export type RemoveableRef<T> = RemovableRef<T>

/**
* Maybe it's a ref, or a plain value
*
* ```ts
* type MaybeRef<T> = T | Ref<T>
* ```
*/
export type MaybeRef<T> = T | Ref<T>

/**
* Maybe it's a ref, or a getter function
*
* ```ts
* type MaybeRef<T> = T | Ref<T>
* ```
*/
export type MaybeComputedRef<T> = T extends Function
? never
: (() => T) | MaybeRef<T>

/**
* Make all the nested attributes of an object or array to MaybeRef<T>
*
Expand Down

0 comments on commit 29fd5ef

Please sign in to comment.