hamsterid 1.0.1
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/LICENCE +21 -0
- package/README.md +115 -0
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +17 -0
- package/lib/cli.js.map +1 -0
- package/lib/hrtime.d.ts +5 -0
- package/lib/hrtime.js +34 -0
- package/lib/hrtime.js.map +1 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +66 -0
- package/lib/index.js.map +1 -0
- package/lib/rng.d.ts +3 -0
- package/lib/rng.js +32 -0
- package/lib/rng.js.map +1 -0
- package/lib/test-utils.d.ts +2 -0
- package/lib/test-utils.js +15 -0
- package/lib/test-utils.js.map +1 -0
- package/package.json +52 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ahmet Çiftci
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# HamsterID
|
|
2
|
+
|
|
3
|
+
A lexicographically sortable, fixed-length unique identifier generator.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
429t78n91cck41njk21rjula6u
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
- **Sortable as strings** — `ORDER BY id` gives chronological order. No separate `created_at` column needed for ordering.
|
|
12
|
+
- **Fixed length** — always 26 chars. No leading-zero padding logic, no variable-width surprises in storage or logs.
|
|
13
|
+
- **Monotonic** — IDs generated in the same process never go backwards. If the random + timer bits would produce a value `<=` the previous ID, it is bumped by 1. Safe for hot-loop generation.
|
|
14
|
+
- **128 bits** — same size as UUID/ULID. Fits in a single `bigint` or a 16-byte column.
|
|
15
|
+
- **No padding, no Crockford alphabet** — straight base32 (`0-9a-v`), so it round-trips through `BigInt` / `parseInt` without lookup tables.
|
|
16
|
+
- **Sub-millisecond ordering** — uses a high-resolution timer (nanoseconds on Node, microseconds in browsers) as part of the value, so two IDs from the same millisecond still sort correctly.
|
|
17
|
+
- **Crypto-strong random** — uses `crypto.getRandomValues` when available, falls back to `Math.random` with a warning.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install hamsterid
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import next from "hamsterid";
|
|
29
|
+
|
|
30
|
+
next(); // "429t78p7lcefqp8ujjr9k2gntf"
|
|
31
|
+
next(); // "429t78p7pcefuvggusflg3k1tp"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
IDs from the same process are strictly increasing:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const a = next();
|
|
38
|
+
const b = next();
|
|
39
|
+
a < b; // true (string comparison)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## CLI
|
|
43
|
+
|
|
44
|
+
Generate IDs from the command line without installing:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npx hamsterid # one ID
|
|
48
|
+
npx hamsterid 5 # five IDs
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
429t797gbcs3cpc2v00ssl51bu
|
|
53
|
+
429t797gdcs3eosr5fgphse0qj
|
|
54
|
+
429t797gdcs3eqikgmhojfdpks
|
|
55
|
+
429t797gdcs3erav5d2vksml7q
|
|
56
|
+
429t797gdcs3errfqn2octkv2g
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## HamsterID Bit layout
|
|
60
|
+
|
|
61
|
+
128 bits, laid out most-significant first:
|
|
62
|
+
|
|
63
|
+
| Field | Bits | Meaning |
|
|
64
|
+
| --------- | ------------ | ------------------------------------------------------------- |
|
|
65
|
+
| Fixed `1` | 1 | Guarantees a fixed 26-char base32 length (no leading zeros) |
|
|
66
|
+
| Timestamp | 42 | Milliseconds since 2024-01-01 UTC (~139 years, until ~2163) |
|
|
67
|
+
| HR time | 0 / 30 / 40 | Sub-millisecond ordering. Width depends on the runtime |
|
|
68
|
+
| Random | 85 / 55 / 45 | Collision resistance. Fills whatever the HR-time field didn't |
|
|
69
|
+
|
|
70
|
+
The HR-time and random widths depend on the environment:
|
|
71
|
+
|
|
72
|
+
| Runtime | HR-time bits | Random bits | HR-time resolution |
|
|
73
|
+
| ------------------------- | ------------ | ----------- | ----------------------- |
|
|
74
|
+
| Node.js | 40 | 45 | 1 nanosecond |
|
|
75
|
+
| Browsers (Chromium-based) | 30 | 55 | 100 microseconds |
|
|
76
|
+
| Firefox / Safari | 0 | 85 | n/a (spectre-mitigated) |
|
|
77
|
+
|
|
78
|
+
When no usable high-resolution timer is available (Firefox/Safari, where `performance.now` is mitigated), the HR-time field is dropped and the full 85 bits go to random — more collision resistance, same fixed length.
|
|
79
|
+
|
|
80
|
+
## Properties
|
|
81
|
+
|
|
82
|
+
### Lexicographic sortability
|
|
83
|
+
|
|
84
|
+
Because the most significant bits are the timestamp and the fixed leading `1`, string comparison of two base32 IDs matches chronological order:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const ids = Array.from({ length: 1000 }, () => next());
|
|
88
|
+
const sorted = [...ids].sort(); // already chronological
|
|
89
|
+
sorted[0] === ids[0]; // true
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Monotonicity within a process
|
|
93
|
+
|
|
94
|
+
Each generated ID is compared against the last. If the new value would be `<=` the previous one (e.g. the clock went backwards or random bits collided), it is replaced with `last + 1`. This guarantees a strictly increasing sequence within a single process.
|
|
95
|
+
|
|
96
|
+
> Monotonicity is **per-process**. Across processes/machines, IDs remain sortable by timestamp but are not strictly monotonic.
|
|
97
|
+
|
|
98
|
+
### Uniqueness
|
|
99
|
+
|
|
100
|
+
The random field plus the monotonic bump makes in-process collisions effectively impossible. Across independent processes generating within the same millisecond, collision probability is governed by the random width (45 / 55 / 85 bits) — the same birthday-bound math as ULID.
|
|
101
|
+
|
|
102
|
+
## Comparison with ULID
|
|
103
|
+
|
|
104
|
+
| | HamsterID | ULID |
|
|
105
|
+
| ------------------ | ------------------- | ------------------------------------------ |
|
|
106
|
+
| Length | 26 chars | 26 chars |
|
|
107
|
+
| Alphabet | base32 `0-9a-v` | Crockford base32 |
|
|
108
|
+
| Sortable as string | yes | yes |
|
|
109
|
+
| Sub-ms ordering | yes (HR timer) | no (ms only; relies on random for same-ms) |
|
|
110
|
+
| Monotonic | yes, in-process | only with monotonic factory |
|
|
111
|
+
| Random source | `crypto` by default | implementation-defined |
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
package/lib/cli.d.ts
ADDED
package/lib/cli.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const index_1 = __importDefault(require("./index"));
|
|
8
|
+
const arg = process.argv[2];
|
|
9
|
+
const count = arg === undefined ? 1 : Number(arg);
|
|
10
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
11
|
+
console.error("Usage: hamsterid [count]");
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
for (let i = 0; i < count; i++) {
|
|
15
|
+
console.log((0, index_1.default)());
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=cli.js.map
|
package/lib/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;;;;AACA,oDAA2B;AAE3B,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC5B,MAAM,KAAK,GAAG,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAElD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;IAC1C,OAAO,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAC1C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC;AAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;IAC/B,OAAO,CAAC,GAAG,CAAC,IAAA,eAAI,GAAE,CAAC,CAAC;AACtB,CAAC"}
|
package/lib/hrtime.d.ts
ADDED
package/lib/hrtime.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// nanosecond resolution 40 bits overflows in around 18 minutes
|
|
4
|
+
const hrtimeNode = {
|
|
5
|
+
value: () => process.hrtime.bigint(),
|
|
6
|
+
bits: 40,
|
|
7
|
+
};
|
|
8
|
+
// 100 microsecond resolution 30 bits overflows in around 18 hours
|
|
9
|
+
const hrtimeBrowser = {
|
|
10
|
+
value: () => BigInt(Math.floor(performance.now() * 10)),
|
|
11
|
+
bits: 30,
|
|
12
|
+
};
|
|
13
|
+
// no usable high-resolution timer: random field expands to 85 bits
|
|
14
|
+
const hrtimeZero = {
|
|
15
|
+
value: () => 0n,
|
|
16
|
+
bits: 0,
|
|
17
|
+
};
|
|
18
|
+
function chooseHrtimeSource() {
|
|
19
|
+
if (typeof process === "object" && typeof process.hrtime === "function") {
|
|
20
|
+
return hrtimeNode;
|
|
21
|
+
}
|
|
22
|
+
// Due to the spectre mitigations, performance.now is useless in firefox and safari
|
|
23
|
+
if (typeof navigator === "object" &&
|
|
24
|
+
/firefox|safari/i.test(navigator.userAgent)) {
|
|
25
|
+
return hrtimeZero;
|
|
26
|
+
}
|
|
27
|
+
if (typeof performance === "object" &&
|
|
28
|
+
typeof performance.now === "function") {
|
|
29
|
+
return hrtimeBrowser;
|
|
30
|
+
}
|
|
31
|
+
return hrtimeZero;
|
|
32
|
+
}
|
|
33
|
+
exports.default = chooseHrtimeSource();
|
|
34
|
+
//# sourceMappingURL=hrtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hrtime.js","sourceRoot":"","sources":["../src/hrtime.ts"],"names":[],"mappings":";;AAAA,+DAA+D;AAC/D,MAAM,UAAU,GAAG;IACjB,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE;IACpC,IAAI,EAAE,EAAE;CACT,CAAC;AAEF,kEAAkE;AAClE,MAAM,aAAa,GAAG;IACpB,KAAK,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;IACvD,IAAI,EAAE,EAAE;CACT,CAAC;AAEF,mEAAmE;AACnE,MAAM,UAAU,GAAG;IACjB,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE;IACf,IAAI,EAAE,CAAC;CACR,CAAC;AAEF,SAAS,kBAAkB;IACzB,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QACxE,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,mFAAmF;IACnF,IACE,OAAO,SAAS,KAAK,QAAQ;QAC7B,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAC3C,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,IACE,OAAO,WAAW,KAAK,QAAQ;QAC/B,OAAO,WAAW,CAAC,GAAG,KAAK,UAAU,EACrC,CAAC;QACD,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,kBAAe,kBAAkB,EAAE,CAAC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function composeBits(timestamp: number, hrtime: bigint, hrtimeBits: number, random: bigint): bigint;
|
|
2
|
+
export declare function UTCTicks(value: Date | number): number;
|
|
3
|
+
declare function nextBigInt(): bigint;
|
|
4
|
+
declare function next(): string;
|
|
5
|
+
declare namespace next {
|
|
6
|
+
var bigint: typeof nextBigInt;
|
|
7
|
+
}
|
|
8
|
+
export default next;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.composeBits = composeBits;
|
|
7
|
+
exports.UTCTicks = UTCTicks;
|
|
8
|
+
exports.default = next;
|
|
9
|
+
const rng_1 = __importDefault(require("./rng"));
|
|
10
|
+
const hrtime_1 = __importDefault(require("./hrtime"));
|
|
11
|
+
/*
|
|
12
|
+
Bit Layout (128 bits total):
|
|
13
|
+
[1 bit] : Fixed value (1) for consistent sized string conversion, leading zeros never looks good
|
|
14
|
+
[42 bits] : Timestamp
|
|
15
|
+
- Epoch: January 1, 2024 (UTC)
|
|
16
|
+
- Unit: Milliseconds
|
|
17
|
+
- Range: ~139 years (until ~2163)
|
|
18
|
+
[0, 40 or 30 bits] : High-resolution time
|
|
19
|
+
- Node.js: 40 bits, nanosecond precision
|
|
20
|
+
* Resolution: 1 nanosecond
|
|
21
|
+
* Overflow period: ~18 minutes
|
|
22
|
+
- Browsers: 30 bits, microsecond precision
|
|
23
|
+
* Resolution: 100 microseconds
|
|
24
|
+
* Overflow period: ~18 hours
|
|
25
|
+
- Firefox/Safari (spectre-mitigated): 0 bits, hrtime omitted
|
|
26
|
+
[85, 45 or 55 bits] : Random value
|
|
27
|
+
- Remaining bits used for collision prevention
|
|
28
|
+
- 45 bits in Node.js, 55 bits in browsers, 85 bits without hrtime
|
|
29
|
+
|
|
30
|
+
Total: 1 + 42 + (40 or 30 or 0) + (45 or 55 or 85) = 128 bits
|
|
31
|
+
*/
|
|
32
|
+
const EPOCH = Date.UTC(2024);
|
|
33
|
+
const MASK_85BIT = 0x1fffffffffffffffffffffn; //2n ** 85n - 1n;
|
|
34
|
+
const MASK_42BIT = 0x3ffffffffffn; //2n ** 42n - 1n;
|
|
35
|
+
function composeBits(timestamp, hrtime, hrtimeBits, random) {
|
|
36
|
+
const ts = BigInt(timestamp) % (MASK_42BIT + 1n);
|
|
37
|
+
if (hrtimeBits > 0) {
|
|
38
|
+
const hrtimeMask = 2n ** BigInt(hrtimeBits);
|
|
39
|
+
const randomBits = 85 - hrtimeBits;
|
|
40
|
+
const randomMask = 2n ** BigInt(randomBits);
|
|
41
|
+
return ((1n << 127n) |
|
|
42
|
+
(ts << 85n) |
|
|
43
|
+
((hrtime % hrtimeMask) << BigInt(randomBits)) |
|
|
44
|
+
(random % randomMask));
|
|
45
|
+
}
|
|
46
|
+
return (1n << 127n) | (ts << 85n) | (random % (MASK_85BIT + 1n));
|
|
47
|
+
}
|
|
48
|
+
function UTCTicks(value) {
|
|
49
|
+
const time = new Date(value);
|
|
50
|
+
return Date.UTC(time.getUTCFullYear(), time.getUTCMonth(), time.getUTCDate(), time.getUTCHours(), time.getUTCMinutes(), time.getUTCSeconds(), time.getUTCMilliseconds());
|
|
51
|
+
}
|
|
52
|
+
let lastid = 0n;
|
|
53
|
+
function nextBigInt() {
|
|
54
|
+
const timestamp = UTCTicks(Date.now()) - EPOCH, hrtimeValue = hrtime_1.default.value(), rand = (0, rng_1.default)();
|
|
55
|
+
let id = composeBits(timestamp, hrtimeValue, hrtime_1.default.bits, rand);
|
|
56
|
+
if (id <= lastid) {
|
|
57
|
+
id = lastid + 1n;
|
|
58
|
+
}
|
|
59
|
+
lastid = id;
|
|
60
|
+
return id;
|
|
61
|
+
}
|
|
62
|
+
function next() {
|
|
63
|
+
return nextBigInt().toString(32);
|
|
64
|
+
}
|
|
65
|
+
next.bigint = nextBigInt;
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AA8BA,kCAqBC;AAED,4BAWC;AAmBD,uBAEC;AArFD,gDAAwB;AACxB,sDAA8B;AAE9B;;;;;;;;;;;;;;;;;;;;EAoBE;AAEF,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAE7B,MAAM,UAAU,GAAG,yBAAyB,CAAC,CAAC,iBAAiB;AAC/D,MAAM,UAAU,GAAG,cAAc,CAAC,CAAC,iBAAiB;AAEpD,SAAgB,WAAW,CACzB,SAAiB,EACjB,MAAc,EACd,UAAkB,EAClB,MAAc;IAEd,MAAM,EAAE,GAAG,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC;IAEjD,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;QACnB,MAAM,UAAU,GAAG,EAAE,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;QAC5C,MAAM,UAAU,GAAG,EAAE,GAAG,UAAU,CAAC;QACnC,MAAM,UAAU,GAAG,EAAE,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;QAC5C,OAAO,CACL,CAAC,EAAE,IAAI,IAAI,CAAC;YACZ,CAAC,EAAE,IAAI,GAAG,CAAC;YACX,CAAC,CAAC,MAAM,GAAG,UAAU,CAAC,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;YAC7C,CAAC,MAAM,GAAG,UAAU,CAAC,CACtB,CAAC;IACJ,CAAC;IAED,OAAO,CAAC,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC,CAAC;AACnE,CAAC;AAED,SAAgB,QAAQ,CAAC,KAAoB;IAC3C,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,IAAI,CAAC,GAAG,CACb,IAAI,CAAC,cAAc,EAAE,EACrB,IAAI,CAAC,WAAW,EAAE,EAClB,IAAI,CAAC,UAAU,EAAE,EACjB,IAAI,CAAC,WAAW,EAAE,EAClB,IAAI,CAAC,aAAa,EAAE,EACpB,IAAI,CAAC,aAAa,EAAE,EACpB,IAAI,CAAC,kBAAkB,EAAE,CAC1B,CAAC;AACJ,CAAC;AAED,IAAI,MAAM,GAAW,EAAE,CAAC;AAExB,SAAS,UAAU;IACjB,MAAM,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,KAAK,EAC5C,WAAW,GAAG,gBAAM,CAAC,KAAK,EAAE,EAC5B,IAAI,GAAG,IAAA,aAAG,GAAE,CAAC;IAEf,IAAI,EAAE,GAAG,WAAW,CAAC,SAAS,EAAE,WAAW,EAAE,gBAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAEhE,IAAI,EAAE,IAAI,MAAM,EAAE,CAAC;QACjB,EAAE,GAAG,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IAED,MAAM,GAAG,EAAE,CAAC;IACZ,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAwB,IAAI;IAC1B,OAAO,UAAU,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AACnC,CAAC;AAED,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC"}
|
package/lib/rng.d.ts
ADDED
package/lib/rng.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function rngCrypto() {
|
|
4
|
+
const arr = crypto.getRandomValues(new Uint32Array(4));
|
|
5
|
+
return ((BigInt(arr[0]) << 96n) +
|
|
6
|
+
(BigInt(arr[1]) << 64n) +
|
|
7
|
+
(BigInt(arr[2]) << 32n) +
|
|
8
|
+
BigInt(arr[3]));
|
|
9
|
+
}
|
|
10
|
+
function rngMathRandom() {
|
|
11
|
+
const rand = () => BigInt(Math.floor(Math.random() * 2 ** 16));
|
|
12
|
+
return ((rand() << 96n) +
|
|
13
|
+
(rand() << 80n) +
|
|
14
|
+
(rand() << 64n) +
|
|
15
|
+
(rand() << 48n) +
|
|
16
|
+
(rand() << 32n) +
|
|
17
|
+
(rand() << 16n) +
|
|
18
|
+
rand());
|
|
19
|
+
}
|
|
20
|
+
function chooseRngSource() {
|
|
21
|
+
if (typeof crypto === 'object' && typeof crypto.getRandomValues === 'function') {
|
|
22
|
+
return rngCrypto;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.error('crypto.getRandomValues is not available. Falling back to Math.random \
|
|
26
|
+
if you are using react native consider adding react-native-get-random-values \
|
|
27
|
+
module to your project');
|
|
28
|
+
return rngMathRandom;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.default = chooseRngSource();
|
|
32
|
+
//# sourceMappingURL=rng.js.map
|
package/lib/rng.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rng.js","sourceRoot":"","sources":["../src/rng.ts"],"names":[],"mappings":";;AAAA,SAAS,SAAS;IACd,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,OAAO,CACH,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;QACvB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;QACvB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;QACvB,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CACjB,CAAA;AACL,CAAC;AAED,SAAS,aAAa;IAClB,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAC/D,OAAO,CACH,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC;QACf,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC;QACf,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC;QACf,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC;QACf,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC;QACf,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC;QACf,IAAI,EAAE,CACT,CAAA;AACL,CAAC;AAED,SAAS,eAAe;IACpB,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,eAAe,KAAK,UAAU,EAAE,CAAC;QAC7E,OAAO,SAAS,CAAC;IACrB,CAAC;SAAM,CAAC;QACJ,OAAO,CAAC,KAAK,CAAC;;8CAEwB,CAAC,CAAC;QACxC,OAAO,aAAa,CAAC;IACzB,CAAC;AACL,CAAC;AAED,kBAAe,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PizzaSliceFunction = pizzaSlice;
|
|
4
|
+
function pizzaSlice(pizza, slices, splitTo) {
|
|
5
|
+
let offsets = slices.reduce((p, c, i) => [...p, i == 0 ? c : p[i - 1] + c], []);
|
|
6
|
+
offsets = [0, ...offsets];
|
|
7
|
+
const parts = slices.map((_, i) => pizza.slice(offsets[i], offsets[i + 1]));
|
|
8
|
+
if (splitTo === undefined) {
|
|
9
|
+
return parts;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return parts.reduce((p, c, i) => ({ ...p, [splitTo[i]]: c }), {});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=test-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-utils.js","sourceRoot":"","sources":["../src/test-utils.ts"],"names":[],"mappings":";;AAyBuB,wCAAkB;AAlBzC,SAAS,UAAU,CACf,KAAa,EACb,MAAgB,EAChB,OAAa;IAEb,IAAI,OAAO,GAAG,MAAM,CAAC,MAAM,CAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC1F,OAAO,GAAG,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC;IAC1B,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5E,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACjB,CAAC;SAAM,CAAC;QACJ,OAAO,KAAK,CAAC,MAAM,CACf,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EACxC,EAAS,CACZ,CAAC;IACN,CAAC;AACL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hamsterid",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "lexicographically sortable unique identifier generator",
|
|
5
|
+
"author": "afarmerdev",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "lib/index.js",
|
|
8
|
+
"types": "lib/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"hamsterid": "lib/cli.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENCE"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"uuid",
|
|
19
|
+
"ulid",
|
|
20
|
+
"unique",
|
|
21
|
+
"lexicographically",
|
|
22
|
+
"sortable",
|
|
23
|
+
"random",
|
|
24
|
+
"uniqueid",
|
|
25
|
+
"identifier",
|
|
26
|
+
"id-generator"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=10.13"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/afarmerdev/hamsterid.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/afarmerdev/hamsterid#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/afarmerdev/hamsterid/issues"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.lib.json",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"test": "jest",
|
|
43
|
+
"prepublishOnly": "npm run build && npm test"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/jest": "^29.5.14",
|
|
47
|
+
"@types/node": "^22.7.7",
|
|
48
|
+
"jest": "^29.7.0",
|
|
49
|
+
"ts-jest": "^29.2.5",
|
|
50
|
+
"typescript": "^5.6.3"
|
|
51
|
+
}
|
|
52
|
+
}
|