@andrew_l/snowflake 0.3.22 → 0.4.0

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
@@ -2,55 +2,55 @@
2
2
  * Object returned by Snowflake#deconstruct
3
3
  */
4
4
  interface DeconstructedSnowflake {
5
- /**
6
- * The id as a number
7
- */
8
- id: bigint;
9
- /**
10
- * The timestamp stored in the snowflake
11
- */
12
- timestamp: number;
13
- /**
14
- * The worker id stored in the snowflake
15
- */
16
- workerId: number;
17
- /**
18
- * The process id stored in the snowflake
19
- */
20
- processId: number;
21
- /**
22
- * The increment stored in the snowflake
23
- */
24
- increment: number;
25
- /**
26
- * The epoch to use in the snowflake
27
- */
28
- epoch: number;
5
+ /**
6
+ * The id as a number
7
+ */
8
+ id: bigint;
9
+ /**
10
+ * The timestamp stored in the snowflake
11
+ */
12
+ timestamp: number;
13
+ /**
14
+ * The worker id stored in the snowflake
15
+ */
16
+ workerId: number;
17
+ /**
18
+ * The process id stored in the snowflake
19
+ */
20
+ processId: number;
21
+ /**
22
+ * The increment stored in the snowflake
23
+ */
24
+ increment: number;
25
+ /**
26
+ * The epoch to use in the snowflake
27
+ */
28
+ epoch: number;
29
29
  }
30
30
  /**
31
31
  * Options for Snowflake
32
32
  */
33
33
  interface SnowflakeOptions {
34
- /**
35
- * Timestamp or date of the snowflake to generate
36
- */
37
- epoch: number | bigint | Date;
38
- /**
39
- * The increment to use
40
- * @default 0
41
- * @remark keep in mind that this number is auto-incremented between generate calls
42
- */
43
- increment?: number | bigint;
44
- /**
45
- * The worker ID to use, will be truncated to 5 bits (0-31)
46
- * @default 0
47
- */
48
- workerId?: number | bigint;
49
- /**
50
- * The process ID to use, will be truncated to 5 bits (0-31)
51
- * @default 1
52
- */
53
- processId?: number | bigint;
34
+ /**
35
+ * Timestamp or date of the snowflake to generate
36
+ */
37
+ epoch: number | bigint | Date;
38
+ /**
39
+ * The increment to use
40
+ * @default 0
41
+ * @remark keep in mind that this number is auto-incremented between generate calls
42
+ */
43
+ increment?: number | bigint;
44
+ /**
45
+ * The worker ID to use, will be truncated to 5 bits (0-31)
46
+ * @default 0
47
+ */
48
+ workerId?: number | bigint;
49
+ /**
50
+ * The process ID to use, will be truncated to 5 bits (0-31)
51
+ * @default 1
52
+ */
53
+ processId?: number | bigint;
54
54
  }
55
55
  /**
56
56
  * The maximum value the `workerId` field accepts in snowflakes.
@@ -80,152 +80,157 @@ declare var MAX_INCREMENT: number;
80
80
  * @group Main
81
81
  */
