diff --git a/types/weighted/index.d.ts b/types/weighted/index.d.ts index b60b38118e57e1..e755bb79abf60e 100644 --- a/types/weighted/index.d.ts +++ b/types/weighted/index.d.ts @@ -1,16 +1,9 @@ -// Type definitions for weighted +// Type definitions for weighted 0.3 // Project: https://github.com/Schoonology/weighted // Definitions by: Craig Citro // Dmitry Minkovsky // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare module 'weighted' { - export interface RandomFunc { - (): number; - } +import weighted = require('./lib/weighted'); - export default select; - - export function select (set: T[], weights: number[], rand?: RandomFunc): T; - export function select (obj: {[index: string]: number}, rand?: RandomFunc): string; -} +export = weighted; diff --git a/types/weighted/lib/weighted.d.ts b/types/weighted/lib/weighted.d.ts new file mode 100644 index 00000000000000..d31e7e0028b2f8 --- /dev/null +++ b/types/weighted/lib/weighted.d.ts @@ -0,0 +1,20 @@ +import { RandomFunc } from '../'; + +/** + * Weighted returns a Function additionally available as `weighted.select` + */ +declare function weighted(set: T[], weights: number[], rand?: RandomFunc): T; +declare function weighted(obj: { [index: string]: number }, rand?: RandomFunc): string; + +/** + * A dead-simple module for picking an item from a set of items while picking some more frequently than others. + * Each item is given a numerical "weight": each item's likelihood to be selected is directly proportional to its share of the total weight. + */ +declare namespace weighted { + interface RandomFunc { + (): number; + } + + const select: typeof weighted; +} +export = weighted; diff --git a/types/weighted/tsconfig.json b/types/weighted/tsconfig.json index bce73e30ed5203..9b66982a6f4b3a 100644 --- a/types/weighted/tsconfig.json +++ b/types/weighted/tsconfig.json @@ -2,8 +2,7 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6", - "dom" + "es6" ], "noImplicitAny": true, "noImplicitThis": true, diff --git a/types/weighted/tslint.json b/types/weighted/tslint.json index 1ff3a57d8f3883..f93cf8562ad24d 100644 --- a/types/weighted/tslint.json +++ b/types/weighted/tslint.json @@ -1,20 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "ban-types": false, - "callable-types": false, - "dt-header": false, - "eofline": false, - "no-declare-current-package": false, - "no-redundant-jsdoc": false, - "no-single-declare-module": false, - "no-trailing-whitespace": false, - "no-unnecessary-generics": false, - "no-var-keyword": false, - "prefer-const": false, - "prefer-method-signature": false, - "space-before-function-paren": false, - "strict-export-declare-modifiers": false, - "npm-naming": [true,{"mode":"code","errors":[["NeedsExportEquals",false]]}] - } + "extends": "dtslint/dt.json" } diff --git a/types/weighted/weighted-tests.ts b/types/weighted/weighted-tests.ts index b922070db3d3ba..73982ddda9df02 100644 --- a/types/weighted/weighted-tests.ts +++ b/types/weighted/weighted-tests.ts @@ -1,32 +1,37 @@ -import weighted, { select } from 'weighted'; +/// +import weighted = require('weighted'); +import { select } from 'weighted'; function testSet() { - var options = ['Wake Up', 'Snooze Alarm']; - var weights = [0.25, 0.75]; + const options = ['Wake Up', 'Snooze Alarm']; + const weights = [0.25, 0.75]; console.log('Decision:', weighted(options, weights)); + console.log('Decision:', weighted.select(options, weights)); console.log('Decision:', select(options, weights)); } function testObj() { - var options = { + const options = { 'Wake Up': 0.25, - 'Snooze Alarm': 0.75 + 'Snooze Alarm': 0.75, }; console.log('Decision:', weighted(options)); + console.log('Decision:', weighted.select(options)); console.log('Decision:', select(options)); } function testOverrideRand() { - var options = ['Wake Up', 'Snooze Alarm']; - var weights = [0.25, 0.75]; + const options = ['Wake Up', 'Snooze Alarm']; + const weights = [0.25, 0.75]; function rand() { return 4; // chosen by fair dice roll. - // guaranteed to be random. + // guaranteed to be random. } console.log('Decision:', weighted(options, weights, rand)); + console.log('Decision:', weighted.select(options, weights, rand)); console.log('Decision:', select(options, weights, rand)); }