Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useTrunc): new function (#1838)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
4 changed files
with
76 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
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,18 @@ | ||
--- | ||
category: '@Math' | ||
--- | ||
|
||
# useTrunc | ||
|
||
Reactive `Math.trunc`. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useTrunc } from '@vueuse/math' | ||
|
||
const value1 = ref(0.95) | ||
const value2 = ref(-2.34) | ||
const result1 = useTrunc(value1) // 0 | ||
const result2 = useTrunc(value2) // -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,44 @@ | ||
import { ref } from 'vue-demi' | ||
import { useTrunc } from '.' | ||
|
||
// Returns: | ||
// 0 -> 0 | ||
// -0 -> -0 | ||
// 0.2 -> 0 | ||
// -0.2 -> -0 | ||
// 0.7 -> 0 | ||
// -0.7 -> -0 | ||
// Infinity -> Infinity | ||
// -Infinity -> -Infinity | ||
// NaN -> NaN | ||
// null -> 0 | ||
|
||
describe('useTrunk', () => { | ||
it('should be defined', () => { | ||
expect(useTrunc).toBeDefined() | ||
}) | ||
it('should work', () => { | ||
const base = ref(1.95) | ||
const result = useTrunc(base) | ||
expect(result.value).toBe(1) | ||
base.value = -7.004 | ||
expect(result.value).toBe(-7) | ||
|
||
base.value = 0 | ||
expect(result.value).toBe(0) | ||
base.value = -0 | ||
expect(result.value).toBe(-0) | ||
|
||
base.value = 0.2 | ||
expect(result.value).toBe(0) | ||
base.value = -0.2 | ||
expect(result.value).toBe(-0) | ||
|
||
base.value = Infinity | ||
expect(result.value).toBe(Infinity) | ||
base.value = -Infinity | ||
expect(result.value).toBe(-Infinity) | ||
base.value = NaN | ||
expect(result.value).toBe(NaN) | ||
}) | ||
}) |
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,13 @@ | ||
import type { ComputedRef } from 'vue-demi' | ||
import { computed } from 'vue-demi' | ||
import type { MaybeComputedRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
|
||
/** | ||
* Reactive `Math.trunc`. | ||
* | ||
* @see https://vueuse.org/useTrunc | ||
*/ | ||
export function useTrunc(value: MaybeComputedRef<number>): ComputedRef<number> { | ||
return computed<number>(() => Math.trunc(resolveUnref(value))) | ||
} |