82
82
  declare class Snowflake {
83
- /**
84
- * Alias for {@link deconstruct}
85
- */
86
- decode: (id: string | bigint | Uint8Array) => DeconstructedSnowflake;
87
- /**
88
- * Internal reference of the epoch passed in the constructor
89
- * @internal
90
- */
91
- private readonly _epoch;
92
- /**
93
- * Internal incrementor for generating snowflakes
94
- * @internal
95
- */
96
- private _increment;
97
- /**
98
- * The process ID that will be used by default in the generate method
99
- * @internal
100
- */
101
- private _processId;
102
- /**
103
- * The worker ID that will be used by default in the generate method
104
- * @internal
105
- */
106
- private _workerId;
107
- /**
108
- * @internal
109
- */
110
- private _buffer;
111
- /**
112
- * @param epoch the epoch to use
113
- */
114
- constructor(opts: SnowflakeOptions | Date | number | bigint);
115
- /**
116
- * The epoch for this snowflake, as a number
117
- */
118
- get epoch(): number;
119
- /**
120
- * Gets the configured process ID
121
- */
122
- get processId(): number;
123
- /**
124
- * Sets the process ID that will be used by default for the {@link generate} method
125
- * @param value The new value, will be masked with `0b11111`
126
- */
127
- set processId(value: number | bigint);
128
- /**
129
- * Gets the configured worker ID
130
- */
131
- get workerId(): number;
132
- /**
133
- * Sets the worker ID that will be used by default for the {@link generate} method
134
- * @param value The new value, will be masked with `0b11111`
135
- */
136
- set workerId(value: number | bigint);
137
- /**
138
- * Get incrementor for generating snowflakes
139
- */
140
- get increment(): number;
141
- /**
142
- * Sets the incrementor for generating snowflakes that will be used by default for the {@link generate} method
143
- * @param value The new value
144
- */
145
- set increment(value: number | bigint);
146
- private _createSavePoint;
147
- /**
148
- * Sets most lowest values for generating snowflakes that will be used by default for the {@link generate} method
149
- */
150
- setLowest(): void;
151
- /**
152
- * Sets most highest values for generating snowflakes that will be used by default for the {@link generate} method
153
- */
154
- setHighest(): void;
155
- /**
156
- * Execute a function in context of most lowest values for generating snowflakes
157
- * @example
158
- * ```typescript
159
- * const epoch = new Date('2000-01-01T00:00:00.000Z');
160
- * const snowflake = new Snowflake();
161
- * const id = snowflake.withLowest((v) => v.generate(epoch)); // the lowest possible id for that epoch
162
- * ```
163
- */
164
- withLowest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
165
- /**
166
- * Execute a function in context of most highest values for generating snowflakes
167
- * @example
168
- * ```typescript
169
- * const epoch = new Date('2000-01-01T00:00:00.000Z');
170
- * const snowflake = new Snowflake();
171
- * const id = snowflake.withLowest((v) => v.generate(epoch)); // the highest possible id for that epoch
172
- * ```
173
- */
174
- withHighest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
175
- /**
176
- * Generates a Snowflake ID as bigint.
177
- */
178
- generate(timestamp?: Date | number): bigint;
179
- /**
180
- * Generates a Snowflake ID as a `Uint8Array` buffer.
181
- *
182
- * ⚠️ Returns a reference to the **same** internal `Uint8Array` instance.
183
- * Its contents may be overwritten on the next ID generation call.
184
- *
185
- * This method is intended for high-performance scenarios where you
186
- * immediately transform the buffer (e.g., to base62) before calling again.
187
- * Avoid storing or mutating the returned buffer directly.
188
- */
189
- generateBufferUnsafe(timestamp?: Date | number): Uint8Array;
190
- /**
191
- * Generates a snowflake given an epoch and optionally a timestamp
192
- * @example
193
- * ```typescript
194
- * const epoch = new Date('2000-01-01T00:00:00.000Z');
195
- * const snowflake = new Snowflake({ epoch }).generate();
196
- * ```
197
- * @returns A unique snowflake as Uint8Array
198
- */
199
- generateBuffer(timestamp?: Date | number): Uint8Array;
200
- /**
201
- * Deconstructs a snowflake given a snowflake ID
202
- * @param id the snowflake to deconstruct
203
- * @returns a deconstructed snowflake
204
- * @example
205
- * ```typescript
206
- * const epoch = new Date('2000-01-01T00:00:00.000Z');
207
- * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
208
- * ```
209
- */
210
- deconstruct(id: string | bigint | Uint8Array): DeconstructedSnowflake;
211
- /**
212
- * Retrieves the timestamp field's value from a snowflake.
213
- * @param id The snowflake to get the timestamp value from.
214
- * @returns The UNIX timestamp that is stored in `id`.
215
- */
216
- timestampFrom(id: string | bigint | Uint8Array): number;
217
- /**
218
- * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
219
- * snowflake in sort order.
220
- * @param a The first snowflake to compare.
221
- * @param b The second snowflake to compare.
222
- * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
223
- */
224
- static compare(a: string | bigint | Uint8Array, b: string | bigint | Uint8Array): -1 | 0 | 1;
225
- /**
226
- * Parse value as Uint8Array buffer
227
- */
228
- static bufferFrom(value: bigint | string): Uint8Array;
83
+ /**
84
+ * Alias for {@link deconstruct}
85
+ */
86
+ decode: (id: string | bigint | Uint8Array) => DeconstructedSnowflake;
87
+ /**
88
+ * Internal reference of the epoch passed in the constructor
89
+ * @internal
90
+ */
91
+ private readonly _epoch;
92
+ /**
93
+ * Internal incrementor for generating snowflakes
94
+ * @internal
95
+ */
96
+ private _increment;
97
+ /**
98
+ * The process ID that will be used by default in the generate method
99
+ * @internal
100
+ */
101
+ private _processId;
102
+ /**
103
+ * The worker ID that will be used by default in the generate method
104
+ * @internal
105
+ */
106
+ private _workerId;
107
+ /**
108
+ * @internal
109
+ */
110
+ private _timestamp;
111
+ private __buf;
112
+ private _packBuffer;
113
+ private _packBigInt;
114
+ private _unpackBuffer;
115
+ private _unpackBigInt;
116
+ /**
117
+ * @param epoch the epoch to use
118
+ */
119
+ constructor(opts: SnowflakeOptions | Date | number | bigint);
120
+ /**
121
+ * The epoch for this snowflake, as a number
122
+ */
123
+ get epoch(): number;
124
+ /**
125
+ * Gets the configured process ID
126
+ */
127
+ get processId(): number;
128
+ /**
129
+ * Sets the process ID that will be used by default for the {@link generate} method
130
+ * @param value The new value, will be masked with `0b11111`
131
+ */
132
+ set processId(value: number | bigint);
133
+ /**
134
+ * Gets the configured worker ID
135
+ */
136
+ get workerId(): number;
137
+ /**
138
+ * Sets the worker ID that will be used by default for the {@link generate} method
139
+ * @param value The new value, will be masked with `0b11111`
140
+ */
141
+ set workerId(value: number | bigint);
142
+ /**
143
+ * Get incrementor for generating snowflakes
144
+ */
145
+ get increment(): number;
146
+ /**
147
+ * Sets the incrementor for generating snowflakes that will be used by default for the {@link generate} method
148
+ * @param value The new value
149
+ */
150
+ set increment(value: number | bigint);
151
+ private _createSavePoint;
152
+ /**
153
+ * Sets most lowest values for generating snowflakes that will be used by default for the {@link generate} method
154
+ */
155
+ setLowest(): void;
156
+ /**
157
+ * Sets most highest values for generating snowflakes that will be used by default for the {@link generate} method
158
+ */
159
+ setHighest(): void;
160
+ /**
161
+ * Execute a function in context of most lowest values for generating snowflakes
162
+ * @example
163
+ * ```typescript
164
+ * const epoch = new Date('2000-01-01T00:00:00.000Z');
165
+ * const snowflake = new Snowflake();
166
+ * const id = snowflake.withLowest((v) => v.generate(epoch)); // the lowest possible id for that epoch
167
+ * ```
168
+ */
169
+ withLowest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
170
+ /**
171
+ * Execute a function in context of most highest values for generating snowflakes
172
+ * @example
173
+ * ```typescript
174
+ * const epoch = new Date('2000-01-01T00:00:00.000Z');
175
+ * const snowflake = new Snowflake();
176
+ * const id = snowflake.withLowest((v) => v.generate(epoch)); // the highest possible id for that epoch
177
+ * ```
178
+ */
179
+ withHighest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
180
+ /**
181
+ * Generates a Snowflake ID as bigint.
182
+ */
183
+ generate(timestamp?: Date | number): bigint;
184
+ /**
185
+ * Generates a Snowflake ID as a `Uint8Array` buffer.
186
+ *
187
+ * ⚠️ Returns a reference to the **same** internal `Uint8Array` instance.
188
+ * Its contents may be overwritten on the next ID generation call.
189
+ *
190
+ * This method is intended for high-performance scenarios where you
191
+ * immediately transform the buffer (e.g., to base62) before calling again.
192
+ * Avoid storing or mutating the returned buffer directly.
193
+ */
194
+ generateBufferUnsafe(timestamp?: Date | number): Uint8Array;
195
+ /**
196
+ * Generates a snowflake given an epoch and optionally a timestamp
197
+ * @example
198
+ * ```typescript
199
+ * const epoch = new Date('2000-01-01T00:00:00.000Z');
200
+ * const snowflake = new Snowflake({ epoch }).generate();
201
+ * ```
202
+ * @returns A unique snowflake as Uint8Array
203
+ */
204
+ generateBuffer(timestamp?: Date | number): Uint8Array;
205
+ /**
206
+ * Deconstructs a snowflake given a snowflake ID
207
+ * @param id the snowflake to deconstruct
208
+ * @returns a deconstructed snowflake
209
+ * @example
210
+ * ```typescript
211
+ * const epoch = new Date('2000-01-01T00:00:00.000Z');
212
+ * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
213
+ * ```
214
+ */
215
+ deconstruct(id: string | bigint | Uint8Array): DeconstructedSnowflake;
216
+ /**
217
+ * Retrieves the timestamp field's value from a snowflake.
218
+ * @param id The snowflake to get the timestamp value from.
219
+ * @returns The UNIX timestamp that is stored in `id`.
220
+ */
221
+ timestampFrom(id: string | bigint | Uint8Array): number;
222
+ /**
223
+ * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
224
+ * snowflake in sort order.
225
+ * @param a The first snowflake to compare.
226
+ * @param b The second snowflake to compare.
227
+ * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
228
+ */
229
+ static compare(a: string | bigint | Uint8Array, b: string | bigint | Uint8Array): -1 | 0 | 1;
230
+ /**
231
+ * Parse value as Uint8Array buffer
232
+ */
233
+ static bufferFrom(value: bigint | string): Uint8Array;
229
234
  }
