@sindresorhus/fnv1a 2.0.1 → 3.1.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 +40 -31
- package/index.js +46 -50
- package/license +1 -1
- package/package.json +11 -11
- package/readme.md +44 -13
package/index.d.ts
CHANGED
|
@@ -1,46 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
size?: 32 | 64 | 128 | 256 | 512 | 1024;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
export interface Options {
|
|
2
|
+
/**
|
|
3
|
+
The bit size of the hash.
|
|
4
|
+
|
|
5
|
+
@default 32
|
|
6
|
+
*/
|
|
7
|
+
readonly size?: 32 | 64 | 128 | 256 | 512 | 1024;
|
|
11
8
|
|
|
12
|
-
declare const fnv1a: {
|
|
13
9
|
/**
|
|
14
|
-
|
|
10
|
+
A Uint8Array used to encode the string into UTF-8 bytes.
|
|
15
11
|
|
|
16
|
-
|
|
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
|
+
|
|
14
|
+
The size of the array does not have to be large enugh to hold the entire string, but performance will be improved if it is.
|
|
15
|
+
|
|
16
|
+
This option is only used when `value` is a string.
|
|
17
17
|
|
|
18
18
|
@example
|
|
19
19
|
```
|
|
20
|
-
import fnv1a
|
|
20
|
+
import fnv1a from '@sindresorhus/fnv1a';
|
|
21
|
+
|
|
22
|
+
const utf8Buffer = new Uint8Array(100);
|
|
21
23
|
|
|
22
|
-
fnv1a('🦄🌈');
|
|
23
|
-
//=>
|
|
24
|
+
fnv1a('🦄🌈', {size: 32, utf8Buffer});
|
|
25
|
+
//=> 2868248295n
|
|
24
26
|
```
|
|
25
27
|
*/
|
|
26
|
-
|
|
28
|
+
readonly utf8Buffer?: Uint8Array;
|
|
29
|
+
}
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
/**
|
|
32
|
+
[FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non-cryptographic hash function.
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
@param value - A string or UTF-8 bytes.
|
|
35
|
+
@returns The hash as a positive `BigInt`.
|
|
32
36
|
|
|
33
|
-
|
|
37
|
+
@example
|
|
38
|
+
```
|
|
39
|
+
import fnv1a from '@sindresorhus/fnv1a';
|
|
34
40
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
import fnv1a = require('@sindresorhus/fnv1a');
|
|
41
|
+
fnv1a('🦄🌈', {size: 32});
|
|
42
|
+
//=> 2868248295n
|
|
38
43
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
44
|
+
fnv1a('🦄🌈', {size: 128});
|
|
45
|
+
//=> 13487074350300261116944693128525960095n
|
|
46
|
+
|
|
47
|
+
Number(fnv1a('🦄🌈', {size: 32}));
|
|
48
|
+
//=> 2868248295
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
const bytes = new Uint8Array([240, 159, 166, 132, 240, 159, 140, 136]);
|
|
51
|
+
fnv1a(bytes, {size: 32});
|
|
52
|
+
//=> 2868248295n
|
|
53
|
+
```
|
|
54
|
+
*/
|
|
55
|
+
export default function fnv1a(value: string | Uint8Array, options?: Options): bigint;
|
package/index.js
CHANGED
|
@@ -1,76 +1,72 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
// FNV_PRIMES and FNV_OFFSETS from
|
|
4
2
|
// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
|
|
5
3
|
|
|
6
4
|
const FNV_PRIMES = {
|
|
7
|
-
32:
|
|
8
|
-
64:
|
|
9
|
-
128:
|
|
10
|
-
256:
|
|
11
|
-
512:
|
|
12
|
-
1024:
|
|
5
|
+
32: 16_777_619n,
|
|
6
|
+
64: 1_099_511_628_211n,
|
|
7
|
+
128: 309_485_009_821_345_068_724_781_371n,
|
|
8
|
+
256: 374_144_419_156_711_147_060_143_317_175_368_453_031_918_731_002_211n,
|
|
9
|
+
512: 35_835_915_874_844_867_368_919_076_489_095_108_449_946_327_955_754_392_558_399_825_615_420_669_938_882_575_126_094_039_892_345_713_852_759n,
|
|
10
|
+
1024: 5_016_456_510_113_118_655_434_598_811_035_278_955_030_765_345_404_790_744_303_017_523_831_112_055_108_147_451_509_157_692_220_295_382_716_162_651_878_526_895_249_385_292_291_816_524_375_083_746_691_371_804_094_271_873_160_484_737_966_720_260_389_217_684_476_157_468_082_573n,
|
|
13
11
|
};
|
|
14
12
|
|
|
15
13
|
const FNV_OFFSETS = {
|
|
16
|
-
32:
|
|
17
|
-
64:
|
|
18
|
-
128:
|
|
19
|
-
256:
|
|
20
|
-
512:
|
|
21
|
-
1024:
|
|
14
|
+
32: 2_166_136_261n,
|
|
15
|
+
64: 14_695_981_039_346_656_037n,
|
|
16
|
+
128: 144_066_263_297_769_815_596_495_629_667_062_367_629n,
|
|
17
|
+
256: 100_029_257_958_052_580_907_070_968_620_625_704_837_092_796_014_241_193_945_225_284_501_741_471_925_557n,
|
|
18
|
+
512: 9_659_303_129_496_669_498_009_435_400_716_310_466_090_418_745_672_637_896_108_374_329_434_462_657_994_582_932_197_716_438_449_813_051_892_206_539_805_784_495_328_239_340_083_876_191_928_701_583_869_517_785n,
|
|
19
|
+
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,
|
|
22
20
|
};
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
function fnv1a(string) {
|
|
26
|
-
// Handle Unicode code points > 0x7f
|
|
27
|
-
let hash = Number(FNV_OFFSETS[32]);
|
|
28
|
-
let isUnicoded = false;
|
|
29
|
-
|
|
30
|
-
for (let i = 0; i < string.length; i++) {
|
|
31
|
-
let characterCode = string.charCodeAt(i);
|
|
22
|
+
const cachedEncoder = new globalThis.TextEncoder();
|
|
32
23
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
characterCode = string.charCodeAt(i);
|
|
37
|
-
isUnicoded = true;
|
|
38
|
-
}
|
|
24
|
+
function fnv1aUint8Array(uint8Array, size) {
|
|
25
|
+
const fnvPrime = FNV_PRIMES[size];
|
|
26
|
+
let hash = FNV_OFFSETS[size];
|
|
39
27
|
|
|
40
|
-
|
|
41
|
-
|
|
28
|
+
// eslint-disable-next-line unicorn/no-for-loop -- This is a performance-sensitive loop
|
|
29
|
+
for (let index = 0; index < uint8Array.length; index++) {
|
|
30
|
+
hash ^= BigInt(uint8Array[index]);
|
|
31
|
+
hash = BigInt.asUintN(size, hash * fnvPrime);
|
|
42
32
|
}
|
|
43
33
|
|
|
44
|
-
return hash
|
|
34
|
+
return hash;
|
|
45
35
|
}
|
|
46
36
|
|
|
47
|
-
function
|
|
48
|
-
if (
|
|
49
|
-
throw new Error('The `
|
|
37
|
+
function fnv1aEncodeInto(string, size, utf8Buffer) {
|
|
38
|
+
if (utf8Buffer.length === 0) {
|
|
39
|
+
throw new Error('The `utf8Buffer` option must have a length greater than zero');
|
|
50
40
|
}
|
|
51
41
|
|
|
52
|
-
let hash = FNV_OFFSETS[size];
|
|
53
42
|
const fnvPrime = FNV_PRIMES[size];
|
|
43
|
+
let hash = FNV_OFFSETS[size];
|
|
44
|
+
let remaining = string;
|
|
45
|
+
|
|
46
|
+
while (remaining.length > 0) {
|
|
47
|
+
const result = cachedEncoder.encodeInto(remaining, utf8Buffer);
|
|
48
|
+
remaining = remaining.slice(result.read);
|
|
49
|
+
for (let index = 0; index < result.written; index++) {
|
|
50
|
+
hash ^= BigInt(utf8Buffer[index]);
|
|
51
|
+
hash = BigInt.asUintN(size, hash * fnvPrime);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
return hash;
|
|
56
|
+
}
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
export default function fnv1a(value, {size = 32, utf8Buffer} = {}) {
|
|
59
|
+
if (!FNV_PRIMES[size]) {
|
|
60
|
+
throw new Error('The `size` option must be one of 32, 64, 128, 256, 512, or 1024');
|
|
61
|
+
}
|
|
60
62
|
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
characterCode = string.charCodeAt(i);
|
|
65
|
-
isUnicoded = true;
|
|
63
|
+
if (typeof value === 'string') {
|
|
64
|
+
if (utf8Buffer) {
|
|
65
|
+
return fnv1aEncodeInto(value, size, utf8Buffer);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
hash = BigInt.asUintN(size, hash * fnvPrime);
|
|
68
|
+
value = cachedEncoder.encode(value);
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
return
|
|
71
|
+
return fnv1aUint8Array(value, size);
|
|
73
72
|
}
|
|
74
|
-
|
|
75
|
-
module.exports = fnv1a;
|
|
76
|
-
module.exports.bigInt = bigInt;
|
package/license
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sindresorhus/fnv1a",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "FNV-1a non-cryptographic hash function",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/fnv1a",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
7
8
|
"author": {
|
|
8
9
|
"name": "Sindre Sorhus",
|
|
9
10
|
"email": "sindresorhus@gmail.com",
|
|
10
|
-
"url": "sindresorhus.com"
|
|
11
|
+
"url": "https://sindresorhus.com"
|
|
11
12
|
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": "./index.js",
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"sideEffects": false,
|
|
12
17
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
18
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
14
19
|
},
|
|
15
20
|
"scripts": {
|
|
16
21
|
"test": "xo && ava && tsd"
|
|
@@ -38,13 +43,8 @@
|
|
|
38
43
|
"bigint"
|
|
39
44
|
],
|
|
40
45
|
"devDependencies": {
|
|
41
|
-
"ava": "
|
|
42
|
-
"tsd": "0.
|
|
43
|
-
"xo": "0.
|
|
44
|
-
},
|
|
45
|
-
"xo": {
|
|
46
|
-
"globals": [
|
|
47
|
-
"BigInt"
|
|
48
|
-
]
|
|
46
|
+
"ava": "3.15.0",
|
|
47
|
+
"tsd": "0.17.0",
|
|
48
|
+
"xo": "0.44.0"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/readme.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# fnv1a
|
|
1
|
+
# fnv1a
|
|
2
2
|
|
|
3
3
|
> [FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non-cryptographic hash function
|
|
4
4
|
|
|
@@ -8,33 +8,42 @@ FNV hashes are designed to be fast while maintaining a low collision rate. The F
|
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
11
|
-
```
|
|
12
|
-
|
|
11
|
+
```sh
|
|
12
|
+
npm install @sindresorhus/fnv1a
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
|
|
18
|
+
import fnv1a from '@sindresorhus/fnv1a';
|
|
19
19
|
|
|
20
|
-
fnv1a('🦄🌈');
|
|
21
|
-
//=>
|
|
20
|
+
fnv1a('🦄🌈', {size: 32});
|
|
21
|
+
//=> 2868248295n
|
|
22
22
|
|
|
23
|
-
fnv1a
|
|
23
|
+
fnv1a('🦄🌈', {size: 128});
|
|
24
24
|
//=> 13487074350300261116944693128525960095n
|
|
25
|
+
|
|
26
|
+
Number(fnv1a('🦄🌈', {size: 32}));
|
|
27
|
+
//=> 2868248295
|
|
28
|
+
|
|
29
|
+
const bytes = new Uint8Array([240, 159, 166, 132, 240, 159, 140, 136]);
|
|
30
|
+
fnv1a(bytes, {size: 32});
|
|
31
|
+
//=> 2868248295n
|
|
25
32
|
```
|
|
26
33
|
|
|
27
34
|
## API
|
|
28
35
|
|
|
29
|
-
### fnv1a(
|
|
36
|
+
### fnv1a(value, options?)
|
|
30
37
|
|
|
31
|
-
Returns the hash as a
|
|
38
|
+
Returns the hash as a positive `BigInt`.
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
If you need it as a `number`, use `32` as `size` and wrap the return value in `Number(…)`.
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
#### value
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
Type: `string | Uint8Array`
|
|
45
|
+
|
|
46
|
+
A string or UTF-8 bytes.
|
|
38
47
|
|
|
39
48
|
#### options
|
|
40
49
|
|
|
@@ -43,10 +52,32 @@ Type: `object`
|
|
|
43
52
|
##### size
|
|
44
53
|
|
|
45
54
|
Type: `number`\
|
|
46
|
-
Values: `32 | 64 | 128 | 256 | 512 | 1024
|
|
55
|
+
Values: `32 | 64 | 128 | 256 | 512 | 1024`\
|
|
56
|
+
Default: `32`
|
|
47
57
|
|
|
48
58
|
The bit size of the hash.
|
|
49
59
|
|
|
60
|
+
##### utf8Buffer
|
|
61
|
+
|
|
62
|
+
Type: `Uint8Array`
|
|
63
|
+
|
|
64
|
+
A Uint8Array used to encode the string into UTF-8 bytes.
|
|
65
|
+
|
|
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
|
+
|
|
68
|
+
The size of the array does not have to be large enugh to hold the entire string, but performance will be improved if it is.
|
|
69
|
+
|
|
70
|
+
This option is only used when `value` is a string.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
import fnv1a from '@sindresorhus/fnv1a';
|
|
74
|
+
|
|
75
|
+
const utf8Buffer = new Uint8Array(100);
|
|
76
|
+
|
|
77
|
+
fnv1a('🦄🌈', {size: 32, utf8Buffer});
|
|
78
|
+
//=> 2868248295n
|
|
79
|
+
```
|
|
80
|
+
|
|
50
81
|
## Related
|
|
51
82
|
|
|
52
83
|
- [djb2a](https://github.com/sindresorhus/djb2a) - DJB2a non-cryptographic hash function
|