@sapphire/snowflake 4.0.0-pr-589.aa473f9.0 → 4.0.0-pr-935.7da5c8bb

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/CHANGELOG.md CHANGED
@@ -2,6 +2,52 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ # [@sapphire/snowflake@3.5.4](https://github.com/sapphiredev/utilities/compare/@sapphire/snowflake@3.5.3...@sapphire/snowflake@3.5.4) - (2024-11-02)
6
+
7
+ ## 🏃 Performance
8
+
9
+ - **snowflake:** Optimize Snowflake.timestampFrom ([b97b30c](https://github.com/sapphiredev/utilities/commit/b97b30cddf7e92eac42e908745a9812011d0f3ff)) ([#792](https://github.com/sapphiredev/utilities/pull/792) by @advaith1)
10
+
11
+ ## 🐛 Bug Fixes
12
+
13
+ - Move browser imports ([100ffb0](https://github.com/sapphiredev/utilities/commit/100ffb0a2471bb9f74cc580d282d11059e1a0a68)) ([#826](https://github.com/sapphiredev/utilities/pull/826) by @kyranet)
14
+ - **deps:** Update all non-major dependencies ([083376a](https://github.com/sapphiredev/utilities/commit/083376aac55094dbeddb5194e8a8f0d794b8cceb)) ([#763](https://github.com/sapphiredev/utilities/pull/763) by @renovate[bot])
15
+ - **deps:** Update all non-major dependencies ([e7fdc5d](https://github.com/sapphiredev/utilities/commit/e7fdc5db3632a7f90292ef3978898da32730343a)) ([#752](https://github.com/sapphiredev/utilities/pull/752) by @renovate[bot])
16
+
17
+ ## 📝 Documentation
18
+
19
+ - **readme:** Fix readme table of contents ([c60e8bd](https://github.com/sapphiredev/utilities/commit/c60e8bd9fc89156cf973871597ec3f0ef0308c08))
20
+
21
+ # [@sapphire/snowflake@3.5.3](https://github.com/sapphiredev/utilities/compare/@sapphire/snowflake@3.5.3...@sapphire/snowflake@3.5.3) - (2024-01-19)
22
+
23
+ ## 🐛 Bug Fixes
24
+
25
+ - Fixed commonjs typings export mapping (#707) ([216ff02](https://github.com/sapphiredev/utilities/commit/216ff0260d63a9590357f9a5069f1ae2b34eaf5d))
26
+
27
+ # [@sapphire/snowflake@3.5.2](https://github.com/sapphiredev/utilities/compare/@sapphire/snowflake@3.5.2...@sapphire/snowflake@3.5.2) - (2023-12-04)
28
+
29
+ ## 🐛 Bug Fixes
30
+
31
+ - **snowflake:** Properly split CJS, ESM and IIFE ([2a1fdde](https://github.com/sapphiredev/utilities/commit/2a1fddefe9fb3e07e7ab9eaee1631ab7e82ef1b3))
32
+ - Update export mapping for proper ESM/CJS split ([dd0cff8](https://github.com/sapphiredev/utilities/commit/dd0cff8e9b03a15812f25f7a1180501a92422629))
33
+ - **deps:** Update all non-major dependencies (#607) ([9cc8bd0](https://github.com/sapphiredev/utilities/commit/9cc8bd0d4b5d650deab2c913e6c3d713861bae28))
34
+
35
+ ## 🧪 Testing
36
+
37
+ - Update vitest to coverage v8 ([a4bc6e4](https://github.com/sapphiredev/utilities/commit/a4bc6e4f24ea60143a150ecc76fda6484f172ab9))
38
+
39
+ # [@sapphire/snowflake@3.5.1](https://github.com/sapphiredev/utilities/compare/@sapphire/snowflake@3.5.0...@sapphire/snowflake@3.5.1) - (2023-05-12)
40
+
41
+ ## 🏠 Refactor
42
+
43
+ - **snowflake:** Handle out-of-bounds `increment` correctly (#596) ([b5276d7](https://github.com/sapphiredev/utilities/commit/b5276d7372c33356975a302bafb5ae8aba604431))
44
+
45
+ # [@sapphire/snowflake@3.5.0](https://github.com/sapphiredev/utilities/compare/@sapphire/snowflake@3.4.3...@sapphire/snowflake@3.5.0) - (2023-05-10)
46
+
47
+ ## 🚀 Features
48
+
49
+ - **snowflake:** Expose `processId` and `workerId` (#595) ([b873c1c](https://github.com/sapphiredev/utilities/commit/b873c1cc3b30cb54d710a49f7618e125ac1132ad))
50
+
5
51
  # [@sapphire/snowflake@3.4.2](https://github.com/sapphiredev/utilities/compare/@sapphire/snowflake@3.4.1...@sapphire/snowflake@3.4.2) - (2023-04-12)
6
52
 
7
53
  ## 🏠 Refactor
package/README.md CHANGED
@@ -7,7 +7,6 @@
7
7
  **Deconstruct and generate snowflake IDs using BigInts.**
8
8
 
9
9
  [![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
10
- [![codecov](https://codecov.io/gh/sapphiredev/utilities/branch/main/graph/badge.svg?token=OEGIV6RFDO)](https://codecov.io/gh/sapphiredev/utilities)
11
10
  [![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/snowflake?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/snowflake)
12
11
  [![npm](https://img.shields.io/npm/v/@sapphire/snowflake?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/snowflake)
13
12
 
@@ -28,7 +27,7 @@
28
27
  - [Snowflake with Discord epoch constant](#snowflake-with-discord-epoch-constant-1)
29
28
  - [Snowflake with Twitter epoch constant](#snowflake-with-twitter-epoch-constant-1)
30
29
  - [Buy us some doughnuts](#buy-us-some-doughnuts)
31
- - [Contributors](#contributors-%E2%9C%A8)
30
+ - [Contributors](#contributors)
32
31
 
33
32
  ## Description
34
33
 
@@ -3,42 +3,21 @@
3
3
  var __defProp = Object.defineProperty;
4
4
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var __publicField = (obj, key, value) => {
7
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
- return value;
9
- };
10
- var __accessCheck = (obj, member, msg) => {
11
- if (!member.has(obj))
12
- throw TypeError("Cannot " + msg);
13
- };
14
- var __privateGet = (obj, member, getter) => {
15
- __accessCheck(obj, member, "read from private field");
16
- return getter ? getter.call(obj) : member.get(obj);
17
- };
18
- var __privateAdd = (obj, member, value) => {
19
- if (member.has(obj))
20
- throw TypeError("Cannot add the same private member more than once");
21
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
22
- };
23
- var __privateSet = (obj, member, value, setter) => {
24
- __accessCheck(obj, member, "write to private field");
25
- setter ? setter.call(obj, value) : member.set(obj, value);
26
- return value;
27
- };
28
- var __privateWrapper = (obj, member, setter, getter) => ({
29
- set _(value) {
30
- __privateSet(obj, member, value, setter);
31
- },
32
- get _() {
33
- return __privateGet(obj, member, getter);
34
- }
35
- });
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
36
7
 
37
8
  // src/lib/Snowflake.ts
38
- var ProcessId = 1n;
39
- var WorkerId = 0n;
40
- var _increment, _epoch;
41
- var Snowflake = class {
9
+ var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
10
+ var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
11
+ var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
12
+ var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
13
+ var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
14
+ var MaximumWorkerId = 0b11111n;
15
+ var MaximumProcessId = 0b11111n;
16
+ var MaximumIncrement = 0b111111111111n;
17
+ var TimestampFieldDivisor = 2 ** 22;
18
+ var _a, _b, _c, _d, _e;
19
+ _e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
20
+ var _Snowflake = class _Snowflake {
42
21
  /**
43
22
  * @param epoch the epoch to use
44
23
  */
@@ -48,23 +27,71 @@ var Snowflake = class {
48
27
  */
49
28
  // eslint-disable-next-line @typescript-eslint/unbound-method
50
29
  __publicField(this, "decode", this.deconstruct);
30
+ /**
31
+ * Internal reference of the epoch passed in the constructor
32
+ * @internal
33
+ */
34
+ __publicField(this, _e);
35
+ /**
36
+ * Internal reference of the epoch passed in the constructor as a number
37
+ * @internal
38
+ */
39
+ __publicField(this, _d);
51
40
  /**
52
41
  * Internal incrementor for generating snowflakes
53
42
  * @internal
54
43
  */
55
- __privateAdd(this, _increment, 0n);
44
+ __publicField(this, _c, 0n);
56
45
  /**
57
- * Internal reference of the epoch passed in the constructor
46
+ * The process ID that will be used by default in the generate method
47
+ * @internal
48
+ */
49
+ __publicField(this, _b, 1n);
50
+ /**
51
+ * The worker ID that will be used by default in the generate method
58
52
  * @internal
59
53
  */
60
- __privateAdd(this, _epoch, void 0);
61
- __privateSet(this, _epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
54
+ __publicField(this, _a, 0n);
55
+ this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
56
+ this[EpochNumberSymbol] = Number(this[EpochSymbol]);
62
57
  }
63
58
  /**
64
- * The epoch for this snowflake.
59
+ * The epoch for this snowflake, as a bigint
65
60
  */
66
61
  get epoch() {
67
- return __privateGet(this, _epoch);
62
+ return this[EpochSymbol];
63
+ }
64
+ /**
65
+ * The epoch for this snowflake, as a number
66
+ */
67
+ get epochNumber() {
68
+ return this[EpochNumberSymbol];
69
+ }
70
+ /**
71
+ * Gets the configured process ID
72
+ */
73
+ get processId() {
74
+ return this[ProcessIdSymbol];
75
+ }
76
+ /**
77
+ * Sets the process ID that will be used by default for the {@link generate} method
78
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
79
+ */
80
+ set processId(value) {
81
+ this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;
82
+ }
83
+ /**
84
+ * Gets the configured worker ID
85
+ */
86
+ get workerId() {
87
+ return this[WorkerIdSymbol];
88
+ }
89
+ /**
90
+ * Sets the worker ID that will be used by default for the {@link generate} method
91
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
92
+ */
93
+ set workerId(value) {
94
+ this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;
68
95
  }
69
96
  /**
70
97
  * Generates a snowflake given an epoch and optionally a timestamp
@@ -78,22 +105,22 @@ var Snowflake = class {
78
105
  * ```
79
106
  * @returns A unique snowflake
80
107
  */
81
- generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId } = {}) {
82
- if (timestamp instanceof Date)
83
- timestamp = BigInt(timestamp.getTime());
84
- else if (typeof timestamp === "number")
85
- timestamp = BigInt(timestamp);
108
+ generate({
109
+ increment,
110
+ timestamp = Date.now(),
111
+ workerId = this[WorkerIdSymbol],
112
+ processId = this[ProcessIdSymbol]
113
+ } = {}) {
114
+ if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
115
+ else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
86
116
  else if (typeof timestamp !== "bigint") {
87
117
  throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
88
118
  }
89
- if (typeof increment === "bigint" && increment >= 4095n)
90
- increment = 0n;
91
- else {
92
- increment = __privateWrapper(this, _increment)._++;
93
- if (__privateGet(this, _increment) >= 4095n)
94
- __privateSet(this, _increment, 0n);
119
+ if (typeof increment !== "bigint") {
120
+ increment = this[IncrementSymbol];
121
+ this[IncrementSymbol] = increment + 1n & MaximumIncrement;
95
122
  }
96
- return timestamp - __privateGet(this, _epoch) << 22n | (workerId & 0b11111n) << 17n | (processId & 0b11111n) << 12n | increment;
123
+ return timestamp - this[EpochSymbol] << 22n | (workerId & MaximumWorkerId) << 17n | (processId & MaximumProcessId) << 12n | increment & MaximumIncrement;
97
124
  }
98
125
  /**
99
126
  * Deconstructs a snowflake given a snowflake ID
@@ -107,13 +134,14 @@ var Snowflake = class {
107
134
  */
108
135
  deconstruct(id) {
109
136
  const bigIntId = BigInt(id);
137
+ const epoch = this[EpochSymbol];
110
138
  return {
111
139
  id: bigIntId,
112
- timestamp: (bigIntId >> 22n) + __privateGet(this, _epoch),
113
- workerId: bigIntId >> 17n & 0b11111n,
114
- processId: bigIntId >> 12n & 0b11111n,
115
- increment: bigIntId & 0b111111111111n,
116
- epoch: __privateGet(this, _epoch)
140
+ timestamp: (bigIntId >> 22n) + epoch,
141
+ workerId: bigIntId >> 17n & MaximumWorkerId,
142
+ processId: bigIntId >> 12n & MaximumProcessId,
143
+ increment: bigIntId & MaximumIncrement,
144
+ epoch
117
145
  };
118
146
  }
119
147
  /**
@@ -122,7 +150,7 @@ var Snowflake = class {
122
150
  * @returns The UNIX timestamp that is stored in `id`.
123
151
  */
124
152
  timestampFrom(id) {
125
- return Number((BigInt(id) >> 22n) + __privateGet(this, _epoch));
153
+ return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
126
154
  }
127
155
  /**
128
156
  * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
@@ -148,9 +176,8 @@ var Snowflake = class {
148
176
  return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
149
177
  }
150
178
  };
151
- __name(Snowflake, "Snowflake");
152
- _increment = new WeakMap();
153
- _epoch = new WeakMap();
179
+ __name(_Snowflake, "Snowflake");
180
+ var Snowflake = _Snowflake;
154
181
  function cmpBigInt(a, b) {
155
182
  return a === b ? 0 : a < b ? -1 : 1;
156
183
  }
@@ -167,7 +194,10 @@ var DiscordSnowflake = new Snowflake(1420070400000n);
167
194
  var TwitterSnowflake = new Snowflake(1288834974657n);
168
195
 
169
196
  exports.DiscordSnowflake = DiscordSnowflake;
197
+ exports.MaximumIncrement = MaximumIncrement;
198
+ exports.MaximumProcessId = MaximumProcessId;
199
+ exports.MaximumWorkerId = MaximumWorkerId;
170
200
  exports.Snowflake = Snowflake;
171
201
  exports.TwitterSnowflake = TwitterSnowflake;
172
- //# sourceMappingURL=out.js.map
173
- //# sourceMappingURL=index.js.map
202
+ //# sourceMappingURL=index.cjs.map
203
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;AAAA,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;AAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,C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AAgC;AACzE,IAAA,MAAM,QAAQ,OAAO,CAAA;AACrB,IAAA,OAAO,UAAU,OAAO,CAAA,GACrB,UAAU,QAAA,GACT,SAAA,CAAU,GAAa,CAAW,CAAA,GAClC,UAAU,CAAA,EAAa,CAAW,IACnC,SAAA,CAAU,MAAA,CAAO,CAAC,CAAA,EAAG,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA,EAClC;AACD,CAAA;AA1LuB,MAAA,CAAA,UAAA,EAAA,WAAA,CAAA;AAAhB,IAAM,SAAA,GAAN;AA6LP,SAAS,SAAA,CAAU,GAAW,CAAA,EAAW;AACxC,EAAA,OAAO,CAAA,KAAM,CAAA,GAAI,CAAA,GAAI,CAAA,GAAI,IAAI,EAAA,GAAK,CAAA;AACnC;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA;AAKT,SAAS,SAAA,CAAU,GAAW,CAAA,EAAW;AACxC,EAAA,OAAO,CAAA,KAAM,CAAA,GAAI,CAAA,GAAI,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA,GAAS,EAAA,GAAK,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA,GAAS,CAAA,GAAI,CAAA,GAAI,IAAI,EAAA,GAAK,CAAA;AACxF;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA;;;AC/NF,IAAM,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc;;;ACArD,IAAM,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc","file":"index.cjs","sourcesContent":["const IncrementSymbol = Symbol('@sapphire/snowflake.increment');\nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\nconst EpochNumberSymbol = Symbol('@sapphire/snowflake.epoch.number');\nconst ProcessIdSymbol = Symbol('@sapphire/snowflake.processId');\nconst WorkerIdSymbol = Symbol('@sapphire/snowflake.workerId');\n\n/**\n * The maximum value the `workerId` field accepts in snowflakes.\n */\nexport const MaximumWorkerId = 0b11111n;\n\n/**\n * The maximum value the `processId` field accepts in snowflakes.\n */\nexport const MaximumProcessId = 0b11111n;\n\n/**\n * The maximum value the `increment` field accepts in snowflakes.\n */\nexport const MaximumIncrement = 0b111111111111n;\n\nconst TimestampFieldDivisor = 2 ** 22;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n */\nexport class Snowflake {\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\tprivate readonly [EpochSymbol]: bigint;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor as a number\n\t * @internal\n\t */\n\tprivate readonly [EpochNumberSymbol]: number;\n\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\tprivate [IncrementSymbol] = 0n;\n\n\t/**\n\t * The process ID that will be used by default in the generate method\n\t * @internal\n\t */\n\tprivate [ProcessIdSymbol] = 1n;\n\n\t/**\n\t * The worker ID that will be used by default in the generate method\n\t * @internal\n\t */\n\tprivate [WorkerIdSymbol] = 0n;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t\tthis[EpochNumberSymbol] = Number(this[EpochSymbol]);\n\t}\n\n\t/**\n\t * The epoch for this snowflake, as a bigint\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this[EpochSymbol];\n\t}\n\n\t/**\n\t * The epoch for this snowflake, as a number\n\t */\n\tpublic get epochNumber(): number {\n\t\treturn this[EpochNumberSymbol];\n\t}\n\n\t/**\n\t * Gets the configured process ID\n\t */\n\tpublic get processId(): bigint {\n\t\treturn this[ProcessIdSymbol];\n\t}\n\n\t/**\n\t * Sets the process ID that will be used by default for the {@link generate} method\n\t * @param value The new value, will be coerced to BigInt and masked with `0b11111n`\n\t */\n\tpublic set processId(value: number | bigint) {\n\t\tthis[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n\t}\n\n\t/**\n\t * Gets the configured worker ID\n\t */\n\tpublic get workerId(): bigint {\n\t\treturn this[WorkerIdSymbol];\n\t}\n\n\t/**\n\t * Sets the worker ID that will be used by default for the {@link generate} method\n\t * @param value The new value, will be coerced to BigInt and masked with `0b11111n`\n\t */\n\tpublic set workerId(value: number | bigint) {\n\t\tthis[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when `increment` is not provided it defaults to the private `increment` of the instance\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate({\n\t\tincrement,\n\t\ttimestamp = Date.now(),\n\t\tworkerId = this[WorkerIdSymbol],\n\t\tprocessId = this[ProcessIdSymbol]\n\t}: SnowflakeGenerateOptions = {}) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\telse if (typeof timestamp === 'number') timestamp = BigInt(timestamp);\n\t\telse if (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(`\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`);\n\t\t}\n\n\t\tif (typeof increment !== 'bigint') {\n\t\t\tincrement = this[IncrementSymbol];\n\t\t\tthis[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n\t\t}\n\n\t\t// timestamp, workerId, processId, increment\n\t\treturn (\n\t\t\t((timestamp - this[EpochSymbol]) << 22n) |\n\t\t\t((workerId & MaximumWorkerId) << 17n) |\n\t\t\t((processId & MaximumProcessId) << 12n) |\n\t\t\t(increment & MaximumIncrement)\n\t\t);\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\tconst epoch = this[EpochSymbol];\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + epoch,\n\t\t\tworkerId: (bigIntId >> 17n) & MaximumWorkerId,\n\t\t\tprocessId: (bigIntId >> 12n) & MaximumProcessId,\n\t\t\tincrement: bigIntId & MaximumIncrement,\n\t\t\tepoch\n\t\t};\n\t}\n\n\t/**\n\t * Retrieves the timestamp field's value from a snowflake.\n\t * @param id The snowflake to get the timestamp value from.\n\t * @returns The UNIX timestamp that is stored in `id`.\n\t */\n\tpublic timestampFrom(id: string | bigint): number {\n\t\treturn Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];\n\t}\n\n\t/**\n\t * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n\t * snowflake in sort order.\n\t * @param a The first snowflake to compare.\n\t * @param b The second snowflake to compare.\n\t * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n\t * @example Sort snowflakes in ascending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n\t * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n\t * ```\n\t * @example Sort snowflakes in descending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n\t * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n\t * ```\n\t */\n\tpublic static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n\t\tconst typeA = typeof a;\n\t\treturn typeA === typeof b\n\t\t\t? typeA === 'string'\n\t\t\t\t? cmpString(a as string, b as string)\n\t\t\t\t: cmpBigInt(a as bigint, b as bigint)\n\t\t\t: cmpBigInt(BigInt(a), BigInt(b));\n\t}\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n\treturn a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use, will be truncated to 5 bits (0-31)\n\t * @default 0n\n\t */\n\tworkerId?: bigint;\n\n\t/**\n\t * The process ID to use, will be truncated to 5 bits (0-31)\n\t * @default 1n\n\t */\n\tprocessId?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerId: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessId: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport const DiscordSnowflake = new Snowflake(1420070400000n);\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Twitter's snowflake epoch\n *\n * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}\n */\nexport const TwitterSnowflake = new Snowflake(1288834974657n);\n"]}
@@ -1,3 +1,20 @@
1
+ declare const IncrementSymbol: unique symbol;
2
+ declare const EpochSymbol: unique symbol;
3
+ declare const EpochNumberSymbol: unique symbol;
4
+ declare const ProcessIdSymbol: unique symbol;
5
+ declare const WorkerIdSymbol: unique symbol;
6
+ /**
7
+ * The maximum value the `workerId` field accepts in snowflakes.
8
+ */
9
+ declare const MaximumWorkerId = 31n;
10
+ /**
11
+ * The maximum value the `processId` field accepts in snowflakes.
12
+ */
13
+ declare const MaximumProcessId = 31n;
14
+ /**
15
+ * The maximum value the `increment` field accepts in snowflakes.
16
+ */
17
+ declare const MaximumIncrement = 4095n;
1
18
  /**
2
19
  * A class for generating and deconstructing Twitter snowflakes.
3
20
  *
@@ -12,19 +29,65 @@
12
29
  * ```
13
30
  */
14
31
  declare class Snowflake {
15
- #private;
16
32
  /**
17
33
  * Alias for {@link deconstruct}
18
34
  */
19
35
  decode: (id: string | bigint) => DeconstructedSnowflake;
36
+ /**
37
+ * Internal reference of the epoch passed in the constructor
38
+ * @internal
39
+ */
40
+ private readonly [EpochSymbol];
41
+ /**
42
+ * Internal reference of the epoch passed in the constructor as a number
43
+ * @internal
44
+ */
45
+ private readonly [EpochNumberSymbol];
46
+ /**
47
+ * Internal incrementor for generating snowflakes
48
+ * @internal
49
+ */
50
+ private [IncrementSymbol];
51
+ /**
52
+ * The process ID that will be used by default in the generate method
53
+ * @internal
54
+ */
55
+ private [ProcessIdSymbol];
56
+ /**
57
+ * The worker ID that will be used by default in the generate method
58
+ * @internal
59
+ */
60
+ private [WorkerIdSymbol];
20
61
  /**
21
62
  * @param epoch the epoch to use
22
63
  */
23
64
  constructor(epoch: number | bigint | Date);
24
65
  /**
25
- * The epoch for this snowflake.
66
+ * The epoch for this snowflake, as a bigint
26
67
  */
27
68
  get epoch(): bigint;
69
+ /**
70
+ * The epoch for this snowflake, as a number
71
+ */
72
+ get epochNumber(): number;
73
+ /**
74
+ * Gets the configured process ID
75
+ */
76
+ get processId(): bigint;
77
+ /**
78
+ * Sets the process ID that will be used by default for the {@link generate} method
79
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
80
+ */
81
+ set processId(value: number | bigint);
82
+ /**
83
+ * Gets the configured worker ID
84
+ */
85
+ get workerId(): bigint;
86
+ /**
87
+ * Sets the worker ID that will be used by default for the {@link generate} method
88
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
89
+ */
90
+ set workerId(value: number | bigint);
28
91
  /**
29
92
  * Generates a snowflake given an epoch and optionally a timestamp
30
93
  * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
@@ -146,4 +209,4 @@ declare const DiscordSnowflake: Snowflake;
146
209
  */
147
210
  declare const TwitterSnowflake: Snowflake;
148
211
 
149
- export { DeconstructedSnowflake, DiscordSnowflake, Snowflake, SnowflakeGenerateOptions, TwitterSnowflake };
212
+ export { type DeconstructedSnowflake, DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, Snowflake, type SnowflakeGenerateOptions, TwitterSnowflake };
@@ -0,0 +1,212 @@
1
+ declare const IncrementSymbol: unique symbol;
2
+ declare const EpochSymbol: unique symbol;
3
+ declare const EpochNumberSymbol: unique symbol;
4
+ declare const ProcessIdSymbol: unique symbol;
5
+ declare const WorkerIdSymbol: unique symbol;
6
+ /**
7
+ * The maximum value the `workerId` field accepts in snowflakes.
8
+ */
9
+ declare const MaximumWorkerId = 31n;
10
+ /**
11
+ * The maximum value the `processId` field accepts in snowflakes.
12
+ */
13
+ declare const MaximumProcessId = 31n;
14
+ /**
15
+ * The maximum value the `increment` field accepts in snowflakes.
16
+ */
17
+ declare const MaximumIncrement = 4095n;
18
+ /**
19
+ * A class for generating and deconstructing Twitter snowflakes.
20
+ *
21
+ * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}
22
+ * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.
23
+ *
24
+ * If we have a snowflake `266241948824764416` we can represent it as binary:
25
+ * ```
26
+ * 64 22 17 12 0
27
+ * 000000111011000111100001101001000101000000 00001 00000 000000000000
28
+ * number of ms since epoch worker pid increment
29
+ * ```
30
+ */
31
+ declare class Snowflake {
32
+ /**
33
+ * Alias for {@link deconstruct}
34
+ */
35
+ decode: (id: string | bigint) => DeconstructedSnowflake;
36
+ /**
37
+ * Internal reference of the epoch passed in the constructor
38
+ * @internal
39
+ */
40
+ private readonly [EpochSymbol];
41
+ /**
42
+ * Internal reference of the epoch passed in the constructor as a number
43
+ * @internal
44
+ */
45
+ private readonly [EpochNumberSymbol];
46
+ /**
47
+ * Internal incrementor for generating snowflakes
48
+ * @internal
49
+ */
50
+ private [IncrementSymbol];
51
+ /**
52
+ * The process ID that will be used by default in the generate method
53
+ * @internal
54
+ */
55
+ private [ProcessIdSymbol];
56
+ /**
57
+ * The worker ID that will be used by default in the generate method
58
+ * @internal
59
+ */
60
+ private [WorkerIdSymbol];
61
+ /**
62
+ * @param epoch the epoch to use
63
+ */
64
+ constructor(epoch: number | bigint | Date);
65
+ /**
66
+ * The epoch for this snowflake, as a bigint
67
+ */
68
+ get epoch(): bigint;
69
+ /**
70
+ * The epoch for this snowflake, as a number
71
+ */
72
+ get epochNumber(): number;
73
+ /**
74
+ * Gets the configured process ID
75
+ */
76
+ get processId(): bigint;
77
+ /**
78
+ * Sets the process ID that will be used by default for the {@link generate} method
79
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
80
+ */
81
+ set processId(value: number | bigint);
82
+ /**
83
+ * Gets the configured worker ID
84
+ */
85
+ get workerId(): bigint;
86
+ /**
87
+ * Sets the worker ID that will be used by default for the {@link generate} method
88
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
89
+ */
90
+ set workerId(value: number | bigint);
91
+ /**
92
+ * Generates a snowflake given an epoch and optionally a timestamp
93
+ * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
94
+ *
95
+ * **note** when `increment` is not provided it defaults to the private `increment` of the instance
96
+ * @example
97
+ * ```typescript
98
+ * const epoch = new Date('2000-01-01T00:00:00.000Z');
99
+ * const snowflake = new Snowflake(epoch).generate();
100
+ * ```
101
+ * @returns A unique snowflake
102
+ */
103
+ generate({ increment, timestamp, workerId, processId }?: SnowflakeGenerateOptions): bigint;
104
+ /**
105
+ * Deconstructs a snowflake given a snowflake ID
106
+ * @param id the snowflake to deconstruct
107
+ * @returns a deconstructed snowflake
108
+ * @example
109
+ * ```typescript
110
+ * const epoch = new Date('2000-01-01T00:00:00.000Z');
111
+ * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
112
+ * ```
113
+ */
114
+ deconstruct(id: string | bigint): DeconstructedSnowflake;
115
+ /**
116
+ * Retrieves the timestamp field's value from a snowflake.
117
+ * @param id The snowflake to get the timestamp value from.
118
+ * @returns The UNIX timestamp that is stored in `id`.
119
+ */
120
+ timestampFrom(id: string | bigint): number;
121
+ /**
122
+ * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
123
+ * snowflake in sort order.
124
+ * @param a The first snowflake to compare.
125
+ * @param b The second snowflake to compare.
126
+ * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
127
+ * @example Sort snowflakes in ascending order
128
+ * ```typescript
129
+ * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
130
+ * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
131
+ * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
132
+ * ```
133
+ * @example Sort snowflakes in descending order
134
+ * ```typescript
135
+ * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
136
+ * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
137
+ * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];
138
+ * ```
139
+ */
140
+ static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1;
141
+ }
142
+ /**
143
+ * Options for Snowflake#generate
144
+ */
145
+ interface SnowflakeGenerateOptions {
146
+ /**
147
+ * Timestamp or date of the snowflake to generate
148
+ * @default Date.now()
149
+ */
150
+ timestamp?: number | bigint | Date;
151
+ /**
152
+ * The increment to use
153
+ * @default 0n
154
+ * @remark keep in mind that this bigint is auto-incremented between generate calls
155
+ */
156
+ increment?: bigint;
157
+ /**
158
+ * The worker ID to use, will be truncated to 5 bits (0-31)
159
+ * @default 0n
160
+ */
161
+ workerId?: bigint;
162
+ /**
163
+ * The process ID to use, will be truncated to 5 bits (0-31)
164
+ * @default 1n
165
+ */
166
+ processId?: bigint;
167
+ }
168
+ /**
169
+ * Object returned by Snowflake#deconstruct
170
+ */
171
+ interface DeconstructedSnowflake {
172
+ /**
173
+ * The id in BigInt form
174
+ */
175
+ id: bigint;
176
+ /**
177
+ * The timestamp stored in the snowflake
178
+ */
179
+ timestamp: bigint;
180
+ /**
181
+ * The worker id stored in the snowflake
182
+ */
183
+ workerId: bigint;
184
+ /**
185
+ * The process id stored in the snowflake
186
+ */
187
+ processId: bigint;
188
+ /**
189
+ * The increment stored in the snowflake
190
+ */
191
+ increment: bigint;
192
+ /**
193
+ * The epoch to use in the snowflake
194
+ */
195
+ epoch: bigint;
196
+ }
197
+
198
+ /**
199
+ * A class for parsing snowflake ids using Discord's snowflake epoch
200
+ *
201
+ * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
202
+ */
203
+ declare const DiscordSnowflake: Snowflake;
204
+
205
+ /**
206
+ * A class for parsing snowflake ids using Twitter's snowflake epoch
207
+ *
208
+ * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}
209
+ */
210
+ declare const TwitterSnowflake: Snowflake;
211
+
212
+ export { type DeconstructedSnowflake, DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, Snowflake, type SnowflakeGenerateOptions, TwitterSnowflake };
@@ -1,42 +1,21 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
- var __publicField = (obj, key, value) => {
5
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
- return value;
7
- };
8
- var __accessCheck = (obj, member, msg) => {
9
- if (!member.has(obj))
10
- throw TypeError("Cannot " + msg);
11
- };
12
- var __privateGet = (obj, member, getter) => {
13
- __accessCheck(obj, member, "read from private field");
14
- return getter ? getter.call(obj) : member.get(obj);
15
- };
16
- var __privateAdd = (obj, member, value) => {
17
- if (member.has(obj))
18
- throw TypeError("Cannot add the same private member more than once");
19
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
20
- };
21
- var __privateSet = (obj, member, value, setter) => {
22
- __accessCheck(obj, member, "write to private field");
23
- setter ? setter.call(obj, value) : member.set(obj, value);
24
- return value;
25
- };
26
- var __privateWrapper = (obj, member, setter, getter) => ({
27
- set _(value) {
28
- __privateSet(obj, member, value, setter);
29
- },
30
- get _() {
31
- return __privateGet(obj, member, getter);
32
- }
33
- });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
34
5
 
35
6
  // src/lib/Snowflake.ts
36
- var ProcessId = 1n;
37
- var WorkerId = 0n;
38
- var _increment, _epoch;
39
- var Snowflake = class {
7
+ var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
8
+ var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
9
+ var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
10
+ var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
11
+ var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
12
+ var MaximumWorkerId = 0b11111n;
13
+ var MaximumProcessId = 0b11111n;
14
+ var MaximumIncrement = 0b111111111111n;
15
+ var TimestampFieldDivisor = 2 ** 22;
16
+ var _a, _b, _c, _d, _e;
17
+ _e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
18
+ var _Snowflake = class _Snowflake {
40
19
  /**
41
20
  * @param epoch the epoch to use
42
21
  */
@@ -46,23 +25,71 @@ var Snowflake = class {
46
25
  */
47
26
  // eslint-disable-next-line @typescript-eslint/unbound-method
48
27
  __publicField(this, "decode", this.deconstruct);
28
+ /**
29
+ * Internal reference of the epoch passed in the constructor
30
+ * @internal
31
+ */
32
+ __publicField(this, _e);
33
+ /**
34
+ * Internal reference of the epoch passed in the constructor as a number
35
+ * @internal
36
+ */
37
+ __publicField(this, _d);
49
38
  /**
50
39
  * Internal incrementor for generating snowflakes
51
40
  * @internal
52
41
  */
53
- __privateAdd(this, _increment, 0n);
42
+ __publicField(this, _c, 0n);
54
43
  /**
55
- * Internal reference of the epoch passed in the constructor
44
+ * The process ID that will be used by default in the generate method
45
+ * @internal
46
+ */
47
+ __publicField(this, _b, 1n);
48
+ /**
49
+ * The worker ID that will be used by default in the generate method
56
50
  * @internal
57
51
  */
58
- __privateAdd(this, _epoch, void 0);
59
- __privateSet(this, _epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
52
+ __publicField(this, _a, 0n);
53
+ this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
54
+ this[EpochNumberSymbol] = Number(this[EpochSymbol]);
60
55
  }
61
56
  /**
62
- * The epoch for this snowflake.
57
+ * The epoch for this snowflake, as a bigint
63
58
  */
64
59
  get epoch() {
65
- return __privateGet(this, _epoch);
60
+ return this[EpochSymbol];
61
+ }
62
+ /**
63
+ * The epoch for this snowflake, as a number
64
+ */
65
+ get epochNumber() {
66
+ return this[EpochNumberSymbol];
67
+ }
68
+ /**
69
+ * Gets the configured process ID
70
+ */
71
+ get processId() {
72
+ return this[ProcessIdSymbol];
73
+ }
74
+ /**
75
+ * Sets the process ID that will be used by default for the {@link generate} method
76
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
77
+ */
78
+ set processId(value) {
79
+ this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;
80
+ }
81
+ /**
82
+ * Gets the configured worker ID
83
+ */
84
+ get workerId() {
85
+ return this[WorkerIdSymbol];
86
+ }
87
+ /**
88
+ * Sets the worker ID that will be used by default for the {@link generate} method
89
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
90
+ */
91
+ set workerId(value) {
92
+ this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;
66
93
  }
67
94
  /**
68
95
  * Generates a snowflake given an epoch and optionally a timestamp
@@ -76,22 +103,22 @@ var Snowflake = class {
76
103
  * ```
77
104
  * @returns A unique snowflake
78
105
  */
79
- generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId } = {}) {
80
- if (timestamp instanceof Date)
81
- timestamp = BigInt(timestamp.getTime());
82
- else if (typeof timestamp === "number")
83
- timestamp = BigInt(timestamp);
106
+ generate({
107
+ increment,
108
+ timestamp = Date.now(),
109
+ workerId = this[WorkerIdSymbol],
110
+ processId = this[ProcessIdSymbol]
111
+ } = {}) {
112
+ if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
113
+ else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
84
114
  else if (typeof timestamp !== "bigint") {
85
115
  throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
86
116
  }
87
- if (typeof increment === "bigint" && increment >= 4095n)
88
- increment = 0n;
89
- else {
90
- increment = __privateWrapper(this, _increment)._++;
91
- if (__privateGet(this, _increment) >= 4095n)
92
- __privateSet(this, _increment, 0n);
117
+ if (typeof increment !== "bigint") {
118
+ increment = this[IncrementSymbol];
119
+ this[IncrementSymbol] = increment + 1n & MaximumIncrement;
93
120
  }
94
- return timestamp - __privateGet(this, _epoch) << 22n | (workerId & 0b11111n) << 17n | (processId & 0b11111n) << 12n | increment;
121
+ return timestamp - this[EpochSymbol] << 22n | (workerId & MaximumWorkerId) << 17n | (processId & MaximumProcessId) << 12n | increment & MaximumIncrement;
95
122
  }
96
123
  /**
97
124
  * Deconstructs a snowflake given a snowflake ID
@@ -105,13 +132,14 @@ var Snowflake = class {
105
132
  */
106
133
  deconstruct(id) {
107
134
  const bigIntId = BigInt(id);
135
+ const epoch = this[EpochSymbol];
108
136
  return {
109
137
  id: bigIntId,
110
- timestamp: (bigIntId >> 22n) + __privateGet(this, _epoch),
111
- workerId: bigIntId >> 17n & 0b11111n,
112
- processId: bigIntId >> 12n & 0b11111n,
113
- increment: bigIntId & 0b111111111111n,
114
- epoch: __privateGet(this, _epoch)
138
+ timestamp: (bigIntId >> 22n) + epoch,
139
+ workerId: bigIntId >> 17n & MaximumWorkerId,
140
+ processId: bigIntId >> 12n & MaximumProcessId,
141
+ increment: bigIntId & MaximumIncrement,
142
+ epoch
115
143
  };
116
144
  }
117
145
  /**
@@ -120,7 +148,7 @@ var Snowflake = class {
120
148
  * @returns The UNIX timestamp that is stored in `id`.
121
149
  */
122
150
  timestampFrom(id) {
123
- return Number((BigInt(id) >> 22n) + __privateGet(this, _epoch));
151
+ return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
124
152
  }
125
153
  /**
126
154
  * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
@@ -146,9 +174,8 @@ var Snowflake = class {
146
174
  return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
147
175
  }
148
176
  };
149
- __name(Snowflake, "Snowflake");
150
- _increment = new WeakMap();
151
- _epoch = new WeakMap();
177
+ __name(_Snowflake, "Snowflake");
178
+ var Snowflake = _Snowflake;
152
179
  function cmpBigInt(a, b) {
153
180
  return a === b ? 0 : a < b ? -1 : 1;
154
181
  }
@@ -164,6 +191,6 @@ var DiscordSnowflake = new Snowflake(1420070400000n);
164
191
  // src/lib/TwitterSnowflake.ts
165
192
  var TwitterSnowflake = new Snowflake(1288834974657n);
166
193
 
167
- export { DiscordSnowflake, Snowflake, TwitterSnowflake };
168
- //# sourceMappingURL=out.js.map
194
+ export { DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, Snowflake, TwitterSnowflake };
195
+ //# sourceMappingURL=index.mjs.map
169
196
  //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;AAAA,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;AAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,CAAA;AACtD,IAAM,iBAAA,GAAoB,OAAO,kCAAkC,CAAA;AACnE,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;AAC9D,IAAM,cAAA,GAAiB,OAAO,8BAA8B,CAAA;AAKrD,IAAM,eAAA,GAAkB;AAKxB,IAAM,gBAAA,GAAmB;AAKzB,IAAM,gBAAA,GAAmB;AAEhC,IAAM,wBAAwB,CAAA,IAAK,EAAA;AArBnC,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA;AA+CmB,EAAA,GAAA,W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AU,GAAW,CAAA,EAAW;AACxC,EAAA,OAAO,CAAA,KAAM,CAAA,GAAI,CAAA,GAAI,CAAA,GAAI,IAAI,EAAA,GAAK,CAAA;AACnC;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA;AAKT,SAAS,SAAA,CAAU,GAAW,CAAA,EAAW;AACxC,EAAA,OAAO,CAAA,KAAM,CAAA,GAAI,CAAA,GAAI,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA,GAAS,EAAA,GAAK,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA,GAAS,CAAA,GAAI,CAAA,GAAI,IAAI,EAAA,GAAK,CAAA;AACxF;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA;;;AC/NF,IAAM,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc;;;ACArD,IAAM,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc","file":"index.mjs","sourcesContent":["const IncrementSymbol = Symbol('@sapphire/snowflake.increment');\nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\nconst EpochNumberSymbol = Symbol('@sapphire/snowflake.epoch.number');\nconst ProcessIdSymbol = Symbol('@sapphire/snowflake.processId');\nconst WorkerIdSymbol = Symbol('@sapphire/snowflake.workerId');\n\n/**\n * The maximum value the `workerId` field accepts in snowflakes.\n */\nexport const MaximumWorkerId = 0b11111n;\n\n/**\n * The maximum value the `processId` field accepts in snowflakes.\n */\nexport const MaximumProcessId = 0b11111n;\n\n/**\n * The maximum value the `increment` field accepts in snowflakes.\n */\nexport const MaximumIncrement = 0b111111111111n;\n\nconst TimestampFieldDivisor = 2 ** 22;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n */\nexport class Snowflake {\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\tprivate readonly [EpochSymbol]: bigint;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor as a number\n\t * @internal\n\t */\n\tprivate readonly [EpochNumberSymbol]: number;\n\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\tprivate [IncrementSymbol] = 0n;\n\n\t/**\n\t * The process ID that will be used by default in the generate method\n\t * @internal\n\t */\n\tprivate [ProcessIdSymbol] = 1n;\n\n\t/**\n\t * The worker ID that will be used by default in the generate method\n\t * @internal\n\t */\n\tprivate [WorkerIdSymbol] = 0n;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t\tthis[EpochNumberSymbol] = Number(this[EpochSymbol]);\n\t}\n\n\t/**\n\t * The epoch for this snowflake, as a bigint\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this[EpochSymbol];\n\t}\n\n\t/**\n\t * The epoch for this snowflake, as a number\n\t */\n\tpublic get epochNumber(): number {\n\t\treturn this[EpochNumberSymbol];\n\t}\n\n\t/**\n\t * Gets the configured process ID\n\t */\n\tpublic get processId(): bigint {\n\t\treturn this[ProcessIdSymbol];\n\t}\n\n\t/**\n\t * Sets the process ID that will be used by default for the {@link generate} method\n\t * @param value The new value, will be coerced to BigInt and masked with `0b11111n`\n\t */\n\tpublic set processId(value: number | bigint) {\n\t\tthis[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n\t}\n\n\t/**\n\t * Gets the configured worker ID\n\t */\n\tpublic get workerId(): bigint {\n\t\treturn this[WorkerIdSymbol];\n\t}\n\n\t/**\n\t * Sets the worker ID that will be used by default for the {@link generate} method\n\t * @param value The new value, will be coerced to BigInt and masked with `0b11111n`\n\t */\n\tpublic set workerId(value: number | bigint) {\n\t\tthis[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when `increment` is not provided it defaults to the private `increment` of the instance\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate({\n\t\tincrement,\n\t\ttimestamp = Date.now(),\n\t\tworkerId = this[WorkerIdSymbol],\n\t\tprocessId = this[ProcessIdSymbol]\n\t}: SnowflakeGenerateOptions = {}) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\telse if (typeof timestamp === 'number') timestamp = BigInt(timestamp);\n\t\telse if (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(`\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`);\n\t\t}\n\n\t\tif (typeof increment !== 'bigint') {\n\t\t\tincrement = this[IncrementSymbol];\n\t\t\tthis[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n\t\t}\n\n\t\t// timestamp, workerId, processId, increment\n\t\treturn (\n\t\t\t((timestamp - this[EpochSymbol]) << 22n) |\n\t\t\t((workerId & MaximumWorkerId) << 17n) |\n\t\t\t((processId & MaximumProcessId) << 12n) |\n\t\t\t(increment & MaximumIncrement)\n\t\t);\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\tconst epoch = this[EpochSymbol];\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + epoch,\n\t\t\tworkerId: (bigIntId >> 17n) & MaximumWorkerId,\n\t\t\tprocessId: (bigIntId >> 12n) & MaximumProcessId,\n\t\t\tincrement: bigIntId & MaximumIncrement,\n\t\t\tepoch\n\t\t};\n\t}\n\n\t/**\n\t * Retrieves the timestamp field's value from a snowflake.\n\t * @param id The snowflake to get the timestamp value from.\n\t * @returns The UNIX timestamp that is stored in `id`.\n\t */\n\tpublic timestampFrom(id: string | bigint): number {\n\t\treturn Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];\n\t}\n\n\t/**\n\t * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n\t * snowflake in sort order.\n\t * @param a The first snowflake to compare.\n\t * @param b The second snowflake to compare.\n\t * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n\t * @example Sort snowflakes in ascending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n\t * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n\t * ```\n\t * @example Sort snowflakes in descending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n\t * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n\t * ```\n\t */\n\tpublic static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n\t\tconst typeA = typeof a;\n\t\treturn typeA === typeof b\n\t\t\t? typeA === 'string'\n\t\t\t\t? cmpString(a as string, b as string)\n\t\t\t\t: cmpBigInt(a as bigint, b as bigint)\n\t\t\t: cmpBigInt(BigInt(a), BigInt(b));\n\t}\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n\treturn a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use, will be truncated to 5 bits (0-31)\n\t * @default 0n\n\t */\n\tworkerId?: bigint;\n\n\t/**\n\t * The process ID to use, will be truncated to 5 bits (0-31)\n\t * @default 1n\n\t */\n\tprocessId?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerId: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessId: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport const DiscordSnowflake = new Snowflake(1420070400000n);\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Twitter's snowflake epoch\n *\n * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}\n */\nexport const TwitterSnowflake = new Snowflake(1288834974657n);\n"]}
@@ -4,42 +4,21 @@ var SapphireSnowflake = (function (exports) {
4
4
  var __defProp = Object.defineProperty;
5
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __publicField = (obj, key, value) => {
8
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
9
- return value;
10
- };
11
- var __accessCheck = (obj, member, msg) => {
12
- if (!member.has(obj))
13
- throw TypeError("Cannot " + msg);
14
- };
15
- var __privateGet = (obj, member, getter) => {
16
- __accessCheck(obj, member, "read from private field");
17
- return getter ? getter.call(obj) : member.get(obj);
18
- };
19
- var __privateAdd = (obj, member, value) => {
20
- if (member.has(obj))
21
- throw TypeError("Cannot add the same private member more than once");
22
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
23
- };
24
- var __privateSet = (obj, member, value, setter) => {
25
- __accessCheck(obj, member, "write to private field");
26
- setter ? setter.call(obj, value) : member.set(obj, value);
27
- return value;
28
- };
29
- var __privateWrapper = (obj, member, setter, getter) => ({
30
- set _(value) {
31
- __privateSet(obj, member, value, setter);
32
- },
33
- get _() {
34
- return __privateGet(obj, member, getter);
35
- }
36
- });
7
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
37
8
 
38
9
  // src/lib/Snowflake.ts
39
- var ProcessId = 1n;
40
- var WorkerId = 0n;
41
- var _increment, _epoch;
42
- var Snowflake = class {
10
+ var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
11
+ var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
12
+ var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
13
+ var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
14
+ var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
15
+ var MaximumWorkerId = 0b11111n;
16
+ var MaximumProcessId = 0b11111n;
17
+ var MaximumIncrement = 0b111111111111n;
18
+ var TimestampFieldDivisor = 2 ** 22;
19
+ var _a, _b, _c, _d, _e;
20
+ _e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
21
+ var _Snowflake = class _Snowflake {
43
22
  /**
44
23
  * @param epoch the epoch to use
45
24
  */
@@ -49,23 +28,71 @@ var SapphireSnowflake = (function (exports) {
49
28
  */
50
29
  // eslint-disable-next-line @typescript-eslint/unbound-method
51
30
  __publicField(this, "decode", this.deconstruct);
31
+ /**
32
+ * Internal reference of the epoch passed in the constructor
33
+ * @internal
34
+ */
35
+ __publicField(this, _e);
36
+ /**
37
+ * Internal reference of the epoch passed in the constructor as a number
38
+ * @internal
39
+ */
40
+ __publicField(this, _d);
52
41
  /**
53
42
  * Internal incrementor for generating snowflakes
54
43
  * @internal
55
44
  */
56
- __privateAdd(this, _increment, 0n);
45
+ __publicField(this, _c, 0n);
57
46
  /**
58
- * Internal reference of the epoch passed in the constructor
47
+ * The process ID that will be used by default in the generate method
48
+ * @internal
49
+ */
50
+ __publicField(this, _b, 1n);
51
+ /**
52
+ * The worker ID that will be used by default in the generate method
59
53
  * @internal
60
54
  */
61
- __privateAdd(this, _epoch, void 0);
62
- __privateSet(this, _epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
55
+ __publicField(this, _a, 0n);
56
+ this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
57
+ this[EpochNumberSymbol] = Number(this[EpochSymbol]);
63
58
  }
64
59
  /**
65
- * The epoch for this snowflake.
60
+ * The epoch for this snowflake, as a bigint
66
61
  */
67
62
  get epoch() {
68
- return __privateGet(this, _epoch);
63
+ return this[EpochSymbol];
64
+ }
65
+ /**
66
+ * The epoch for this snowflake, as a number
67
+ */
68
+ get epochNumber() {
69
+ return this[EpochNumberSymbol];
70
+ }
71
+ /**
72
+ * Gets the configured process ID
73
+ */
74
+ get processId() {
75
+ return this[ProcessIdSymbol];
76
+ }
77
+ /**
78
+ * Sets the process ID that will be used by default for the {@link generate} method
79
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
80
+ */
81
+ set processId(value) {
82
+ this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;
83
+ }
84
+ /**
85
+ * Gets the configured worker ID
86
+ */
87
+ get workerId() {
88
+ return this[WorkerIdSymbol];
89
+ }
90
+ /**
91
+ * Sets the worker ID that will be used by default for the {@link generate} method
92
+ * @param value The new value, will be coerced to BigInt and masked with `0b11111n`
93
+ */
94
+ set workerId(value) {
95
+ this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;
69
96
  }
70
97
  /**
71
98
  * Generates a snowflake given an epoch and optionally a timestamp
@@ -79,22 +106,22 @@ var SapphireSnowflake = (function (exports) {
79
106
  * ```
80
107
  * @returns A unique snowflake
81
108
  */
82
- generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId } = {}) {
83
- if (timestamp instanceof Date)
84
- timestamp = BigInt(timestamp.getTime());
85
- else if (typeof timestamp === "number")
86
- timestamp = BigInt(timestamp);
109
+ generate({
110
+ increment,
111
+ timestamp = Date.now(),
112
+ workerId = this[WorkerIdSymbol],
113
+ processId = this[ProcessIdSymbol]
114
+ } = {}) {
115
+ if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
116
+ else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
87
117
  else if (typeof timestamp !== "bigint") {
88
118
  throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
89
119
  }
90
- if (typeof increment === "bigint" && increment >= 4095n)
91
- increment = 0n;
92
- else {
93
- increment = __privateWrapper(this, _increment)._++;
94
- if (__privateGet(this, _increment) >= 4095n)
95
- __privateSet(this, _increment, 0n);
120
+ if (typeof increment !== "bigint") {
121
+ increment = this[IncrementSymbol];
122
+ this[IncrementSymbol] = increment + 1n & MaximumIncrement;
96
123
  }
97
- return timestamp - __privateGet(this, _epoch) << 22n | (workerId & 0b11111n) << 17n | (processId & 0b11111n) << 12n | increment;
124
+ return timestamp - this[EpochSymbol] << 22n | (workerId & MaximumWorkerId) << 17n | (processId & MaximumProcessId) << 12n | increment & MaximumIncrement;
98
125
  }
99
126
  /**
100
127
  * Deconstructs a snowflake given a snowflake ID
@@ -108,13 +135,14 @@ var SapphireSnowflake = (function (exports) {
108
135
  */
109
136
  deconstruct(id) {
110
137
  const bigIntId = BigInt(id);
138
+ const epoch = this[EpochSymbol];
111
139
  return {
112
140
  id: bigIntId,
113
- timestamp: (bigIntId >> 22n) + __privateGet(this, _epoch),
114
- workerId: bigIntId >> 17n & 0b11111n,
115
- processId: bigIntId >> 12n & 0b11111n,
116
- increment: bigIntId & 0b111111111111n,
117
- epoch: __privateGet(this, _epoch)
141
+ timestamp: (bigIntId >> 22n) + epoch,
142
+ workerId: bigIntId >> 17n & MaximumWorkerId,
143
+ processId: bigIntId >> 12n & MaximumProcessId,
144
+ increment: bigIntId & MaximumIncrement,
145
+ epoch
118
146
  };
119
147
  }
120
148
  /**
@@ -123,7 +151,7 @@ var SapphireSnowflake = (function (exports) {
123
151
  * @returns The UNIX timestamp that is stored in `id`.
124
152
  */
125
153
  timestampFrom(id) {
126
- return Number((BigInt(id) >> 22n) + __privateGet(this, _epoch));
154
+ return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
127
155
  }
128
156
  /**
129
157
  * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
@@ -149,9 +177,8 @@ var SapphireSnowflake = (function (exports) {
149
177
  return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
150
178
  }
151
179
  };
152
- __name(Snowflake, "Snowflake");
153
- _increment = new WeakMap();
154
- _epoch = new WeakMap();
180
+ __name(_Snowflake, "Snowflake");
181
+ var Snowflake = _Snowflake;
155
182
  function cmpBigInt(a, b) {
156
183
  return a === b ? 0 : a < b ? -1 : 1;
157
184
  }
@@ -168,11 +195,14 @@ var SapphireSnowflake = (function (exports) {
168
195
  var TwitterSnowflake = new Snowflake(1288834974657n);
169
196
 
170
197
  exports.DiscordSnowflake = DiscordSnowflake;
198
+ exports.MaximumIncrement = MaximumIncrement;
199
+ exports.MaximumProcessId = MaximumProcessId;
200
+ exports.MaximumWorkerId = MaximumWorkerId;
171
201
  exports.Snowflake = Snowflake;
172
202
  exports.TwitterSnowflake = TwitterSnowflake;
173
203
 
174
204
  return exports;
175
205
 
176
206
  })({});
177
- //# sourceMappingURL=out.js.map
207
+ //# sourceMappingURL=index.global.js.map
178
208
  //# sourceMappingURL=index.global.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;;EAAA,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;EAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,CAAA;EACtD,IAAM,iBAAA,GAAoB,OAAO,kCAAkC,CAAA;EACnE,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;EAC9D,IAAM,cAAA,GAAiB,OAAO,8BAA8B,CAAA;AAKrD,MAAM,eAAA,GAAkB;AAKxB,MAAM,gBAAA,GAAmB;AAKzB,MAAM,gBAAA,GAAmB;EAEhC,IAAM,wBAAwB,CAAA,IAAK,EAAA;EArBnC,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA;EA+CmB,EAAA,GAAA,WAAA,EAMA,EAAA,GAAA,iBAAA,EAMT,sBAMA,EAAA,GAAA,eAAA,EAMA,EAAA,GAAA,cAAA;EAnCH,IAAM,UAAA,GAAN,MAAM,UAAA,CAAU;EAAA;EAAA;EAAA;EAAA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A,CAAA;EAKT,SAAS,SAAA,CAAU,GAAW,CAAA,EAAW;EACxC,EAAA,OAAO,CAAA,KAAM,CAAA,GAAI,CAAA,GAAI,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA,GAAS,EAAA,GAAK,CAAA,CAAE,SAAS,CAAA,CAAE,MAAA,GAAS,CAAA,GAAI,CAAA,GAAI,IAAI,EAAA,GAAK,CAAA;EACxF;EAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA;;;AC/NF,MAAM,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc;;;ACArD,MAAM,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc","file":"index.global.js","sourcesContent":["const IncrementSymbol = Symbol('@sapphire/snowflake.increment');\nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\nconst EpochNumberSymbol = Symbol('@sapphire/snowflake.epoch.number');\nconst ProcessIdSymbol = Symbol('@sapphire/snowflake.processId');\nconst WorkerIdSymbol = Symbol('@sapphire/snowflake.workerId');\n\n/**\n * The maximum value the `workerId` field accepts in snowflakes.\n */\nexport const MaximumWorkerId = 0b11111n;\n\n/**\n * The maximum value the `processId` field accepts in snowflakes.\n */\nexport const MaximumProcessId = 0b11111n;\n\n/**\n * The maximum value the `increment` field accepts in snowflakes.\n */\nexport const MaximumIncrement = 0b111111111111n;\n\nconst TimestampFieldDivisor = 2 ** 22;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n */\nexport class Snowflake {\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\tprivate readonly [EpochSymbol]: bigint;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor as a number\n\t * @internal\n\t */\n\tprivate readonly [EpochNumberSymbol]: number;\n\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\tprivate [IncrementSymbol] = 0n;\n\n\t/**\n\t * The process ID that will be used by default in the generate method\n\t * @internal\n\t */\n\tprivate [ProcessIdSymbol] = 1n;\n\n\t/**\n\t * The worker ID that will be used by default in the generate method\n\t * @internal\n\t */\n\tprivate [WorkerIdSymbol] = 0n;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t\tthis[EpochNumberSymbol] = Number(this[EpochSymbol]);\n\t}\n\n\t/**\n\t * The epoch for this snowflake, as a bigint\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this[EpochSymbol];\n\t}\n\n\t/**\n\t * The epoch for this snowflake, as a number\n\t */\n\tpublic get epochNumber(): number {\n\t\treturn this[EpochNumberSymbol];\n\t}\n\n\t/**\n\t * Gets the configured process ID\n\t */\n\tpublic get processId(): bigint {\n\t\treturn this[ProcessIdSymbol];\n\t}\n\n\t/**\n\t * Sets the process ID that will be used by default for the {@link generate} method\n\t * @param value The new value, will be coerced to BigInt and masked with `0b11111n`\n\t */\n\tpublic set processId(value: number | bigint) {\n\t\tthis[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n\t}\n\n\t/**\n\t * Gets the configured worker ID\n\t */\n\tpublic get workerId(): bigint {\n\t\treturn this[WorkerIdSymbol];\n\t}\n\n\t/**\n\t * Sets the worker ID that will be used by default for the {@link generate} method\n\t * @param value The new value, will be coerced to BigInt and masked with `0b11111n`\n\t */\n\tpublic set workerId(value: number | bigint) {\n\t\tthis[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when `increment` is not provided it defaults to the private `increment` of the instance\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate({\n\t\tincrement,\n\t\ttimestamp = Date.now(),\n\t\tworkerId = this[WorkerIdSymbol],\n\t\tprocessId = this[ProcessIdSymbol]\n\t}: SnowflakeGenerateOptions = {}) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\telse if (typeof timestamp === 'number') timestamp = BigInt(timestamp);\n\t\telse if (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(`\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`);\n\t\t}\n\n\t\tif (typeof increment !== 'bigint') {\n\t\t\tincrement = this[IncrementSymbol];\n\t\t\tthis[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n\t\t}\n\n\t\t// timestamp, workerId, processId, increment\n\t\treturn (\n\t\t\t((timestamp - this[EpochSymbol]) << 22n) |\n\t\t\t((workerId & MaximumWorkerId) << 17n) |\n\t\t\t((processId & MaximumProcessId) << 12n) |\n\t\t\t(increment & MaximumIncrement)\n\t\t);\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\tconst epoch = this[EpochSymbol];\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + epoch,\n\t\t\tworkerId: (bigIntId >> 17n) & MaximumWorkerId,\n\t\t\tprocessId: (bigIntId >> 12n) & MaximumProcessId,\n\t\t\tincrement: bigIntId & MaximumIncrement,\n\t\t\tepoch\n\t\t};\n\t}\n\n\t/**\n\t * Retrieves the timestamp field's value from a snowflake.\n\t * @param id The snowflake to get the timestamp value from.\n\t * @returns The UNIX timestamp that is stored in `id`.\n\t */\n\tpublic timestampFrom(id: string | bigint): number {\n\t\treturn Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];\n\t}\n\n\t/**\n\t * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n\t * snowflake in sort order.\n\t * @param a The first snowflake to compare.\n\t * @param b The second snowflake to compare.\n\t * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n\t * @example Sort snowflakes in ascending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n\t * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n\t * ```\n\t * @example Sort snowflakes in descending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n\t * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n\t * ```\n\t */\n\tpublic static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n\t\tconst typeA = typeof a;\n\t\treturn typeA === typeof b\n\t\t\t? typeA === 'string'\n\t\t\t\t? cmpString(a as string, b as string)\n\t\t\t\t: cmpBigInt(a as bigint, b as bigint)\n\t\t\t: cmpBigInt(BigInt(a), BigInt(b));\n\t}\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n\treturn a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use, will be truncated to 5 bits (0-31)\n\t * @default 0n\n\t */\n\tworkerId?: bigint;\n\n\t/**\n\t * The process ID to use, will be truncated to 5 bits (0-31)\n\t * @default 1n\n\t */\n\tprocessId?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerId: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessId: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport const DiscordSnowflake = new Snowflake(1420070400000n);\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Twitter's snowflake epoch\n *\n * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}\n */\nexport const TwitterSnowflake = new Snowflake(1288834974657n);\n"]}
package/package.json CHANGED
@@ -1,29 +1,37 @@
1
1
  {
2
2
  "name": "@sapphire/snowflake",
3
- "version": "4.0.0-pr-589.aa473f9.0",
3
+ "version": "4.0.0-pr-935.7da5c8bb",
4
4
  "description": "Deconstructs and generates snowflake IDs using BigInts",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
7
- "main": "dist/index.js",
8
- "module": "dist/index.mjs",
9
- "browser": "dist/index.global.js",
10
- "unpkg": "dist/index.global.js",
11
- "types": "dist/index.d.ts",
7
+ "main": "dist/cjs/index.cjs",
8
+ "module": "dist/esm/index.mjs",
9
+ "browser": "dist/iife/index.global.js",
10
+ "unpkg": "dist/iife/index.global.js",
11
+ "types": "dist/cjs/index.d.cts",
12
12
  "exports": {
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.js",
15
- "types": "./dist/index.d.ts"
13
+ "import": {
14
+ "types": "./dist/esm/index.d.mts",
15
+ "default": "./dist/esm/index.mjs"
16
+ },
17
+ "require": {
18
+ "types": "./dist/cjs/index.d.cts",
19
+ "default": "./dist/cjs/index.cjs"
20
+ },
21
+ "browser": "./dist/iife/index.global.js"
16
22
  },
17
23
  "sideEffects": false,
18
24
  "homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/snowflake",
19
25
  "scripts": {
20
26
  "test": "vitest run",
21
27
  "lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
22
- "build": "tsup",
28
+ "build": "yarn gen-index && tsup && yarn build:rename-cjs-index",
29
+ "build:rename-cjs-index": "tsx ../../scripts/rename-cjs-index.cts",
23
30
  "docs": "typedoc-json-parser",
24
31
  "prepack": "yarn build",
25
32
  "bump": "cliff-jumper",
26
- "check-update": "cliff-jumper --dry-run"
33
+ "check-update": "cliff-jumper --dry-run",
34
+ "gen-index": "tsx ../../scripts/gen-index.cts snowflake --write"
27
35
  },
28
36
  "repository": {
29
37
  "type": "git",
@@ -31,9 +39,7 @@
31
39
  "directory": "packages/snowflake"
32
40
  },
33
41
  "files": [
34
- "dist/**/*.js*",
35
- "dist/**/*.mjs*",
36
- "dist/**/*.d*"
42
+ "dist/"
37
43
  ],
38
44
  "engines": {
39
45
  "node": ">=v14.0.0",
@@ -56,12 +62,13 @@
56
62
  "access": "public"
57
63
  },
58
64
  "devDependencies": {
59
- "@favware/cliff-jumper": "^2.0.0",
60
- "@vitest/coverage-c8": "^0.30.1",
61
- "tsup": "^6.7.0",
62
- "typedoc": "^0.24.6",
63
- "typedoc-json-parser": "^7.3.2",
64
- "typescript": "^5.0.4",
65
- "vitest": "^0.30.1"
65
+ "@favware/cliff-jumper": "^6.0.0",
66
+ "@vitest/coverage-v8": "^3.2.4",
67
+ "tsup": "^8.5.0",
68
+ "tsx": "^4.20.6",
69
+ "typedoc": "^0.26.11",
70
+ "typedoc-json-parser": "^10.2.0",
71
+ "typescript": "~5.4.5",
72
+ "vitest": "^3.2.4"
66
73
  }
67
74
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/lib/Snowflake.ts","../src/lib/DiscordSnowflake.ts","../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAM,YAAY;AAClB,IAAM,WAAW;AADjB;AAgBO,IAAM,YAAN,MAAgB;AAAA;AAAA;AAAA;AAAA,EAsBf,YAAY,OAA+B;AAjBlD;AAAA;AAAA;AAAA;AAAA,wBAAO,UAAS,KAAK;AAMrB;AAAA;AAAA;AAAA;AAAA,mCAAa;AAMb;AAAA;AAAA;AAAA;AAAA;AAMC,uBAAK,QAAS,OAAO,iBAAiB,OAAO,MAAM,QAAQ,IAAI,KAAK;AAAA,EACrE;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,QAAgB;AAC1B,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcO,SAAS,EAAE,WAAW,YAAY,KAAK,IAAI,GAAG,WAAW,UAAU,YAAY,UAAU,IAA8B,CAAC,GAAG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,UAAU,GAAW,GAAW;AACxC,SAAO,MAAM,IAAI,IAAI,IAAI,IAAI,KAAK;AACnC;AAFS;AAKT,SAAS,UAAU,GAAW,GAAW;AACxC,SAAO,MAAM,IAAI,IAAI,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AACxF;AAFS;;;ACzIF,IAAM,mBAAmB,IAAI,UAAU,cAAc;;;ACArD,IAAM,mBAAmB,IAAI,UAAU,cAAc","sourcesContent":["const ProcessId = 1n;\nconst WorkerId = 0n;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n */\nexport class Snowflake {\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * The epoch for this snowflake.\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this.#epoch;\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when `increment` is not provided it defaults to the private `increment` of the instance\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId }: SnowflakeGenerateOptions = {}) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\telse if (typeof timestamp === 'number') timestamp = BigInt(timestamp);\n\t\telse if (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(`\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`);\n\t\t}\n\n\t\tif (typeof increment === 'bigint' && increment >= 4095n) increment = 0n;\n\t\telse {\n\t\t\tincrement = this.#increment++;\n\t\t\tif (this.#increment >= 4095n) this.#increment = 0n;\n\t\t}\n\n\t\t// timestamp, workerId, processId, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | ((workerId & 0b11111n) << 17n) | ((processId & 0b11111n) << 12n) | increment;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerId: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessId: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n\n\t/**\n\t * Retrieves the timestamp field's value from a snowflake.\n\t * @param id The snowflake to get the timestamp value from.\n\t * @returns The UNIX timestamp that is stored in `id`.\n\t */\n\tpublic timestampFrom(id: string | bigint): number {\n\t\treturn Number((BigInt(id) >> 22n) + this.#epoch);\n\t}\n\n\t/**\n\t * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n\t * snowflake in sort order.\n\t * @param a The first snowflake to compare.\n\t * @param b The second snowflake to compare.\n\t * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n\t * @example Sort snowflakes in ascending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n\t * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n\t * ```\n\t * @example Sort snowflakes in descending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n\t * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n\t * ```\n\t */\n\tpublic static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n\t\tconst typeA = typeof a;\n\t\treturn typeA === typeof b\n\t\t\t? typeA === 'string'\n\t\t\t\t? cmpString(a as string, b as string)\n\t\t\t\t: cmpBigInt(a as bigint, b as bigint)\n\t\t\t: cmpBigInt(BigInt(a), BigInt(b));\n\t}\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n\treturn a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use, will be truncated to 5 bits (0-31)\n\t * @default 0n\n\t */\n\tworkerId?: bigint;\n\n\t/**\n\t * The process ID to use, will be truncated to 5 bits (0-31)\n\t * @default 1n\n\t */\n\tprocessId?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerId: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessId: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport const DiscordSnowflake = new Snowflake(1420070400000n);\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Twitter's snowflake epoch\n *\n * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}\n */\nexport const TwitterSnowflake = new Snowflake(1288834974657n);\n"]}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/lib/Snowflake.ts","../src/lib/DiscordSnowflake.ts","../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAM,YAAY;AAClB,IAAM,WAAW;AADjB;AAgBO,IAAM,YAAN,MAAgB;AAAA;AAAA;AAAA;AAAA,EAsBf,YAAY,OAA+B;AAjBlD;AAAA;AAAA;AAAA;AAAA,wBAAO,UAAS,KAAK;AAMrB;AAAA;AAAA;AAAA;A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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,OAAc,QAAQ,GAAoB,GAAgC;AACzE,UAAM,QAAQ,OAAO;AACrB,WAAO,UAAU,OAAO,IACrB,UAAU,WACT,UAAU,GAAa,CAAW,IAClC,UAAU,GAAa,CAAW,IACnC,UAAU,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC;AAAA,EAClC;AACD;AAxHa;AAWZ;AAMA;AA0GD,SAAS,UAAU,GAAW,GAAW;AACxC,SAAO,MAAM,IAAI,IAAI,IAAI,IAAI,KAAK;AACnC;AAFS;AAKT,SAAS,UAAU,GAAW,GAAW;AACxC,SAAO,MAAM,IAAI,IAAI,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AACxF;AAFS;;;ACzIF,IAAM,mBAAmB,IAAI,UAAU,cAAc;;;ACArD,IAAM,mBAAmB,IAAI,UAAU,cAAc","sourcesContent":["const ProcessId = 1n;\nconst WorkerId = 0n;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n */\nexport class Snowflake {\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * The epoch for this snowflake.\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this.#epoch;\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when `increment` is not provided it defaults to the private `increment` of the instance\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId }: SnowflakeGenerateOptions = {}) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\telse if (typeof timestamp === 'number') timestamp = BigInt(timestamp);\n\t\telse if (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(`\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`);\n\t\t}\n\n\t\tif (typeof increment === 'bigint' && increment >= 4095n) increment = 0n;\n\t\telse {\n\t\t\tincrement = this.#increment++;\n\t\t\tif (this.#increment >= 4095n) this.#increment = 0n;\n\t\t}\n\n\t\t// timestamp, workerId, processId, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | ((workerId & 0b11111n) << 17n) | ((processId & 0b11111n) << 12n) | increment;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerId: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessId: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n\n\t/**\n\t * Retrieves the timestamp field's value from a snowflake.\n\t * @param id The snowflake to get the timestamp value from.\n\t * @returns The UNIX timestamp that is stored in `id`.\n\t */\n\tpublic timestampFrom(id: string | bigint): number {\n\t\treturn Number((BigInt(id) >> 22n) + this.#epoch);\n\t}\n\n\t/**\n\t * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n\t * snowflake in sort order.\n\t * @param a The first snowflake to compare.\n\t * @param b The second snowflake to compare.\n\t * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n\t * @example Sort snowflakes in ascending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n\t * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n\t * ```\n\t * @example Sort snowflakes in descending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n\t * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n\t * ```\n\t */\n\tpublic static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n\t\tconst typeA = typeof a;\n\t\treturn typeA === typeof b\n\t\t\t? typeA === 'string'\n\t\t\t\t? cmpString(a as string, b as string)\n\t\t\t\t: cmpBigInt(a as bigint, b as bigint)\n\t\t\t: cmpBigInt(BigInt(a), BigInt(b));\n\t}\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n\treturn a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use, will be truncated to 5 bits (0-31)\n\t * @default 0n\n\t */\n\tworkerId?: bigint;\n\n\t/**\n\t * The process ID to use, will be truncated to 5 bits (0-31)\n\t * @default 1n\n\t */\n\tprocessId?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerId: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessId: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport const DiscordSnowflake = new Snowflake(1420070400000n);\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Twitter's snowflake epoch\n *\n * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}\n */\nexport const TwitterSnowflake = new Snowflake(1288834974657n);\n"]}
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M,WAAW,OAAO,EAAE;AAC1B,WAAO;AAAA,MACN,IAAI;AAAA,MACJ,YAAY,YAAY,OAAO,mBAAK;AAAA,MACpC,UAAW,YAAY,MAAO;AAAA,MAC9B,WAAY,YAAY,MAAO;AAAA,MAC/B,WAAW,WAAW;AAAA,MACtB,OAAO,mBAAK;AAAA,IACb;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,cAAc,IAA6B;AACjD,WAAO,QAAQ,OAAO,EAAE,KAAK,OAAO,mBAAK,OAAM;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,OAAc,QAAQ,GAAoB,GAAgC;AACzE,UAAM,QAAQ,OAAO;AACrB,WAAO,UAAU,OAAO,IACrB,UAAU,WACT,UAAU,GAAa,CAAW,IAClC,UAAU,GAAa,CAAW,IACnC,UAAU,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC;AAAA,EAClC;AACD;AAxHa;AAWZ;AAMA;AA0GD,SAAS,UAAU,GAAW,GAAW;AACxC,SAAO,MAAM,IAAI,IAAI,IAAI,IAAI,KAAK;AACnC;AAFS;AAKT,SAAS,UAAU,GAAW,GAAW;AACxC,SAAO,MAAM,IAAI,IAAI,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AACxF;AAFS;;;ACzIF,IAAM,mBAAmB,IAAI,UAAU,cAAc;;;ACArD,IAAM,mBAAmB,IAAI,UAAU,cAAc","sourcesContent":["const ProcessId = 1n;\nconst WorkerId = 0n;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n */\nexport class Snowflake {\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * The epoch for this snowflake.\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this.#epoch;\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when `increment` is not provided it defaults to the private `increment` of the instance\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId }: SnowflakeGenerateOptions = {}) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\telse if (typeof timestamp === 'number') timestamp = BigInt(timestamp);\n\t\telse if (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(`\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`);\n\t\t}\n\n\t\tif (typeof increment === 'bigint' && increment >= 4095n) increment = 0n;\n\t\telse {\n\t\t\tincrement = this.#increment++;\n\t\t\tif (this.#increment >= 4095n) this.#increment = 0n;\n\t\t}\n\n\t\t// timestamp, workerId, processId, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | ((workerId & 0b11111n) << 17n) | ((processId & 0b11111n) << 12n) | increment;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```typescript\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerId: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessId: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n\n\t/**\n\t * Retrieves the timestamp field's value from a snowflake.\n\t * @param id The snowflake to get the timestamp value from.\n\t * @returns The UNIX timestamp that is stored in `id`.\n\t */\n\tpublic timestampFrom(id: string | bigint): number {\n\t\treturn Number((BigInt(id) >> 22n) + this.#epoch);\n\t}\n\n\t/**\n\t * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n\t * snowflake in sort order.\n\t * @param a The first snowflake to compare.\n\t * @param b The second snowflake to compare.\n\t * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n\t * @example Sort snowflakes in ascending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n\t * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n\t * ```\n\t * @example Sort snowflakes in descending order\n\t * ```typescript\n\t * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n\t * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n\t * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n\t * ```\n\t */\n\tpublic static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n\t\tconst typeA = typeof a;\n\t\treturn typeA === typeof b\n\t\t\t? typeA === 'string'\n\t\t\t\t? cmpString(a as string, b as string)\n\t\t\t\t: cmpBigInt(a as bigint, b as bigint)\n\t\t\t: cmpBigInt(BigInt(a), BigInt(b));\n\t}\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n\treturn a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use, will be truncated to 5 bits (0-31)\n\t * @default 0n\n\t */\n\tworkerId?: bigint;\n\n\t/**\n\t * The process ID to use, will be truncated to 5 bits (0-31)\n\t * @default 1n\n\t */\n\tprocessId?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerId: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessId: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport const DiscordSnowflake = new Snowflake(1420070400000n);\n","import { Snowflake } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Twitter's snowflake epoch\n *\n * Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}\n */\nexport const TwitterSnowflake = new Snowflake(1288834974657n);\n"]}