230
-
231
- export { type DeconstructedSnowflake, MAX_INCREMENT, MAX_PROCESS_ID, MAX_WORKER_ID, Snowflake, type SnowflakeOptions };
235
+ export { DeconstructedSnowflake, MAX_INCREMENT, MAX_PROCESS_ID, MAX_WORKER_ID, Snowflake, SnowflakeOptions };
236
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":["id","timestamp","workerId","processId","increment","epoch","Date","decode","Uint8Array","DeconstructedSnowflake","_epoch","_increment","_processId","_workerId","_timestamp","__buf","_packBuffer","_packBigInt","_unpackBuffer","_unpackBigInt","constructor","SnowflakeOptions","opts","value","_createSavePoint","setLowest","setHighest","withLowest","Result","Snowflake","instance","fn","withHighest","generate","generateBufferUnsafe","generateBuffer","deconstruct","timestampFrom","compare","a","b","bufferFrom"],"sources":["../src/Snowflake.d.ts"],"mappings":"AAGA;;;AAAA,UAAiB,sBAAA;EAIbA;;;EAAAA,EAAAA;EAgBAI;;;EAZAH,SAAAA;EAqBa;;;EAjBbC,QAAAA;EAqBAG;;;EAjBAF,SAAAA;EAiCAA;;AAAS;EA7BTC,SAAAA;EAkCoC;;;EA9BpCC,KAAAA;AAAAA;;;;UAKa,gBAAA;EAiCE;;;EA7BfA,KAAAA,oBAAyB,IAAI;EA6BO;AAgBxC;;;;EAvCID,SAAAA;EA4EkB;;;;EAvElBF,QAAAA;EAmI+C;;;;EA9H/CC,SAAAA;AAAAA;;;;YAKe,aAAA;;;;YAIA,cAAA;;;;YAIA,aAAA;;;;;;;;;;;;;;;;cAgBE,SAAA;EAyCbE;;;EArCJE,MAAAA,GAASP,EAAAA,oBAAsB,UAAA,KAAe,sBAAA;EAkD1CE;;;;EAAAA,iBA7CaQ,MAAAA;EA2DHa;;;;EAAAA,QAtDNZ,UAAAA;EAyEGiB;;;;EAAAA,QApEHhB,UAAAA;EAoE6DgB;;;;EAAAA,QA/D7Df,SAAAA;EAyEqDe;;;EAAAA,QArErDd,UAAAA;EAAAA,QACAC,KAAAA;EAAAA,QACAC,WAAAA;EAAAA,QACAC,WAAAA;EAAAA,QACAC,aAAAA;EAAAA,QACAC,aAAAA;EA+EyCX;;;EA3Ej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