Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 26, 2019
1 parent 1427fc0 commit 0c28c8d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 48 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
56 changes: 56 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
declare const roundTo: {
/**
Round the decimals with [`Math.round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round).
Numbers are rounded to a specific number of fractional digits. Specifying a negative `precision` will round to any number of places to the left of the decimal.
@param number - Number to adjust.
@param precision - (Integer) Number of decimal places.
@example
```
import roundTo = require('round-to');
roundTo(1.234, 2);
//=> 1.23
roundTo(1234.56, -2);
//=> 1200
```
*/
(number: number, precision: number): number;

/**
Round up the decimals with [`Math.ceil`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil).
@param number - Number to adjust.
@param precision - (Integer) number of decimal places.
@example
```
import roundTo = require('round-to');
roundTo.up(1.234, 2);
//=> 1.24
```
*/
up(number: number, precision: number): number;

/**
Round down the decimals with [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor).
@param number - Number to adjust.
@param precision - (Integer) number of decimal places.
@example
```
import roundTo = require('round-to');
roundTo.down(1.234, 2);
//=> 1.23
```
*/
down(number: number, precision: number): number;
};

export = roundTo;
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
'use strict';

function round(method, input, precision) {
if (typeof input !== 'number') {
function round(method, number, precision) {
if (typeof number !== 'number') {
throw new TypeError('Expected value to be a number');
}

if (!Number.isInteger(precision)) {
throw new TypeError('Expected precision to be an integer');
}

const isRoundingAndNegative = method === 'round' && input < 0;
const isRoundingAndNegative = method === 'round' && number < 0;
if (isRoundingAndNegative) {
input = Math.abs(input);
number = Math.abs(number);
}

let [number, exponent] = `${input}e`.split('e');
let ret = Math[method](`${number}e${Number(exponent) + precision}`);
let exponent;
[number, exponent] = `${number}e`.split('e');
let result = Math[method](`${number}e${Number(exponent) + precision}`);

[number, exponent] = `${ret}e`.split('e');
ret = Number(`${number}e${Number(exponent) - precision}`);
[number, exponent] = `${result}e`.split('e');
result = Number(`${number}e${Number(exponent) - precision}`);

if (isRoundingAndNegative) {
ret = -ret;
result = -result;
}

return ret;
return result;
}

module.exports = round.bind(null, 'round');
Expand Down
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import roundTo = require('.');

expectType<number>(roundTo(1.234, 2));
expectType<number>(roundTo.up(1.234, 2));
expectType<number>(roundTo.down(1.234, 2));
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"round",
Expand All @@ -30,7 +31,8 @@
"increment"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ roundTo.down(1.234, 2);
//=> 1.23
```

Numbers are rounded to a specific number of fractional digits. Specifying a negative precision will round to any number of places to the left of the decimal.
Numbers are rounded to a specific number of fractional digits. Specifying a negative `precision` will round to any number of places to the left of the decimal.

```js
roundTo(1234.56, -2);
Expand All @@ -34,27 +34,27 @@ roundTo(1234.56, -2);

## API

### roundTo(value, precision)
### roundTo(number, precision)

Round the decimals with [`Math.round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round).

### roundTo.up(value, precision)
### roundTo.up(number, precision)

Round up the decimals with [`Math.ceil`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil).

### roundTo.down(value, precision)
### roundTo.down(number, precision)

Round down the decimals with [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor).

#### value
#### number

Type: `number`

Number to adjust.

#### precision

Type: `number` (integer)
Type: `number` (Integer)

Number of decimal places.

Expand Down
48 changes: 24 additions & 24 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import test from 'ava';
import m from '.';
import roundTo from '.';

test('roundTo()', t => {
t.is(m(0.129, 3), 0.129);
t.is(m(0.129, 2), 0.13);
t.is(m(0.129, 1), 0.1);
t.is(m(1.005, 2), 1.01);
t.is(m(1.005, 0), 1);
t.is(m(111.1, -2), 100);
t.is(m(-0.375, 2), -0.38);
t.false(Number.isNaN(m(10000000000000, 8)));
t.is(m(0.37542323423423432432432432432, 8), 0.37542323);
t.is(roundTo(0.129, 3), 0.129);
t.is(roundTo(0.129, 2), 0.13);
t.is(roundTo(0.129, 1), 0.1);
t.is(roundTo(1.005, 2), 1.01);
t.is(roundTo(1.005, 0), 1);
t.is(roundTo(111.1, -2), 100);
t.is(roundTo(-0.375, 2), -0.38);
t.false(Number.isNaN(roundTo(10000000000000, 8)));
t.is(roundTo(0.37542323423423432432432432432, 8), 0.37542323);
});

test('roundTo.up()', t => {
t.is(m.up(0.111, 3), 0.111);
t.is(m.up(0.111, 2), 0.12);
t.is(m.up(0.111, 1), 0.2);
t.is(m.up(1.004, 2), 1.01);
t.is(m.up(1.111, 0), 2);
t.is(m.up(111.1, -2), 200);
t.is(m.up(-0.375, 2), -0.37);
t.is(roundTo.up(0.111, 3), 0.111);
t.is(roundTo.up(0.111, 2), 0.12);
t.is(roundTo.up(0.111, 1), 0.2);
t.is(roundTo.up(1.004, 2), 1.01);
t.is(roundTo.up(1.111, 0), 2);
t.is(roundTo.up(111.1, -2), 200);
t.is(roundTo.up(-0.375, 2), -0.37);
});

test('roundTo.down()', t => {
t.is(m.down(0.666, 3), 0.666);
t.is(m.down(0.666, 2), 0.66);
t.is(m.down(0.666, 1), 0.6);
t.is(m.down(1.006, 2), 1.00);
t.is(m.down(1.006, 0), 1);
t.is(m.down(111.6, -2), 100);
t.is(m.down(-0.375, 2), -0.38);
t.is(roundTo.down(0.666, 3), 0.666);
t.is(roundTo.down(0.666, 2), 0.66);
t.is(roundTo.down(0.666, 1), 0.6);
t.is(roundTo.down(1.006, 2), 1.0);
t.is(roundTo.down(1.006, 0), 1);
t.is(roundTo.down(111.6, -2), 100);
t.is(roundTo.down(-0.375, 2), -0.38);
});

0 comments on commit 0c28c8d

Please sign in to comment.