Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 5, 2021
1 parent 46c0df6 commit bd5fa9a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 94 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
- 6
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
35 changes: 18 additions & 17 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import ColorClass = require('color');
import Color from 'color';

declare namespace Randoma {
interface Options {
/**
[Initialization seed.](https://en.wikipedia.org/wiki/Random_seed) Multiple instances of `Randoma` with the same seed will generate the same random numbers.
*/
readonly seed: string | number;
}
export interface Options {
/**
[Initialization seed.](https://en.wikipedia.org/wiki/Random_seed)
type Color = ColorClass;
Multiple instances of `Randoma` with the same seed will generate the same random numbers.
*/
readonly seed: string | number;
}

declare class Randoma {
export default class Randoma {
/**
@returns A random seed you could use in the `seed` option if you for some reason don't want deterministic randomness.
*/
Expand All @@ -24,7 +22,7 @@ declare class Randoma {
@example
```
import Randoma = require('randoma');
import Randoma from 'randoma';
const random = new Randoma({seed: 10});
Expand All @@ -41,19 +39,19 @@ declare class Randoma {
//=> 1659974344
```
*/
constructor(options: Randoma.Options);
constructor(options: Options);

integer(): number;
integerInRange(min: number, max: number): number;
integerInRange(minimum: number, maximum: number): number;
float(): number;
floatInRange(min: number, max: number): number;
floatInRange(minimum: number, maximum: number): number;
boolean(): boolean;
arrayItem<T>(array: readonly T[]): T;
date(): Date;
dateInRange(startDate: Date, endDate: Date): Date;

/**
@param saturation - Saturation percentage in the range `0...1`. Default: `0.5`.
@param saturation - A percentage in the range `0...1`. Default: `0.5`.
@returns A random [aesthetically pleasing color](https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/) as a [`color`](https://github.com/Qix-/color) object.
@example
Expand All @@ -62,7 +60,10 @@ declare class Randoma {
//=> '#AAF2B0'
```
*/
color(saturation?: number): Randoma.Color;
color(saturation?: number): Color;
}

export = Randoma;
export {Color};

// TODO: When `color` package is ESM.
// export {default as Color} from 'color';
35 changes: 17 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict';
const ParkMiller = require('park-miller');
const stringHash = require('@sindresorhus/string-hash');
const color = require('color');
import ParkMiller from 'park-miller';
import stringHash from '@sindresorhus/string-hash';
import color from 'color';

const MAX_INT32 = 2147483647;
const GOLDEN_RATIO_CONJUGATE = 0.618033988749895;
const MAX_INT32 = 2_147_483_647;
const GOLDEN_RATIO_CONJUGATE = 0.618_033_988_749_895;

class Randoma {
export default class Randoma {
static seed() {
return Math.floor(Math.random() * MAX_INT32);
}

#random;

constructor({seed}) {
if (typeof seed === 'string') {
seed = stringHash(seed);
Expand All @@ -20,27 +21,27 @@ class Randoma {
throw new TypeError('Expected `seed` to be a `integer`');
}

this._random = new ParkMiller(seed);
this.#random = new ParkMiller(seed);
}

integer() {
return this._random.integer();
return this.#random.integer();
}

integerInRange(min, max) {
return this._random.integerInRange(min, max);
integerInRange(minimum, maximum) {
return this.#random.integerInRange(minimum, maximum);
}

float() {
return this._random.float();
return this.#random.float();
}

floatInRange(min, max) {
return this._random.floatInRange(min, max);
floatInRange(minimum, maximum) {
return this.#random.floatInRange(minimum, maximum);
}

boolean() {
return this._random.boolean();
return this.#random.boolean();
}

arrayItem(array) {
Expand All @@ -63,9 +64,7 @@ class Randoma {
return color({
h: hue * 360,
s: saturation * 100,
v: 95
v: 95,
});
}
}

module.exports = Randoma;
10 changes: 5 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {expectType} from 'tsd';
import Randoma = require('.');
import Randoma, {Options, Color} from './index.js';

const options: Randoma.Options = {seed: 10};
const options: Options = {seed: 10};

const random = new Randoma(options);
new Randoma({seed: '🦄'});
new Randoma({seed: Randoma.seed()});
new Randoma({seed: '🦄'}); // eslint-disable-line no-new
new Randoma({seed: Randoma.seed()}); // eslint-disable-line no-new

expectType<number>(random.integer());
expectType<number>(random.integerInRange(0, 1));
Expand All @@ -15,7 +15,7 @@ expectType<boolean>(random.boolean());
expectType<string>(random.arrayItem(['🦄']));
expectType<Date>(random.date());
expectType<Date>(random.dateInRange(new Date(), new Date()));
expectType<Randoma.Color>(random.color());
expectType<Color>(random.color());
random
.color(0.5)
.hex()
Expand Down
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
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "User-friendly pseudorandom number generator (PRNG)",
"license": "MIT",
"repository": "sindresorhus/randoma",
"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": ">=6"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,15 +39,15 @@
"rng"
],
"dependencies": {
"@sindresorhus/string-hash": "^1.2.0",
"@types/color": "^3.0.0",
"color": "^3.1.1",
"park-miller": "^1.1.0"
"@sindresorhus/string-hash": "^2.0.0",
"@types/color": "^3.0.2",
"color": "^4.0.1",
"park-miller": "^2.0.0"
},
"devDependencies": {
"@sindresorhus/is": "^0.15.0",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"@sindresorhus/is": "^4.2.0",
"ava": "^3.15.0",
"tsd": "^0.18.0",
"xo": "^0.46.4"
}
}
33 changes: 13 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
This is not cryptographically secure.

*“Pull request welcome” for additional commonly used random methods.*

## Install

```sh
npm install randoma
```
$ npm install randoma
```


## Usage

```js
const Randoma = require('randoma');
import Randoma from 'randoma';

const random = new Randoma({seed: 10});

Expand All @@ -33,7 +33,6 @@ random.integer();
//=> 1659974344
```


## API

### `const random = new Randoma(options)`
Expand All @@ -44,21 +43,23 @@ Type: `object`

##### seed

*Required*<br>
*Required*\
Type: `string | number`

[Initialization seed.](https://en.m.wikipedia.org/wiki/Random_seed) Multiple instances of `Randoma` with the same seed will generate the same random numbers.
[Initialization seed.](https://en.m.wikipedia.org/wiki/Random_seed)

Multiple instances of `Randoma` with the same seed will generate the same random numbers.

#### random.integer()
#### random.integerInRange(min, max)
#### random.integerInRange(minimum, maximum)
#### random.float()
#### random.floatInRange(min, max)
#### random.floatInRange(minimum, maximum)
#### random.boolean()
#### random.arrayItem(array)
#### random.date()
#### random.dateInRange(startDate, endDate)

#### random.color([saturation])
#### random.color(saturation?)

Returns a random [aesthetically pleasing color](https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/) as a [`color`](https://github.com/Qix-/color) object.

Expand All @@ -69,23 +70,15 @@ random.color(0.5).hex().toString()

##### saturation

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

Saturation percentage in the range `0...1`.

*"Pull request welcome" for additional commonly used random methods.*
A percentage in the range `0...1`.

### Randoma.seed()

Returns a random seed you could use in the `seed` option if you for some reason don't want deterministic randomness.


## Related

- [park-miller](https://github.com/sindresorhus/park-miller) - Park-Miller pseudorandom number generator (PRNG)


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
Loading

0 comments on commit bd5fa9a

Please sign in to comment.