@journium/react 1.1.2 → 1.1.3

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.mjs CHANGED
@@ -3,7 +3,7 @@ import React, { createContext, useState, useEffect, useContext, useCallback } fr
3
3
  /**
4
4
  * uuidv7: A JavaScript implementation of UUID version 7
5
5
  *
6
- * Copyright 2021-2024 LiosK
6
+ * Copyright 2021-2025 LiosK
7
7
  *
8
8
  * @license Apache-2.0
9
9
  * @packageDocumentation
@@ -232,7 +232,10 @@ class V7Generator {
232
232
  * number generator should be cryptographically strong and securely seeded.
233
233
  */
234
234
  constructor(randomNumberGenerator) {
235
- this.timestamp = 0;
235
+ /**
236
+ * Biased by one to distinguish zero (uninitialized) and zero (UNIX epoch).
237
+ */
238
+ this.timestamp_biased = 0;
236
239
  this.counter = 0;
237
240
  this.random = randomNumberGenerator !== null && randomNumberGenerator !== void 0 ? randomNumberGenerator : getDefaultRandom();
238
241
  }
@@ -278,13 +281,13 @@ class V7Generator {
278
281
  *
279
282
  * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
280
283
  * considered significant. A suggested value is `10_000` (milliseconds).
281
- * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
284
+ * @throws RangeError if `unixTsMs` is not a 48-bit unsigned integer.
282
285
  */
283
286
  generateOrResetCore(unixTsMs, rollbackAllowance) {
284
287
  let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
285
288
  if (value === undefined) {
286
289
  // reset state and resume
287
- this.timestamp = 0;
290
+ this.timestamp_biased = 0;
288
291
  value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
289
292
  }
290
293
  return value;
@@ -298,28 +301,29 @@ class V7Generator {
298
301
  *
299
302
  * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
300
303
  * considered significant. A suggested value is `10_000` (milliseconds).
301
- * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
304
+ * @throws RangeError if `unixTsMs` is not a 48-bit unsigned integer.
302
305
  */
303
306
  generateOrAbortCore(unixTsMs, rollbackAllowance) {
304
307
  const MAX_COUNTER = 4398046511103;
305
308
  if (!Number.isInteger(unixTsMs) ||
306
- unixTsMs < 1 ||
309
+ unixTsMs < 0 ||
307
310
  unixTsMs > 281474976710655) {
308
- throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
311
+ throw new RangeError("`unixTsMs` must be a 48-bit unsigned integer");
309
312
  }
310
313
  else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
311
314
  throw new RangeError("`rollbackAllowance` out of reasonable range");
312
315
  }
313
- if (unixTsMs > this.timestamp) {
314
- this.timestamp = unixTsMs;
316
+ unixTsMs++;
317
+ if (unixTsMs > this.timestamp_biased) {
318
+ this.timestamp_biased = unixTsMs;
315
319
  this.resetCounter();
316
320
  }
317
- else if (unixTsMs + rollbackAllowance >= this.timestamp) {
321
+ else if (unixTsMs + rollbackAllowance >= this.timestamp_biased) {
318
322
  // go on with previous timestamp if new one is not much smaller
319
323
  this.counter++;
320
324
  if (this.counter > MAX_COUNTER) {
321
325
  // increment timestamp at counter overflow
322
- this.timestamp++;
326
+ this.timestamp_biased++;
323
327
  this.resetCounter();
324
328
  }
325
329
  }
@@ -327,7 +331,7 @@ class V7Generator {
327
331
  // abort if clock went backwards to unbearable extent
328
332
  return undefined;
329
333
  }
330
- return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & (2 ** 30 - 1), this.random.nextUint32());
334
+ return UUID.fromFieldsV7(this.timestamp_biased - 1, Math.trunc(this.counter / 2 ** 30), this.counter & (2 ** 30 - 1), this.random.nextUint32());
331
335
  }
332
336
  /** Initializes the counter at a 42-bit random integer. */
333
337
  resetCounter() {