Skip to content

Commit

Permalink
Add TypeScript definition (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 24, 2019
1 parent 2c32302 commit ec25adc
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
68 changes: 68 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import ColorClass = require('color');

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

type Color = ColorClass;
}

declare class Randoma {
/**
@returns A random seed you could use in the `seed` option if you for some reason don't want deterministic randomness.
*/
static seed(): number;

/**
User-friendly [pseudorandom number generator (PRNG)](https://en.wikipedia.org/wiki/Pseudorandom_number_generator).
This is not cryptographically secure.
@example
```
import Randoma = require('randoma');
const random = new Randoma({seed: 10});
random.integer();
//=> 2027521326
random.integer();
//=> 677268843
(new Randoma({seed: '🦄'}).integer());
//=> 1659974344
(new Randoma({seed: '🦄'}).integer());
//=> 1659974344
```
*/
constructor(options: Randoma.Options);

integer(): number;
integerInRange(min: number, max: number): number;
float(): number;
floatInRange(min: number, max: 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`.
@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
```
random.color(0.5).hex().toString()
//=> '#AAF2B0'
```
*/
color(saturation?: number): Randoma.Color;
}

export = Randoma;
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const color = require('color');
const MAX_INT32 = 2147483647;
const GOLDEN_RATIO_CONJUGATE = 0.618033988749895;

module.exports = class {
class Randoma {
static seed() {
return Math.floor(Math.random() * MAX_INT32);
}

constructor(options = {}) {
let {seed} = options;

Expand Down Expand Up @@ -66,6 +70,6 @@ module.exports = class {
v: 95
});
}
};
}

module.exports.seed = () => Math.floor(Math.random() * MAX_INT32);
module.exports = Randoma;
22 changes: 22 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expectType} from 'tsd';
import Randoma = require('.');

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

const random = new Randoma(options);
new Randoma({seed: '🦄'});
new Randoma({seed: Randoma.seed()});

expectType<number>(random.integer());
expectType<number>(random.integerInRange(0, 1));
expectType<number>(random.float());
expectType<number>(random.floatInRange(0, 1));
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());
random
.color(0.5)
.hex()
.toString();
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"pseudorandom",
Expand All @@ -35,13 +36,15 @@
"rng"
],
"dependencies": {
"@sindresorhus/string-hash": "^1.0.0",
"color": "^2.0.1",
"park-miller": "^1.0.0"
"@sindresorhus/string-hash": "^1.2.0",
"@types/color": "^3.0.0",
"color": "^3.1.1",
"park-miller": "^1.1.0"
},
"devDependencies": {
"@sindresorhus/is": "^0.7.0",
"ava": "*",
"xo": "*"
"@sindresorhus/is": "^0.15.0",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ random.integer();

#### options

Type: `Object`
Type: `object`

##### seed

*Required*<br>
Type: `string` `integer`
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.

Expand Down

0 comments on commit ec25adc

Please sign in to comment.