@astarship/subsecond-id 0.0.0-stage → 0.5.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/dist/index.mjs ADDED
@@ -0,0 +1,759 @@
1
+ // Source/SsId.ts
2
+ var SsIdTicker_ = 0;
3
+ var SsIdLastId_ = 0n;
4
+ var SsIdSource_ = 0;
5
+ function SsIdSetSource(source) {
6
+ if (source < 0 || source > SubsecondIdSourceMax) {
7
+ throw new RangeError(`SsId source out of range: ${source}`);
8
+ }
9
+ SsIdSource_ = source;
10
+ }
11
+ function SsIdGetSource() {
12
+ return SsIdSource_;
13
+ }
14
+ var SsIdSecondsPerYear = 365.25 * 24 * 60 * 60;
15
+ var SsIdFourYearSeconds = 4 * SsIdSecondsPerYear;
16
+ function SsIdWindowStart(now) {
17
+ const t = now ?? TimestampSeconds();
18
+ const Year = Math.floor(t / SsIdSecondsPerYear);
19
+ const WindowYear = Math.floor(Year / 4) * 4 * SsIdSecondsPerYear;
20
+ return Math.floor(WindowYear);
21
+ }
22
+ function SsIdWindowCount(now) {
23
+ const t = now ?? TimestampSeconds();
24
+ return Math.floor(Math.floor(t / SsIdSecondsPerYear) / 4);
25
+ }
26
+ function SsIdNext(_rng) {
27
+ const Ticker_ = SsIdTicker_;
28
+ const Ticker = Ticker_ < SsIdTickerMax ? Ticker_ + 1 : SsIdResetTicker();
29
+ SsIdTicker_ = Ticker;
30
+ const Seconds = TimestampSeconds() - SsIdWindowStart() & SsIdHotTimestampMax;
31
+ const Id = BigInt(SsIdHotMsb) << 63n | BigInt(Seconds) << 36n | BigInt(Ticker) << 28n | BigInt(SsIdSource_);
32
+ if (Id <= SsIdLastId_) {
33
+ const [LastSeconds, LastTicker] = SsIdUnpack(SsIdLastId_);
34
+ let NewSeconds = LastSeconds;
35
+ let NewTicker = LastTicker + 1;
36
+ if (NewTicker > SsIdTickerMax) {
37
+ NewTicker = 1;
38
+ NewSeconds = LastSeconds + 1 & SsIdHotTimestampMax;
39
+ }
40
+ const Stepped = BigInt(SsIdHotMsb) << 63n | BigInt(NewSeconds) << 36n | BigInt(NewTicker) << 28n | BigInt(SsIdSource_);
41
+ SsIdLastId_ = Stepped;
42
+ SsIdTicker_ = NewTicker;
43
+ return Stepped;
44
+ }
45
+ SsIdLastId_ = Id;
46
+ return Id;
47
+ }
48
+ function SsIdResetTicker() {
49
+ SsIdTicker_ = 1;
50
+ return SsIdTicker_;
51
+ }
52
+ function SsIdPack(timestamp, ticker, source) {
53
+ const sec = timestamp & SsIdHotTimestampMax;
54
+ return BigInt(SsIdHotMsb) << 63n | BigInt(sec) << 36n | BigInt(ticker) << 28n | BigInt(source);
55
+ }
56
+ function SsIdUnpack(id) {
57
+ const seconds = Number(id >> 36n & 0x7FFFFFFn);
58
+ const ticker = Number(id >> 28n & 0xFFn);
59
+ const source = Number(id & 0x0FFFFFFFn);
60
+ return [seconds, ticker, source];
61
+ }
62
+ function SsIdTimestamp(id) {
63
+ return SsIdUnpack(id)[0];
64
+ }
65
+ function SsIdTicker(id) {
66
+ return SsIdUnpack(id)[1];
67
+ }
68
+ function SsIdSource(id) {
69
+ return SsIdUnpack(id)[2];
70
+ }
71
+ function SsIdMsb(id) {
72
+ return Number(id >> 63n & 1n);
73
+ }
74
+ function SsIdNextHex(rng) {
75
+ const HexCount = 16;
76
+ return SsIdNext(rng).toString(16).padStart(HexCount, "0");
77
+ }
78
+ function SsIdPrint(id) {
79
+ const [seconds, ticker, source] = SsIdUnpack(id);
80
+ return SsIdMsb(id).toString().padStart(1, "0") + "." + BinaryPadBits(seconds, SsIdHotTimestampBits) + "." + BinaryPadBits(ticker, SsIdTickerBits) + "." + BinaryPadBits(source, SsIdSourceBits);
81
+ }
82
+ function BinaryPadBits(value, bits) {
83
+ return value.toString(2).padStart(bits, "0");
84
+ }
85
+
86
+ // Source/SsLId.ts
87
+ var tmt_ticker = 0;
88
+ var tmt_timestamp = TimestampSeconds();
89
+ function SsLIdTimestamp(id) {
90
+ return Number(id >> 32n);
91
+ }
92
+ function SsLIdTicker(id) {
93
+ return Number(id & 0xFFFFFFFFn);
94
+ }
95
+ function SsLIdUnpack(id) {
96
+ return [SsLIdTimestamp(id), SsLIdTicker(id)];
97
+ }
98
+ function SsLIdPack(timestamp, ticker) {
99
+ return BigInt(timestamp) << 32n | BigInt(ticker);
100
+ }
101
+ function SsLIdPrint(id) {
102
+ const [timestamp, ticker] = SsLIdUnpack(id);
103
+ const time = new Date(timestamp * 1e3);
104
+ return "TMT:{ " + time.toISOString().split("T")[0] + " tick:" + ticker + " }";
105
+ }
106
+ function SsLIdNext() {
107
+ const timestamp = TimestampSeconds();
108
+ let ticker = tmt_ticker;
109
+ if (timestamp !== tmt_timestamp) {
110
+ tmt_timestamp = timestamp;
111
+ ticker = 0;
112
+ } else if (ticker >= TmtTickerMax) {
113
+ let now = timestamp;
114
+ while (now === tmt_timestamp) now = TimestampSeconds();
115
+ tmt_timestamp = now;
116
+ ticker = 0;
117
+ }
118
+ tmt_ticker = ticker + 1;
119
+ return SsLIdPack(timestamp, ticker);
120
+ }
121
+ function SsLIdNextHex() {
122
+ return SsLIdNext().toString(16).padStart(16, "0");
123
+ }
124
+
125
+ // Source/SsUId.ts
126
+ var uid_source = 0n;
127
+ var uid_ticker = 0;
128
+ var uid_timestamp = TimestampSeconds();
129
+ var uid_last_id = 0n;
130
+ function SsUIdTimestamp(subsec_id) {
131
+ const shift = BigInt(SsUIdSourceBits + SsUIdTickerBits);
132
+ return subsec_id >> shift;
133
+ }
134
+ function SsUIdTicker(subsec_id) {
135
+ const sourceBits = BigInt(SsUIdSourceBits);
136
+ const mask = (1n << BigInt(SsUIdTickerBits)) - 1n;
137
+ return subsec_id >> sourceBits & mask;
138
+ }
139
+ function SsUIdSource(subsec_id) {
140
+ const mask = (1n << BigInt(SsUIdSourceBits)) - 1n;
141
+ return subsec_id & mask;
142
+ }
143
+ function SsUIdPack(timestamp, ticker, source) {
144
+ const ts = BigInt(timestamp);
145
+ const tk = BigInt(ticker);
146
+ const shift = BigInt(SsUIdTickerBits + SsUIdSourceBits);
147
+ return ts << shift | tk << BigInt(SsUIdSourceBits) | source;
148
+ }
149
+ function SsUIdUnpack(subsec_id) {
150
+ return [SsUIdTimestamp(subsec_id), SsUIdTicker(subsec_id), SsUIdSource(subsec_id)];
151
+ }
152
+ function SsUIdPrint(subsec_id) {
153
+ const [timestamp, ticker, source] = SsUIdUnpack(subsec_id);
154
+ const time = new Date(Number(timestamp) * 1e3);
155
+ return "SSE:{ timestamp: " + time.toISOString().split("T")[0] + "\n tick :" + ticker.toString() + "\n source :" + source.toString(16) + " }";
156
+ }
157
+ function SsUIdSourceId() {
158
+ return uid_source;
159
+ }
160
+ function SsUIdSourceNext(rng) {
161
+ const Rng = rng ?? SsIdRng;
162
+ let source = 0n;
163
+ while (source === 0n) {
164
+ source = BigIntInRange(Rng, 1n, (1n << BigInt(SsUIdSourceBits)) - 1n);
165
+ }
166
+ uid_source = source;
167
+ }
168
+ function SsUIdSourceIncrement() {
169
+ const max = (1n << BigInt(SsUIdSourceBits)) - 1n;
170
+ if (uid_source >= max) {
171
+ uid_source = 1n;
172
+ } else {
173
+ uid_source = uid_source + 1n;
174
+ }
175
+ }
176
+ function SsUIdNext(rng) {
177
+ const Rng = rng ?? SsIdRng;
178
+ const timestamp = TimestampSeconds();
179
+ let ticker = uid_ticker;
180
+ if (timestamp !== uid_timestamp) {
181
+ uid_timestamp = timestamp;
182
+ ticker = 0;
183
+ } else if (ticker >= SsUIdTickerMax) {
184
+ const next = TimestampSecondsNext();
185
+ uid_timestamp = next;
186
+ ticker = 0;
187
+ }
188
+ uid_ticker = ticker + 1;
189
+ if (uid_source === 0n) {
190
+ SsUIdSourceNext(Rng);
191
+ }
192
+ const id = SsUIdPack(timestamp, ticker, uid_source);
193
+ if (id <= uid_last_id) {
194
+ uid_last_id = uid_last_id + 1n;
195
+ return uid_last_id;
196
+ }
197
+ uid_last_id = id;
198
+ return id;
199
+ }
200
+ function SsUIdNextHex(rng, dest = "") {
201
+ return dest + SsUIdNext(rng).toString(16).padStart(32, "0");
202
+ }
203
+ function SsUIdNextBuffer(rng) {
204
+ const id = SsUIdNext(rng);
205
+ const buf = Buffer.alloc(16);
206
+ for (let i = 0; i < 16; i++) {
207
+ buf[i] = Number(id >> BigInt(i * 8) & 0xFFn);
208
+ }
209
+ return buf;
210
+ }
211
+
212
+ // Source/index.ts
213
+ import { randomInt as _cryptoRng } from "crypto";
214
+ var SsIdRng = (min, max) => {
215
+ if (max + 1 < 2 ** 48) {
216
+ return _cryptoRng(min, max + 1);
217
+ }
218
+ const hi = _cryptoRng(0, 2 ** 24);
219
+ const lo = _cryptoRng(0, 2 ** 24);
220
+ const val = hi * 2 ** 24 + lo;
221
+ return Math.min(min + val, max);
222
+ };
223
+ function SsIdSetRng(rng) {
224
+ SsIdRng = rng;
225
+ }
226
+ var NumberMantissaBits = 53;
227
+ var SsIdSourceBits = 28;
228
+ var SsIdTickerBits = 8;
229
+ var SsIdHotTimestampBits = 27;
230
+ var SsIdHotMsb = 1;
231
+ var SsIdTickerSourceBits = SsIdSourceBits + SsIdTickerBits;
232
+ var SsIdHotTimestampMax = (1 << SsIdHotTimestampBits) - 1;
233
+ var SsIdColdTimestampBits = 34;
234
+ var SsIdColdTickerBits = 29;
235
+ var SsIdColdMsb = 0;
236
+ var SsIdAnonymousTimestampBits = 27;
237
+ var SsIdAnonymousRandomBits = 31;
238
+ var SsIdAnonymousSecondsMin = 126230400;
239
+ var SsIdAnonymousSecondsMax = 130424704;
240
+ var SsIdTickerMax = (1 << SsIdTickerBits) - 1;
241
+ var SubsecondIdSourceMax = (1 << SsIdSourceBits) - 1;
242
+ var TmtTimestampBits = 32;
243
+ var TmtTickerBits = 32;
244
+ var TmtTickerMax = 2 ** TmtTickerBits - 1;
245
+ var SsUIdSourceBits = 76;
246
+ var SsUIdTickerBits = 16;
247
+ var SsUIdTimestampBits2 = 36;
248
+ var SsUIdTickerMax = (1n << BigInt(SsUIdTickerBits)) - 1n;
249
+ function NumberToHex(value, dest = "") {
250
+ let hex = value.toString(16);
251
+ if (value < 10) return dest + "0" + hex;
252
+ return dest + hex;
253
+ }
254
+ function PrintHexByte(input, dest = "") {
255
+ if (input < 0n || input > 15n) return dest;
256
+ return dest + NumberToHex(input & 15) + NumberToHex(input >> 4 & 15);
257
+ }
258
+ function ByteCountBits(value) {
259
+ if (value < 16) {
260
+ if (value < 4) {
261
+ if (value < 2) {
262
+ if (value < 1) return 0;
263
+ else return 1;
264
+ } else return 2;
265
+ } else {
266
+ if (value < 8) return 3;
267
+ else return 4;
268
+ }
269
+ } else {
270
+ if (value < 64) {
271
+ if (value < 32) return 5;
272
+ else return 6;
273
+ } else if (value < 128) return 7;
274
+ }
275
+ return 8;
276
+ }
277
+ function CountAssertedBits(value) {
278
+ let count = 0;
279
+ while (value < 64) {
280
+ count += value & 1;
281
+ value >>= 1;
282
+ }
283
+ return count;
284
+ }
285
+ function CountLeadingOnes(value) {
286
+ let count = 0;
287
+ while (count < 64) {
288
+ if (!(value >> 63 - count++ & 1)) break;
289
+ }
290
+ return count;
291
+ }
292
+ function CountLeadingZeros(value) {
293
+ let count = 0;
294
+ while (count < 64) {
295
+ if (value >> 63 - count++ & 1) break;
296
+ }
297
+ return count;
298
+ }
299
+ function NumberCountBytes(value) {
300
+ if (value <= 4294967295) {
301
+ if (value <= 65535) {
302
+ if (value <= 255)
303
+ return 1;
304
+ else
305
+ return 2;
306
+ } else {
307
+ if (value <= 16777215)
308
+ return 3;
309
+ else
310
+ return 4;
311
+ }
312
+ } else {
313
+ if (value <= 281474976710655) {
314
+ if (value <= 1099511627775)
315
+ return 5;
316
+ else
317
+ return 6;
318
+ }
319
+ }
320
+ return 7;
321
+ }
322
+ function NumberCountBits(value) {
323
+ if (value === 0) return 1;
324
+ const Bits = NumberCountBytes(value) - 1 << 3;
325
+ let msb = value < 1 << 30 ? value >> Bits : Number(BigInt(value) >> BigInt(Bits));
326
+ return Bits + ByteCountBits(msb);
327
+ }
328
+ function NumberPrint(value, char_count, pad = " ") {
329
+ const Value = value.toString();
330
+ if (char_count <= 3) return "0".repeat(char_count);
331
+ if (Value.length > char_count)
332
+ return Value.substring(0, char_count - 3) + "...";
333
+ return pad.repeat(char_count - Value.length) + Value;
334
+ }
335
+ function BigIntPrint(value, char_count, pad = " ") {
336
+ const Value = value.toString();
337
+ if (char_count <= 3) return "0".repeat(char_count);
338
+ if (Value.length > char_count)
339
+ return Value.substring(0, char_count - 3) + "...";
340
+ return pad.repeat(char_count - Value.length) + Value;
341
+ }
342
+ function BinaryCount(value) {
343
+ const v = BigInt(value);
344
+ if (v === 0n) return 1;
345
+ return v.toString(2).length;
346
+ }
347
+ function BigIntCountBytes(value) {
348
+ const Bits = BinaryCount(value);
349
+ return (Bits >> 3) + (Bits & 7 ? 1 : 0);
350
+ }
351
+ function BigIntCountBits(value) {
352
+ const ByteCount = BigIntCountBytes(value);
353
+ return (ByteCount - 1 << 3) + NumberCountBits(Number(value));
354
+ }
355
+ function NumberCountDecimals(value) {
356
+ if (value < 0) value *= -1;
357
+ if (value < 1e8) {
358
+ if (value < 1e4) {
359
+ if (value < 100) {
360
+ if (value < 10)
361
+ return 1;
362
+ else
363
+ return 2;
364
+ } else {
365
+ if (value < 1e3)
366
+ return 3;
367
+ else
368
+ return 4;
369
+ }
370
+ } else {
371
+ if (value < 1e6) {
372
+ if (value < 1e5)
373
+ return 5;
374
+ else
375
+ return 6;
376
+ } else {
377
+ if (value < 1e7)
378
+ return 7;
379
+ else
380
+ return 8;
381
+ }
382
+ }
383
+ } else {
384
+ if (value < 1e12) {
385
+ if (value < 1e10) {
386
+ if (value < 1e9)
387
+ return 9;
388
+ else
389
+ return 10;
390
+ } else {
391
+ if (value < 1e11)
392
+ return 11;
393
+ else
394
+ return 12;
395
+ }
396
+ } else {
397
+ if (value < 1e14) {
398
+ if (value < 1e13)
399
+ return 13;
400
+ else
401
+ return 14;
402
+ } else {
403
+ if (value < 1e15)
404
+ return 15;
405
+ else
406
+ return 16;
407
+ }
408
+ }
409
+ }
410
+ }
411
+ function NumberPad(value, digit_count, pad = " ") {
412
+ const DecimalCount = NumberCountDecimals(value);
413
+ if (DecimalCount > digit_count) {
414
+ if (digit_count <= 3) return ".".repeat(digit_count);
415
+ return value.toString().substring(0, digit_count - 3) + "...";
416
+ }
417
+ return pad.repeat(digit_count - DecimalCount) + value;
418
+ }
419
+ function CountDecimals(value) {
420
+ switch (typeof value) {
421
+ case "bigint":
422
+ return BigIntCountDecimals(BigInt(value));
423
+ case "number":
424
+ return NumberCountDecimals(Number(value));
425
+ }
426
+ const Value = String(value);
427
+ let i = 0;
428
+ let c = Value.charCodeAt(i);
429
+ let leading_zero_count = 0;
430
+ if (c == "0".charCodeAt(0)) {
431
+ c = Value.charCodeAt(++i);
432
+ while (c == "0".charCodeAt(0)) c = Value.charCodeAt(++i);
433
+ leading_zero_count = i;
434
+ }
435
+ while (c != void 0 && c >= "0".charCodeAt(0) && c <= "9".charCodeAt(0))
436
+ c = Value.charCodeAt(++i);
437
+ return i - leading_zero_count;
438
+ }
439
+ function BigIntCountDecimals(value) {
440
+ if (value < 0n) value *= -1n;
441
+ let decimal_count = 0;
442
+ const B53DecimalMax = 1000000000000000n;
443
+ while (value >= B53DecimalMax) {
444
+ decimal_count += 15;
445
+ value /= B53DecimalMax;
446
+ }
447
+ return decimal_count + NumberCountDecimals(Number(value));
448
+ }
449
+ function BigIntToBuffer2(value) {
450
+ const ByteCount = BigIntCountBytes(value);
451
+ const Buf = Buffer.alloc(ByteCount);
452
+ for (let index = 0; index < ByteCount; ++index) {
453
+ Buf[index] = Number(value >> BigInt(index << 3) & 0xffn);
454
+ }
455
+ return Buf;
456
+ }
457
+ function NumberToBuffer(value) {
458
+ const ByteCount = NumberCountBytes(value);
459
+ const Buf = Buffer.alloc(ByteCount);
460
+ for (let index = 0; index < ByteCount; ++index) {
461
+ Buf[index] = value >> (index << 3) & 255;
462
+ }
463
+ return Buf;
464
+ }
465
+ function BigIntPad(value, decimals_max, pad = " ") {
466
+ const DecimalCount = BigIntCountDecimals(value);
467
+ if (DecimalCount > decimals_max) {
468
+ if (decimals_max <= 3) return pad.repeat(decimals_max);
469
+ return value.toString().substring(0, decimals_max - 3) + "...";
470
+ }
471
+ return pad.repeat(decimals_max - DecimalCount) + value;
472
+ }
473
+ function BinaryPad(value, bit_count = 64, prefix = "0b", pad = "0") {
474
+ if (bit_count <= 0) return "";
475
+ const str = typeof value === "string" ? String(value) : value == void 0 ? "" : value.toString(2);
476
+ if (bit_count < str.length) {
477
+ if (bit_count < 3) return prefix + ".".repeat(bit_count);
478
+ return prefix + str.substring(0, bit_count - 3) + "...";
479
+ }
480
+ return prefix + pad.repeat(bit_count - str.length) + str;
481
+ }
482
+ function BinaryPadBits2(value, bit_count = 64, prefix = "0b") {
483
+ return BinaryPad(value, bit_count, prefix) + ":" + (typeof value === "string" ? String(value).length : BigInt(value ?? 0n).toString(2).length);
484
+ }
485
+ function BufferToBigInt(buf) {
486
+ let result = 0n;
487
+ for (let i = 0; i < buf.length; ++i)
488
+ result |= BigInt(buf[i] ?? 0) << BigInt(i << 3);
489
+ return result;
490
+ }
491
+ function BufferToHex(buf) {
492
+ let result = "";
493
+ for (let i = 0; i < buf.length; ++i)
494
+ result = PrintHexByte(buf[i] ?? 0, result);
495
+ return result;
496
+ }
497
+ function HexPad(value, bit_count = 64, prefix = "0x", pad = "0") {
498
+ if (bit_count <= 0) return "";
499
+ const HexCount = (bit_count >> 2) + (bit_count & 3 ? 1 : 0);
500
+ let hex = typeof value === "string" ? String(value) : value == void 0 ? "" : value.toString(16);
501
+ if (hex.length > HexCount) {
502
+ if (HexCount < 3) return prefix + ".".repeat(HexCount);
503
+ return prefix + hex.substring(0, HexCount - 3) + "...";
504
+ }
505
+ return prefix + pad.repeat(HexCount - hex.length) + hex;
506
+ }
507
+ function HexPadBits(value, bit_count = 64, prefix = "0x") {
508
+ return HexPad(value, bit_count, prefix) + ":" + (typeof value === "string" ? String(value).length : BinaryCount(BigInt(value ?? 0n)));
509
+ }
510
+ function HexToNibble(input) {
511
+ if (input == void 0) return -1;
512
+ let c = input.charCodeAt(0);
513
+ if (c < "0".charCodeAt(0) || c > "z".charCodeAt(0)) return -1;
514
+ if (c <= "9".charCodeAt(0))
515
+ return c - "0".charCodeAt(0);
516
+ if (c >= "a".charCodeAt(0))
517
+ return c - "a".charCodeAt(0) + 10;
518
+ if (c < "A".charCodeAt(0) || c > "Z".charCodeAt(0))
519
+ return -1;
520
+ return c - "A".charCodeAt(0) + 10;
521
+ }
522
+ function HexToBigInt(hex) {
523
+ let result = 0n;
524
+ let length = hex.length;
525
+ if (hex == void 0 || length == 0) return result;
526
+ let i = 0;
527
+ while (--length >= 0)
528
+ result |= BigInt(HexToNibble(hex[i++]) << (length << 2));
529
+ return result;
530
+ }
531
+ function HexToNumber(hex) {
532
+ let result = 0;
533
+ let length = hex.length;
534
+ if (hex == void 0 || length == 0) return result;
535
+ let i = 0;
536
+ while (--length >= 0)
537
+ result |= HexToNibble(hex[i++]) << (length << 2);
538
+ return result;
539
+ }
540
+ function HexToBuffer(hex) {
541
+ let length = hex.length;
542
+ let j = 0;
543
+ let nibble = hex[j];
544
+ while (nibble == "0") nibble = hex[++j];
545
+ let count = length - j;
546
+ const LSb = count & 1;
547
+ const ByteCount = (count >> 1) + LSb;
548
+ const Buf = Buffer.alloc(ByteCount);
549
+ if (ByteCount <= 0) return Buf;
550
+ let i = ByteCount - 1;
551
+ let a = HexToNibble(nibble);
552
+ if (a < 0) return Buf;
553
+ if (LSb == 1) {
554
+ Buf[i--] = a;
555
+ a = HexToNibble(hex[++j]);
556
+ if (a < 0) return Buf;
557
+ }
558
+ while (a >= 0) {
559
+ let b = HexToNibble(hex[++j]);
560
+ if (b < 0) {
561
+ Buf[i] = a;
562
+ return Buf;
563
+ }
564
+ Buf[i--] = a << 4 | b;
565
+ a = HexToNibble(hex[++j]);
566
+ }
567
+ return Buf;
568
+ }
569
+ function BigIntInRange(rng, min = 0, max = 281474976710655) {
570
+ if (min > max) return 0n;
571
+ if (min == max) return BigInt(min);
572
+ const Max = BigInt(max);
573
+ const Min = BigInt(min);
574
+ const Range = Max - Min;
575
+ const RangeBits = BigInt(BinaryCount(Range));
576
+ let result = 0n;
577
+ let count = RangeBits - 1n;
578
+ while (count > 48n) {
579
+ count -= 48n;
580
+ result |= BigInt(rng(0, 281474976710655)) << count;
581
+ }
582
+ const Shift = RangeBits - count;
583
+ const Remainder = Number(Range >> Shift);
584
+ if (Remainder != 0)
585
+ result |= BigInt(rng(0, Remainder)) << Shift;
586
+ return Min + result;
587
+ }
588
+ function NumberInRange(rng, min = 0, max = 281474976710655) {
589
+ return Number(BigIntInRange(rng, min, max));
590
+ }
591
+ function BigIntInBitRange(rng, bit_min = 1, bit_max = 64) {
592
+ if (bit_min <= 0 || bit_max <= 0 || bit_min > bit_max) return 0n;
593
+ const Max = (1n << BigInt(bit_max)) - 1n;
594
+ const Min = bit_min == 1n ? 0n : 1n << BigInt(bit_min) - 1n;
595
+ return BigIntInRange(rng, Min, Max);
596
+ }
597
+ function NumberInBitRange(rng, bit_min, bit_max) {
598
+ if (bit_min <= 0 || bit_max <= 0 || bit_min > bit_max || bit_max > 52) return 0;
599
+ const Max = (1 << bit_max) - 1;
600
+ const Min = bit_min == 1 ? 0 : 1 << bit_min - 1;
601
+ return NumberInRange(rng, Min, Max);
602
+ }
603
+ function BitRangeMinMax(bit_min = 0, bit_max = 64) {
604
+ const Max = (1n << BigInt(bit_max)) - 1n;
605
+ const Min = bit_min <= 1n ? -1n : (1n << BigInt(bit_min) - 1n) - 1n;
606
+ return [Number(Min), Number(Max)];
607
+ }
608
+ function BigIntIsInBitRange(value, bit_min = 0, bit_max = 64) {
609
+ const V = BigInt(value);
610
+ const Max = (1n << BigInt(bit_max)) - 1n;
611
+ const Min = bit_min <= 1n ? 0n : 1n << BigInt(bit_min) - 1n;
612
+ return V >= Min && V <= Max;
613
+ }
614
+ function BigIntRandom(rng, bit_count = 64) {
615
+ return BigIntInBitRange(rng, 0, bit_count);
616
+ }
617
+ function NumberRandom(rng) {
618
+ return rng(Number.MIN_VALUE, Number.MAX_VALUE);
619
+ }
620
+ function NumberNZ(rng, min = Number.MIN_VALUE, max = Number.MAX_VALUE) {
621
+ let generated = 0;
622
+ if (min >= max || max - min == generated) return generated;
623
+ while (generated == 0) {
624
+ generated = rng(Number.MIN_VALUE, Number.MAX_VALUE);
625
+ }
626
+ return generated;
627
+ }
628
+ function NumberNZFromNeg1To1(rng) {
629
+ let generated = 0;
630
+ while (generated == 0) {
631
+ generated = rng(-1, 1);
632
+ }
633
+ return generated;
634
+ }
635
+ function TimestampSeconds() {
636
+ return Math.floor((/* @__PURE__ */ new Date()).getTime() / 1e3);
637
+ }
638
+ function TimestampSecondsNext() {
639
+ let time_start = TimestampSeconds();
640
+ let now = time_start;
641
+ while (now == time_start) now = TimestampSeconds();
642
+ return now;
643
+ }
644
+ function TimestampSecondsNextBigInt() {
645
+ return BigInt(TimestampSecondsNext());
646
+ }
647
+ function TimestampSecondsAsBigInt() {
648
+ return BigInt(TimestampSeconds());
649
+ }
650
+ export {
651
+ BigIntCountBits,
652
+ BigIntCountBytes,
653
+ BigIntCountDecimals,
654
+ BigIntInBitRange,
655
+ BigIntInRange,
656
+ BigIntIsInBitRange,
657
+ BigIntPad,
658
+ BigIntPrint,
659
+ BigIntRandom,
660
+ BigIntToBuffer2 as BigIntToBuffer,
661
+ BinaryCount,
662
+ BinaryPad,
663
+ BinaryPadBits2 as BinaryPadBits,
664
+ BitRangeMinMax,
665
+ BufferToBigInt,
666
+ BufferToHex,
667
+ ByteCountBits,
668
+ CountAssertedBits,
669
+ CountDecimals,
670
+ CountLeadingOnes,
671
+ CountLeadingZeros,
672
+ HexPad,
673
+ HexPadBits,
674
+ HexToBigInt,
675
+ HexToBuffer,
676
+ HexToNibble,
677
+ HexToNumber,
678
+ NumberCountBits,
679
+ NumberCountBytes,
680
+ NumberCountDecimals,
681
+ NumberInBitRange,
682
+ NumberInRange,
683
+ NumberMantissaBits,
684
+ NumberNZ,
685
+ NumberNZFromNeg1To1,
686
+ NumberPad,
687
+ NumberPrint,
688
+ NumberRandom,
689
+ NumberToBuffer,
690
+ NumberToHex,
691
+ PrintHexByte,
692
+ SsIdAnonymousRandomBits,
693
+ SsIdAnonymousSecondsMax,
694
+ SsIdAnonymousSecondsMin,
695
+ SsIdAnonymousTimestampBits,
696
+ SsIdColdMsb,
697
+ SsIdColdTickerBits,
698
+ SsIdColdTimestampBits,
699
+ SsIdFourYearSeconds,
700
+ SsIdGetSource,
701
+ SsIdHotMsb,
702
+ SsIdHotTimestampBits,
703
+ SsIdHotTimestampMax,
704
+ SsIdLastId_,
705
+ SsIdMsb,
706
+ SsIdNext,
707
+ SsIdNextHex,
708
+ SsIdPack,
709
+ SsIdPrint,
710
+ SsIdResetTicker,
711
+ SsIdRng,
712
+ SsIdSecondsPerYear,
713
+ SsIdSetRng,
714
+ SsIdSetSource,
715
+ SsIdSource,
716
+ SsIdSourceBits,
717
+ SsIdSource_,
718
+ SsIdTicker,
719
+ SsIdTickerBits,
720
+ SsIdTickerMax,
721
+ SsIdTickerSourceBits,
722
+ SsIdTicker_,
723
+ SsIdTimestamp,
724
+ SsIdUnpack,
725
+ SsIdWindowCount,
726
+ SsIdWindowStart,
727
+ SsLIdNext,
728
+ SsLIdNextHex,
729
+ SsLIdPack,
730
+ SsLIdPrint,
731
+ SsLIdTicker,
732
+ SsLIdTimestamp,
733
+ SsLIdUnpack,
734
+ SsUIdNext,
735
+ SsUIdNextBuffer,
736
+ SsUIdNextHex,
737
+ SsUIdPack,
738
+ SsUIdPrint,
739
+ SsUIdSource,
740
+ SsUIdSourceBits,
741
+ SsUIdSourceId,
742
+ SsUIdSourceIncrement,
743
+ SsUIdSourceNext,
744
+ SsUIdTicker,
745
+ SsUIdTickerBits,
746
+ SsUIdTickerMax,
747
+ SsUIdTimestamp,
748
+ SsUIdTimestampBits2 as SsUIdTimestampBits,
749
+ SsUIdUnpack,
750
+ SubsecondIdSourceMax,
751
+ TimestampSeconds,
752
+ TimestampSecondsAsBigInt,
753
+ TimestampSecondsNext,
754
+ TimestampSecondsNextBigInt,
755
+ TmtTickerBits,
756
+ TmtTickerMax,
757
+ TmtTimestampBits
758
+ };
759
+ //# sourceMappingURL=index.mjs.map