feat(murmurhash): revise definition to support no module usage (#44802)

This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-05-20 00:31:59 +02:00 committed by GitHub
parent c9cbaf2b25
commit cd44fa1195
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 23 deletions

View File

@ -1,16 +0,0 @@
import MurmurHash3 from "imurmurhash";
const hash = MurmurHash3();
const result: number = hash
.hash("123")
.hash("456")
.reset(123)
.result();
const hash2 = MurmurHash3("test");
const hash3 = MurmurHash3("test", 123);
hash2.result() + hash3.result();
const another = new MurmurHash3();

View File

@ -1,20 +1,50 @@
// Type definitions for imurmurhash 0.1
// Project: https://github.com/jensyt/imurmurhash-js
// Definitions by: Jiayu Liu <https://github.com/Jimexist>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.7
/**
* An incremental implementation of MurmurHash3 for JavaScript
*/
interface MurmurHash3 {
/**
* Get the result of the hash as a 32-bit positive integer.
* This performs the tail and finalizer portions of the algorithm, but does not store the result in the state object.
* This means that it is perfectly safe to get results and then continue adding strings via hash.
*/
result(): number;
reset(seed?: number): MurmurHash3;
/**
* Reset the state object for reuse, optionally using the given seed (defaults to 0 like the constructor). Returns this so calls can be chained.
* @default 0
*/
reset(seed?: number): this;
hash(value: string): MurmurHash3;
/**
* Incrementally add string to the hash.
* This can be called as many times as you want for the hash state object, including after a call to result()
*/
hash(value: string): this;
}
declare var MurmurHash: {
declare var MurmurHash3: {
/**
* Get a hash state object, optionally initialized with the given string and seed.
* Seed must be a positive integer if provided.
* Calling this function without the new keyword will return a cached state object that has been reset.
* This is safe to use as long as the object is only used from a single thread and no other hashes are created while operating on this one.
* If this constraint cannot be met, you can use new to create a new state object
*/
(text?: string, seed?: number): MurmurHash3;
/**
* Get a hash state object, optionally initialized with the given string and seed.
* Seed must be a positive integer if provided.
* Calling this function without the new keyword will return a cached state object that has been reset.
* This is safe to use as long as the object is only used from a single thread and no other hashes are created while operating on this one.
* If this constraint cannot be met, you can use new to create a new state object
*/
new (text?: string, seed?: number): MurmurHash3;
};
export = MurmurHash;
export as namespace MurmurHash3;
export = MurmurHash3;

View File

@ -0,0 +1,10 @@
const hash = MurmurHash3();
const result: number = hash.hash('123').hash('456').reset(123).result();
const hash2 = MurmurHash3('test');
const hash3 = MurmurHash3('test', 123);
hash2.result() + hash3.result();
const another = new MurmurHash3();

View File

@ -0,0 +1,12 @@
import MurmurHash3 = require('imurmurhash');
const hash = MurmurHash3();
const result: number = hash.hash('123').hash('456').reset(123).result();
const hash2 = MurmurHash3('test');
const hash3 = MurmurHash3('test', 123);
hash2.result() + hash3.result();
const another = new MurmurHash3();

View File

@ -13,5 +13,5 @@
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "imurmurhash-tests.ts"]
"files": ["index.d.ts", "test/imurmurhash-tests.module.ts", "test/imurmurhash-tests.global.ts"]
}