Skip to content

Commit

Permalink
Float 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jan 11, 2022
1 parent 0507370 commit 13f5d19
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
13 changes: 6 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Global
node_modules/
coverage

# OS Generated
.DS_Store*
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<a name="2.0.1"></a>
# [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.

Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface Options {
min?: number;
max?: number;
fixed?: number;
}
export default function float(options?: Options): number;
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -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));
};
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));
}
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import float from './index.js';

expectType<number>(float());
expectType<number>(float({min: 0}));
expectType<number>(float({min: 0, max: 10}));
expectType<number>(float({min: 0, max: 10, fixed: 10}));
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
35 changes: 20 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
@@ -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.');
});
const error = t.throws(() => {
float({min: 10, max: 0});
}, {instanceOf: TypeError});

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

0 comments on commit 13f5d19

Please sign in to comment.