diff --git a/types/roll/index.d.ts b/types/roll/index.d.ts new file mode 100644 index 0000000000..7c3e4f70be --- /dev/null +++ b/types/roll/index.d.ts @@ -0,0 +1,55 @@ +// Type definitions for roll 1.2 +// Project: https://github.com/troygoode/node-roll/ +// Definitions by: 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; diff --git a/types/roll/roll-tests.ts b/types/roll/roll-tests.ts new file mode 100644 index 0000000000..bf28963e05 --- /dev/null +++ b/types/roll/roll-tests.ts @@ -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; diff --git a/types/roll/tsconfig.json b/types/roll/tsconfig.json new file mode 100644 index 0000000000..6d7640286d --- /dev/null +++ b/types/roll/tsconfig.json @@ -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" + ] +} diff --git a/types/roll/tslint.json b/types/roll/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/roll/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }