From 6fdf789f545fe9baa1d5cd028dc3b3eddb690937 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 6 Oct 2016 07:07:45 -0700 Subject: [PATCH] Types 2.0: Move UUID.js and add type definitions for npm package uuid (#11785) * Move UUID.js type definitions from uuid to uuidjs Issue: #10766 * Add type definitions for npm package: uuid Issue: #10766 * Minor fix in uuidjs tests file * Clean up UUID types * Convert to external module * Formatting fixes * Allow the `export = uuid` to be called as a function directly --- uuid/index.d.ts | 101 +++++++++------------------------ uuid/tsconfig.json | 2 +- uuid/uuid-tests.ts | 31 ++++++++++ {uuid => uuidjs}/UUID-tests.ts | 2 +- uuidjs/index.d.ts | 82 ++++++++++++++++++++++++++ uuidjs/tsconfig.json | 19 +++++++ 6 files changed, 161 insertions(+), 76 deletions(-) create mode 100644 uuid/uuid-tests.ts rename {uuid => uuidjs}/UUID-tests.ts (98%) create mode 100644 uuidjs/index.d.ts create mode 100644 uuidjs/tsconfig.json diff --git a/uuid/index.d.ts b/uuid/index.d.ts index 24ab5eb160..2fd5467e3e 100644 --- a/uuid/index.d.ts +++ b/uuid/index.d.ts @@ -1,82 +1,35 @@ -// Type definitions for UUID.js v3.3.0 -// Project: https://github.com/LiosK/UUID.js -// Definitions by: Jason Jarrett +// Type definitions for uuid v2.0.3 +// Project: https://github.com/defunctzombie/node-uuid +// Definitions by: Oliver Hoffmann // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// +declare namespace uuid { + interface V1Options { + node?: number[]; + clockseq?: number; + msecs?: number | Date; + nsecs?: number; + } -interface UUID { - intFields: UUIDArray; - bitFields: UUIDArray; - hexFields: UUIDArray; - version: number; - bitString: string; - hexString: string; - urn: string; + type V4Options = { random: number[] } | { rng: () => number[]; } + interface UuidStatic { + (options?: V4Options): string; + (options: V4Options | null, buffer: number[], offset?: number): number[]; + (options: V4Options | null, buffer: Buffer, offset?: number): Buffer; - /** - * Tests if two {@link UUID} objects are equal. - * @param {UUID} uuid - * @returns {bool} True if two {@link UUID} objects are equal. - */ - equals(uuid: UUID): boolean; - - /** - * Returns UUID string representation. - * @returns {string} {@link UUID#hexString}. - */ - toString(): string; + v1(options?: V1Options): string; + v1(options: V1Options | null, buffer: number[], offset?: number): number[]; + v1(options: V1Options | null, buffer: Buffer, offset?: number): Buffer; + v4: UuidStatic; + parse(id: string): number[]; + parse(id: string, buffer: number[], offset?: number): number[]; + parse(id: string, buffer: Buffer, offset?: number): Buffer; + unparse(buffer: number[] | Buffer, offset?: number): string; + } } -interface UUIDArray extends Array { - timeLow: string; - timeMid: string; - timeHiAndVersion: string; - clockSeqHiAndReserved: string; - clockSeqLow: string; - node: string; -} - -/** - * The simplest function to get an UUID string. - * @returns {string} A version 4 UUID string. - */ -export declare function generate(): string; - -/** - * Generates a version 4 {@link UUID}. - * @returns {UUID} A version 4 {@link UUID} object. - * @since 3.0 - */ -export declare function genV4(): UUID; - - -/** - * Generates a version 1 {@link UUID}. - * @returns {UUID} A version 1 {@link UUID} object. - * @since 3.0 - */ -export declare function genV1(): UUID; - -/** - * Converts hexadecimal UUID string to an {@link UUID} object. - * @param {string} uuid UUID hexadecimal string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"). - * @returns {UUID} {@link UUID} object or null. - * @since 3.0 - */ -export declare function parse(uuid: string): UUID; - - -/** - * Re-initializes version 1 UUID state. - * @since 3.0 - */ -export declare function resetState(): void; - -/** - * Reinstalls {@link UUID.generate} method to emulate the interface of UUID.js version 2.x. - * @since 3.1 - * @deprecated Version 2.x. compatible interface is not recommended. - */ -export declare function makeBackwardCompatible(): void; +declare const uuid: uuid.UuidStatic +export = uuid diff --git a/uuid/tsconfig.json b/uuid/tsconfig.json index 07fa3344c9..02a01e4383 100644 --- a/uuid/tsconfig.json +++ b/uuid/tsconfig.json @@ -16,4 +16,4 @@ "index.d.ts", "uuid-tests.ts" ] -} \ No newline at end of file +} diff --git a/uuid/uuid-tests.ts b/uuid/uuid-tests.ts new file mode 100644 index 0000000000..2dd5ea7f46 --- /dev/null +++ b/uuid/uuid-tests.ts @@ -0,0 +1,31 @@ +import uuid = require('uuid'); + +let uuidv1: string = uuid.v1(); + +uuidv1 = uuid.v1({ + node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab], + clockseq: 0x1234, + msecs: new Date('2011-11-01').getTime(), + nsecs: 5678 +}); + +let bufferv1: number[] = new Array(32); +bufferv1 = uuid.v1(null, bufferv1); +bufferv1 = uuid.v1(null, bufferv1, 16); + +let uuidv4: string = uuid.v4(); + +const randoms = [ + 0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, + 0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36 +]; +uuidv4 = uuid({ random: randoms }); +uuidv4 = uuid({ rng: () => randoms }) + +let bufferv4: number[] = new Array(32); +bufferv4 = uuid(null, bufferv4); +bufferv4 = uuid(null, bufferv4, 16); + +let nodeBufferv4 = Buffer.alloc(32); +nodeBufferv4 = uuid.v4(null, nodeBufferv4); +nodeBufferv4 = uuid.v4(null, nodeBufferv4, 16); diff --git a/uuid/UUID-tests.ts b/uuidjs/UUID-tests.ts similarity index 98% rename from uuid/UUID-tests.ts rename to uuidjs/UUID-tests.ts index 2ba8fa57eb..51bed1c700 100644 --- a/uuid/UUID-tests.ts +++ b/uuidjs/UUID-tests.ts @@ -1,4 +1,4 @@ -import UUID = require("uuid"); +import UUID = require("uuidjs"); const uuid1: string = UUID.generate() const uuid2: UUID.UUID = UUID.genV4() diff --git a/uuidjs/index.d.ts b/uuidjs/index.d.ts new file mode 100644 index 0000000000..24ab5eb160 --- /dev/null +++ b/uuidjs/index.d.ts @@ -0,0 +1,82 @@ +// Type definitions for UUID.js v3.3.0 +// Project: https://github.com/LiosK/UUID.js +// Definitions by: Jason Jarrett +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + + + +interface UUID { + intFields: UUIDArray; + bitFields: UUIDArray; + hexFields: UUIDArray; + version: number; + bitString: string; + hexString: string; + urn: string; + + + /** + * Tests if two {@link UUID} objects are equal. + * @param {UUID} uuid + * @returns {bool} True if two {@link UUID} objects are equal. + */ + equals(uuid: UUID): boolean; + + /** + * Returns UUID string representation. + * @returns {string} {@link UUID#hexString}. + */ + toString(): string; +} + +interface UUIDArray extends Array { + timeLow: string; + timeMid: string; + timeHiAndVersion: string; + clockSeqHiAndReserved: string; + clockSeqLow: string; + node: string; +} + +/** + * The simplest function to get an UUID string. + * @returns {string} A version 4 UUID string. + */ +export declare function generate(): string; + +/** + * Generates a version 4 {@link UUID}. + * @returns {UUID} A version 4 {@link UUID} object. + * @since 3.0 + */ +export declare function genV4(): UUID; + + +/** + * Generates a version 1 {@link UUID}. + * @returns {UUID} A version 1 {@link UUID} object. + * @since 3.0 + */ +export declare function genV1(): UUID; + +/** + * Converts hexadecimal UUID string to an {@link UUID} object. + * @param {string} uuid UUID hexadecimal string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"). + * @returns {UUID} {@link UUID} object or null. + * @since 3.0 + */ +export declare function parse(uuid: string): UUID; + + +/** + * Re-initializes version 1 UUID state. + * @since 3.0 + */ +export declare function resetState(): void; + +/** + * Reinstalls {@link UUID.generate} method to emulate the interface of UUID.js version 2.x. + * @since 3.1 + * @deprecated Version 2.x. compatible interface is not recommended. + */ +export declare function makeBackwardCompatible(): void; diff --git a/uuidjs/tsconfig.json b/uuidjs/tsconfig.json new file mode 100644 index 0000000000..559d117755 --- /dev/null +++ b/uuidjs/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "UUID-tests.ts" + ] +}