@sodiumlabs/snowflake 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -18,11 +18,9 @@ declare const MaximumIncrement = 4095n;
18
18
  /**
19
19
  * A class for generating and deconstructing snowflakes.
20
20
  *
21
- *
22
21
  * A {@link https://en.wikipedia.org/wiki/Snowflake_ID | snowflake}
23
22
  * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.
24
23
  *
25
- *
26
24
  * If we have a snowflake `266241948824764416` we can represent it as binary:
27
25
  *
28
26
  * ```
@@ -118,14 +116,18 @@ declare class Snowflake {
118
116
  * Returns a number indicating whether a reference snowflake comes before,
119
117
  * or after, or is same as the given snowflake in sort order.
120
118
  *
121
- * @example Sort snowflakes in ascending order
119
+ * @example
120
+ * Sort snowflakes in ascending order
121
+ *
122
122
  * ```typescript
123
123
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
124
124
  * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
125
125
  * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
126
126
  * ```
127
127
  *
128
- * @example Sort snowflakes in descending order
128
+ * @example
129
+ * Sort snowflakes in descending order
130
+ *
129
131
  * ```typescript
130
132
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
131
133
  * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
package/dist/index.d.ts CHANGED
@@ -18,11 +18,9 @@ declare const MaximumIncrement = 4095n;
18
18
  /**
19
19
  * A class for generating and deconstructing snowflakes.
20
20
  *
21
- *
22
21
  * A {@link https://en.wikipedia.org/wiki/Snowflake_ID | snowflake}
23
22
  * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.
24
23
  *
25
- *
26
24
  * If we have a snowflake `266241948824764416` we can represent it as binary:
27
25
  *
28
26
  * ```
@@ -118,14 +116,18 @@ declare class Snowflake {
118
116
  * Returns a number indicating whether a reference snowflake comes before,
119
117
  * or after, or is same as the given snowflake in sort order.
120
118
  *
121
- * @example Sort snowflakes in ascending order
119
+ * @example
120
+ * Sort snowflakes in ascending order
121
+ *
122
122
  * ```typescript
123
123
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
124
124
  * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
125
125
  * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
126
126
  * ```
127
127
  *
128
- * @example Sort snowflakes in descending order
128
+ * @example
129
+ * Sort snowflakes in descending order
130
+ *
129
131
  * ```typescript
130
132
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
131
133
  * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
package/dist/index.js CHANGED
@@ -174,14 +174,18 @@ var Snowflake = class {
174
174
  * Returns a number indicating whether a reference snowflake comes before,
175
175
  * or after, or is same as the given snowflake in sort order.
176
176
  *
177
- * @example Sort snowflakes in ascending order
177
+ * @example
178
+ * Sort snowflakes in ascending order
179
+ *
178
180
  * ```typescript
179
181
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
180
182
  * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
181
183
  * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
182
184
  * ```
183
185
  *
184
- * @example Sort snowflakes in descending order
186
+ * @example
187
+ * Sort snowflakes in descending order
188
+ *
185
189
  * ```typescript
186
190
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
187
191
  * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/snowflake.ts","../src/discord-snowflake.ts"],"sourcesContent":["export * from \"./snowflake\";\nexport * from \"./discord-snowflake\";\n","const IncrementSymbol = Symbol(\"@sodiumlabs/snowflake.increment\");\nconst EpochSymbol = Symbol(\"@sodiumlabs/snowflake.epoch\");\nconst EpochNumberSymbol = Symbol(\"@sodiumlabs/snowflake.epoch.number\");\nconst ProcessIdSymbol = Symbol(\"@sodiumlabs/snowflake.processId\");\nconst WorkerIdSymbol = Symbol(\"@sodiumlabs/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 snowflakes.\n *\n *\n * A {@link https://en.wikipedia.org/wiki/Snowflake_ID | snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n *\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 /**\n * Internal reference of the epoch passed in the constructor.\n * @internal\n */\n private readonly [EpochSymbol]: bigint;\n\n /**\n * Internal reference of the epoch passed in the constructor as a number.\n * @internal\n */\n private readonly [EpochNumberSymbol]: number;\n\n /**\n * Internal incrementor for generating snowflakes.\n * @internal\n */\n private [IncrementSymbol] = 0n;\n\n /**\n * The process ID that will be used by default in the generate method.\n * @internal\n */\n private [ProcessIdSymbol] = 1n;\n\n /**\n * The worker ID that will be used by default in the generate method.\n * @internal\n */\n private [WorkerIdSymbol] = 0n;\n\n /**\n * @param epoch - The epoch to use.\n */\n public constructor(epoch: number | bigint | Date) {\n this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n this[EpochNumberSymbol] = Number(this[EpochSymbol]);\n }\n\n /**\n * The epoch for this snowflake, as a bigint.\n */\n public get epoch(): bigint {\n return this[EpochSymbol];\n }\n\n /**\n * The epoch for this snowflake, as a number.\n */\n public get epochNumber(): number {\n return this[EpochNumberSymbol];\n }\n\n /**\n * Gets or sets the configured process ID.\n */\n public get processId(): bigint {\n return this[ProcessIdSymbol];\n }\n\n public set processId(value: number | bigint) {\n this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n }\n\n /**\n * Gets or sets the configured worker ID.\n */\n public get workerId(): bigint {\n return this[WorkerIdSymbol];\n }\n\n public set workerId(value: number | bigint) {\n this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n }\n\n /**\n * Generates a snowflake given an epoch and optionally a timestamp.\n *\n * **Note:** when `increment` is not provided it defaults to the private `increment` of the instance.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).generate();\n * ```\n *\n * @param options - Options to pass into the generator, see {@link SnowflakeGenerateOptions}.\n * @returns A unique snowflake.\n */\n public generate({\n increment,\n timestamp = Date.now(),\n workerId = this[WorkerIdSymbol],\n processId = this[ProcessIdSymbol],\n }: SnowflakeGenerateOptions = {}) {\n if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n else if (typeof timestamp === \"number\") timestamp = BigInt(timestamp);\n else if (typeof timestamp !== \"bigint\") {\n throw new TypeError(\n `\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`,\n );\n }\n\n if (typeof increment !== \"bigint\") {\n increment = this[IncrementSymbol];\n this[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n }\n\n // timestamp, workerId, processId, increment\n return (\n ((timestamp - this[EpochSymbol]) << 22n) |\n ((workerId & MaximumWorkerId) << 17n) |\n ((processId & MaximumProcessId) << 12n) |\n (increment & MaximumIncrement)\n );\n }\n\n /**\n * Deconstructs a snowflake.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n * ```\n *\n * @param id - The snowflake to deconstruct.\n * @returns A deconstructed snowflake.\n */\n public deconstruct(id: string | bigint): DeconstructedSnowflake {\n const bigIntId = BigInt(id);\n const epoch = this[EpochSymbol];\n return {\n id: bigIntId,\n timestamp: (bigIntId >> 22n) + epoch,\n workerId: (bigIntId >> 17n) & MaximumWorkerId,\n processId: (bigIntId >> 12n) & MaximumProcessId,\n increment: bigIntId & MaximumIncrement,\n epoch,\n };\n }\n\n /**\n * Retrieves the timestamp field's value from a snowflake.\n *\n * @param id - The snowflake to get the timestamp value from.\n * @returns The UNIX timestamp that is stored in `id`.\n */\n public timestampFrom(id: string | bigint): number {\n return Number((BigInt(id) >> 22n) + this.epoch);\n }\n\n /**\n * Returns a number indicating whether a reference snowflake comes before,\n * or after, or is same as the given snowflake in sort order.\n *\n * @example Sort snowflakes in ascending order\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n * ```\n *\n * @example Sort snowflakes in descending order\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n * ```\n *\n * @param a - The first snowflake to compare.\n * @param b - The second snowflake to compare.\n * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n */\n public static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n const typeA = typeof a;\n return typeA === typeof b\n ? typeA === \"string\"\n ? cmpString(a as string, b as string)\n : cmpBigInt(a as bigint, b as bigint)\n : cmpBigInt(BigInt(a), BigInt(b));\n }\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n return a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n return 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 /**\n * Timestamp or date of the snowflake to generate.\n *\n * @defaultValue Date.now()\n */\n timestamp?: number | bigint | Date;\n /**\n * The increment to use.\n *\n * @defaultValue 0n\n * @remarks keep in mind that this bigint is auto-incremented between generate calls.\n */\n increment?: bigint;\n /**\n * The worker ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 0n\n */\n workerId?: bigint;\n /**\n * The process ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 1n\n */\n processId?: bigint;\n}\n\n/**\n * Object returned by `Snowflake#deconstruct`.\n */\nexport interface DeconstructedSnowflake {\n /**\n * The id in BigInt form.\n */\n id: bigint;\n /**\n * The timestamp stored in the snowflake.\n */\n timestamp: bigint;\n /**\n * The worker id stored in the snowflake.\n */\n workerId: bigint;\n /**\n * The process id stored in the snowflake.\n */\n processId: bigint;\n /**\n * The increment stored in the snowflake.\n */\n increment: bigint;\n /**\n * The epoch to use in the snowflake.\n */\n epoch: bigint;\n}\n","import { Snowflake } from \"./snowflake\";\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch.\n *\n * The Discord epoch is `1420070400000` (`2015-01-01T00:00:00.000Z`), see {@link https://discord.com/developers/docs/reference#snowflakes}.\n */\nexport const discordSnowflake = new Snowflake(1420070400000n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAM,kBAAkB,uBAAO,iCAAiC;AAChE,IAAM,cAAc,uBAAO,6BAA6B;AACxD,IAAM,oBAAoB,uBAAO,oCAAoC;AACrE,IAAM,kBAAkB,uBAAO,iCAAiC;AAChE,IAAM,iBAAiB,uBAAO,gCAAgC;AAKvD,IAAM,kBAAkB;AAKxB,IAAM,mBAAmB;AAKzB,IAAM,mBAAmB;AAkBzB,IAAM,YAAN,MAAgB;AAAA,EArCvB,OAqCuB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnB,CAAkB,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,CAAkB,iBAAiB;AA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AI,IAAI,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AAC3F;AAFS;;;AC5NF,IAAM,mBAAmB,IAAI,UAAU,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/snowflake.ts","../src/discord-snowflake.ts"],"sourcesContent":["export * from \"./snowflake\";\nexport * from \"./discord-snowflake\";\n","const IncrementSymbol = Symbol(\"@sodiumlabs/snowflake.increment\");\nconst EpochSymbol = Symbol(\"@sodiumlabs/snowflake.epoch\");\nconst EpochNumberSymbol = Symbol(\"@sodiumlabs/snowflake.epoch.number\");\nconst ProcessIdSymbol = Symbol(\"@sodiumlabs/snowflake.processId\");\nconst WorkerIdSymbol = Symbol(\"@sodiumlabs/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 snowflakes.\n *\n * A {@link https://en.wikipedia.org/wiki/Snowflake_ID | 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 * ```\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 /**\n * Internal reference of the epoch passed in the constructor.\n * @internal\n */\n private readonly [EpochSymbol]: bigint;\n\n /**\n * Internal reference of the epoch passed in the constructor as a number.\n * @internal\n */\n private readonly [EpochNumberSymbol]: number;\n\n /**\n * Internal incrementor for generating snowflakes.\n * @internal\n */\n private [IncrementSymbol] = 0n;\n\n /**\n * The process ID that will be used by default in the generate method.\n * @internal\n */\n private [ProcessIdSymbol] = 1n;\n\n /**\n * The worker ID that will be used by default in the generate method.\n * @internal\n */\n private [WorkerIdSymbol] = 0n;\n\n /**\n * @param epoch - The epoch to use.\n */\n public constructor(epoch: number | bigint | Date) {\n this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n this[EpochNumberSymbol] = Number(this[EpochSymbol]);\n }\n\n /**\n * The epoch for this snowflake, as a bigint.\n */\n public get epoch(): bigint {\n return this[EpochSymbol];\n }\n\n /**\n * The epoch for this snowflake, as a number.\n */\n public get epochNumber(): number {\n return this[EpochNumberSymbol];\n }\n\n /**\n * Gets or sets the configured process ID.\n */\n public get processId(): bigint {\n return this[ProcessIdSymbol];\n }\n\n public set processId(value: number | bigint) {\n this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n }\n\n /**\n * Gets or sets the configured worker ID.\n */\n public get workerId(): bigint {\n return this[WorkerIdSymbol];\n }\n\n public set workerId(value: number | bigint) {\n this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n }\n\n /**\n * Generates a snowflake given an epoch and optionally a timestamp.\n *\n * **Note:** when `increment` is not provided it defaults to the private `increment` of the instance.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).generate();\n * ```\n *\n * @param options - Options to pass into the generator, see {@link SnowflakeGenerateOptions}.\n * @returns A unique snowflake.\n */\n public generate({\n increment,\n timestamp = Date.now(),\n workerId = this[WorkerIdSymbol],\n processId = this[ProcessIdSymbol],\n }: SnowflakeGenerateOptions = {}) {\n if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n else if (typeof timestamp === \"number\") timestamp = BigInt(timestamp);\n else if (typeof timestamp !== \"bigint\") {\n throw new TypeError(\n `\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`,\n );\n }\n\n if (typeof increment !== \"bigint\") {\n increment = this[IncrementSymbol];\n this[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n }\n\n // timestamp, workerId, processId, increment\n return (\n ((timestamp - this[EpochSymbol]) << 22n) |\n ((workerId & MaximumWorkerId) << 17n) |\n ((processId & MaximumProcessId) << 12n) |\n (increment & MaximumIncrement)\n );\n }\n\n /**\n * Deconstructs a snowflake.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n * ```\n *\n * @param id - The snowflake to deconstruct.\n * @returns A deconstructed snowflake.\n */\n public deconstruct(id: string | bigint): DeconstructedSnowflake {\n const bigIntId = BigInt(id);\n const epoch = this[EpochSymbol];\n return {\n id: bigIntId,\n timestamp: (bigIntId >> 22n) + epoch,\n workerId: (bigIntId >> 17n) & MaximumWorkerId,\n processId: (bigIntId >> 12n) & MaximumProcessId,\n increment: bigIntId & MaximumIncrement,\n epoch,\n };\n }\n\n /**\n * Retrieves the timestamp field's value from a snowflake.\n *\n * @param id - The snowflake to get the timestamp value from.\n * @returns The UNIX timestamp that is stored in `id`.\n */\n public timestampFrom(id: string | bigint): number {\n return Number((BigInt(id) >> 22n) + this.epoch);\n }\n\n /**\n * Returns a number indicating whether a reference snowflake comes before,\n * or after, or is same as the given snowflake in sort order.\n *\n * @example\n * Sort snowflakes in ascending order\n *\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n * ```\n *\n * @example\n * Sort snowflakes in descending order\n *\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n * ```\n *\n * @param a - The first snowflake to compare.\n * @param b - The second snowflake to compare.\n * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n */\n public static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n const typeA = typeof a;\n return typeA === typeof b\n ? typeA === \"string\"\n ? cmpString(a as string, b as string)\n : cmpBigInt(a as bigint, b as bigint)\n : cmpBigInt(BigInt(a), BigInt(b));\n }\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n return a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n return 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 /**\n * Timestamp or date of the snowflake to generate.\n *\n * @defaultValue Date.now()\n */\n timestamp?: number | bigint | Date;\n /**\n * The increment to use.\n *\n * @defaultValue 0n\n * @remarks keep in mind that this bigint is auto-incremented between generate calls.\n */\n increment?: bigint;\n /**\n * The worker ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 0n\n */\n workerId?: bigint;\n /**\n * The process ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 1n\n */\n processId?: bigint;\n}\n\n/**\n * Object returned by `Snowflake#deconstruct`.\n */\nexport interface DeconstructedSnowflake {\n /**\n * The id in BigInt form.\n */\n id: bigint;\n /**\n * The timestamp stored in the snowflake.\n */\n timestamp: bigint;\n /**\n * The worker id stored in the snowflake.\n */\n workerId: bigint;\n /**\n * The process id stored in the snowflake.\n */\n processId: bigint;\n /**\n * The increment stored in the snowflake.\n */\n increment: bigint;\n /**\n * The epoch to use in the snowflake.\n */\n epoch: bigint;\n}\n","import { Snowflake } from \"./snowflake\";\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch.\n *\n * The Discord epoch is `1420070400000` (`2015-01-01T00:00:00.000Z`), see {@link https://discord.com/developers/docs/reference#snowflakes}.\n */\nexport const discordSnowflake = new Snowflake(1420070400000n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAM,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA4BA,OAAc,QAAQ,GAAoB,GAAgC;AACtE,UAAM,QAAQ,OAAO;AACrB,WAAO,UAAU,OAAO,IAClB,UAAU,WACN,UAAU,GAAa,CAAW,IAClC,UAAU,GAAa,CAAW,IACtC,UAAU,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC;AAAA,EACxC;AACJ;AAGA,SAAS,UAAU,GAAW,GAAW;AACrC,SAAO,MAAM,IAAI,IAAI,IAAI,IAAI,KAAK;AACtC;AAFS;AAKT,SAAS,UAAU,GAAW,GAAW;AACrC,SAAO,MAAM,IAAI,IAAI,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AAC3F;AAFS;;;AC9NF,IAAM,mBAAmB,IAAI,UAAU,cAAc;","names":[]}
package/dist/index.mjs CHANGED
@@ -146,14 +146,18 @@ var Snowflake = class {
146
146
  * Returns a number indicating whether a reference snowflake comes before,
147
147
  * or after, or is same as the given snowflake in sort order.
148
148
  *
149
- * @example Sort snowflakes in ascending order
149
+ * @example
150
+ * Sort snowflakes in ascending order
151
+ *
150
152
  * ```typescript
151
153
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
152
154
  * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
153
155
  * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
154
156
  * ```
155
157
  *
156
- * @example Sort snowflakes in descending order
158
+ * @example
159
+ * Sort snowflakes in descending order
160
+ *
157
161
  * ```typescript
158
162
  * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
159
163
  * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/snowflake.ts","../src/discord-snowflake.ts"],"sourcesContent":["const IncrementSymbol = Symbol(\"@sodiumlabs/snowflake.increment\");\nconst EpochSymbol = Symbol(\"@sodiumlabs/snowflake.epoch\");\nconst EpochNumberSymbol = Symbol(\"@sodiumlabs/snowflake.epoch.number\");\nconst ProcessIdSymbol = Symbol(\"@sodiumlabs/snowflake.processId\");\nconst WorkerIdSymbol = Symbol(\"@sodiumlabs/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 snowflakes.\n *\n *\n * A {@link https://en.wikipedia.org/wiki/Snowflake_ID | snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n *\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 /**\n * Internal reference of the epoch passed in the constructor.\n * @internal\n */\n private readonly [EpochSymbol]: bigint;\n\n /**\n * Internal reference of the epoch passed in the constructor as a number.\n * @internal\n */\n private readonly [EpochNumberSymbol]: number;\n\n /**\n * Internal incrementor for generating snowflakes.\n * @internal\n */\n private [IncrementSymbol] = 0n;\n\n /**\n * The process ID that will be used by default in the generate method.\n * @internal\n */\n private [ProcessIdSymbol] = 1n;\n\n /**\n * The worker ID that will be used by default in the generate method.\n * @internal\n */\n private [WorkerIdSymbol] = 0n;\n\n /**\n * @param epoch - The epoch to use.\n */\n public constructor(epoch: number | bigint | Date) {\n this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n this[EpochNumberSymbol] = Number(this[EpochSymbol]);\n }\n\n /**\n * The epoch for this snowflake, as a bigint.\n */\n public get epoch(): bigint {\n return this[EpochSymbol];\n }\n\n /**\n * The epoch for this snowflake, as a number.\n */\n public get epochNumber(): number {\n return this[EpochNumberSymbol];\n }\n\n /**\n * Gets or sets the configured process ID.\n */\n public get processId(): bigint {\n return this[ProcessIdSymbol];\n }\n\n public set processId(value: number | bigint) {\n this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n }\n\n /**\n * Gets or sets the configured worker ID.\n */\n public get workerId(): bigint {\n return this[WorkerIdSymbol];\n }\n\n public set workerId(value: number | bigint) {\n this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n }\n\n /**\n * Generates a snowflake given an epoch and optionally a timestamp.\n *\n * **Note:** when `increment` is not provided it defaults to the private `increment` of the instance.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).generate();\n * ```\n *\n * @param options - Options to pass into the generator, see {@link SnowflakeGenerateOptions}.\n * @returns A unique snowflake.\n */\n public generate({\n increment,\n timestamp = Date.now(),\n workerId = this[WorkerIdSymbol],\n processId = this[ProcessIdSymbol],\n }: SnowflakeGenerateOptions = {}) {\n if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n else if (typeof timestamp === \"number\") timestamp = BigInt(timestamp);\n else if (typeof timestamp !== \"bigint\") {\n throw new TypeError(\n `\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`,\n );\n }\n\n if (typeof increment !== \"bigint\") {\n increment = this[IncrementSymbol];\n this[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n }\n\n // timestamp, workerId, processId, increment\n return (\n ((timestamp - this[EpochSymbol]) << 22n) |\n ((workerId & MaximumWorkerId) << 17n) |\n ((processId & MaximumProcessId) << 12n) |\n (increment & MaximumIncrement)\n );\n }\n\n /**\n * Deconstructs a snowflake.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n * ```\n *\n * @param id - The snowflake to deconstruct.\n * @returns A deconstructed snowflake.\n */\n public deconstruct(id: string | bigint): DeconstructedSnowflake {\n const bigIntId = BigInt(id);\n const epoch = this[EpochSymbol];\n return {\n id: bigIntId,\n timestamp: (bigIntId >> 22n) + epoch,\n workerId: (bigIntId >> 17n) & MaximumWorkerId,\n processId: (bigIntId >> 12n) & MaximumProcessId,\n increment: bigIntId & MaximumIncrement,\n epoch,\n };\n }\n\n /**\n * Retrieves the timestamp field's value from a snowflake.\n *\n * @param id - The snowflake to get the timestamp value from.\n * @returns The UNIX timestamp that is stored in `id`.\n */\n public timestampFrom(id: string | bigint): number {\n return Number((BigInt(id) >> 22n) + this.epoch);\n }\n\n /**\n * Returns a number indicating whether a reference snowflake comes before,\n * or after, or is same as the given snowflake in sort order.\n *\n * @example Sort snowflakes in ascending order\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n * ```\n *\n * @example Sort snowflakes in descending order\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n * ```\n *\n * @param a - The first snowflake to compare.\n * @param b - The second snowflake to compare.\n * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n */\n public static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n const typeA = typeof a;\n return typeA === typeof b\n ? typeA === \"string\"\n ? cmpString(a as string, b as string)\n : cmpBigInt(a as bigint, b as bigint)\n : cmpBigInt(BigInt(a), BigInt(b));\n }\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n return a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n return 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 /**\n * Timestamp or date of the snowflake to generate.\n *\n * @defaultValue Date.now()\n */\n timestamp?: number | bigint | Date;\n /**\n * The increment to use.\n *\n * @defaultValue 0n\n * @remarks keep in mind that this bigint is auto-incremented between generate calls.\n */\n increment?: bigint;\n /**\n * The worker ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 0n\n */\n workerId?: bigint;\n /**\n * The process ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 1n\n */\n processId?: bigint;\n}\n\n/**\n * Object returned by `Snowflake#deconstruct`.\n */\nexport interface DeconstructedSnowflake {\n /**\n * The id in BigInt form.\n */\n id: bigint;\n /**\n * The timestamp stored in the snowflake.\n */\n timestamp: bigint;\n /**\n * The worker id stored in the snowflake.\n */\n workerId: bigint;\n /**\n * The process id stored in the snowflake.\n */\n processId: bigint;\n /**\n * The increment stored in the snowflake.\n */\n increment: bigint;\n /**\n * The epoch to use in the snowflake.\n */\n epoch: bigint;\n}\n","import { Snowflake } from \"./snowflake\";\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch.\n *\n * The Discord epoch is `1420070400000` (`2015-01-01T00:00:00.000Z`), see {@link https://discord.com/developers/docs/reference#snowflakes}.\n */\nexport const discordSnowflake = new Snowflake(1420070400000n);\n"],"mappings":";;;;AAAA,IAAM,kBAAkB,uBAAO,iCAAiC;AAChE,IAAM,cAAc,uBAAO,6BAA6B;AACxD,IAAM,oBAAoB,uBAAO,oCAAoC;AACrE,IAAM,kBAAkB,uBAAO,iCAAiC;AAChE,IAAM,iBAAiB,uBAAO,gCAAgC;AAKvD,IAAM,kBAAkB;AAKxB,IAAM,mBAAmB;AAKzB,IAAM,mBAAmB;AAkBzB,IAAM,YAAN,MAAgB;AAAA,EArCvB,OAqCuB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnB,CAAkB,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,CAAkB,iBAAiB;AAAA;AAAA;AA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AE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AAC3F;AAFS;;;AC5NF,IAAM,mBAAmB,IAAI,UAAU,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/snowflake.ts","../src/discord-snowflake.ts"],"sourcesContent":["const IncrementSymbol = Symbol(\"@sodiumlabs/snowflake.increment\");\nconst EpochSymbol = Symbol(\"@sodiumlabs/snowflake.epoch\");\nconst EpochNumberSymbol = Symbol(\"@sodiumlabs/snowflake.epoch.number\");\nconst ProcessIdSymbol = Symbol(\"@sodiumlabs/snowflake.processId\");\nconst WorkerIdSymbol = Symbol(\"@sodiumlabs/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 snowflakes.\n *\n * A {@link https://en.wikipedia.org/wiki/Snowflake_ID | 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 * ```\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 /**\n * Internal reference of the epoch passed in the constructor.\n * @internal\n */\n private readonly [EpochSymbol]: bigint;\n\n /**\n * Internal reference of the epoch passed in the constructor as a number.\n * @internal\n */\n private readonly [EpochNumberSymbol]: number;\n\n /**\n * Internal incrementor for generating snowflakes.\n * @internal\n */\n private [IncrementSymbol] = 0n;\n\n /**\n * The process ID that will be used by default in the generate method.\n * @internal\n */\n private [ProcessIdSymbol] = 1n;\n\n /**\n * The worker ID that will be used by default in the generate method.\n * @internal\n */\n private [WorkerIdSymbol] = 0n;\n\n /**\n * @param epoch - The epoch to use.\n */\n public constructor(epoch: number | bigint | Date) {\n this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n this[EpochNumberSymbol] = Number(this[EpochSymbol]);\n }\n\n /**\n * The epoch for this snowflake, as a bigint.\n */\n public get epoch(): bigint {\n return this[EpochSymbol];\n }\n\n /**\n * The epoch for this snowflake, as a number.\n */\n public get epochNumber(): number {\n return this[EpochNumberSymbol];\n }\n\n /**\n * Gets or sets the configured process ID.\n */\n public get processId(): bigint {\n return this[ProcessIdSymbol];\n }\n\n public set processId(value: number | bigint) {\n this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;\n }\n\n /**\n * Gets or sets the configured worker ID.\n */\n public get workerId(): bigint {\n return this[WorkerIdSymbol];\n }\n\n public set workerId(value: number | bigint) {\n this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;\n }\n\n /**\n * Generates a snowflake given an epoch and optionally a timestamp.\n *\n * **Note:** when `increment` is not provided it defaults to the private `increment` of the instance.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).generate();\n * ```\n *\n * @param options - Options to pass into the generator, see {@link SnowflakeGenerateOptions}.\n * @returns A unique snowflake.\n */\n public generate({\n increment,\n timestamp = Date.now(),\n workerId = this[WorkerIdSymbol],\n processId = this[ProcessIdSymbol],\n }: SnowflakeGenerateOptions = {}) {\n if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n else if (typeof timestamp === \"number\") timestamp = BigInt(timestamp);\n else if (typeof timestamp !== \"bigint\") {\n throw new TypeError(\n `\"timestamp\" argument must be a number, bigint, or Date (received ${typeof timestamp})`,\n );\n }\n\n if (typeof increment !== \"bigint\") {\n increment = this[IncrementSymbol];\n this[IncrementSymbol] = (increment + 1n) & MaximumIncrement;\n }\n\n // timestamp, workerId, processId, increment\n return (\n ((timestamp - this[EpochSymbol]) << 22n) |\n ((workerId & MaximumWorkerId) << 17n) |\n ((processId & MaximumProcessId) << 12n) |\n (increment & MaximumIncrement)\n );\n }\n\n /**\n * Deconstructs a snowflake.\n *\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n * ```\n *\n * @param id - The snowflake to deconstruct.\n * @returns A deconstructed snowflake.\n */\n public deconstruct(id: string | bigint): DeconstructedSnowflake {\n const bigIntId = BigInt(id);\n const epoch = this[EpochSymbol];\n return {\n id: bigIntId,\n timestamp: (bigIntId >> 22n) + epoch,\n workerId: (bigIntId >> 17n) & MaximumWorkerId,\n processId: (bigIntId >> 12n) & MaximumProcessId,\n increment: bigIntId & MaximumIncrement,\n epoch,\n };\n }\n\n /**\n * Retrieves the timestamp field's value from a snowflake.\n *\n * @param id - The snowflake to get the timestamp value from.\n * @returns The UNIX timestamp that is stored in `id`.\n */\n public timestampFrom(id: string | bigint): number {\n return Number((BigInt(id) >> 22n) + this.epoch);\n }\n\n /**\n * Returns a number indicating whether a reference snowflake comes before,\n * or after, or is same as the given snowflake in sort order.\n *\n * @example\n * Sort snowflakes in ascending order\n *\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => Snowflake.compare(a, b)));\n * // → ['254360814063058944', '737141877803057244', '1056191128120082432'];\n * ```\n *\n * @example\n * Sort snowflakes in descending order\n *\n * ```typescript\n * const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];\n * console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));\n * // → ['1056191128120082432', '737141877803057244', '254360814063058944'];\n * ```\n *\n * @param a - The first snowflake to compare.\n * @param b - The second snowflake to compare.\n * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n */\n public static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1 {\n const typeA = typeof a;\n return typeA === typeof b\n ? typeA === \"string\"\n ? cmpString(a as string, b as string)\n : cmpBigInt(a as bigint, b as bigint)\n : cmpBigInt(BigInt(a), BigInt(b));\n }\n}\n\n/** @internal */\nfunction cmpBigInt(a: bigint, b: bigint) {\n return a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** @internal */\nfunction cmpString(a: string, b: string) {\n return 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 /**\n * Timestamp or date of the snowflake to generate.\n *\n * @defaultValue Date.now()\n */\n timestamp?: number | bigint | Date;\n /**\n * The increment to use.\n *\n * @defaultValue 0n\n * @remarks keep in mind that this bigint is auto-incremented between generate calls.\n */\n increment?: bigint;\n /**\n * The worker ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 0n\n */\n workerId?: bigint;\n /**\n * The process ID to use, will be truncated to 5 bits (0-31).\n *\n * @defaultValue 1n\n */\n processId?: bigint;\n}\n\n/**\n * Object returned by `Snowflake#deconstruct`.\n */\nexport interface DeconstructedSnowflake {\n /**\n * The id in BigInt form.\n */\n id: bigint;\n /**\n * The timestamp stored in the snowflake.\n */\n timestamp: bigint;\n /**\n * The worker id stored in the snowflake.\n */\n workerId: bigint;\n /**\n * The process id stored in the snowflake.\n */\n processId: bigint;\n /**\n * The increment stored in the snowflake.\n */\n increment: bigint;\n /**\n * The epoch to use in the snowflake.\n */\n epoch: bigint;\n}\n","import { Snowflake } from \"./snowflake\";\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch.\n *\n * The Discord epoch is `1420070400000` (`2015-01-01T00:00:00.000Z`), see {@link https://discord.com/developers/docs/reference#snowflakes}.\n */\nexport const discordSnowflake = new Snowflake(1420070400000n);\n"],"mappings":";;;;AAAA,IAAM,kBAAkB,uBAAO,iCAAiC;AAChE,IAAM,cAAc,uBAAO,6BAA6B;AACxD,IAAM,oBAAoB,uBAAO,oCAAoC;AACrE,IAAM,kBAAkB,uBAAO,iCAAiC;AAChE,IA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,WAAO,UAAU,OAAO,IAClB,UAAU,WACN,UAAU,GAAa,CAAW,IAClC,UAAU,GAAa,CAAW,IACtC,UAAU,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC;AAAA,EACxC;AACJ;AAGA,SAAS,UAAU,GAAW,GAAW;AACrC,SAAO,MAAM,IAAI,IAAI,IAAI,IAAI,KAAK;AACtC;AAFS;AAKT,SAAS,UAAU,GAAW,GAAW;AACrC,SAAO,MAAM,IAAI,IAAI,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AAC3F;AAFS;;;AC9NF,IAAM,mBAAmB,IAAI,UAAU,cAAc;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@sodiumlabs/snowflake",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "description": "Snowflake utility package",
6
6
  "keywords": [
7
7
  "id",