@sapphire/snowflake 3.5.4-next.9ef6dfd8.0 → 3.5.4-next.a133f01a

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/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
 
@@ -3,20 +3,20 @@
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
- };
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
10
7
 
11
8
  // src/lib/Snowflake.ts
12
9
  var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
13
10
  var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
11
+ var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
14
12
  var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
15
13
  var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
16
14
  var MaximumWorkerId = 0b11111n;
17
15
  var MaximumProcessId = 0b11111n;
18
16
  var MaximumIncrement = 0b111111111111n;
19
- var _a, _b, _c, _d;
17
+ var TimestampFieldDivisor = 2 ** 22;
18
+ var _a, _b, _c, _d, _e;
19
+ _e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
20
20
  var _Snowflake = class _Snowflake {
21
21
  /**
22
22
  * @param epoch the epoch to use
@@ -31,30 +31,42 @@ var _Snowflake = class _Snowflake {
31
31
  * Internal reference of the epoch passed in the constructor
32
32
  * @internal
33
33
  */
34
- __publicField(this, _a);
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);
35
40
  /**
36
41
  * Internal incrementor for generating snowflakes
37
42
  * @internal
38
43
  */
39
- __publicField(this, _b, 0n);
44
+ __publicField(this, _c, 0n);
40
45
  /**
41
46
  * The process ID that will be used by default in the generate method
42
47
  * @internal
43
48
  */
44
- __publicField(this, _c, 1n);
49
+ __publicField(this, _b, 1n);
45
50
  /**
46
51
  * The worker ID that will be used by default in the generate method
47
52
  * @internal
48
53
  */
49
- __publicField(this, _d, 0n);
54
+ __publicField(this, _a, 0n);
50
55
  this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
56
+ this[EpochNumberSymbol] = Number(this[EpochSymbol]);
51
57
  }
52
58
  /**
53
- * The epoch for this snowflake
59
+ * The epoch for this snowflake, as a bigint
54
60
  */
55
61
  get epoch() {
56
62
  return this[EpochSymbol];
57
63
  }
64
+ /**
65
+ * The epoch for this snowflake, as a number
66
+ */
67
+ get epochNumber() {
68
+ return this[EpochNumberSymbol];
69
+ }
58
70
  /**
59
71
  * Gets the configured process ID
60
72
  */
