Skip to content

Commit d971adc

Browse files
committed
feat: throw a TypeError if hash is not a string for murmur functions
The hash functions murmurhash2_x86_32 and murmurhash3_x86_32 now throw a TypeError with a helpful error message if the value passed for the hash is not a string. Beforhand both functions already throwed an error, but with a non helpful message.
1 parent 31b2b42 commit d971adc

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/fns/murmurhash2_x86_32.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/** @module number-generator/lib/murmurhash2_x86_32 */
2-
import { uMul32Getter, uInt32Getter, throwInvalidMurmurSeed } from '../utils';
2+
import {
3+
uMul32Getter,
4+
uInt32Getter,
5+
throwInvalidMurmurSeed,
6+
throwInvalidStringHash
7+
} from '../utils';
38

49
/**
510
* Generate a non-cryptic number hash with murmur2 algorithm
@@ -31,13 +36,15 @@ export default (() => {
3136

3237
/**
3338
* Generate a non-cryptic number hash with murmur2 algorithm
34-
39+
*
40+
* @throws {TypeError} Throws an exception if hash is not a string
3541
* @throws {TypeError} Throws an exception if seed is a float
3642
* @param {string} hash The base string hash to generate number
3743
* @param {number} [seed=0] An optional seed value
3844
* @return {number} Generated number
3945
*/
4046
function murmurhash2_x86_32(hash, seed = 0) {
47+
throwInvalidStringHash(hash, 'murmurhash2_x86_32');
4148
throwInvalidMurmurSeed(seed);
4249

4350
let currentIndex = 0;

src/fns/murmurhash3_x86_32.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/** @module number-generator/lib/murmurhash3_x86_32 */
2-
import { uMul32Getter, uInt32Getter, throwInvalidMurmurSeed } from '../utils';
2+
import {
3+
uMul32Getter,
4+
uInt32Getter,
5+
throwInvalidMurmurSeed,
6+
throwInvalidStringHash
7+
} from '../utils';
38

49
/**
510
* Generate a non-cryptic number hash with murmur3 algorithm
@@ -33,13 +38,15 @@ export default (() => {
3338

3439
/**
3540
* Generate a non-cryptic number hash with murmur3 algorithm
36-
41+
*
42+
* @throws {TypeError} Throws an exception if hash is not a string
3743
* @throws {TypeError} Throws an exception if seed is a float
3844
* @param {string} hash The base string hash to generate number
3945
* @param {number} [seed=0] An optional seed value
4046
* @return {number} Generated number
4147
*/
4248
function murmurhash3_x86_32(hash, seed = 0) {
49+
throwInvalidStringHash(hash, 'murmurhash3_x86_32');
4350
throwInvalidMurmurSeed(seed);
4451

4552
const remainder = hash.length % 4;

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@ export function throwInvalidAleaSeed(seed) {
5757
);
5858
}
5959
}
60+
61+
/**
62+
* Throw an error if a given hash is not a string
63+
*
64+
* @private
65+
* @param {string} hash The possible empty hash value
66+
* @param {string} [functionName] An optional function to enhance the error message
67+
*/
68+
export function throwInvalidStringHash(hash, functionName = '') {
69+
if (typeof hash !== 'string') {
70+
const errorMessagePrefix = functionName ? `${functionName}() ` : '';
71+
throw new TypeError(
72+
`${errorMessagePrefix}first argument is not a string.`
73+
);
74+
}
75+
}

0 commit comments

Comments
 (0)