diff --git a/.editorconfig b/.editorconfig index e98f58d..ea8f4ab 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,18 +1,17 @@ -# EditorConfig: http://EditorConfig.org +# EditorConfig is awesome: https://EditorConfig.org -# top-most EditorConfig file root = true -# Unix-style newlines with a newline ending every file [*] -charset = utf-8 end_of_line = lf -trim_trailing_whitespace = true insert_final_newline = true + +[*.{js,d.ts,ts}] +charset = utf-8 +trim_trailing_whitespace = true indent_style = space indent_size = 4 -# 2 space indentation -[*.yaml, *.yml] +[package.json,*.yaml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore index e7ef1ee..bac2cd5 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Global node_modules/ +coverage # OS Generated .DS_Store* diff --git a/CHANGELOG.md b/CHANGELOG.md index 96efed7..76be936 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -# [2.0.1](https://github.com/faker-javascript/float) (2022-01-10) +# [2.0.1](https://github.com/faker-javascript/float) (2022-01-11) * GitHub docs updates. * Codebase fixes. diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..783ba47 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,6 @@ +interface Options { + min?: number; + max?: number; + fixed?: number; +} +export default function float(options?: Options): number; diff --git a/index.js b/index.js index d6b4c21..b4f97c8 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,17 @@ +/* eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}] */ export default function float(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; + const fixed = 10 ** options.fixed === undefined ? 4 : options.fixed; + const max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max; + const 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.'); - } + throw new TypeError('Expected all arguments to be numbers.'); + } + if (min > max) { - throw new TypeError('Min cannot be greater than Max.'); - } - let result = Math.random() * (max - min) + min; - return Number(Number(parseFloat(result.toString())).toFixed(4)); -}; \ No newline at end of file + throw new TypeError('Min cannot be greater than Max.'); + } + + const result = Math.random() * ((max - min) + min); + return Number(Number(Number.parseFloat(result.toString())).toFixed(4)); +} diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..98bf1ab --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import float from './index.js'; + +expectType(float()); +expectType(float({min: 0})); +expectType(float({min: 0, max: 10})); +expectType(float({min: 0, max: 10, fixed: 10})); diff --git a/package.json b/package.json index 71a64e9..84363af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fakerjs/float", - "version": "2.0.1", + "version": "2.1.0", "description": "Float package provides functionality to generate a fake float value.", "license": "MIT", "repository": "faker-javascript/float", @@ -15,13 +15,17 @@ "node": ">=12" }, "scripts": { - "test": "ava" + "test": "c8 ava; xo --space 4; tsd;" }, "devDependencies": { - "ava": "^3.15.0" + "ava": "^4.0.0", + "c8": "^7.11.0", + "tsd": "^0.19.1", + "xo": "^0.47.0" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "fakerjs", diff --git a/test.js b/test.js index 5c36cdb..0626794 100644 --- a/test.js +++ b/test.js @@ -1,34 +1,39 @@ -import float from './index.js'; import test from 'ava'; +import float from './index.js'; test('float return type to be number', t => { - t.is(typeof float(), 'number'); + t.is(typeof float(), 'number'); }); test('float with number min 0 return type to be number', t => { - t.is(typeof float({min: 0}), 'number'); + t.is(typeof float({min: 0}), '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'); + t.is(typeof float({min: 0, max: 10}), 'number'); }); test('float with number min 0 and max 10 less than 11', t => { - t.true(float({min: 0, max: 10}) < 11); + t.true(float({min: 0, max: 10}) < 11); +}); + +test('float with number min 0 and max 10 and fixed 10 less than 11', t => { + t.true(float({min: 0, max: 10, fixed: 10}) < 11); }); test('float with string to thow error on string', t => { - const error = t.throws(() => { - float({min: 'string', max: 'string', fixed: 'string'}) - }, {instanceOf: TypeError}); + const error = t.throws(() => { + // @ts-ignore + float({min: 'string', max: 'string', fixed: 'string'}); + }, {instanceOf: TypeError}); - t.is(error.message, 'Expected all arguments to be numbers.'); + t.is(error.message, 'Expected all arguments to be numbers.'); }); test('float with min and max to thow error on min > max', t => { - const error = t.throws(() => { - float({min: 10, max: 0}) - }, {instanceOf: TypeError}); - - t.is(error.message, 'Min cannot be greater than Max.'); -}); \ No newline at end of file + const error = t.throws(() => { + float({min: 10, max: 0}); + }, {instanceOf: TypeError}); + + t.is(error.message, 'Min cannot be greater than Max.'); +});