@@ -99,10 +111,8 @@ var _Snowflake = class _Snowflake {
99
111
  workerId = this[WorkerIdSymbol],
100
112
  processId = this[ProcessIdSymbol]
101
113
  } = {}) {
102
- if (timestamp instanceof Date)
103
- timestamp = BigInt(timestamp.getTime());
104
- else if (typeof timestamp === "number")
105
- timestamp = BigInt(timestamp);
114
+ if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
115
+ else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
106
116
  else if (typeof timestamp !== "bigint") {
107
117
  throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
108
118
  }
@@ -140,7 +150,7 @@ var _Snowflake = class _Snowflake {
140
150
  * @returns The UNIX timestamp that is stored in `id`.
141
151
  */
142
152
  timestampFrom(id) {
143
- return Number((BigInt(id) >> 22n) + this[EpochSymbol]);
153
+ return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
144
154
  }
145
155
  /**
146
156
  * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
@@ -166,7 +176,6 @@ var _Snowflake = class _Snowflake {
166
176
  return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
167
177
  }
168
178
  };
169
- _a = EpochSymbol, _b = IncrementSymbol, _c = ProcessIdSymbol, _d = WorkerIdSymbol;
170
179
  __name(_Snowflake, "Snowflake");
171
180
  var Snowflake = _Snowflake;
172
181
  function cmpBigInt(a, b) {
@@ -190,5 +199,5 @@ exports.MaximumProcessId = MaximumProcessId;
190
199
  exports.MaximumWorkerId = MaximumWorkerId;
191
200
  exports.Snowflake = Snowflake;
192
201
  exports.TwitterSnowflake = TwitterSnowflake;
193
- //# sourceMappingURL=out.js.map
202
+ //# sourceMappingURL=index.cjs.map
194
203
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;;AAAA,IAAM,kBAAkB,OAAO,+BAA+B;AAC9D,IAAM,cAAc,OAAO,2BAA2B;AACtD,IAAM,kBAAkB,OAAO,+BAA+B;AAC9D,IAAM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xC,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;;;AC9MF,IAAM,mBAAmB,IAAI,UAAU,cAAc;;;ACArD,IAAM,mBAAmB,IAAI,UAAU,cAAc","sourcesContent":["const IncrementSymbol = Symbol('@sapphire/snowflake.increment');\nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\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\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 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}\n\n\t/**\n\t * The epoch for this snowflake\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this[EpochSymbol];\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 Number((BigInt(id) >> 22n) + this[EpochSymbol]);\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
+ {"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,CAAA;AAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,CAAA,CAAA;AACtD,IAAM,iBAAA,GAAoB,OAAO,kCAAkC,CAAA,CAAA;AACnE,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA,CAAA;AAC9D,IAAM,cAAA,GAAiB,OAAO,8BAA8B,CAAA,CAAA;AAKrD,IAAM,eAAkB,GAAA,SAAA;AAKxB,IAAM,gBAAmB,GAAA,SAAA;AAKzB,IAAM,gBAAmB,GAAA,gB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A;AACrB,IAAA,OAAO,UAAU,OAAO,CAAA,GACrB,UAAU,QACT,GAAA,SAAA,CAAU,GAAa,CAAW,CAAA,GAClC,UAAU,CAAa,EAAA,CAAW,IACnC,SAAU,CAAA,MAAA,CAAO,CAAC,CAAG,EAAA,MAAA,CAAO,CAAC,CAAC,CAAA,CAAA;AAAA,GAClC;AACD,CAAA,CAAA;AA1LuB,MAAA,CAAA,UAAA,EAAA,WAAA,CAAA,CAAA;AAAhB,IAAM,SAAN,GAAA,WAAA;AA6LP,SAAS,SAAA,CAAU,GAAW,CAAW,EAAA;AACxC,EAAA,OAAO,CAAM,KAAA,CAAA,GAAI,CAAI,GAAA,CAAA,GAAI,IAAI,CAAK,CAAA,GAAA,CAAA,CAAA;AACnC,CAAA;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;AAKT,SAAS,SAAA,CAAU,GAAW,CAAW,EAAA;AACxC,EAAA,OAAO,CAAM,KAAA,CAAA,GAAI,CAAI,GAAA,CAAA,CAAE,SAAS,CAAE,CAAA,MAAA,GAAS,CAAK,CAAA,GAAA,CAAA,CAAE,SAAS,CAAE,CAAA,MAAA,GAAS,CAAI,GAAA,CAAA,GAAI,IAAI,CAAK,CAAA,GAAA,CAAA,CAAA;AACxF,CAAA;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;;;AC/NI,IAAA,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc,EAAA;;;ACA/C,IAAA,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,5 +1,6 @@
1
1
  declare const IncrementSymbol: unique symbol;
2
2
  declare const EpochSymbol: unique symbol;
3
+ declare const EpochNumberSymbol: unique symbol;
3
4
  declare const ProcessIdSymbol: unique symbol;
4
5
  declare const WorkerIdSymbol: unique symbol;
5
6
  /**
@@ -37,6 +38,11 @@ declare class Snowflake {
37
38
  * @internal
38
39
  */
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];
40
46
  /**
41
47
  * Internal incrementor for generating snowflakes
42
48
  * @internal
@@ -57,9 +63,13 @@ declare class Snowflake {
57
63
  */
58
64
  constructor(epoch: number | bigint | Date);
59
65
  /**
60
- * The epoch for this snowflake
66
+ * The epoch for this snowflake, as a bigint
61
67
  */
62
68
  get epoch(): bigint;
69
+ /**
70
+ * The epoch for this snowflake, as a number
71
+ */
72
+ get epochNumber(): number;
63
73
  /**
64
74
  * Gets the configured process ID
65
75
  */
@@ -1,5 +1,6 @@
1
1
  declare const IncrementSymbol: unique symbol;
2
2
  declare const EpochSymbol: unique symbol;
3
+ declare const EpochNumberSymbol: unique symbol;
3
4
  declare const ProcessIdSymbol: unique symbol;
4
5
  declare const WorkerIdSymbol: unique symbol;
5
6
  /**
@@ -37,6 +38,11 @@ declare class Snowflake {
37
38
  * @internal
38
39
  */
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];
40
46
  /**
41
47
  * Internal incrementor for generating snowflakes
42
48
  * @internal
@@ -57,9 +63,13 @@ declare class Snowflake {
57
63
  */
