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

feat(resolveRef): new function #1743

Merged
merged 4 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: resolveRef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related: resolveUnref

antfu marked this conversation as resolved.
Show resolved Hide resolved
---

# 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: resolveUnref
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related: resolveRef

antfu marked this conversation as resolved.
Show resolved Hide resolved
---

# 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