@sindresorhus/fnv1a 3.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +5 -3
- package/index.js +8 -5
- package/package.json +9 -13
- package/readme.md +1 -1
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Options = {
|
|
2
2
|
/**
|
|
3
3
|
The bit size of the hash.
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ export interface Options {
|
|
|
11
11
|
|
|
12
12
|
This array can be reused across calls to `fnv1a`. Doing so will improve performance because it avoids allocating a new Uint8Array when encoding the string.
|
|
13
13
|
|
|
14
|
-
The size of the array does not have to be large
|
|
14
|
+
The size of the array does not have to be large enough to hold the entire string, but performance will be improved if it is. It must have a length of at least 4, which is the largest size of a single character in UTF-8.
|
|
15
15
|
|
|
16
16
|
This option is only used when `value` is a string.
|
|
17
17
|
|
|
@@ -26,7 +26,7 @@ export interface Options {
|
|
|
26
26
|
```
|
|
27
27
|
*/
|
|
28
28
|
readonly utf8Buffer?: Uint8Array;
|
|
29
|
-
}
|
|
29
|
+
};
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
[FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non-cryptographic hash function.
|
|
@@ -53,3 +53,5 @@ fnv1a(bytes, {size: 32});
|
|
|
53
53
|
```
|
|
54
54
|
*/
|
|
55
55
|
export default function fnv1a(value: string | Uint8Array, options?: Options): bigint;
|
|
56
|
+
|
|
57
|
+
export {}; // eslint-disable-line unicorn/require-module-specifiers -- It stops the implicit export of the declarations without `export`.
|
package/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// FNV_PRIMES and FNV_OFFSETS from
|
|
2
|
-
//
|
|
2
|
+
// https://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
|
|
3
|
+
|
|
4
|
+
/* eslint-disable no-bitwise -- FNV-1a is defined in terms of XOR. Unlike the 32-bit number operators, `^=` on BigInt is exact and never coerces its operands to int32. */
|
|
3
5
|
|
|
4
6
|
const FNV_PRIMES = {
|
|
5
7
|
32: 16_777_619n,
|
|
@@ -19,7 +21,7 @@ const FNV_OFFSETS = {
|
|
|
19
21
|
1024: 14_197_795_064_947_621_068_722_070_641_403_218_320_880_622_795_441_933_960_878_474_914_617_582_723_252_296_732_303_717_722_150_864_096_521_202_355_549_365_628_174_669_108_571_814_760_471_015_076_148_029_755_969_804_077_320_157_692_458_563_003_215_304_957_150_157_403_644_460_363_550_505_412_711_285_966_361_610_267_868_082_893_823_963_790_439_336_411_086_884_584_107_735_010_676_915n,
|
|
20
22
|
};
|
|
21
23
|
|
|
22
|
-
const cachedEncoder = new
|
|
24
|
+
const cachedEncoder = new TextEncoder();
|
|
23
25
|
|
|
24
26
|
function fnv1aUint8Array(uint8Array, size) {
|
|
25
27
|
const fnvPrime = FNV_PRIMES[size];
|
|
@@ -35,8 +37,9 @@ function fnv1aUint8Array(uint8Array, size) {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
function fnv1aEncodeInto(string, size, utf8Buffer) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
// `encodeInto` will not write a partial code point, so a buffer that cannot hold the next one reads nothing and the loop would never finish. A code point is at most 4 bytes in UTF-8.
|
|
41
|
+
if (utf8Buffer.length < 4) {
|
|
42
|
+
throw new RangeError('The `utf8Buffer` option must have a length of at least 4');
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
const fnvPrime = FNV_PRIMES[size];
|
|
@@ -56,7 +59,7 @@ function fnv1aEncodeInto(string, size, utf8Buffer) {
|
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
export default function fnv1a(value, {size = 32, utf8Buffer} = {}) {
|
|
59
|
-
if (!FNV_PRIMES
|
|
62
|
+
if (!Object.hasOwn(FNV_PRIMES, size)) {
|
|
60
63
|
throw new Error('The `size` option must be one of 32, 64, 128, 256, 512, or 1024');
|
|
61
64
|
}
|
|
62
65
|
|
package/package.json
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sindresorhus/fnv1a",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "FNV-1a non-cryptographic hash function",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/fnv1a",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
-
"author":
|
|
9
|
-
"name": "Sindre Sorhus",
|
|
10
|
-
"email": "sindresorhus@gmail.com",
|
|
11
|
-
"url": "https://sindresorhus.com"
|
|
12
|
-
},
|
|
8
|
+
"author": "Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)",
|
|
13
9
|
"type": "module",
|
|
14
|
-
"exports":
|
|
15
|
-
|
|
10
|
+
"exports": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"default": "./index.js"
|
|
13
|
+
},
|
|
16
14
|
"sideEffects": false,
|
|
17
15
|
"engines": {
|
|
18
|
-
"node": "
|
|
16
|
+
"node": ">=22"
|
|
19
17
|
},
|
|
20
18
|
"scripts": {
|
|
21
|
-
"test": "xo &&
|
|
19
|
+
"test": "xo && node --test"
|
|
22
20
|
},
|
|
23
21
|
"files": [
|
|
24
22
|
"index.js",
|
|
@@ -43,8 +41,6 @@
|
|
|
43
41
|
"bigint"
|
|
44
42
|
],
|
|
45
43
|
"devDependencies": {
|
|
46
|
-
"
|
|
47
|
-
"tsd": "0.17.0",
|
|
48
|
-
"xo": "0.44.0"
|
|
44
|
+
"xo": "^5.0.1"
|
|
49
45
|
}
|
|
50
46
|
}
|
package/readme.md
CHANGED
|
@@ -65,7 +65,7 @@ A Uint8Array used to encode the string into UTF-8 bytes.
|
|
|
65
65
|
|
|
66
66
|
This array can be reused across calls to `fnv1a`. Doing so will improve performance because it avoids allocating a new Uint8Array when encoding the string.
|
|
67
67
|
|
|
68
|
-
The size of the array does not have to be large
|
|
68
|
+
The size of the array does not have to be large enough to hold the entire string, but performance will be improved if it is. It must have a length of at least 4, which is the largest size of a single character in UTF-8.
|
|
69
69
|
|
|
70
70
|
This option is only used when `value` is a string.
|
|
71
71
|
|