58
64
  constructor(epoch: number | bigint | Date);
59
65
  /**
60
- * The epoch for this snowflake
66
+ * The epoch for this snowflake, as a bigint
61
67
  */
62
68
  get epoch(): bigint;
69
+ /**
70
+ * The epoch for this snowflake, as a number
71
+ */
72
+ get epochNumber(): number;
63
73
  /**
64
74
  * Gets the configured process ID
65
75
  */
@@ -1,20 +1,20 @@
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
- };
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
5
 
9
6
  // src/lib/Snowflake.ts
10
7
  var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
11
8
  var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
9
+ var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
12
10
  var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
13
11
  var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
14
12
  var MaximumWorkerId = 0b11111n;
15
13
  var MaximumProcessId = 0b11111n;
16
14
  var MaximumIncrement = 0b111111111111n;
17
- var _a, _b, _c, _d;
15
+ var TimestampFieldDivisor = 2 ** 22;
16
+ var _a, _b, _c, _d, _e;
17
+ _e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
18
18
  var _Snowflake = class _Snowflake {
19
19
  /**
20
20
  * @param epoch the epoch to use
@@ -29,30 +29,42 @@ var _Snowflake = class _Snowflake {
29
29
  * Internal reference of the epoch passed in the constructor
30
30
  * @internal
31
31
  */
32
- __publicField(this, _a);
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);
33
38
  /**
34
39
  * Internal incrementor for generating snowflakes
35
40
  * @internal
36
41
  */
37
- __publicField(this, _b, 0n);
42
+ __publicField(this, _c, 0n);
38
43
  /**
39
44
  * The process ID that will be used by default in the generate method
40
45
  * @internal
41
46
  */
42
- __publicField(this, _c, 1n);
47
+ __publicField(this, _b, 1n);
43
48
  /**
44
49
  * The worker ID that will be used by default in the generate method
45
50
  * @internal
46
51
  */
47
- __publicField(this, _d, 0n);
52
+ __publicField(this, _a, 0n);
48
53
  this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
54
+ this[EpochNumberSymbol] = Number(this[EpochSymbol]);
49
55
  }
50
56
  /**
51
- * The epoch for this snowflake
57
+ * The epoch for this snowflake, as a bigint
52
58
  */
53
59
  get epoch() {
54
60
  return this[EpochSymbol];
55
61
  }
62
+ /**
63
+ * The epoch for this snowflake, as a number
64
+ */
65
+ get epochNumber() {
66
+ return this[EpochNumberSymbol];
67
+ }
56
68
  /**
57
69
  * Gets the configured process ID
58
70
  */
