SupportBot 7.3
This commit is contained in:
1 parent
19a12f6c8f
commit
09f0873f27
4762 files changed
+504608
-52
No files matched your search
+4
@@ -0,0 +1,4 @@
|
||||
export * from './lib/DiscordSnowflake';
|
||||
export * from './lib/Snowflake';
|
||||
export * from './lib/TwitterSnowflake';
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC"}
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
/*! *****************************************************************************
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
***************************************************************************** */
|
||||
|
||||
function __classPrivateFieldGet(receiver, privateMap) {
|
||||
if (!privateMap.has(receiver)) {
|
||||
throw new TypeError("attempted to get private field on non-instance");
|
||||
}
|
||||
return privateMap.get(receiver);
|
||||
}
|
||||
|
||||
function __classPrivateFieldSet(receiver, privateMap, value) {
|
||||
if (!privateMap.has(receiver)) {
|
||||
throw new TypeError("attempted to set private field on non-instance");
|
||||
}
|
||||
privateMap.set(receiver, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
||||
var _Snowflake_increment, _Snowflake_epoch;
|
||||
/**
|
||||
* A class for parsing snowflake ids
|
||||
*/
|
||||
class Snowflake {
|
||||
/**
|
||||
* @param epoch the epoch to use
|
||||
*/
|
||||
constructor(epoch) {
|
||||
/**
|
||||
* Internal incrementor for generating snowflakes
|
||||
* @internal
|
||||
*/
|
||||
_Snowflake_increment.set(this, 0n);
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor
|
||||
* @internal
|
||||
*/
|
||||
_Snowflake_epoch.set(this, void 0);
|
||||
/**
|
||||
* Alias for {@link deconstruct}
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this
|
||||
Object.defineProperty(this, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: this.deconstruct
|
||||
});
|
||||
__classPrivateFieldSet(this, _Snowflake_epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to the private increment of the instance
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
generate({ increment = __classPrivateFieldGet(this, _Snowflake_increment), timestamp = Date.now(), workerID = 1n, processID = 1n } = {
|
||||
increment: __classPrivateFieldGet(this, _Snowflake_increment),
|
||||
timestamp: Date.now(),
|
||||
workerID: 1n,
|
||||
processID: 1n
|
||||
}) {
|
||||
if (timestamp instanceof Date)
|
||||
timestamp = BigInt(timestamp.getTime());
|
||||
if (typeof timestamp === 'number' && !Number.isNaN(timestamp))
|
||||
timestamp = BigInt(timestamp);
|
||||
if (typeof timestamp !== 'bigint') {
|
||||
throw new TypeError(`"timestamp" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`);
|
||||
}
|
||||
if (increment >= 4095n)
|
||||
increment = 0n;
|
||||
// timestamp, workerID, processID, increment
|
||||
return ((timestamp - __classPrivateFieldGet(this, _Snowflake_epoch)) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
deconstruct(id) {
|
||||
const bigIntId = BigInt(id);
|
||||
return {
|
||||
id: bigIntId,
|
||||
timestamp: (bigIntId >> 22n) + __classPrivateFieldGet(this, _Snowflake_epoch),
|
||||
workerID: (bigIntId >> 17n) & 31n,
|
||||
processID: (bigIntId >> 12n) & 31n,
|
||||
increment: bigIntId & 4095n,
|
||||
epoch: __classPrivateFieldGet(this, _Snowflake_epoch)
|
||||
};
|
||||
}
|
||||
}
|
||||
_Snowflake_increment = new WeakMap(), _Snowflake_epoch = new WeakMap();
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Discord's snowflake epoch
|
||||
*
|
||||
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
class DiscordSnowflake extends Snowflake {
|
||||
constructor() {
|
||||
super(DiscordSnowflake.Epoch);
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id) {
|
||||
return new DiscordSnowflake().deconstruct(id);
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options = { timestamp: Date.now() }) {
|
||||
return new DiscordSnowflake().generate(options);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Discord epoch (`2015-01-01T00:00:00.000Z`)
|
||||
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
Object.defineProperty(DiscordSnowflake, "Epoch", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: 1420070400000n
|
||||
});
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Object.defineProperty(DiscordSnowflake, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: DiscordSnowflake.deconstruct
|
||||
});
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Twitter's snowflake epoch
|
||||
*
|
||||
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
|
||||
*/
|
||||
class TwitterSnowflake extends Snowflake {
|
||||
constructor() {
|
||||
super(TwitterSnowflake.Epoch);
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id) {
|
||||
return new TwitterSnowflake().deconstruct(id);
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options = { timestamp: Date.now() }) {
|
||||
return new TwitterSnowflake().generate(options);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
|
||||
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
|
||||
*/
|
||||
Object.defineProperty(TwitterSnowflake, "Epoch", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: 1142974214000n
|
||||
});
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Object.defineProperty(TwitterSnowflake, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: TwitterSnowflake.deconstruct
|
||||
});
|
||||
|
||||
exports.DiscordSnowflake = DiscordSnowflake;
|
||||
exports.Snowflake = Snowflake;
|
||||
exports.TwitterSnowflake = TwitterSnowflake;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sources":["../src/lib/Snowflake.ts","../src/lib/DiscordSnowflake.ts","../src/lib/TwitterSnowflake.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\n\n/**\n * A class for parsing snowflake ids\n */\nexport class Snowflake {\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * 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 * ```ts\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\t{ increment = this.#increment, timestamp = Date.now(), workerID = 1n, processID = 1n }: SnowflakeGenerateOptions = {\n\t\t\tincrement: this.#increment,\n\t\t\ttimestamp: Date.now(),\n\t\t\tworkerID: 1n,\n\t\t\tprocessID: 1n\n\t\t}\n\t) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\tif (typeof timestamp === 'number' && !Number.isNaN(timestamp)) timestamp = BigInt(timestamp);\n\n\t\tif (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(\n\t\t\t\t`\"timestamp\" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`\n\t\t\t);\n\t\t}\n\n\t\tif (increment >= 4095n) increment = 0n;\n\n\t\t// timestamp, workerID, processID, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```ts\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerID: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessID: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n}\n\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\n\t * @default 1n\n\t */\n\tworkerID?: bigint;\n\n\t/**\n\t * The process ID to use\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 { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } 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 class DiscordSnowflake extends Snowflake {\n\tpublic constructor() {\n\t\tsuper(DiscordSnowflake.Epoch);\n\t}\n\n\t/**\n\t * Discord epoch (`2015-01-01T00:00:00.000Z`)\n\t * @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n\t */\n\tpublic static readonly Epoch = 1420070400000n;\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 * ```ts\n\t * const snowflake = DiscordSnowflake.decode('3971046231244935168');\n\t * ```\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-meLine truncated
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
/*! *****************************************************************************
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
***************************************************************************** */
|
||||
|
||||
function __classPrivateFieldGet(receiver, privateMap) {
|
||||
if (!privateMap.has(receiver)) {
|
||||
throw new TypeError("attempted to get private field on non-instance");
|
||||
}
|
||||
return privateMap.get(receiver);
|
||||
}
|
||||
|
||||
function __classPrivateFieldSet(receiver, privateMap, value) {
|
||||
if (!privateMap.has(receiver)) {
|
||||
throw new TypeError("attempted to set private field on non-instance");
|
||||
}
|
||||
privateMap.set(receiver, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
||||
var _Snowflake_increment, _Snowflake_epoch;
|
||||
/**
|
||||
* A class for parsing snowflake ids
|
||||
*/
|
||||
class Snowflake {
|
||||
/**
|
||||
* @param epoch the epoch to use
|
||||
*/
|
||||
constructor(epoch) {
|
||||
/**
|
||||
* Internal incrementor for generating snowflakes
|
||||
* @internal
|
||||
*/
|
||||
_Snowflake_increment.set(this, 0n);
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor
|
||||
* @internal
|
||||
*/
|
||||
_Snowflake_epoch.set(this, void 0);
|
||||
/**
|
||||
* Alias for {@link deconstruct}
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this
|
||||
Object.defineProperty(this, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: this.deconstruct
|
||||
});
|
||||
__classPrivateFieldSet(this, _Snowflake_epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to the private increment of the instance
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
generate({ increment = __classPrivateFieldGet(this, _Snowflake_increment), timestamp = Date.now(), workerID = 1n, processID = 1n } = {
|
||||
increment: __classPrivateFieldGet(this, _Snowflake_increment),
|
||||
timestamp: Date.now(),
|
||||
workerID: 1n,
|
||||
processID: 1n
|
||||
}) {
|
||||
if (timestamp instanceof Date)
|
||||
timestamp = BigInt(timestamp.getTime());
|
||||
if (typeof timestamp === 'number' && !Number.isNaN(timestamp))
|
||||
timestamp = BigInt(timestamp);
|
||||
if (typeof timestamp !== 'bigint') {
|
||||
throw new TypeError(`"timestamp" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`);
|
||||
}
|
||||
if (increment >= 4095n)
|
||||
increment = 0n;
|
||||
// timestamp, workerID, processID, increment
|
||||
return ((timestamp - __classPrivateFieldGet(this, _Snowflake_epoch)) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
deconstruct(id) {
|
||||
const bigIntId = BigInt(id);
|
||||
return {
|
||||
id: bigIntId,
|
||||
timestamp: (bigIntId >> 22n) + __classPrivateFieldGet(this, _Snowflake_epoch),
|
||||
workerID: (bigIntId >> 17n) & 31n,
|
||||
processID: (bigIntId >> 12n) & 31n,
|
||||
increment: bigIntId & 4095n,
|
||||
epoch: __classPrivateFieldGet(this, _Snowflake_epoch)
|
||||
};
|
||||
}
|
||||
}
|
||||
_Snowflake_increment = new WeakMap(), _Snowflake_epoch = new WeakMap();
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Discord's snowflake epoch
|
||||
*
|
||||
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
class DiscordSnowflake extends Snowflake {
|
||||
constructor() {
|
||||
super(DiscordSnowflake.Epoch);
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id) {
|
||||
return new DiscordSnowflake().deconstruct(id);
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options = { timestamp: Date.now() }) {
|
||||
return new DiscordSnowflake().generate(options);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Discord epoch (`2015-01-01T00:00:00.000Z`)
|
||||
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
Object.defineProperty(DiscordSnowflake, "Epoch", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: 1420070400000n
|
||||
});
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Object.defineProperty(DiscordSnowflake, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: DiscordSnowflake.deconstruct
|
||||
});
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Twitter's snowflake epoch
|
||||
*
|
||||
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
|
||||
*/
|
||||
class TwitterSnowflake extends Snowflake {
|
||||
constructor() {
|
||||
super(TwitterSnowflake.Epoch);
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id) {
|
||||
return new TwitterSnowflake().deconstruct(id);
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options = { timestamp: Date.now() }) {
|
||||
return new TwitterSnowflake().generate(options);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
|
||||
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
|
||||
*/
|
||||
Object.defineProperty(TwitterSnowflake, "Epoch", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: 1142974214000n
|
||||
});
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Object.defineProperty(TwitterSnowflake, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: TwitterSnowflake.deconstruct
|
||||
});
|
||||
|
||||
export { DiscordSnowflake, Snowflake, TwitterSnowflake };
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.mjs","sources":["../src/lib/Snowflake.ts","../src/lib/DiscordSnowflake.ts","../src/lib/TwitterSnowflake.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\n\n/**\n * A class for parsing snowflake ids\n */\nexport class Snowflake {\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * 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 * ```ts\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\t{ increment = this.#increment, timestamp = Date.now(), workerID = 1n, processID = 1n }: SnowflakeGenerateOptions = {\n\t\t\tincrement: this.#increment,\n\t\t\ttimestamp: Date.now(),\n\t\t\tworkerID: 1n,\n\t\t\tprocessID: 1n\n\t\t}\n\t) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\tif (typeof timestamp === 'number' && !Number.isNaN(timestamp)) timestamp = BigInt(timestamp);\n\n\t\tif (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(\n\t\t\t\t`\"timestamp\" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`\n\t\t\t);\n\t\t}\n\n\t\tif (increment >= 4095n) increment = 0n;\n\n\t\t// timestamp, workerID, processID, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```ts\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerID: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessID: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n}\n\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\n\t * @default 1n\n\t */\n\tworkerID?: bigint;\n\n\t/**\n\t * The process ID to use\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 { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } 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 class DiscordSnowflake extends Snowflake {\n\tpublic constructor() {\n\t\tsuper(DiscordSnowflake.Epoch);\n\t}\n\n\t/**\n\t * Discord epoch (`2015-01-01T00:00:00.000Z`)\n\t * @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n\t */\n\tpublic static readonly Epoch = 1420070400000n;\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 * ```ts\n\t * const snowflake = DiscordSnowflake.decode('3971046231244935168');\n\t * ```\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-mLine truncated
|
||||
+256
@@ -0,0 +1,256 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SapphireSnowflake = {}));
|
||||
}(this, (function (exports) { 'use strict';
|
||||
|
||||
/*! *****************************************************************************
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
***************************************************************************** */
|
||||
|
||||
function __classPrivateFieldGet(receiver, privateMap) {
|
||||
if (!privateMap.has(receiver)) {
|
||||
throw new TypeError("attempted to get private field on non-instance");
|
||||
}
|
||||
return privateMap.get(receiver);
|
||||
}
|
||||
|
||||
function __classPrivateFieldSet(receiver, privateMap, value) {
|
||||
if (!privateMap.has(receiver)) {
|
||||
throw new TypeError("attempted to set private field on non-instance");
|
||||
}
|
||||
privateMap.set(receiver, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
||||
var _Snowflake_increment, _Snowflake_epoch;
|
||||
/**
|
||||
* A class for parsing snowflake ids
|
||||
*/
|
||||
class Snowflake {
|
||||
/**
|
||||
* @param epoch the epoch to use
|
||||
*/
|
||||
constructor(epoch) {
|
||||
/**
|
||||
* Internal incrementor for generating snowflakes
|
||||
* @internal
|
||||
*/
|
||||
_Snowflake_increment.set(this, 0n);
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor
|
||||
* @internal
|
||||
*/
|
||||
_Snowflake_epoch.set(this, void 0);
|
||||
/**
|
||||
* Alias for {@link deconstruct}
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this
|
||||
Object.defineProperty(this, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: this.deconstruct
|
||||
});
|
||||
__classPrivateFieldSet(this, _Snowflake_epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to the private increment of the instance
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
generate({ increment = __classPrivateFieldGet(this, _Snowflake_increment), timestamp = Date.now(), workerID = 1n, processID = 1n } = {
|
||||
increment: __classPrivateFieldGet(this, _Snowflake_increment),
|
||||
timestamp: Date.now(),
|
||||
workerID: 1n,
|
||||
processID: 1n
|
||||
}) {
|
||||
if (timestamp instanceof Date)
|
||||
timestamp = BigInt(timestamp.getTime());
|
||||
if (typeof timestamp === 'number' && !Number.isNaN(timestamp))
|
||||
timestamp = BigInt(timestamp);
|
||||
if (typeof timestamp !== 'bigint') {
|
||||
throw new TypeError(`"timestamp" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`);
|
||||
}
|
||||
if (increment >= 4095n)
|
||||
increment = 0n;
|
||||
// timestamp, workerID, processID, increment
|
||||
return ((timestamp - __classPrivateFieldGet(this, _Snowflake_epoch)) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
deconstruct(id) {
|
||||
const bigIntId = BigInt(id);
|
||||
return {
|
||||
id: bigIntId,
|
||||
timestamp: (bigIntId >> 22n) + __classPrivateFieldGet(this, _Snowflake_epoch),
|
||||
workerID: (bigIntId >> 17n) & 31n,
|
||||
processID: (bigIntId >> 12n) & 31n,
|
||||
increment: bigIntId & 4095n,
|
||||
epoch: __classPrivateFieldGet(this, _Snowflake_epoch)
|
||||
};
|
||||
}
|
||||
}
|
||||
_Snowflake_increment = new WeakMap(), _Snowflake_epoch = new WeakMap();
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Discord's snowflake epoch
|
||||
*
|
||||
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
class DiscordSnowflake extends Snowflake {
|
||||
constructor() {
|
||||
super(DiscordSnowflake.Epoch);
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id) {
|
||||
return new DiscordSnowflake().deconstruct(id);
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options = { timestamp: Date.now() }) {
|
||||
return new DiscordSnowflake().generate(options);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Discord epoch (`2015-01-01T00:00:00.000Z`)
|
||||
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
Object.defineProperty(DiscordSnowflake, "Epoch", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: 1420070400000n
|
||||
});
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Object.defineProperty(DiscordSnowflake, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: DiscordSnowflake.deconstruct
|
||||
});
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Twitter's snowflake epoch
|
||||
*
|
||||
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
|
||||
*/
|
||||
class TwitterSnowflake extends Snowflake {
|
||||
constructor() {
|
||||
super(TwitterSnowflake.Epoch);
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id) {
|
||||
return new TwitterSnowflake().deconstruct(id);
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options = { timestamp: Date.now() }) {
|
||||
return new TwitterSnowflake().generate(options);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
|
||||
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
|
||||
*/
|
||||
Object.defineProperty(TwitterSnowflake, "Epoch", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: 1142974214000n
|
||||
});
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Object.defineProperty(TwitterSnowflake, "decode", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: TwitterSnowflake.deconstruct
|
||||
});
|
||||
|
||||
exports.DiscordSnowflake = DiscordSnowflake;
|
||||
exports.Snowflake = Snowflake;
|
||||
exports.TwitterSnowflake = TwitterSnowflake;
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
})));
|
||||
//# sourceMappingURL=index.umd.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.umd.js","sources":["../src/lib/Snowflake.ts","../src/lib/DiscordSnowflake.ts","../src/lib/TwitterSnowflake.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\n\n/**\n * A class for parsing snowflake ids\n */\nexport class Snowflake {\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * 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 * ```ts\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\t{ increment = this.#increment, timestamp = Date.now(), workerID = 1n, processID = 1n }: SnowflakeGenerateOptions = {\n\t\t\tincrement: this.#increment,\n\t\t\ttimestamp: Date.now(),\n\t\t\tworkerID: 1n,\n\t\t\tprocessID: 1n\n\t\t}\n\t) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\tif (typeof timestamp === 'number' && !Number.isNaN(timestamp)) timestamp = BigInt(timestamp);\n\n\t\tif (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(\n\t\t\t\t`\"timestamp\" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`\n\t\t\t);\n\t\t}\n\n\t\tif (increment >= 4095n) increment = 0n;\n\n\t\t// timestamp, workerID, processID, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```ts\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerID: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessID: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n}\n\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\n\t * @default 1n\n\t */\n\tworkerID?: bigint;\n\n\t/**\n\t * The process ID to use\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 { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } 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 class DiscordSnowflake extends Snowflake {\n\tpublic constructor() {\n\t\tsuper(DiscordSnowflake.Epoch);\n\t}\n\n\t/**\n\t * Discord epoch (`2015-01-01T00:00:00.000Z`)\n\t * @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n\t */\n\tpublic static readonly Epoch = 1420070400000n;\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 * ```ts\n\t * const snowflake = DiscordSnowflake.decode('3971046231244935168');\n\t * ```\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbounLine truncated
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
import { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';
|
||||
/**
|
||||
* A class for parsing snowflake ids using Discord's snowflake epoch
|
||||
*
|
||||
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
export declare class DiscordSnowflake extends Snowflake {
|
||||
constructor();
|
||||
/**
|
||||
* Discord epoch (`2015-01-01T00:00:00.000Z`)
|
||||
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
static readonly Epoch = 1420070400000n;
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static decode: typeof DiscordSnowflake.deconstruct;
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id: string | bigint): DeconstructedSnowflake;
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = DiscordSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options?: SnowflakeGenerateOptions): bigint;
|
||||
}
|
||||
//# sourceMappingURL=DiscordSnowflake.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"DiscordSnowflake.d.ts","sourceRoot":"","sources":["../../src/lib/DiscordSnowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAE1F;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;;IAK9C;;;OAGG;IACH,gBAAuB,KAAK,kBAAkB;IAE9C;;;;;;;;OAQG;IAEH,OAAc,MAAM,sCAAgC;IAEpD;;;;;;;;OAQG;WACW,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;IAItE;;;;;;;;;;OAUG;WACW,QAAQ,CAAC,OAAO,GAAE,wBAAoD;CAGpF"}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* A class for parsing snowflake ids
|
||||
*/
|
||||
export declare class Snowflake {
|
||||
#private;
|
||||
/**
|
||||
* Alias for {@link deconstruct}
|
||||
*/
|
||||
decode: (id: string | bigint) => DeconstructedSnowflake;
|
||||
/**
|
||||
* @param epoch the epoch to use
|
||||
*/
|
||||
constructor(epoch: number | bigint | Date);
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to the private increment of the instance
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
generate({ increment, timestamp, workerID, processID }?: SnowflakeGenerateOptions): bigint;
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
deconstruct(id: string | bigint): DeconstructedSnowflake;
|
||||
}
|
||||
/**
|
||||
* Options for Snowflake#generate
|
||||
*/
|
||||
export interface SnowflakeGenerateOptions {
|
||||
/**
|
||||
* Timestamp or date of the snowflake to generate
|
||||
* @default Date.now()
|
||||
*/
|
||||
timestamp?: number | bigint | Date;
|
||||
/**
|
||||
* The increment to use
|
||||
* @default 0n
|
||||
* @remark keep in mind that this bigint is auto-incremented between generate calls
|
||||
*/
|
||||
increment?: bigint;
|
||||
/**
|
||||
* The worker ID to use
|
||||
* @default 1n
|
||||
*/
|
||||
workerID?: bigint;
|
||||
/**
|
||||
* The process ID to use
|
||||
* @default 1n
|
||||
*/
|
||||
processID?: bigint;
|
||||
}
|
||||
/**
|
||||
* Object returned by Snowflake#deconstruct
|
||||
*/
|
||||
export interface DeconstructedSnowflake {
|
||||
/**
|
||||
* The id in BigInt form
|
||||
*/
|
||||
id: bigint;
|
||||
/**
|
||||
* The timestamp stored in the snowflake
|
||||
*/
|
||||
timestamp: bigint;
|
||||
/**
|
||||
* The worker id stored in the snowflake
|
||||
*/
|
||||
workerID: bigint;
|
||||
/**
|
||||
* The process id stored in the snowflake
|
||||
*/
|
||||
processID: bigint;
|
||||
/**
|
||||
* The increment stored in the snowflake
|
||||
*/
|
||||
increment: bigint;
|
||||
/**
|
||||
* The epoch to use in the snowflake
|
||||
*/
|
||||
epoch: bigint;
|
||||
}
|
||||
//# sourceMappingURL=Snowflake.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Snowflake.d.ts","sourceRoot":"","sources":["../../src/lib/Snowflake.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,qBAAa,SAAS;;IAarB;;OAEG;IAEI,MAAM,OAsDU,MAAM,GAAG,MAAM,KAAG,sBAAsB,CAtD9B;IAEjC;;OAEG;gBACgB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIhD;;;;;;;;;;;OAWG;IACI,QAAQ,CACd,EAAE,SAA2B,EAAE,SAAsB,EAAE,QAAa,EAAE,SAAc,EAAE,GAAE,wBAKvF;IAiBF;;;;;;;;;OASG;IACI,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;CAW/D;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAEnC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACtC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACd"}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
import { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';
|
||||
/**
|
||||
* A class for parsing snowflake ids using Twitter's snowflake epoch
|
||||
*
|
||||
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
|
||||
*/
|
||||
export declare class TwitterSnowflake extends Snowflake {
|
||||
constructor();
|
||||
/**
|
||||
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
|
||||
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
|
||||
*/
|
||||
static readonly Epoch = 1142974214000n;
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static decode: typeof TwitterSnowflake.deconstruct;
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
static deconstruct(id: string | bigint): DeconstructedSnowflake;
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when increment is not provided it defaults to `0n`
|
||||
* @example
|
||||
* ```ts
|
||||
* const snowflake = TwitterSnowflake.generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
static generate(options?: SnowflakeGenerateOptions): bigint;
|
||||
}
|
||||
//# sourceMappingURL=TwitterSnowflake.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"TwitterSnowflake.d.ts","sourceRoot":"","sources":["../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAE1F;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;;IAK9C;;;OAGG;IACH,gBAAuB,KAAK,kBAAkB;IAE9C;;;;;;;;OAQG;IAEH,OAAc,MAAM,sCAAgC;IAEpD;;;;;;;;OAQG;WACW,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;IAItE;;;;;;;;;;OAUG;WACW,QAAQ,CAAC,OAAO,GAAE,wBAAoD;CAGpF"}
|
||||
Reference in new issue
Block a user