random-js: Provides its own types (#39499)

This commit is contained in:
Alexander T
2019-10-28 09:48:03 -07:00
committed by Jesse Trinity
parent b3e533357d
commit eba15598ce
5 changed files with 6 additions and 226 deletions
+6
View File
@@ -2964,6 +2964,12 @@
"sourceRepoURL": "https://github.com/sindresorhus/random-item",
"asOfVersion": "2.0.0"
},
{
"libraryName": "random-js",
"typingsPackageName": "random-js",
"sourceRepoURL": "https://github.com/ckknight/random-js",
"asOfVersion": "2.0.0"
},
{
"libraryName": "random-obj-key",
"typingsPackageName": "random-obj-key",
-70
View File
@@ -1,70 +0,0 @@
// Type definitions for random-js 1.0
// Project: https://github.com/ckknight/random-js
// Definitions by: Gustavo Di Pietro <https://github.com/pistacchio>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace Random {
interface Engine {
(): number; // tslint:disable-line callable-types
}
interface MT19937 extends Engine {
seed(value: number): MT19937;
seedWithArray(array: number[]): MT19937;
autoSeed(): MT19937;
discard(count: number): MT19937;
getUseCount(): number;
}
}
interface Random {
integer(min: number, max: number): number;
real(min: number, max: number, inclusive?: boolean): number;
bool(percentage?: number): boolean; // tslint:disable-line unified-signatures
bool(numerator: number, denominator: number): boolean; // tslint:disable-line unified-signatures
pick<T>(array: T[], begin?: number, end?: number): T;
shuffle<T>(array: T[]): T[];
sample<T>(population: T[], sampleSize: number): T[];
die(sideCount: number): number;
dice(sideCount: number, dieCount: number): number[];
uuid4(engine?: Random.Engine): string;
string(engine: Random.Engine, length: number): string;
string(length?: number, pool?: string): string;
hex(length?: number, upperCase?: boolean): string;
date(start: Date, end: Date): Date;
}
interface RandomConstructor {
(engine?: Random.Engine): Random;
new (engine?: Random.Engine): Random;
engines: {
nativeMath: Random.Engine,
browserCrypto: Random.Engine,
mt19937(): Random.MT19937
};
Engine(): number;
MT19937(): number;
integer(min: number, max: number): (engine: Random.Engine) => number;
real(min: number, max: number, inclusive?: boolean): (engine: Random.Engine) => number;
bool(percentage?: number): (engine: Random.Engine) => boolean; // tslint:disable-line unified-signatures
bool(numerator: number, denominator: number): (engine: Random.Engine) => boolean; // tslint:disable-line unified-signatures
pick<T>(engine: Random.Engine, array: T[], begin?: number, end?: number): T;
picker<T>(array: T[], begin?: number, end?: number): (engine: Random.Engine) => T;
shuffle<T>(engine: Random.Engine, array: T[]): T[];
sample<T>(engine: Random.Engine, population: T[], sampleSize: number): T[];
die(sideCount: number): (engine: Random.Engine) => number;
dice(sideCount: number, dieCount: number): (engine: Random.Engine) => number[];
uuid4(engine: Random.Engine): string;
string(engine: Random.Engine, length: number): string;
string(pool?: string): (engine: Random.Engine, length?: number) => string;
hex(upperCase?: boolean): (engine: Random.Engine, length?: number) => string;
date(start: Date, end: Date): (engine: Random.Engine) => Date;
}
declare const Random: RandomConstructor;
export = Random;
export as namespace Random;
-129
View File
@@ -1,129 +0,0 @@
import Engine = Random.Engine;
import MersenneTwisterEngine = Random.MT19937;
class CustomType {}
const customTypeArray = [new CustomType(), new CustomType(), new CustomType()];
// APIs taken from the documentation at https://github.com/ckknight/random-js
// Basic engine types (https://github.com/ckknight/random-js#engines)
let engine: Engine = Random.engines.nativeMath;
engine = Random.engines.browserCrypto;
// All engines can be used to generate numbers
let generated: number = engine();
// Mersenne Twister API (https://github.com/ckknight/random-js#mersenne-twister-api)
let mersenneTwister: MersenneTwisterEngine = Random.engines.mt19937();
generated = mersenneTwister();
mersenneTwister = mersenneTwister.seed(0x9a0b1d43);
mersenneTwister = mersenneTwister.seedWithArray([0x12345678, 0x90abcdef]);
mersenneTwister = mersenneTwister.autoSeed();
mersenneTwister = mersenneTwister.discard(10);
const useCount: number = mersenneTwister.getUseCount();
// Static "Distributions" API (https://github.com/ckknight/random-js#distributions)
let numeric: number = Random.integer(0, 99)(engine);
numeric = Random.real(0, 10)(engine);
numeric = Random.real(0, 10, true)(engine);
let bool: boolean = Random.bool()(engine);
bool = Random.bool(20)(engine);
bool = Random.bool(1, 2)(engine);
let chosen: CustomType = Random.pick(engine, customTypeArray);
chosen = Random.pick(engine, customTypeArray, 0);
chosen = Random.pick(engine, customTypeArray, 0, 2);
chosen = Random.picker(customTypeArray);
chosen = Random.picker(customTypeArray, 0);
chosen = Random.picker(customTypeArray, 0, 2);
let shuffled: CustomType[] = Random.shuffle(engine, customTypeArray);
let sampled: CustomType[] = Random.sample(engine, customTypeArray, 2);
numeric = Random.die(5)(engine);
let dieValues: number[] = Random.dice(5, 2)(engine);
let uuid: string = Random.uuid4(engine);
let str: string = Random.string()(engine);
str = Random.string()(engine);
str = Random.string()(engine, 10);
str = Random.string('abc')(engine);
str = Random.string('abc')(engine, 2);
let hex: string = Random.hex()(engine);
hex = Random.hex()(engine, 5);
hex = Random.hex(false)(engine);
hex = Random.hex(false)(engine, 5);
hex = Random.hex(true)(engine);
hex = Random.hex(true)(engine, 5);
let date: Date = Random.date(new Date(), new Date())(engine);
// "Alternate" API (https://github.com/ckknight/random-js#alternate-api)
let random: Random = new Random(engine);
random = new Random();
random = Random();
numeric = random.integer(0, 99);
numeric = random.real(0, 10);
numeric = random.real(0, 10, true);
bool = random.bool();
bool = random.bool(50);
bool = random.bool(1, 2);
chosen = random.pick(customTypeArray);
chosen = random.pick(customTypeArray, 0);
chosen = random.pick(customTypeArray, 0, 2);
shuffled = random.shuffle(customTypeArray);
sampled = random.sample(customTypeArray, 2);
numeric = random.die(5);
dieValues = random.dice(5, 2);
uuid = random.uuid4(engine);
uuid = random.uuid4();
str = random.string();
str = random.string(10);
str = random.string(10, 'abc');
hex = random.hex();
hex = random.hex(5);
hex = random.hex(5, false);
hex = random.hex(undefined, false);
hex = random.hex(5, true);
hex = random.hex(undefined, true);
date = random.date(new Date(), new Date());
// Examples taken from the documentation at https://github.com/ckknight/random-js
// create a Mersenne Twister-19937 that is auto-seeded based on time and other random values
engine = Random.engines.mt19937().autoSeed();
// create a distribution that will consistently produce integers within inclusive range [0, 99].
const distribution: ((engine: Engine) => number) = Random.integer(0, 99);
// generate a number that is guaranteed to be within [0, 99] without any particular bias.
function generateNaturalLessThan100(): number {
return distribution(engine);
}
// NOTE: The documentation for this example is incorrect in the library documentation
// using essentially Math.random()
const engine2: Engine = Random.engines.nativeMath;
// lower-case Hex string distribution
const distribution2: ((engine: Engine, length: number) => string) = Random.hex(false);
// generate a 40-character hex string
function generateSHA1(): string {
return distribution2(engine2, 40);
}
let r: Random = new Random(Random.engines.mt19937().seedWithArray([0x12345678, 0x90abcdef]));
const value = r.integer(0, 99);
r = new Random(); // same as new Random(Random.engines.nativeMath)
-23
View File
@@ -1,23 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"random-js-tests.ts"
]
}
-4
View File
@@ -1,4 +0,0 @@
{
"extends": "dtslint/dt.json",
"rules": {}
}