Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 17, 2021
1 parent 315fdf9 commit 48bebe6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 48 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
10 changes: 4 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ Generate a random integer.
@example
```
import randomInt = require('random-int');
import randomInteger from 'random-int';
randomInt(5);
randomInteger(5);
//=> 3
randomInt(10, 100);
randomInteger(10, 100);
//=> 54
```
*/
declare function randomInt(minimum?: number, maximum?: number): number;

export = randomInt;
export default function randomInteger(minimum?: number, maximum?: number): number;
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = (minimum, maximum) => {
export default function randomInteger(minimum, maximum) {
if (maximum === undefined) {
maximum = minimum;
minimum = 0;
Expand All @@ -13,4 +11,4 @@ module.exports = (minimum, maximum) => {
return Math.floor(
(Math.random() * (maximum - minimum + 1)) + minimum
);
};
}
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expectType} from 'tsd';
import randomInt = require('.');
import randomInteger from './index.js';

expectType<number>(randomInt());
expectType<number>(randomInt(5));
expectType<number>(randomInt(10, 100));
expectType<number>(randomInteger());
expectType<number>(randomInteger(5));
expectType<number>(randomInteger(10, 100));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Generate a random integer",
"license": "MIT",
"repository": "sindresorhus/random-int",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -20,7 +23,6 @@
"index.d.ts"
],
"keywords": [
"rand",
"random",
"randomize",
"int",
Expand All @@ -33,10 +35,10 @@
"generator"
],
"devDependencies": {
"ava": "^1.4.1",
"in-range": "^2.0.0",
"stable-fn": "^2.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"in-range": "^3.0.0",
"stable-fn": "^3.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
23 changes: 7 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,48 @@

> Generate a random integer

## Install

```
$ npm install random-int
```


## Usage

```js
const randomInt = require('random-int');
import randomInteger from 'random-int';

randomInt(5);
randomInteger(5);
//=> 3

randomInt(10, 100);
randomInteger(10, 100);
//=> 54
```


## API

### randomInt([maximum])
### randomInteger(maximum?)

Returns an integer from `0` to `maximum`.

### randomInt(minimum, maximum)
### randomInteger(minimum, maximum)

Returns an integer from `minimum` to `maximum`.

#### minimum

Type: `number`<br>
Type: `number`\
Default: `0`

Minimum integer to return.

#### maximum

Type: `number`<br>
Type: `number`\
Default: `1`

Maximum integer to return.


## Related

- [random-float](https://github.com/sindresorhus/random-float) - Generate a random float
Expand All @@ -57,8 +53,3 @@ Maximum integer to return.
- [unique-random](https://github.com/sindresorhus/unique-random) - Generate random numbers that are consecutively unique
- [unique-random-array](https://github.com/sindresorhus/unique-random-array) - Get consecutively unique elements from an array
- [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) - Generate a cryptographically strong random string


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
12 changes: 6 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import test from 'ava';
import inRange from 'in-range';
import stableFn from 'stable-fn';
import randomInt from '.';
import randomInteger from './index.js';

const inRangeCheck = ({start, end}) => stableFn(() => inRange(randomInt(start, end), {start, end}));
const inRangeCheck = ({start, end}) => stableFn(() => inRange(randomInteger(start, end), {start, end}));

test('main', t => {
t.true(Number.isInteger(randomInt(0, 10)));
t.false(stableFn(() => randomInt(1)));
t.true(stableFn(() => inRange(randomInt(1), {start: 0, end: 1})));
t.true(inRange(randomInt(1), {start: 0, end: 1}));
t.true(Number.isInteger(randomInteger(0, 10)));
t.false(stableFn(() => randomInteger(1)));
t.true(stableFn(() => inRange(randomInteger(1), {start: 0, end: 1})));
t.true(inRange(randomInteger(1), {start: 0, end: 1}));
t.true(inRangeCheck({start: 0, end: 1}));
t.true(inRangeCheck({start: 0, end: 2}));
t.true(inRangeCheck({start: 0, end: 10}));
Expand Down

0 comments on commit 48bebe6

Please sign in to comment.