@oh-my-pi/pi-utils 11.0.0 → 11.0.2
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/package.json +1 -1
- package/src/snowflake.ts +7 -11
package/package.json
CHANGED
package/src/snowflake.ts
CHANGED
|
@@ -39,12 +39,10 @@ namespace Snowflake {
|
|
|
39
39
|
// Formats a sequence and timestamp into a snowflake hex string.
|
|
40
40
|
//
|
|
41
41
|
export function formatParts(dt: number, seq: number): Snowflake {
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const lo = (dt << 22) | seq;
|
|
47
|
-
|
|
42
|
+
// Keep everything in Number space; dt is < 2^53 for current epochs.
|
|
43
|
+
const value = dt * 0x400000 + seq; // dt << 22
|
|
44
|
+
const hi = Math.floor(value / 0x100000000);
|
|
45
|
+
const lo = (value - hi * 0x100000000) >>> 0;
|
|
48
46
|
const hi1 = (hi >>> 16) & 0xffff;
|
|
49
47
|
const hi2 = hi & 0xffff;
|
|
50
48
|
const lo1 = (lo >>> 16) & 0xffff;
|
|
@@ -99,9 +97,8 @@ namespace Snowflake {
|
|
|
99
97
|
//
|
|
100
98
|
export function lowerbound(timelike: Date | number | Snowflake): Snowflake {
|
|
101
99
|
switch (typeof timelike) {
|
|
102
|
-
// biome-ignore lint/suspicious/noFallthroughSwitchClause: intentional fallthrough
|
|
103
100
|
case "object": // Date
|
|
104
|
-
|
|
101
|
+
return formatParts(timelike.getTime() - EPOCH, 0);
|
|
105
102
|
case "number":
|
|
106
103
|
return formatParts(timelike - EPOCH, 0);
|
|
107
104
|
case "string": // Snowflake hex string
|
|
@@ -110,11 +107,10 @@ namespace Snowflake {
|
|
|
110
107
|
}
|
|
111
108
|
export function upperbound(timelike: Date | number | Snowflake): Snowflake {
|
|
112
109
|
switch (typeof timelike) {
|
|
113
|
-
// biome-ignore lint/suspicious/noFallthroughSwitchClause: intentional fallthrough
|
|
114
110
|
case "object": // Date
|
|
115
|
-
|
|
111
|
+
return formatParts(timelike.getTime() - EPOCH, MAX_SEQ);
|
|
116
112
|
case "number":
|
|
117
|
-
return formatParts(timelike - EPOCH,
|
|
113
|
+
return formatParts(timelike - EPOCH, MAX_SEQ);
|
|
118
114
|
case "string": // Snowflake hex string
|
|
119
115
|
return timelike;
|
|
120
116
|
}
|