@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/snowflake.ts +7 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oh-my-pi/pi-utils",
3
- "version": "11.0.0",
3
+ "version": "11.0.2",
4
4
  "description": "Shared utilities for pi packages",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
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
- // Split 64-bit value into two 32-bit parts for number arithmetic
43
- // high32 = delta >> 10 (timestamp bits 41-10)
44
- // low32 = (delta & 0x3ff) << 22 | seq
45
- const hi = dt >>> 10;
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
- timelike = timelike.getTime();
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
- timelike = timelike.getTime();
111
+ return formatParts(timelike.getTime() - EPOCH, MAX_SEQ);
116
112
  case "number":
117
- return formatParts(timelike - EPOCH, 0x3fffff);
113
+ return formatParts(timelike - EPOCH, MAX_SEQ);
118
114
  case "string": // Snowflake hex string
119
115
  return timelike;
120
116
  }