diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c1faa..e5c918c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ + +# [2.0.0](https://github.com/faker-javascript/float) (2022-01-09) + +### BREAKING CHANGES + +* New function `float` istead of `fakeFloat` + # [1.0.2](https://github.com/faker-javascript/float) (2022-01-08) * Package fixes diff --git a/README.md b/README.md index af5be3a..463d7d5 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/index.js b/index.js index 9488210..869fcf3 100644 --- a/index.js +++ b/index.js @@ -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.'); } diff --git a/package.json b/package.json index 91dacd0..5486dfc 100644 --- a/package.json +++ b/package.json @@ -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", @@ -26,6 +26,7 @@ "keywords": [ "fakerjs", "faker", + "fake", "random", "float", "number" diff --git a/test.js b/test.js index c69db79..5c36cdb 100644 --- a/test.js +++ b/test.js @@ -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.');