diff --git a/types/xxhashjs/index.d.ts b/types/xxhashjs/index.d.ts index 40c45cf3ab..9c9d7e4318 100644 --- a/types/xxhashjs/index.d.ts +++ b/types/xxhashjs/index.d.ts @@ -1,19 +1,29 @@ -// Type definitions for xxhashjs 0.1 +// Type definitions for xxhashjs 0.2 // Project: https://github.com/pierrec/js-xxhash // Definitions by: Dibyo Majumdar +// Nick Zahn // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + export as namespace XXH; +// Ideally we would have a type definition for the "cuint" package. +// The following interface `UINT` is to resolve the bare minimum. +interface UINT { + toNumber(): number; + toString(radix?: number): string; +} + export interface HashObject { - init(seed: number): this; - update(data: string | ArrayBuffer): this; - digest(): number; + init(seed: number): this; + update(data: string | ArrayBuffer | Buffer): this; + digest(): UINT; } export interface HashInterface { - (seed?: number): HashObject; - (data: string | ArrayBuffer, seed: number): number; + (seed?: number): HashObject; + (data: string | ArrayBuffer | Buffer, seed: number): UINT; } export const h32: HashInterface; diff --git a/types/xxhashjs/xxhashjs-tests.ts b/types/xxhashjs/xxhashjs-tests.ts index ee2d7ef8f3..889bf9bb40 100644 --- a/types/xxhashjs/xxhashjs-tests.ts +++ b/types/xxhashjs/xxhashjs-tests.ts @@ -1,7 +1,37 @@ import XXH from 'xxhashjs'; -// examples adapted from https://github.com/pierrec/js-xxhash -const h1 = XXH.h32('abcd', 0xABCD).toString(16); +// Test data +const seed = 0xABCD; +const stringData = 'abcd'; +const arrayBufferData = new ArrayBuffer(4); +const bufferData = Buffer.from([1, 2, 3, 4]); -const H = XXH.h32(0xABCD); -const h2 = H.update('abcd').digest().toString(16); +// Test XXH.h32 methods, initialising with string data +const value32_1 = XXH.h32(stringData, seed); +value32_1.toString(); +value32_1.toString(16); +value32_1.toNumber(); + +const value32_2 = XXH.h32(seed).update(stringData).digest(); +value32_2.toString(); +value32_2.toString(16); +value32_2.toNumber(); + +// Test XXH.h64 methods, initialising with string data +const value64_1 = XXH.h64(stringData, seed); +value64_1.toString(); +value64_1.toString(16); +value64_1.toNumber(); + +const value64_2 = XXH.h64(seed).update(stringData).digest(); +value64_2.toString(); +value64_2.toString(16); +value64_2.toNumber(); + +// Initialise with ArrayBuffer +XXH.h32(arrayBufferData, seed); +XXH.h64(arrayBufferData, seed); + +// Initialise with Buffer +XXH.h32(bufferData, seed); +XXH.h64(bufferData, seed);