Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update(weighted): DT definition changes (#45939)
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
1 parent
181b253
commit 7b85557
Showing
5 changed files
with
38 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |