Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: zero not being a valid time #14

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class UUID {
* generated UUIDs. See their respective documentation for details.
*/
export class V7Generator {
private timestamp = 0;
private timestamp: number | undefined;
private counter = 0;

/** The random number generator used by the generator. */
Expand Down Expand Up @@ -312,13 +312,13 @@ export class V7Generator {
*
* @param rollbackAllowance - The amount of `unixTsMs` rollback that is
* considered significant. A suggested value is `10_000` (milliseconds).
* @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
* @throws RangeError if `unixTsMs` is not a 48-bit positive integer or zero.
*/
generateOrResetCore(unixTsMs: number, rollbackAllowance: number): UUID {
let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
if (value === undefined) {
// reset state and resume
this.timestamp = 0;
this.timestamp = undefined;
value = this.generateOrAbortCore(unixTsMs, rollbackAllowance)!;
}
return value;
Expand All @@ -333,7 +333,7 @@ export class V7Generator {
*
* @param rollbackAllowance - The amount of `unixTsMs` rollback that is
* considered significant. A suggested value is `10_000` (milliseconds).
* @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
* @throws RangeError if `unixTsMs` is not a 48-bit positive integer or zero.
*/
generateOrAbortCore(
unixTsMs: number,
Expand All @@ -343,15 +343,15 @@ export class V7Generator {

if (
!Number.isInteger(unixTsMs) ||
unixTsMs < 1 ||
unixTsMs < 0 ||
unixTsMs > 0xffff_ffff_ffff
) {
throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
} else if (rollbackAllowance < 0 || rollbackAllowance > 0xffff_ffff_ffff) {
throw new RangeError("`rollbackAllowance` out of reasonable range");
}

if (unixTsMs > this.timestamp) {
if (this.timestamp === undefined || unixTsMs > this.timestamp) {
this.timestamp = unixTsMs;
this.resetCounter();
} else if (unixTsMs + rollbackAllowance >= this.timestamp) {
Expand Down