mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
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!
This commit is contained in:
parent
181b253d3a
commit
7b855576c6
13
types/weighted/index.d.ts
vendored
13
types/weighted/index.d.ts
vendored
@ -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
types/weighted/lib/weighted.d.ts
vendored
Normal file
20
types/weighted/lib/weighted.d.ts
vendored
Normal file
@ -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;
|
||||
@ -2,8 +2,7 @@
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
|
||||
@ -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"
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user