Add .d.ts for weighted (#9687)

This commit is contained in:
Craig Citro 2016-06-18 22:21:39 -05:00 committed by Masahiro Wakame
parent eb1ebbda02
commit 2ae9de3df3
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,30 @@
/// <reference path="./weighted.d.ts" />
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));
}

13
weighted/weighted.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
// Type definitions for weighted
// Project: https://github.com/Schoonology/weighted
// Definitions by: Craig Citro <https://github.com/ccitro>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module 'weighted' {
export interface RandomFunc {
(): Number;
}
export function select<T> (set: T[], weights: Number[], rand?: RandomFunc): T;
export function select<T> (obj: Object, rand?: RandomFunc): T;
}