Merge pull request #18001 from Morfent/integer

[integer] Add type declaration file
This commit is contained in:
Ryan Cavanaugh
2017-07-14 11:10:33 -07:00
committed by GitHub
4 changed files with 164 additions and 0 deletions
+110
View File
@@ -0,0 +1,110 @@
// Type definitions for integer 1.0
// Project: https://github.com/JoshuaWise/integer#readme
// Definitions by: Ben Davies <https://github.com/Morfent>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// This can't just be written as a simple class since Integer instances can't
// be constructed by its actual constructor without throwing, but calling it as
// a regular function constructs new instances just fine...
declare function Integer(val: Integer.IntLike): Integer.IntClass;
declare namespace Integer {
type IntLike = number | string | IntClass;
function fromBits(low: number, high?: number): IntClass;
function fromNumber(val: number, defaultVal?: number | IntClass): IntClass;
function fromString(val: string, radix?: number, defaultVal?: string | IntClass): IntClass;
function isInstance(val: any): boolean;
const MAX_VALUE: IntClass;
const MIN_VALUE: IntClass;
const ZERO: IntClass;
const ONE: IntClass;
const NEG_ONE: IntClass;
class IntClass {
low: number;
high: number;
constructor(val: IntLike);
add(augend: IntLike): IntClass;
plus(augend: IntLike): IntClass;
sub(subtrahend: IntLike): IntClass;
subtract(subtrahend: IntLike): IntClass;
minus(subtrahend: IntLike): IntClass;
mul(multiplier: IntLike): IntClass;
multiply(multiplier: IntLike): IntClass;
times(multiplier: IntLike): IntClass;
div(divisor: IntLike): IntClass;
divide(divisor: IntLike): IntClass;
divideBy(divisor: IntLike): IntClass;
dividedBy(divisor: IntLike): IntClass;
over(divisor: IntLike): IntClass;
mod(divisor: IntLike): IntClass;
modulo(divisor: IntLike): IntClass;
neg(): IntClass;
negate(): IntClass;
abs(): IntClass;
absoluteValue(): IntClass;
and(bits: IntLike): IntClass;
or(bits: IntLike): IntClass;
xor(bits: IntLike): IntClass;
not(): IntClass;
shl(bits: number): IntClass;
shiftLeft(bits: number): IntClass;
shr(bits: number): IntClass;
shiftRight(bits: number): IntClass;
eq(val: IntLike): boolean;
equals(val: IntLike): boolean;
isEqualTo(val: IntLike): boolean;
neq(val: IntLike): boolean;
notEquals(val: IntLike): boolean;
isNotEqualTo(val: IntLike): boolean;
doesNotEqual(val: IntLike): boolean;
gt(val: IntLike): boolean;
greaterThan(val: IntLike): boolean;
isGreaterThan(val: IntLike): boolean;
gte(val: IntLike): boolean;
greaterThanOrEquals(val: IntLike): boolean;
isGreaterThanOrEqualTo(val: IntLike): boolean;
lt(val: IntLike): boolean;
lessThan(val: IntLike): boolean;
isLessThan(val: IntLike): boolean;
lte(val: IntLike): boolean;
lessThanOrEquals(val: IntLike): boolean;
isLessThanOrEqualTo(val: IntLike): boolean;
compare(val: IntLike): -1 | 0 | 1;
bitSizeAbs(): number;
isEven(): boolean;
isOdd(): boolean;
isPositive(): boolean;
isNegative(): boolean;
isZero(): boolean;
isNonZero(): boolean;
isNotZero(): boolean;
isSafe(): boolean;
isUnsafe(): boolean;
toNumber(): number;
toNumberUnsafe(): number;
toString(radix?: number): string;
valueOf(): number;
}
}
export = Integer;
+30
View File
@@ -0,0 +1,30 @@
/// <reference types="node" />
import Integer = require('integer');
let num0: Integer.IntClass = Integer(0);
num0 = num0.add(10);
console.assert(!num0.compare(20));
num0 = num0.add('10');
console.assert(!num0.compare(30));
num0 = num0.add(num0);
console.assert(!num0.compare(60));
let num1: Integer.IntClass = Integer.fromBits(0xFF);
let num2: Integer.IntClass = Integer.fromBits(0xFF, 0xFF);
num1 = num1.shl(32);
console.assert(!num1.compare(num2));
let num3: Integer.IntClass = Integer.fromNumber(10);
let num4: Integer.IntClass = Integer.fromNumber(10, 10);
console.assert(!num3.compare(num4));
num4 = Integer.fromNumber(10, num3);
console.assert(!num3.compare(num4));
let num5: Integer.IntClass = Integer.fromString('255');
let num6: Integer.IntClass = Integer.fromString('ff', 16);
console.assert(!num5.compare(num6));
let num7: Integer.IntClass = Integer.fromString('ff', 16, '255');
console.assert(!num5.compare(num7));
num7 = Integer.fromString('ff', 16, num5);
console.assert(!num6.compare(num7));
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"integer-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }