Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
category: '@Math' | ||
--- | ||
|
||
# useMath | ||
|
||
Reactive `Math` methods. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useMath } from '@vueuse/math' | ||
|
||
const base = ref(2) | ||
const exponent = ref(3) | ||
const result = useMath('pow', base, exponent) // Ref<8> | ||
|
||
const num = ref(2) | ||
const root = useMath('sqrt', num) // Ref<1.4142135623730951> | ||
|
||
num.value = 4 | ||
console.log(root.value) // 2 | ||
``` |
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,29 @@ | ||
import { ref } from 'vue-demi' | ||
import { useMath } from '.' | ||
|
||
describe('useMath', () => { | ||
it('should be defined', () => { | ||
expect(useMath).toBeDefined() | ||
}) | ||
|
||
it('should accept numbers', () => { | ||
const v = useMath('pow', 2, 3) | ||
expect(v.value).toBe(8) | ||
}) | ||
|
||
it('should accept refs', () => { | ||
const base = ref(2) | ||
const exponent = ref(3) | ||
const result = useMath('pow', base, exponent) | ||
|
||
expect(result.value).toBe(8) | ||
|
||
const num = ref(4) | ||
const root = useMath('sqrt', num) | ||
|
||
expect(root.value).toBe(2) | ||
|
||
num.value = 16 | ||
expect(root.value).toBe(4) | ||
}) | ||
}) |
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 @@ | ||
import type { ArgumentsType, Reactified } from '@vueuse/shared' | ||
import { reactify } from '@vueuse/shared' | ||
|
||
export type UseMathKeys = keyof { [K in keyof Math as Math[K] extends (...args: any) => any ? K : never]: unknown } | ||
|
||
/** | ||
* Reactive `Math` methods. | ||
* | ||
* @see https://vueuse.org/useMath | ||
*/ | ||
export function useMath<K extends keyof Math>( | ||
key: K, | ||
...args: ArgumentsType<Reactified<Math[K], true>> | ||
): ReturnType<Reactified<Math[K], true>> { | ||
return reactify(Math[key] as any)(...args) as any | ||
} |
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