Skip to content

Commit

Permalink
feat(and,or,not): new functions (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 19, 2021
1 parent c2f1dd6 commit 0f7a90e
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 10 deletions.
45 changes: 45 additions & 0 deletions packages/shared/and/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
category: Utilities
---

# and

`AND` condition for refs.

## Usage

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

const a = ref(true)
const b = ref(false)

whenever(and(a, b), () => {
console.log('both a and b are now truthy!')
})
```

## Related Functions

- `or`
- `not`
- `whenever`

<!--FOOTER_STARTS-->
## Type Declarations

```typescript
export declare function set<T>(ref: Ref<T>, value: T): void
export declare function set<O extends object, K extends keyof O>(
target: O,
key: K,
value: O[K]
): void
```

## Source

[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/set/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/set/index.md)


<!--FOOTER_ENDS-->
11 changes: 11 additions & 0 deletions packages/shared/and/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MaybeRef } from '../utils'
import { computed, ComputedRef, unref } from 'vue-demi'

/**
* `AND` conditions for refs.
*
* @link https://vueuse.org/and
*/
export function and(...args: MaybeRef<any>[]): ComputedRef<boolean> {
return computed(() => args.every(i => unref(i)))
}
10 changes: 0 additions & 10 deletions packages/shared/get/index.stories.tsx

This file was deleted.

44 changes: 44 additions & 0 deletions packages/shared/not/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
category: Utilities
---

# not

`NOT` condition for ref.

## Usage

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

const a = ref(true)

whenever(not(a), () => {
console.log('a is now falsy!')
})
```

## Related Functions

- `and`
- `or`
- `whenever`

<!--FOOTER_STARTS-->
## Type Declarations

```typescript
export declare function set<T>(ref: Ref<T>, value: T): void
export declare function set<O extends object, K extends keyof O>(
target: O,
key: K,
value: O[K]
): void
```

## Source

[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/set/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/set/index.md)


<!--FOOTER_ENDS-->
11 changes: 11 additions & 0 deletions packages/shared/not/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MaybeRef } from '../utils'
import { computed, ComputedRef, unref } from 'vue-demi'

/**
* `NOT` conditions for refs.
*
* @link https://vueuse.org/not
*/
export function not(v: MaybeRef<any>): ComputedRef<boolean> {
return computed(() => !unref(v))
}
45 changes: 45 additions & 0 deletions packages/shared/or/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
category: Utilities
---

# and

`OR` conditions for refs.

## Usage

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

const a = ref(true)
const b = ref(false)

whenever(or(a, b), () => {
console.log('either a or b is truthy!')
})
```

## Related Functions

- `and`
- `not`
- `whenever`

<!--FOOTER_STARTS-->
## Type Declarations

```typescript
export declare function set<T>(ref: Ref<T>, value: T): void
export declare function set<O extends object, K extends keyof O>(
target: O,
key: K,
value: O[K]
): void
```

## Source

[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/set/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/set/index.md)


<!--FOOTER_ENDS-->
11 changes: 11 additions & 0 deletions packages/shared/or/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MaybeRef } from '../utils'
import { computed, ComputedRef, unref } from 'vue-demi'

/**
* `OR` conditions for refs.
*
* @link https://vueuse.org/or
*/
export function or(...args: MaybeRef<any>[]): ComputedRef<boolean> {
return computed(() => args.some(i => unref(i)))
}

0 comments on commit 0f7a90e

Please sign in to comment.