-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rename useMax to useMinimum
- Loading branch information
Showing
9 changed files
with
124 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
export * from './logicAnd' | ||
export * from './logicNot' | ||
export * from './logicOr' | ||
export * from './useCeil' | ||
export * from './useClamp' | ||
export * from './useFloor' | ||
export * from './useMinimum' | ||
export * from './useProjection' | ||
export * from './useRound' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { useMinimum } from '@vueuse/math' | ||
const other = ref(10) | ||
const value = useMinimum(0, other) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div> | ||
other: | ||
<input v-model="other" type="number"> | ||
<button @click="other--"> | ||
other -= 1 | ||
</button> | ||
<button @click="other++"> | ||
other += 1 | ||
</button> | ||
</div> | ||
value:{{ value }} | ||
<div> | ||
<button @click="value--"> | ||
value -= 1 | ||
</button> | ||
<button @click="value++"> | ||
value += 1 | ||
</button> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
category: '@Math' | ||
--- | ||
|
||
# useMinimum | ||
|
||
Reactively access a value no greater than max. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useMinimum } from '@vueuse/math' | ||
|
||
const other = ref(10) | ||
const value = useMinimum(0, other) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ref } from 'vue-demi' | ||
import { useMin, useMinimum } from '.' | ||
|
||
describe('useClamp', () => { | ||
it('should be defined', () => { | ||
expect(useMin).toBeDefined() | ||
expect(useMinimum).toBeDefined() | ||
}) | ||
|
||
it('should accept numbers', () => { | ||
const v = useMinimum(0, 100) | ||
expect(v.value).toBe(100) | ||
}) | ||
|
||
it('should accept refs', () => { | ||
const value1 = ref(10) | ||
const value2 = ref(100) | ||
const value3 = ref(1000) | ||
|
||
const v = useMinimum(0, value1, value2, value3) | ||
|
||
expect(v.value).toBe(10) | ||
|
||
v.value = 1001 | ||
expect(v.value).toBe(10) | ||
|
||
value1.value = 9 | ||
expect(v.value).toBe(9) | ||
|
||
value2.value = 8 | ||
expect(v.value).toBe(8) | ||
|
||
value3.value = 7 | ||
expect(v.value).toBe(7) | ||
}) | ||
|
||
it('should accept numbers and refs', () => { | ||
const value1 = 10 | ||
const value2 = ref(100) | ||
|
||
const v = useMinimum(0, value1, value2) | ||
|
||
expect(v.value).toBe(10) | ||
|
||
value2.value = 9 | ||
expect(v.value).toBe(9) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Ref } from 'vue-demi' | ||
import { computed, ref } from 'vue-demi' | ||
import type { MaybeComputedRef, MaybeRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
|
||
/** | ||
* Reactively access a value no greater than max. | ||
* | ||
* @see https://vueuse.org/useMinimum | ||
* @param value1 | ||
* @param value2 | ||
*/ | ||
export function useMinimum(defaultValue: MaybeRef<number>, ...mins: MaybeComputedRef<number>[]): Ref<number> { | ||
const _value = ref(defaultValue) | ||
return computed<number>({ | ||
get() { | ||
return _value.value = Math.min(_value.value, ...mins.map(value => resolveUnref(value))) | ||
}, | ||
set(value) { | ||
_value.value = Math.min(value, ...mins.map(value => resolveUnref(value))) | ||
}, | ||
}) | ||
} | ||
|
||
export const useMin = useMinimum |