0.1.14 • Published 2 years ago

@wareset-utilites/nearly v0.1.14

Weekly downloads
8
License
MIT
Repository
github
Last release
2 years ago

@wareset-utilites/nearly

Returns the nearest value from the array, or from a pattern.

Installation

npm i @wareset-utilites/nearly ## yarn add @wareset-utilites/nearly

Usage

nearly(value: number, pattern: number | number[], method?: -1 | 0 | 1): number

'method':

  • -1 - floor
  • 0 - round // By default
  • 1 - ceil
import nearly from '@wareset-utilites/nearly'

// If the pattern is an array, returns the nearest value.
expect(nearly(1.25, [1, 1.24, 1.26, 2])).toBe(1.26)

expect(nearly(1.25, [1, 1.24, 1.26, 2], -1)).toBe(1.24)
expect(nearly(1.25, [1, 1.24, 1.26, 2], 0)).toBe(1.26)
expect(nearly(1.25, [1, 1.24, 1.26, 2], 1)).toBe(1.26)

expect(nearly(1.25, [], -1)).toBe(1.25)
expect(nearly(1.25, [], 0)).toBe(1.25)
expect(nearly(1.25, [], 1)).toBe(1.25)

// If the pattern is a number, it returns a multiple of it.
expect(nearly(3040.10576, 150.101)).toBe(3002.02)

expect(nearly(3040.10576, 150.101, -1)).toBe(3002.02)
expect(nearly(3040.10576, 150.101, 0)).toBe(3002.02)
expect(nearly(3040.10576, 150.101, 1)).toBe(3152.121)

// If the pattern is zero, it returns standard rounding.
expect(nearly(3040.10576, 0, -1)).toBe(Math.floor(3040.10576))
expect(nearly(3040.10576, 0, 0)).toBe(Math.round(3040.10576))
expect(nearly(3040.10576, 0, 1)).toBe(Math.ceil(3040.10576))

License

MIT

0.1.14

2 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

4 years ago