mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
Merge pull request #32509 from Manc/xxhashjs-0.2
[xxhashjs] Upgrade to 0.2, fix return type, add more tests
This commit is contained in:
Vendored
+16
-6
@@ -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 <https://github.com/mDibyo>
|
||||
// Nick Zahn <https://github.com/Manc>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user