From 8fe93213c5d85883fcf329905a582b5eaabab9c4 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Wed, 12 Jul 2017 19:41:34 -0300 Subject: [PATCH] [integer] Add type declaration file --- types/integer/index.d.ts | 110 +++++++++++++++++++++++++++++++++ types/integer/integer-tests.ts | 30 +++++++++ types/integer/tsconfig.json | 23 +++++++ types/integer/tslint.json | 1 + 4 files changed, 164 insertions(+) create mode 100644 types/integer/index.d.ts create mode 100644 types/integer/integer-tests.ts create mode 100644 types/integer/tsconfig.json create mode 100644 types/integer/tslint.json diff --git a/types/integer/index.d.ts b/types/integer/index.d.ts new file mode 100644 index 0000000000..c6e256f1bc --- /dev/null +++ b/types/integer/index.d.ts @@ -0,0 +1,110 @@ +// Type definitions for integer 1.0 +// Project: https://github.com/JoshuaWise/integer#readme +// Definitions by: Ben Davies +// 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; diff --git a/types/integer/integer-tests.ts b/types/integer/integer-tests.ts new file mode 100644 index 0000000000..2450423ff7 --- /dev/null +++ b/types/integer/integer-tests.ts @@ -0,0 +1,30 @@ +/// + +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)); diff --git a/types/integer/tsconfig.json b/types/integer/tsconfig.json new file mode 100644 index 0000000000..7c5e9c9791 --- /dev/null +++ b/types/integer/tsconfig.json @@ -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" + ] +} diff --git a/types/integer/tslint.json b/types/integer/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/integer/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }