mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-02-03 23:42:50 +00:00
Add roll (#25340)
This commit is contained in:
parent
363fbd9d5f
commit
c19e1e8289
55
types/roll/index.d.ts
vendored
Normal file
55
types/roll/index.d.ts
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
// Type definitions for roll 1.2
|
||||
// Project: https://github.com/troygoode/node-roll/
|
||||
// Definitions by: icopp <https://github.com/icopp>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
type RollTransformation = RollTransformationKey | [RollTransformationKey, number] | ((results: number[]) => number[]);
|
||||
|
||||
type RollTransformationKey = 'sum' | 'add' | 'subtract' | 'multiply' | 'divide' | 'best-of' | 'worst-of';
|
||||
|
||||
interface RollObject {
|
||||
quantity: number;
|
||||
sides: number;
|
||||
transformations: RollTransformation[];
|
||||
toString: () => string;
|
||||
}
|
||||
|
||||
interface RollOutput {
|
||||
input: RollObject;
|
||||
calculations: number[];
|
||||
rolled: number[];
|
||||
result: number;
|
||||
}
|
||||
|
||||
declare class InvalidInputError extends Error {
|
||||
name: 'InvalidInputError';
|
||||
}
|
||||
|
||||
declare class Roll {
|
||||
static InvalidInputError: InvalidInputError;
|
||||
|
||||
constructor(seed?: () => number);
|
||||
|
||||
/**
|
||||
* Validate user input
|
||||
*/
|
||||
validate(input: string): boolean;
|
||||
|
||||
/**
|
||||
* Parse a string into a roll object
|
||||
* @throws InvalidInputError
|
||||
*/
|
||||
parse(input: string): {
|
||||
quantity: number;
|
||||
sides: number;
|
||||
transformations: RollTransformation[];
|
||||
toString: () => string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Roll based on a string or roll object
|
||||
*/
|
||||
roll(input: string | RollObject): RollOutput;
|
||||
}
|
||||
|
||||
export = Roll;
|
||||
20
types/roll/roll-tests.ts
Normal file
20
types/roll/roll-tests.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import Roll = require('roll');
|
||||
|
||||
// $ExpectType Roll
|
||||
const roll = new Roll();
|
||||
|
||||
// $ExpectType number
|
||||
roll.roll('d6').result;
|
||||
|
||||
// $ExpectType number
|
||||
roll.roll({
|
||||
quantity: 2,
|
||||
sides: 6,
|
||||
transformations: [
|
||||
'sum',
|
||||
['add', 2]
|
||||
]
|
||||
}).result;
|
||||
|
||||
// $ExpectType InvalidInputError
|
||||
Roll.InvalidInputError;
|
||||
23
types/roll/tsconfig.json
Normal file
23
types/roll/tsconfig.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"roll-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/roll/tslint.json
Normal file
1
types/roll/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user