@@ -97,10 +109,8 @@ var _Snowflake = class _Snowflake {
97
109
  workerId = this[WorkerIdSymbol],
98
110
  processId = this[ProcessIdSymbol]
99
111
  } = {}) {
100
- if (timestamp instanceof Date)
101
- timestamp = BigInt(timestamp.getTime());
102
- else if (typeof timestamp === "number")
103
- timestamp = BigInt(timestamp);
112
+ if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
113
+ else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
104
114
  else if (typeof timestamp !== "bigint") {
105
115
  throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
106
116
  }
@@ -138,7 +148,7 @@ var _Snowflake = class _Snowflake {
138
148
  * @returns The UNIX timestamp that is stored in `id`.
139
149
  */
140
150
  timestampFrom(id) {
141
- return Number((BigInt(id) >> 22n) + this[EpochSymbol]);
151
+ return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
142
152
  }
143
153
  /**
144
154
  * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
@@ -164,7 +174,6 @@ var _Snowflake = class _Snowflake {
164
174
  return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
165
175
  }
166
176
  };
167
- _a = EpochSymbol, _b = IncrementSymbol, _c = ProcessIdSymbol, _d = WorkerIdSymbol;
168
177
  __name(_Snowflake, "Snowflake");
169
178
  var Snowflake = _Snowflake;
170
179
  function cmpBigInt(a, b) {
@@ -183,5 +192,5 @@ var DiscordSnowflake = new Snowflake(1420070400000n);
183
192
  var TwitterSnowflake = new Snowflake(1288834974657n);
184
193
 
185
194
  export { DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, Snowflake, TwitterSnowflake };
186
- //# sourceMappingURL=out.js.map
195
+ //# sourceMappingURL=index.mjs.map
187
196
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;;AAAA,IAAM,kBAAkB,OAAO,+BAA+B;AAC9D,IAAM,cAAc,OAAO,2BAA2B;AACtD,IAAM,kBAAkB,OAAO,+BAA+B;AAC9D,IAAM,iBAAiB,OAAO,8BAA8B;A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I,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;;;AC9MF,IAAM,mBAAmB,IAAI,UAAU,cAAc;;;ACArD,IAAM,mBAAmB,IAAI,UAAU,cAAc","sourcesContent":["const IncrementSymbol = Symbol('@sapphire/snowflake.increment');\nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\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\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 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}\n\n\t/**\n\t * The epoch for this snowflake\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this[EpochSymbol];\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 Number((BigInt(id) >> 22n) + this[EpochSymbol]);\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
+ {"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,CAAA;AAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,CAAA,CAAA;AACtD,IAAM,iBAAA,GAAoB,OAAO,kCAAkC,CAAA,CAAA;AACnE,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA,CAAA;AAC9D,IAAM,cAAA,GAAiB,OAAO,8BAA8B,CAAA,CAAA;AAKrD,IAAM,eAAkB,GAAA,SAAA;AAKxB,IAAM,gBAAmB,GAAA,SAAA;AAKzB,IAAM,gBAAmB,GAAA,gBAAA;AAEhC,IAAM,wBAAwB,C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AAO,CAAA,GACrB,UAAU,QACT,GAAA,SAAA,CAAU,GAAa,CAAW,CAAA,GAClC,UAAU,CAAa,EAAA,CAAW,IACnC,SAAU,CAAA,MAAA,CAAO,CAAC,CAAG,EAAA,MAAA,CAAO,CAAC,CAAC,CAAA,CAAA;AAAA,GAClC;AACD,CAAA,CAAA;AA1LuB,MAAA,CAAA,UAAA,EAAA,WAAA,CAAA,CAAA;AAAhB,IAAM,SAAN,GAAA,WAAA;AA6LP,SAAS,SAAA,CAAU,GAAW,CAAW,EAAA;AACxC,EAAA,OAAO,CAAM,KAAA,CAAA,GAAI,CAAI,GAAA,CAAA,GAAI,IAAI,CAAK,CAAA,GAAA,CAAA,CAAA;AACnC,CAAA;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;AAKT,SAAS,SAAA,CAAU,GAAW,CAAW,EAAA;AACxC,EAAA,OAAO,CAAM,KAAA,CAAA,GAAI,CAAI,GAAA,CAAA,CAAE,SAAS,CAAE,CAAA,MAAA,GAAS,CAAK,CAAA,GAAA,CAAA,CAAE,SAAS,CAAE,CAAA,MAAA,GAAS,CAAI,GAAA,CAAA,GAAI,IAAI,CAAK,CAAA,GAAA,CAAA,CAAA;AACxF,CAAA;AAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;;;AC/NI,IAAA,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc,EAAA;;;ACA/C,IAAA,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,20 +4,20 @@ 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
- };
7
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
11
8
 
12
9
  // src/lib/Snowflake.ts
13
10
  var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
14
11
  var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
12
+ var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
15
13
  var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
16
14
  var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
17
15
  var MaximumWorkerId = 0b11111n;
18
16
  var MaximumProcessId = 0b11111n;
19
17
  var MaximumIncrement = 0b111111111111n;
20
- var _a, _b, _c, _d;
18
+ var TimestampFieldDivisor = 2 ** 22;
19
+ var _a, _b, _c, _d, _e;
20
+ _e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
21
21
  var _Snowflake = class _Snowflake {
22
22
  /**
23
23
  * @param epoch the epoch to use
@@ -32,30 +32,42 @@ var SapphireSnowflake = (function (exports) {
32
32
  * Internal reference of the epoch passed in the constructor
33
33
  * @internal
34
34
  */
35
- __publicField(this, _a);
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);
36
41
  /**
37
42
  * Internal incrementor for generating snowflakes
38
43
  * @internal
39
44
  */
40
- __publicField(this, _b, 0n);
45
+ __publicField(this, _c, 0n);
41
46
  /**
42
47
  * The process ID that will be used by default in the generate method
43
48
  * @internal
44
49
  */
45
- __publicField(this, _c, 1n);
50
+ __publicField(this, _b, 1n);
46
51
  /**
47
52
  * The worker ID that will be used by default in the generate method
48
53
  * @internal
49
54
  */
50
- __publicField(this, _d, 0n);
55
+ __publicField(this, _a, 0n);
51
56
  this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
57
+ this[EpochNumberSymbol] = Number(this[EpochSymbol]);
52
58
  }
