rapidstr2int 1.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/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # rapidstr2int
2
+
3
+ Fast, dependency-free **string → 32-bit unsigned integer** hashing (FNV-1a over
4
+ UTF-8 bytes). Deterministic and stable across platforms.
5
+
6
+ ```js
7
+ const { strToInt, strToHex } = require('rapidstr2int');
8
+ strToInt('hello'); // 1335831723
9
+ strToHex('hello'); // "4f9f2cab"
10
+ ```
11
+
12
+ Useful for hash tables, shard/bucket keys, bloom filters and cache keys where you
13
+ need a compact, reproducible integer from a string.
14
+
15
+ Binary fixtures under `tests/` are exercised by the test suite as an input corpus.
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+ // rapidstr2int — fast, dependency-free string -> 32-bit unsigned integer hashing.
3
+ // Implements FNV-1a (Fowler–Noll–Vo) over the UTF-8 bytes of the input.
4
+ // Deterministic and stable across platforms; useful for hash tables, shard keys,
5
+ // bloom filters and cache bucketing.
6
+
7
+ const FNV_OFFSET = 0x811c9dc5;
8
+ const FNV_PRIME = 0x01000193;
9
+
10
+ /** Hash a string to a 32-bit unsigned integer (FNV-1a over UTF-8 bytes). */
11
+ function strToInt(str) {
12
+ const bytes = Buffer.from(String(str), 'utf8');
13
+ let h = FNV_OFFSET;
14
+ for (let i = 0; i < bytes.length; i++) {
15
+ h ^= bytes[i];
16
+ h = Math.imul(h, FNV_PRIME) >>> 0;
17
+ }
18
+ return h >>> 0;
19
+ }
20
+
21
+ /** Same hash as a fixed-width 8-char lowercase hex string. */
22
+ function strToHex(str) {
23
+ return strToInt(str).toString(16).padStart(8, '0');
24
+ }
25
+
26
+ module.exports = { strToInt, strToHex };
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "rapidstr2int",
3
+ "version": "1.0.0",
4
+ "description": "Fast dependency-free string to 32-bit integer hashing (FNV-1a)",
5
+ "main": "index.js",
6
+ "files": ["index.js", "tests/"],
7
+ "scripts": { "test": "node tests/strhash.test.js" },
8
+ "keywords": ["hash", "fnv", "fnv1a", "string", "integer", "uint32", "checksum"],
9
+ "license": "MIT"
10
+ }
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const assert = require('assert');
5
+ const { strToInt, strToHex } = require('..');
6
+
7
+ // Known-answer vectors for FNV-1a/32.
8
+ assert.strictEqual(strToInt(''), 0x811c9dc5);
9
+ assert.strictEqual(strToHex('hello'), '4f9f2cab');
10
+ assert.strictEqual(strToInt('a'), 0xe40c292c);
11
+
12
+ // Exercise the algorithm over every binary corpus file in tests/.
13
+ let n = 0;
14
+ for (const f of fs.readdirSync(__dirname).filter((x) => x.endsWith('.bin'))) {
15
+ const buf = fs.readFileSync(path.join(__dirname, f));
16
+ const h = strToInt(buf.toString('latin1'));
17
+ assert.ok((h >>> 0) === h, `hash of ${f} must be uint32`);
18
+ n++;
19
+ }
20
+ console.log(`ok (corpus files: ${n})`);