-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
108 additions
and
1 deletion.
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,18 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# useToNumber | ||
|
||
Reactively convert a string ref to number. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useToNumber } from '@vueuse/core' | ||
|
||
const str = ref('123') | ||
const number = useToNumber(str) | ||
|
||
number.value // 123 | ||
``` |
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,41 @@ | ||
import { ref } from 'vue-demi' | ||
import { useToNumber } from '.' | ||
|
||
describe('useToNumber', () => { | ||
it('default', () => { | ||
const value = ref<string | number>('123.345') | ||
const float = useToNumber(value) | ||
const int = useToNumber(value, { method: 'parseInt' }) | ||
|
||
expect(float.value).toBe(123.345) | ||
expect(int.value).toBe(123) | ||
|
||
value.value = 'hi' | ||
|
||
expect(float.value).toBe(NaN) | ||
expect(int.value).toBe(NaN) | ||
|
||
value.value = 123.4 | ||
|
||
expect(float.value).toBe(123.4) | ||
expect(int.value).toBe(123.4) | ||
|
||
value.value = '-43.53' | ||
|
||
expect(float.value).toBe(-43.53) | ||
expect(int.value).toBe(-43) | ||
}) | ||
|
||
it('radix', () => { | ||
const value = ref<string | number>('0xFA') | ||
const int = useToNumber(value, { method: 'parseInt', radix: 16 }) | ||
|
||
expect(int.value).toBe(250) | ||
}) | ||
|
||
it('nanToZero', () => { | ||
const value = ref<string | number>('Hi') | ||
const float = useToNumber(value, { nanToZero: true }) | ||
expect(float.value).toBe(0) | ||
}) | ||
}) |
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,49 @@ | ||
import type { ComputedRef } from 'vue-demi' | ||
import { computed } from 'vue-demi' | ||
import { resolveUnref } from '../resolveUnref' | ||
import type { MaybeComputedRef } from '../utils' | ||
|
||
export interface UseToNumberOptions { | ||
/** | ||
* Method to use to convert the value to a number. | ||
* | ||
* @default 'parseFloat' | ||
*/ | ||
method?: 'parseFloat' | 'parseInt' | ||
|
||
/** | ||
* The base in mathematical numeral systems passed to `parseInt`. | ||
* Only works with `method: 'parseInt'` | ||
*/ | ||
radix?: number | ||
|
||
/** | ||
* Replace NaN with zero | ||
* | ||
* @default false | ||
*/ | ||
nanToZero?: boolean | ||
} | ||
|
||
/** | ||
* Computed reactive object. | ||
*/ | ||
export function useToNumber( | ||
value: MaybeComputedRef<number | string>, | ||
options: UseToNumberOptions = {}, | ||
): ComputedRef<number> { | ||
const { | ||
method = 'parseFloat', | ||
radix, | ||
nanToZero, | ||
} = options | ||
|
||
return computed(() => { | ||
let resolved = resolveUnref(value) | ||
if (typeof resolved === 'string') | ||
resolved = Number[method](resolved, radix) | ||
if (nanToZero && isNaN(resolved)) | ||
resolved = 0 | ||
return resolved | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -42,7 +42,6 @@ | |
], | ||
"exclude": [ | ||
"node_modules", | ||
"**/**/*.stories.tsx", | ||
"**/**/*.md", | ||
"**/dist", | ||
"packages/.test", | ||
|