Skip to content

Commit

Permalink
Float 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jan 9, 2022
1 parent 38600c2 commit d29b5ba
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<a name="2.0.0"></a>
# [2.0.0](https://github.com/faker-javascript/float) (2022-01-09)

### BREAKING CHANGES

* New function `float` istead of `fakeFloat`

<a name="1.0.2"></a>
# [1.0.2](https://github.com/faker-javascript/float) (2022-01-08)
* Package fixes
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ $ npm install --save @fakerjs/float
## Usage

```js
import fakeFloat from '@fakerjs/float';
import float from '@fakerjs/float';

fakeFloat();
float();
//=> -120920142888.5024

fakeFloat(0, 10);
float({min: 0, max: 10});
//=> 7.6913

fakeFloat(0, 10, 6);
float({min: 0, max: 10, fixed: 6);
//=> 7.691312
```
Expand Down
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
export default function fakeFloat(min, max, fixed) {
fixed = Math.pow(10, ((fixed === undefined) ? 4 : fixed));
if (max === undefined) {
max = Number.MAX_SAFE_INTEGER / fixed;
}
if (min === undefined) {
min = Number.MIN_SAFE_INTEGER / fixed;
}
export default function fakeFloat(options) {
options = options || {};
let fixed = Math.pow(10, ((options.fixed === undefined) ? 4 : options.fixed));
let max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max;
let min = (options.min === undefined) ? Number.MIN_SAFE_INTEGER / fixed : options.min;
if (typeof min !== 'number' || typeof max !== 'number') {
throw new TypeError('Expected all arguments to be numbers.');
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fakerjs/float",
"version": "1.0.2",
"version": "2.0.0",
"description": "Float package provides functionality to generate a fake float value.",
"license": "MIT",
"repository": "faker-javascript/float",
Expand All @@ -26,6 +26,7 @@
"keywords": [
"fakerjs",
"faker",
"fake",
"random",
"float",
"number"
Expand Down
26 changes: 13 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import fakeFloat from './index.js';
import float from './index.js';
import test from 'ava';

test('fakeFloat return type to be number', t => {
t.is(typeof fakeFloat(), 'number');
test('float return type to be number', t => {
t.is(typeof float(), 'number');
});

test('fakeFloat with number min 0 return type to be number', t => {
t.is(typeof fakeFloat(0), 'number');
test('float with number min 0 return type to be number', t => {
t.is(typeof float({min: 0}), 'number');
});

test('fakeFloat with number min 0 and max 10 return type to be number', t => {
t.is(typeof fakeFloat(0, 10), 'number');
test('float with number min 0 and max 10 return type to be number', t => {
t.is(typeof float({min: 0, max: 10}), 'number');
});

test('fakeFloat with number min 0 and max 10 less than 11', t => {
t.true(fakeFloat(0, 10) < 11);
test('float with number min 0 and max 10 less than 11', t => {
t.true(float({min: 0, max: 10}) < 11);
});

test('fakeFloat with string to thow error on string', t => {
test('float with string to thow error on string', t => {
const error = t.throws(() => {
fakeFloat('string', 'string', 'string')
float({min: 'string', max: 'string', fixed: 'string'})
}, {instanceOf: TypeError});

t.is(error.message, 'Expected all arguments to be numbers.');
});

test('fakeFloat with min and max to thow error on min > max', t => {
test('float with min and max to thow error on min > max', t => {
const error = t.throws(() => {
fakeFloat(10, 0)
float({min: 10, max: 0})
}, {instanceOf: TypeError});

t.is(error.message, 'Min cannot be greater than Max.');
Expand Down

0 comments on commit d29b5ba

Please sign in to comment.