Skip to content

Commit 13f5d19

Browse files
committed
Float 2.1.0
1 parent 0507370 commit 13f5d19

9 files changed

+63
-37
lines changed

.editorconfig

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
# EditorConfig: http://EditorConfig.org
1+
# EditorConfig is awesome: https://EditorConfig.org
22

3-
# top-most EditorConfig file
43
root = true
54

6-
# Unix-style newlines with a newline ending every file
75
[*]
8-
charset = utf-8
96
end_of_line = lf
10-
trim_trailing_whitespace = true
117
insert_final_newline = true
8+
9+
[*.{js,d.ts,ts}]
10+
charset = utf-8
11+
trim_trailing_whitespace = true
1212
indent_style = space
1313
indent_size = 4
1414

15-
# 2 space indentation
16-
[*.yaml, *.yml]
15+
[package.json,*.yaml]
1716
indent_style = space
1817
indent_size = 2

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Global
22
node_modules/
3+
coverage
34

45
# OS Generated
56
.DS_Store*

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<a name="2.0.1"></a>
2-
# [2.0.1](https://github.com/faker-javascript/float) (2022-01-10)
2+
# [2.0.1](https://github.com/faker-javascript/float) (2022-01-11)
33
* GitHub docs updates.
44
* Codebase fixes.
55

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface Options {
2+
min?: number;
3+
max?: number;
4+
fixed?: number;
5+
}
6+
export default function float(options?: Options): number;

index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
/* eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}] */
12
export default function float(options) {
23
options = options || {};
3-
let fixed = Math.pow(10, ((options.fixed === undefined) ? 4 : options.fixed));
4-
let max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max;
5-
let min = (options.min === undefined) ? Number.MIN_SAFE_INTEGER / fixed : options.min;
4+
const fixed = 10 ** options.fixed === undefined ? 4 : options.fixed;
5+
const max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max;
6+
const min = (options.min === undefined) ? Number.MIN_SAFE_INTEGER / fixed : options.min;
67
if (typeof min !== 'number' || typeof max !== 'number') {
7-
throw new TypeError('Expected all arguments to be numbers.');
8-
}
8+
throw new TypeError('Expected all arguments to be numbers.');
9+
}
10+
911
if (min > max) {
10-
throw new TypeError('Min cannot be greater than Max.');
11-
}
12-
let result = Math.random() * (max - min) + min;
13-
return Number(Number(parseFloat(result.toString())).toFixed(4));
14-
};
12+
throw new TypeError('Min cannot be greater than Max.');
13+
}
14+
15+
const result = Math.random() * ((max - min) + min);
16+
return Number(Number(Number.parseFloat(result.toString())).toFixed(4));
17+
}

index.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {expectType} from 'tsd';
2+
import float from './index.js';
3+
4+
expectType<number>(float());
5+
expectType<number>(float({min: 0}));
6+
expectType<number>(float({min: 0, max: 10}));
7+
expectType<number>(float({min: 0, max: 10, fixed: 10}));

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fakerjs/float",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "Float package provides functionality to generate a fake float value.",
55
"license": "MIT",
66
"repository": "faker-javascript/float",
@@ -15,13 +15,17 @@
1515
"node": ">=12"
1616
},
1717
"scripts": {
18-
"test": "ava"
18+
"test": "c8 ava; xo --space 4; tsd;"
1919
},
2020
"devDependencies": {
21-
"ava": "^3.15.0"
21+
"ava": "^4.0.0",
22+
"c8": "^7.11.0",
23+
"tsd": "^0.19.1",
24+
"xo": "^0.47.0"
2225
},
2326
"files": [
24-
"index.js"
27+
"index.js",
28+
"index.d.ts"
2529
],
2630
"keywords": [
2731
"fakerjs",

test.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
import float from './index.js';
21
import test from 'ava';
2+
import float from './index.js';
33

44
test('float return type to be number', t => {
5-
t.is(typeof float(), 'number');
5+
t.is(typeof float(), 'number');
66
});
77

88
test('float with number min 0 return type to be number', t => {
9-
t.is(typeof float({min: 0}), 'number');
9+
t.is(typeof float({min: 0}), 'number');
1010
});
1111

1212
test('float with number min 0 and max 10 return type to be number', t => {
13-
t.is(typeof float({min: 0, max: 10}), 'number');
13+
t.is(typeof float({min: 0, max: 10}), 'number');
1414
});
1515

1616
test('float with number min 0 and max 10 less than 11', t => {
17-
t.true(float({min: 0, max: 10}) < 11);
17+
t.true(float({min: 0, max: 10}) < 11);
18+
});
19+
20+
test('float with number min 0 and max 10 and fixed 10 less than 11', t => {
21+
t.true(float({min: 0, max: 10, fixed: 10}) < 11);
1822
});
1923

2024
test('float with string to thow error on string', t => {
21-
const error = t.throws(() => {
22-
float({min: 'string', max: 'string', fixed: 'string'})
23-
}, {instanceOf: TypeError});
25+
const error = t.throws(() => {
26+
// @ts-ignore
27+
float({min: 'string', max: 'string', fixed: 'string'});
28+
}, {instanceOf: TypeError});
2429

25-
t.is(error.message, 'Expected all arguments to be numbers.');
30+
t.is(error.message, 'Expected all arguments to be numbers.');
2631
});
2732

2833
test('float with min and max to thow error on min > max', t => {
29-
const error = t.throws(() => {
30-
float({min: 10, max: 0})
31-
}, {instanceOf: TypeError});
32-
33-
t.is(error.message, 'Min cannot be greater than Max.');
34-
});
34+
const error = t.throws(() => {
35+
float({min: 10, max: 0});
36+
}, {instanceOf: TypeError});
37+
38+
t.is(error.message, 'Min cannot be greater than Max.');
39+
});

0 commit comments

Comments
 (0)