Skip to content

Commit

Permalink
update(weighted): DT definition changes (#45939)
Browse files Browse the repository at this point in the history
This adds changes discussed in merged PR:
`#45633

- CommonJS module shape
- proper alias for select
- TS config and TSLint config updated to just follow DT defaults
- tests amended with modern syntax - but still using existing tests

Thanks!
  • Loading branch information
peterblazejewicz committed Jul 10, 2020
1 parent 181b253 commit 7b85557
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
13 changes: 3 additions & 10 deletions 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 <https://github.com/ccitro>
// Dmitry Minkovsky <https://github.com/dminkovsky>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

declare module 'weighted' {
export interface RandomFunc {
(): number;
}
import weighted = require('./lib/weighted');

export default select;

export function select<T> (set: T[], weights: number[], rand?: RandomFunc): T;
export function select (obj: {[index: string]: number}, rand?: RandomFunc): string;
}
export = weighted;
20 changes: 20 additions & 0 deletions 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<T>(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;
3 changes: 1 addition & 2 deletions types/weighted/tsconfig.json
Expand Up @@ -2,8 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
Expand Down
19 changes: 1 addition & 18 deletions 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"
}
21 changes: 13 additions & 8 deletions types/weighted/weighted-tests.ts
@@ -1,32 +1,37 @@
import weighted, { select } from 'weighted';
/// <reference lib="dom" />
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));
}

0 comments on commit 7b85557

Please sign in to comment.