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(useMax): new function #1829

Merged
merged 4 commits into from
Jul 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from './logicOr'
export * from './useCeil'
export * from './useClamp'
export * from './useFloor'
export * from './useMax'
export * from './useProjection'
export * from './useRound'
21 changes: 21 additions & 0 deletions packages/math/useMax/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
category: '@Math'
---

# useMax

Reactive `Math.max`.

## Usage

```ts
import { useMax } from '@vueuse/math'

const max1 = ref(10)
const max2 = ref(20)
const result = useMax(max1, max2)
// result.value is 20

max1.value = 50
// result.value is 50 now
```
53 changes: 53 additions & 0 deletions packages/math/useMax/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ref } from 'vue-demi'
import { useMax } from '.'

describe('useMax', () => {
it('should be defined', () => {
expect(useMax).toBeDefined()
})

it('should accept numbers', () => {
const v = useMax(50, 100)
expect(v.value).toBe(100)
})

it('should accept refs', () => {
const value1 = ref(10)
const value2 = ref(100)
const value3 = ref(1000)

const v = useMax(value1, value2, value3)
expect(v.value).toBe(1000)

value1.value = 2000
expect(v.value).toBe(2000)

value2.value = 2001
expect(v.value).toBe(2001)

value3.value = 2002
expect(v.value).toBe(2002)
})

it('should accept numbers and refs', () => {
const value1 = 10
const value2 = ref(100)

const v = useMax(50, value1, value2)

expect(v.value).toBe(100)

value2.value = 200
expect(v.value).toBe(200)
})

it('should accept single arg', () => {
const v = useMax(50)
expect(v.value).toBe(50)
})

it('should accept zero arg', () => {
const v = useMax()
expect(v.value).toBe(-Infinity)
})
})
15 changes: 15 additions & 0 deletions packages/math/useMax/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { computed } from 'vue-demi'
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref } from '@vueuse/shared'

/**
* Reactively get maximum of values.
*
* @see https://vueuse.org/useMax
* @param values
*/
export function useMax(...values: MaybeComputedRef<number>[]) {
return computed<number>(() =>
Math.max(...values.map(value => resolveUnref(value))),
)
}