diff --git a/weighted/weighted-tests.ts b/weighted/weighted-tests.ts new file mode 100644 index 0000000000..8b8b828f0a --- /dev/null +++ b/weighted/weighted-tests.ts @@ -0,0 +1,30 @@ +/// +import * as weighted from 'weighted'; + +function testSet() { + var options = ['Wake Up', 'Snooze Alarm']; + var weights = [0.25, 0.75]; + + console.log('Decision:', weighted.select(options, weights)); +} + +function testObj() { + var options = { + 'Wake Up': 0.25, + 'Snooze Alarm': 0.75 + }; + + console.log('Decision:', weighted.select(options)); +} + +function testOverrideRand() { + var options = ['Wake Up', 'Snooze Alarm']; + var weights = [0.25, 0.75]; + + function rand() { + return 4; // chosen by fair dice roll. + // guaranteed to be random. + } + + console.log('Decision:', weighted.select(options, weights, rand)); +} diff --git a/weighted/weighted.d.ts b/weighted/weighted.d.ts new file mode 100644 index 0000000000..3f479ef0c1 --- /dev/null +++ b/weighted/weighted.d.ts @@ -0,0 +1,13 @@ +// Type definitions for weighted +// Project: https://github.com/Schoonology/weighted +// Definitions by: Craig Citro +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module 'weighted' { + export interface RandomFunc { + (): Number; + } + + export function select (set: T[], weights: Number[], rand?: RandomFunc): T; + export function select (obj: Object, rand?: RandomFunc): T; +} \ No newline at end of file