Skip to content

Commit

Permalink
Integer 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jan 12, 2022
1 parent a9d649e commit a6b2502
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 34 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<a name="2.1.0"></a>
# [2.1.0](https://github.com/faker-javascript/integer) (2022-01-12)
* Added xo, tsd, c8.
* Improved tests.

<a name="2.0.1"></a>
# [2.0.1](https://github.com/faker-javascript/integer) (2022-01-10)
* GitHub docs updates.
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Options {
min?: number;
max?: number;
}
export default function integer(options?: Options): number;
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ export default function integer(options) {
let max = options.max === undefined ? Number.MAX_SAFE_INTEGER : options.max;
let min = options.min === undefined ? Number.MIN_SAFE_INTEGER : 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.');
}
throw new TypeError('Min cannot be greater than Max.');
}

min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
}
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import integer from './index.js';

expectType<number>(integer());
expectType<number>(integer({min: 0}));
expectType<number>(integer({min: 0, max: 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/integer",
"version": "2.0.1",
"version": "2.1.0",
"description": "Integer package provides functionality to generate a fake integer value.",
"license": "MIT",
"repository": "faker-javascript/integer",
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
28 changes: 10 additions & 18 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import integer from './index.js';
import test from 'ava';
import integer from './index.js';

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

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

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

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

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

t.is(error.message, 'Expected all arguments to be numbers.');
t.is(error.message, 'Min cannot be greater than Max.');
});

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

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

0 comments on commit a6b2502

Please sign in to comment.