File tree Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 1
1
/** @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' ;
3
8
4
9
/**
5
10
* Generate a non-cryptic number hash with murmur2 algorithm
@@ -31,13 +36,15 @@ export default (() => {
31
36
32
37
/**
33
38
* Generate a non-cryptic number hash with murmur2 algorithm
34
-
39
+ *
40
+ * @throws {TypeError } Throws an exception if hash is not a string
35
41
* @throws {TypeError } Throws an exception if seed is a float
36
42
* @param {string } hash The base string hash to generate number
37
43
* @param {number } [seed=0] An optional seed value
38
44
* @return {number } Generated number
39
45
*/
40
46
function murmurhash2_x86_32 ( hash , seed = 0 ) {
47
+ throwInvalidStringHash ( hash , 'murmurhash2_x86_32' ) ;
41
48
throwInvalidMurmurSeed ( seed ) ;
42
49
43
50
let currentIndex = 0 ;
Original file line number Diff line number Diff line change 1
1
/** @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' ;
3
8
4
9
/**
5
10
* Generate a non-cryptic number hash with murmur3 algorithm
@@ -33,13 +38,15 @@ export default (() => {
33
38
34
39
/**
35
40
* Generate a non-cryptic number hash with murmur3 algorithm
36
-
41
+ *
42
+ * @throws {TypeError } Throws an exception if hash is not a string
37
43
* @throws {TypeError } Throws an exception if seed is a float
38
44
* @param {string } hash The base string hash to generate number
39
45
* @param {number } [seed=0] An optional seed value
40
46
* @return {number } Generated number
41
47
*/
42
48
function murmurhash3_x86_32 ( hash , seed = 0 ) {
49
+ throwInvalidStringHash ( hash , 'murmurhash3_x86_32' ) ;
43
50
throwInvalidMurmurSeed ( seed ) ;
44
51
45
52
const remainder = hash . length % 4 ;
Original file line number Diff line number Diff line change @@ -57,3 +57,19 @@ export function throwInvalidAleaSeed(seed) {
57
57
) ;
58
58
}
59
59
}
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
+ }
You can’t perform that action at this time.
0 commit comments