From 5ac9294b5d3d2bde4da09265b4e2afda5345fcca Mon Sep 17 00:00:00 2001 From: Christoph Tavan Date: Wed, 12 Aug 2020 07:52:30 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#46395=20Upgrade=20?= =?UTF-8?q?types=20for=20uuid@8.3.0=20by=20@ctavan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revert "added typing for "validate" function added in uuid release 8.3.0 (#46557)" This reverts commit c50f1201c79757354dc893347c85f1f1b3592b5c. * Upgrade types for uuid@8.3.0 * Export v1/v4 options --- types/uuid/index.d.ts | 67 ++++++++++++++++++++++++++++++++++++-- types/uuid/interfaces.d.ts | 52 ----------------------------- types/uuid/uuid-tests.ts | 41 +++++++++++++++++++++-- 3 files changed, 104 insertions(+), 56 deletions(-) delete mode 100644 types/uuid/interfaces.d.ts diff --git a/types/uuid/index.d.ts b/types/uuid/index.d.ts index c418e8b3e1..1810c3ba07 100644 --- a/types/uuid/index.d.ts +++ b/types/uuid/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for uuid 8.0 +// Type definitions for uuid 8.3 // Project: https://github.com/uuidjs/uuid // Definitions by: Oliver Hoffmann // Felipe Ochoa @@ -8,10 +8,73 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 -import { v1, v3, v4, v5, validate } from './interfaces'; +// disable automatic export +export {}; +// Uses ArrayLike to admit Unit8 and co. +type OutputBuffer = ArrayLike; +type InputBuffer = ArrayLike; + +interface RandomOptions { + random?: InputBuffer; +} +interface RngOptions { + rng?: () => InputBuffer; +} + +interface V1BaseOptions { + node?: InputBuffer; + clockseq?: number; + msecs?: number | Date; + nsecs?: number; +} +interface V1RandomOptions extends V1BaseOptions, RandomOptions {} +interface V1RngOptions extends V1BaseOptions, RngOptions {} + +export type V1Options = V1RandomOptions | V1RngOptions; +export type V4Options = RandomOptions | RngOptions; + +type v1String = (options?: V1Options) => string; +type v1Buffer = (options: V1Options | null | undefined, buffer: T, offset?: number) => T; +type v1 = v1Buffer & v1String; + +type v4String = (options?: V4Options) => string; +type v4Buffer = (options: V4Options | null | undefined, buffer: T, offset?: number) => T; +type v4 = v4Buffer & v4String; + +type v3String = (name: string | InputBuffer, namespace: string | InputBuffer) => string; +type v3Buffer = (name: string | InputBuffer, namespace: string | InputBuffer, buffer: T, offset?: number) => T; +interface v3Static { + // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L22 + DNS: string; + // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L23 + URL: string; +} +type v3 = v3Buffer & v3String & v3Static; + +type v5String = (name: string | InputBuffer, namespace: string | InputBuffer) => string; +type v5Buffer = (name: string | InputBuffer, namespace: string | InputBuffer, buffer: T, offset?: number) => T; +interface v5Static { + // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L22 + DNS: string; + // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L23 + URL: string; +} +type v5 = v5Buffer & v5String & v5Static; + +type NIL = string; + +type parse = (uuid: string) => OutputBuffer; +type stringify = (buffer: InputBuffer, offset?: number) => string; +type validate = (uuid: string) => boolean; +type version = (uuid: string) => number; + +export const NIL: NIL; +export const parse: parse; +export const stringify: stringify; export const v1: v1; export const v3: v3; export const v4: v4; export const v5: v5; export const validate: validate; +export const version: version; diff --git a/types/uuid/interfaces.d.ts b/types/uuid/interfaces.d.ts deleted file mode 100644 index ce2c6da645..0000000000 --- a/types/uuid/interfaces.d.ts +++ /dev/null @@ -1,52 +0,0 @@ -// Uses ArrayLike to admit Unit8 and co. -export type OutputBuffer = ArrayLike; -export type InputBuffer = ArrayLike; - -export interface RandomOptions { - random?: InputBuffer; -} -export interface RngOptions { - rng?: () => InputBuffer; -} - -export interface V1BaseOptions { - node?: InputBuffer; - clockseq?: number; - msecs?: number | Date; - nsecs?: number; -} -export interface V1RandomOptions extends V1BaseOptions, RandomOptions {} -export interface V1RngOptions extends V1BaseOptions, RngOptions {} - -export type V1Options = V1RandomOptions | V1RngOptions; -export type V4Options = RandomOptions | RngOptions; - -export type v1String = (options?: V1Options) => string; -export type v1Buffer = (options: V1Options | null | undefined, buffer: T, offset?: number) => T; -export type v1 = v1Buffer & v1String; - -export type v4String = (options?: V4Options) => string; -export type v4Buffer = (options: V4Options | null | undefined, buffer: T, offset?: number) => T; -export type v4 = v4Buffer & v4String; - -export type v3String = (name: string | InputBuffer, namespace: string | InputBuffer) => string; -export type v3Buffer = (name: string | InputBuffer, namespace: string | InputBuffer, buffer: T, offset?: number) => T; -export interface v3Static { - // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L22 - DNS: string; - // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L23 - URL: string; -} -export type v3 = v3Buffer & v3String & v3Static; - -export type v5String = (name: string | InputBuffer, namespace: string | InputBuffer) => string; -export type v5Buffer = (name: string | InputBuffer, namespace: string | InputBuffer, buffer: T, offset?: number) => T; -export interface v5Static { - // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L22 - DNS: string; - // https://github.com/uuidjs/uuid/blob/master/src/v35.js#L23 - URL: string; -} -export type v5 = v5Buffer & v5String & v5Static; - -export type validate = (uuid: string) => boolean; diff --git a/types/uuid/uuid-tests.ts b/types/uuid/uuid-tests.ts index 59cefc5c2d..f5cb7b3b0e 100644 --- a/types/uuid/uuid-tests.ts +++ b/types/uuid/uuid-tests.ts @@ -1,6 +1,18 @@ /// -import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5, validate as uuidValidate } from 'uuid'; +import { + v1 as uuidv1, + v4 as uuidv4, + v3 as uuidv3, + v5 as uuidv5, + NIL as NIL_UUID, + parse as uuidParse, + stringify as uuidStringify, + validate as uuidValidate, + version as uuidVersion, + V1Options, + V4Options, +} from 'uuid'; const randoms = [ 0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, @@ -25,6 +37,13 @@ stringv1 = uuidv1({ rng: () => randoms, }); +const v1Options: V1Options = { + msecs: new Date('2011-11-01').getTime(), + nsecs: 5678, + rng: () => randoms, +}; +stringv1 = uuidv1(v1Options); + let bufferv1 = new Uint8Array(32); bufferv1 = uuidv1(null, bufferv1); bufferv1 = uuidv1(undefined, bufferv1, 16); @@ -33,6 +52,9 @@ let stringv4: string = uuidv4(); stringv4 = uuidv4({ random: randoms }); stringv4 = uuidv4({ rng: () => randoms }); +const v4Options: V4Options = { random: randoms }; +stringv4 = uuidv4(v4Options); + let bufferv4: number[] = new Array(32); bufferv4 = uuidv4(undefined, bufferv4); bufferv4 = uuidv4(null, bufferv4, 16); @@ -62,4 +84,19 @@ class CustomBuffer extends Uint8Array {} const h = new CustomBuffer(10); uuidv4(null, h); // $ExpectType CustomBuffer -const av: boolean = uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); +const nil5: string = uuidv5('hello', NIL_UUID); + +const stringified: string = uuidStringify(bufferv4); +const parsed: ArrayLike = uuidParse(stringified); + +let valid: boolean = uuidValidate(stringv1); +valid = uuidValidate(stringv4); +valid = uuidValidate(a3); +valid = uuidValidate(a5); +valid = uuidValidate(NIL_UUID); + +let version: number = uuidVersion(stringv1); +version = uuidVersion(stringv4); +version = uuidVersion(a3); +version = uuidVersion(a5); +version = uuidVersion(NIL_UUID);