53
59
  /**
54
- * The epoch for this snowflake
60
+ * The epoch for this snowflake, as a bigint
55
61
  */
56
62
  get epoch() {
57
63
  return this[EpochSymbol];
58
64
  }
65
+ /**
66
+ * The epoch for this snowflake, as a number
67
+ */
68
+ get epochNumber() {
69
+ return this[EpochNumberSymbol];
70
+ }
59
71
  /**
60
72
  * Gets the configured process ID
61
73
  */
@@ -100,10 +112,8 @@ var SapphireSnowflake = (function (exports) {
100
112
  workerId = this[WorkerIdSymbol],
101
113
  processId = this[ProcessIdSymbol]
102
114
  } = {}) {
103
- if (timestamp instanceof Date)
104
- timestamp = BigInt(timestamp.getTime());
105
- else if (typeof timestamp === "number")
106
- timestamp = BigInt(timestamp);
115
+ if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
116
+ else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
107
117
  else if (typeof timestamp !== "bigint") {
108
118
  throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
109
119
  }
@@ -141,7 +151,7 @@ var SapphireSnowflake = (function (exports) {
141
151
  * @returns The UNIX timestamp that is stored in `id`.
142
152
  */
143
153
  timestampFrom(id) {
144
- return Number((BigInt(id) >> 22n) + this[EpochSymbol]);
154
+ return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
145
155
  }
146
156
  /**
147
157
  * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
@@ -167,7 +177,6 @@ var SapphireSnowflake = (function (exports) {
167
177
  return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
168
178
  }
169
179
  };
170
- _a = EpochSymbol, _b = IncrementSymbol, _c = ProcessIdSymbol, _d = WorkerIdSymbol;
171
180
  __name(_Snowflake, "Snowflake");
172
181
  var Snowflake = _Snowflake;
173
182
  function cmpBigInt(a, b) {
@@ -195,5 +204,5 @@ var SapphireSnowflake = (function (exports) {
195
204
  return exports;
196
205
 
197
206
  })({});
198
- //# sourceMappingURL=out.js.map
207
+ //# sourceMappingURL=index.global.js.map
199
208
  //# sourceMappingURL=index.global.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;;AAAA,IAAM,kBAAkB,OAAO,+BAA+B;AAC9D,IAAM,cAAc,OAAO,2BAA2B;AACtD,IAAM,kBAAkB,OAAO,+BAA+B;AAC9D,IAAM,iBAAiB,OAAO,8BAA8B;AAKrD,IAAM,kBAAkB;AAKxB,IAAM,mBAAmB;AAKzB,IAAM,mBAAmB;AAlBhC;AAiCO,IAAM,aAAN,MAAM,WAAU;AAAA;AAAA;A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KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AACxF;AAFS;;;AC9MF,IAAM,mBAAmB,IAAI,UAAU,cAAc;;;ACArD,IAAM,mBAAmB,IAAI,UAAU,cAAc","sourcesContent":["const IncrementSymbol = Symbol('@sapphire/snowflake.increment');\nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\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\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 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}\n\n\t/**\n\t * The epoch for this snowflake\n\t */\n\tpublic get epoch(): bigint {\n\t\treturn this[EpochSymbol];\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 Number((BigInt(id) >> 22n) + this[EpochSymbol]);\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
+ {"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,CAAA;EAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,CAAA,CAAA;EACtD,IAAM,iBAAA,GAAoB,OAAO,kCAAkC,CAAA,CAAA;EACnE,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA,CAAA;EAC9D,IAAM,cAAA,GAAiB,OAAO,8BAA8B,CAAA,CAAA;AAKrD,MAAM,eAAkB,GAAA,SAAA;AAKxB,MAAM,gBAAmB,GAAA,SAAA;AAKzB,MAAM,gBAAmB,GAAA,gBAAA;EAEhC,IAAM,wBAAwB,CAAK,IAAA,EAAA,CAAA;EArBnC,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,CAAA;EA+CmB,EAMA,GA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AAA,CAAO,CAAC,CAAG,EAAA,MAAA,CAAO,CAAC,CAAC,CAAA,CAAA;EAAA,GAClC;EACD,CAAA,CAAA;EA1LuB,MAAA,CAAA,UAAA,EAAA,WAAA,CAAA,CAAA;AAAhB,MAAM,SAAN,GAAA,WAAA;EA6LP,SAAS,SAAA,CAAU,GAAW,CAAW,EAAA;EACxC,EAAA,OAAO,CAAM,KAAA,CAAA,GAAI,CAAI,GAAA,CAAA,GAAI,IAAI,CAAK,CAAA,GAAA,CAAA,CAAA;EACnC,CAAA;EAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;EAKT,SAAS,SAAA,CAAU,GAAW,CAAW,EAAA;EACxC,EAAA,OAAO,CAAM,KAAA,CAAA,GAAI,CAAI,GAAA,CAAA,CAAE,SAAS,CAAE,CAAA,MAAA,GAAS,CAAK,CAAA,GAAA,CAAA,CAAE,SAAS,CAAE,CAAA,MAAA,GAAS,CAAI,GAAA,CAAA,GAAI,IAAI,CAAK,CAAA,GAAA,CAAA,CAAA;EACxF,CAAA;EAFS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;;;AC/NI,MAAA,gBAAA,GAAmB,IAAI,SAAA,CAAU,cAAc,EAAA;;;ACA/C,MAAA,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,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/snowflake",
3
- "version": "3.5.4-next.9ef6dfd8.0",
3
+ "version": "3.5.4-next.a133f01a",
4
4
  "description": "Deconstructs and generates snowflake IDs using BigInts",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -25,12 +25,13 @@
25
25
  "scripts": {
26
26
  "test": "vitest run",
27
27
  "lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
28
- "build": "tsup && yarn build:rename-cjs-index",
29
- "build:rename-cjs-index": "tsx --tsconfig ../../scripts/tsconfig.json ../../scripts/rename-cjs-index.cts",
28
+ "build": "yarn gen-index && tsup && yarn build:rename-cjs-index",
29
+ "build:rename-cjs-index": "tsx ../../scripts/rename-cjs-index.cts",
30
30
  "docs": "typedoc-json-parser",
31
31
  "prepack": "yarn build",
32
32
  "bump": "cliff-jumper",
33
- "check-update": "cliff-jumper --dry-run"
33
+ "check-update": "cliff-jumper --dry-run",
34
+ "gen-index": "tsx ../../scripts/gen-index.cts snowflake --write"
34
35
  },
35
36
  "repository": {
36
37
  "type": "git",
@@ -61,13 +62,13 @@
61
62
  "access": "public"
62
63
  },
63
64
  "devDependencies": {
64
- "@favware/cliff-jumper": "^3.0.3",
65
- "@vitest/coverage-v8": "^1.6.0",
66
- "tsup": "^8.0.2",
67
- "tsx": "^4.10.5",
65
+ "@favware/cliff-jumper": "^4.1.0",
66
+ "@vitest/coverage-v8": "^2.1.1",
67
+ "tsup": "^8.2.4",
68
+ "tsx": "^4.19.1",
68
69
  "typedoc": "^0.25.13",
69
70
  "typedoc-json-parser": "^10.0.0",
70
- "typescript": "^5.4.5",
71
- "vitest": "^1.6.0"
71
+ "typescript": "~5.4.5",
72
+ "vitest": "^2.1.1"
72
73
  }
73
74
  }