Skip to content

Commit

Permalink
feat(useFloor): new function (#1817)
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz committed Jul 10, 2022
1 parent 1b258ac commit e300c06
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/math/useFloor/index.md
@@ -0,0 +1,16 @@
---
category: '@Math'
---

# useFloor

Reactively Math.floor(value).

## Usage

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

const value = ref(45.95)
const result = useFloor(value)
```
15 changes: 15 additions & 0 deletions packages/math/useFloor/index.test.ts
@@ -0,0 +1,15 @@
import { ref } from 'vue-demi'
import { useFloor } from '.'

describe('useFloor', () => {
it('should be defined', () => {
expect(useFloor).toBeDefined()
})
it('should work', () => {
const base = ref(45.95)
const result = useFloor(base)
expect(result.value).toBe(45)
base.value = -45.05
expect(result.value).toBe(-46)
})
})
15 changes: 15 additions & 0 deletions packages/math/useFloor/index.ts
@@ -0,0 +1,15 @@
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref } from '@vueuse/shared'

/**
* Reactively Math.floor(value).
*
* @see https://vueuse.org/useFloor
* @param base
* @param exponent
*/
export function useFloor(value: MaybeComputedRef<number>): ComputedRef<number> {
return computed<number>(() => Math.floor(resolveUnref(value)))
}

0 comments on commit e300c06

Please sign in to comment.