@sapphire/snowflake 4.0.0-pr-601.2f2c308a.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 +34 -0
- package/README.md +1 -2
- package/dist/{index.js → cjs/index.cjs} +30 -20
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{index.d.ts → cjs/index.d.cts} +12 -2
- package/dist/esm/index.d.mts +212 -0
- package/dist/{index.mjs → esm/index.mjs} +29 -19
- package/dist/esm/index.mjs.map +1 -0
- package/dist/{index.global.js → iife/index.global.js} +29 -19
- package/dist/iife/index.global.js.map +1 -0
- package/package.json +28 -21
- package/dist/index.global.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,40 @@
|
|
|
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
|
+
|
|
5
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)
|
|
6
40
|
|
|
7
41
|
## 🏠 Refactor
|
package/README.md
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
**Deconstruct and generate snowflake IDs using BigInts.**
|
|
8
8
|
|
|
9
9
|
[](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
|
|
10
|
-
[](https://codecov.io/gh/sapphiredev/utilities)
|
|
11
10
|
[](https://bundlephobia.com/result?p=@sapphire/snowflake)
|
|
12
11
|
[](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
|
|
30
|
+
- [Contributors](#contributors)
|
|
32
31
|
|
|
33
32
|
## Description
|
|
34
33
|
|
|
@@ -3,21 +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
|
-
};
|
|
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
|
|
20
|
-
var
|
|
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 {
|
|
21
21
|
/**
|
|
22
22
|
* @param epoch the epoch to use
|
|
23
23
|
*/
|
|
@@ -31,30 +31,42 @@ var Snowflake = class {
|
|
|
31
31
|
* Internal reference of the epoch passed in the constructor
|
|
32
32
|
* @internal
|
|
33
33
|
*/
|
|
34
|
-
__publicField(this,
|
|
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,
|
|
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,
|
|
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,
|
|
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 {
|
|
|
99
111
|
workerId = this[WorkerIdSymbol],
|
|
100
112
|
processId = this[ProcessIdSymbol]
|
|
101
113
|
} = {}) {
|
|
102
|
-
if (timestamp instanceof Date)
|
|
103
|
-
|
|
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 {
|
|
|
140
150
|
* @returns The UNIX timestamp that is stored in `id`.
|
|
141
151
|
*/
|
|
142
152
|
timestampFrom(id) {
|
|
143
|
-
return Number(
|
|
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,8 +176,8 @@ var Snowflake = class {
|
|
|
166
176
|
return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
|
|
167
177
|
}
|
|
168
178
|
};
|
|
169
|
-
__name(
|
|
170
|
-
|
|
179
|
+
__name(_Snowflake, "Snowflake");
|
|
180
|
+
var Snowflake = _Snowflake;
|
|
171
181
|
function cmpBigInt(a, b) {
|
|
172
182
|
return a === b ? 0 : a < b ? -1 : 1;
|
|
173
183
|
}
|
|
@@ -189,5 +199,5 @@ exports.MaximumProcessId = MaximumProcessId;
|
|
|
189
199
|
exports.MaximumWorkerId = MaximumWorkerId;
|
|
190
200
|
exports.Snowflake = Snowflake;
|
|
191
201
|
exports.TwitterSnowflake = TwitterSnowflake;
|
|
192
|
-
//# sourceMappingURL=
|
|
193
|
-
//# sourceMappingURL=index.
|
|
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,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,WAAA,EAMA,EAAA,GAAA,iBAAA,EAMT,sBAMA,EAAA,GAAA,eAAA,EAMA,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A,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,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
|
*/
|
|
@@ -199,4 +209,4 @@ declare const DiscordSnowflake: Snowflake;
|
|
|
199
209
|
*/
|
|
200
210
|
declare const TwitterSnowflake: Snowflake;
|
|
201
211
|
|
|
202
|
-
export { DeconstructedSnowflake, DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, 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,21 +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
|
-
};
|
|
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
|
|
18
|
-
var
|
|
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 {
|
|
19
19
|
/**
|
|
20
20
|
* @param epoch the epoch to use
|
|
21
21
|
*/
|
|
@@ -29,30 +29,42 @@ var Snowflake = class {
|
|
|
29
29
|
* Internal reference of the epoch passed in the constructor
|
|
30
30
|
* @internal
|
|
31
31
|
*/
|
|
32
|
-
__publicField(this,
|
|
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,
|
|
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,
|
|
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,
|
|
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 {
|
|
|
97
109
|
workerId = this[WorkerIdSymbol],
|
|
98
110
|
processId = this[ProcessIdSymbol]
|
|
99
111
|
} = {}) {
|
|
100
|
-
if (timestamp instanceof Date)
|
|
101
|
-
|
|
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 {
|
|
|
138
148
|
* @returns The UNIX timestamp that is stored in `id`.
|
|
139
149
|
*/
|
|
140
150
|
timestampFrom(id) {
|
|
141
|
-
return Number(
|
|
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,8 +174,8 @@ var Snowflake = class {
|
|
|
164
174
|
return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
|
|
165
175
|
}
|
|
166
176
|
};
|
|
167
|
-
__name(
|
|
168
|
-
|
|
177
|
+
__name(_Snowflake, "Snowflake");
|
|
178
|
+
var Snowflake = _Snowflake;
|
|
169
179
|
function cmpBigInt(a, b) {
|
|
170
180
|
return a === b ? 0 : a < b ? -1 : 1;
|
|
171
181
|
}
|
|
@@ -182,5 +192,5 @@ var DiscordSnowflake = new Snowflake(1420070400000n);
|
|
|
182
192
|
var TwitterSnowflake = new Snowflake(1288834974657n);
|
|
183
193
|
|
|
184
194
|
export { DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, Snowflake, TwitterSnowflake };
|
|
185
|
-
//# sourceMappingURL=
|
|
195
|
+
//# sourceMappingURL=index.mjs.map
|
|
186
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,GA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,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.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,21 +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
|
-
};
|
|
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
|
|
21
|
-
var
|
|
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 {
|
|
22
22
|
/**
|
|
23
23
|
* @param epoch the epoch to use
|
|
24
24
|
*/
|
|
@@ -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,
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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(
|
|
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,8 +177,8 @@ 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
|
-
__name(
|
|
171
|
-
|
|
180
|
+
__name(_Snowflake, "Snowflake");
|
|
181
|
+
var Snowflake = _Snowflake;
|
|
172
182
|
function cmpBigInt(a, b) {
|
|
173
183
|
return a === b ? 0 : a < b ? -1 : 1;
|
|
174
184
|
}
|
|
@@ -194,5 +204,5 @@ var SapphireSnowflake = (function (exports) {
|
|
|
194
204
|
return exports;
|
|
195
205
|
|
|
196
206
|
})({});
|
|
197
|
-
//# sourceMappingURL=
|
|
207
|
+
//# sourceMappingURL=index.global.js.map
|
|
198
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,EAwCf,YAAY,KAAA,EAA+B;EAnClD;EAAA;EAAA;EAAA;EAAA,IAAA,aAAA,CAAA,IAAA,EAAO,UAAS,IAAA,CAAK,WAAA,CAAA;EAMrB;EAAA;EAAA;EAAA;EAAA,IAAA,aAAA,CAAA,IAAA,EAAkB,EAAA,CAAA;EAMlB;EAAA;EAAA;EAAA;EAAA,IAAA,aAAA,CAAA,IAAA,EAAkB,EAAA,CAAA;EAMlB;EAAA;EAAA;EAAA;EAAA,IAAA,aAAA,CAAA,IAAA,EAAS,EAAA,EAAmB,EAAA,CAAA;EAM5B;EAAA;EAAA;EAAA;EAAA,IAAA,aAAA,CAAA,IAAA,EAAS,EAAA,EAA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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-
|
|
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.
|
|
8
|
-
"module": "dist/index.mjs",
|
|
9
|
-
"browser": "dist/index.global.js",
|
|
10
|
-
"unpkg": "dist/index.global.js",
|
|
11
|
-
"types": "dist/index.d.
|
|
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":
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
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": "^
|
|
60
|
-
"@vitest/coverage-
|
|
61
|
-
"tsup": "^
|
|
62
|
-
"
|
|
63
|
-
"typedoc
|
|
64
|
-
"
|
|
65
|
-
"
|
|
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
|
}
|
package/dist/index.global.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,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,YAAN,MAAgB;AAAA;AAAA;AAAA;AAAA,EAkCf,YAAY,OAA+B;AA7BlD;AAAA;AAAA;AAAA;AAAA,wBAAO,UAAS,KAAK;AAMrB;AAAA;AAAA;AAAA;AAAA,wBAAkB;AAMlB;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAmB;AAM5B;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAmB;AAM5B;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAkB;AAM1B,SAAK,WAAW,IAAI,OAAO,iBAAiB,OAAO,MAAM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AAO,EAAE;AAC1B,UAAM,QAAQ,KAAK,WAAW;AAC9B,WAAO;AAAA,MACN,IAAI;AAAA,MACJ,YAAY,YAAY,OAAO;AAAA,MAC/B,UAAW,YAAY,MAAO;AAAA,MAC9B,WAAY,YAAY,MAAO;AAAA,MAC/B,WAAW,WAAW;AAAA,MACtB;AAAA,IACD;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,cAAc,IAA6B;AACjD,WAAO,QAAQ,OAAO,EAAE,KAAK,OAAO,KAAK,WAAW,CAAC;AAAA,EACtD;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;AA5Ka;AAWM,kBAMT,sBAMA,sBAMA;AAkJV,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;;;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"]}
|
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,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,YAAN,MAAgB;AAAA;AAAA;AAAA;AAAA,EAkCf,YAAY,OAA+B;AA7BlD;AAAA;AAAA;AAAA;AAAA,wBAAO,UAAS,KAAK;AAMrB;AAAA;AAAA;AAAA;AAAA,wBAAkB;AAMlB;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAmB;AAM5B;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAmB;AAM5B;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAkB;AAM1B,SAAK,WAAW,IAAI,OAAO,iBAAiB,OAAO,MAAM,QAAQ,IAAI,KAAK;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,QAAgB;AAC1B,WAAO,KAAK,WAAW;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,YAAoB;AAC9B,WAAO,KAAK,eAAe;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA,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AAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,cAAc,IAA6B;AACjD,WAAO,QAAQ,OAAO,EAAE,KAAK,OAAO,KAAK,WAAW,CAAC;AAAA,EACtD;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;AA5Ka;AAWM,kBAMT,sBAMA,sBAMA;AAkJV,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;;;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"]}
|
package/dist/index.mjs.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,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,YAAN,MAAgB;AAAA;AAAA;AAAA;AAAA,EAkCf,YAAY,OAA+B;AA7BlD;AAAA;AAAA;AAAA;AAAA,wBAAO,UAAS,KAAK;AAMrB;AAAA;AAAA;AAAA;AAAA,wBAAkB;AAMlB;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAmB;AAM5B;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAmB;AAM5B;AAAA;AAAA;AAAA;AAAA,wBAAS,IAAkB;AAM1B,SAAK,WAAW,IAAI,OAAO,iBAAiB,OAAO,MAAM,QAAQ,IAAI,KAAK;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,QAAgB;AAC1B,WAAO,KAAK,WAAW;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,YAAoB;AAC9B,WAAO,KAAK,eAAe;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,UAAU,OAAwB;AAC5C,SAAK,eAAe,IAAI,OAAO,KAAK,IAAI;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,WAAmB;AAC7B,WAAO,KAAK,cAAc;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,SAAS,OAAwB;AAC3C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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;AA5Ka;AAWM,kBAMT,sBAMA,sBAMA;AAkJV,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;;;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"]}
|