diff options
| author | netop://ウィビ <paul@webb.page> | 2026-10-17 16:29:19 -0700 |
|---|---|---|
| committer | netop://ウィビ <paul@webb.page> | 2026-10-17 16:29:19 -0700 |
| commit | 14279df2e5affd44f957f5b52ad5d6267e31fa57 (patch) | |
| tree | fad5858161a34a4adc163a54c334d37c0a7529a0 /mod.ts | |
| parent | 6fdfb6f5758b354861d837982ab9de76020739b2 (diff) | |
| download | chronver-14279df2e5affd44f957f5b52ad5d6267e31fa57.tar.gz chronver-14279df2e5affd44f957f5b52ad5d6267e31fa57.zip | |
stricter parsing - v3.0.0 - fixes CLI changeset flag, padded year, debug output - updates deno.json permissionsHEADprimary
Diffstat (limited to 'mod.ts')
| -rw-r--r-- | mod.ts | 78 |
1 files changed, 43 insertions, 35 deletions
@@ -5,7 +5,8 @@ export class ChronVer { private static readonly DATE_REGEX = /^(\d{4})\.(\d{2})\.(\d{2})$/; - private static readonly VERSION_REGEX = /^(\d{4})\.(?:0[1-9]|1[0-2])\.(?:0[1-9]|[12]\d|3[01])(?:\.(\d+))?(?:-(break|[a-zA-Z0-9-]+)(?:\.(\d+))?)?$/; + private static readonly FEATURE_REGEX = /^-*[a-zA-Z0-9][a-zA-Z0-9-]*$/; /*** at least one letter or digit ***/ + private static readonly VERSION_REGEX = /^(\d{4})\.(?:0[1-9]|1[0-2])\.(?:0[1-9]|[12]\d|3[01])(?:\.([1-9]\d*))?(?:-(break|-*[a-zA-Z0-9][a-zA-Z0-9-]*))?$/; readonly changeset: number; /*** changeset number (0 if not specified) ***/ readonly day: number; /*** day component (1-31) ***/ @@ -15,7 +16,7 @@ export class ChronVer { readonly year: number; /*** year component ***/ constructor(version?: string) { - if (!version) { + if (version === undefined) { /*** only undefined means today, "" is invalid ***/ const now = new Date(); this.changeset = 0; @@ -51,7 +52,7 @@ export class ChronVer { if (!match) throw new ChronVerError(`Invalid ChronVer format: "${version}"`); - const [, yearStr, changesetStr, label, featureChangeset] = match; + const [, yearStr, changesetStr, label] = match; const parts = version.split("."); if (parts.length < 3) @@ -62,21 +63,16 @@ export class ChronVer { const day = parseInt(parts[2], 10); const isBreaking = label === "break"; - let changeset = changesetStr ? + const changeset = changesetStr ? parseInt(changesetStr, 10) : 0; - let feature: string | undefined; + if (!Number.isSafeInteger(changeset)) + throw new ChronVerError(`Changeset must be at most ${Number.MAX_SAFE_INTEGER}`); - if (label && !isBreaking) { - feature = label; - - if (!changeset) { - changeset = featureChangeset ? - parseInt(featureChangeset, 10) : - 0; - } - } + const feature = label && !isBreaking ? + label : + undefined; return { changeset, @@ -89,8 +85,8 @@ export class ChronVer { } private validate(): void { - if (this.year < 1) - throw new ChronVerError("Year must be positive"); + if (this.year < 1 || this.year > 9999) + throw new ChronVerError("Year must be between 1 and 9999"); if (this.month < 1 || this.month > 12) throw new ChronVerError("Month must be between 1 and 12"); @@ -100,11 +96,11 @@ export class ChronVer { if (this.day < 1 || this.day > maxDays) throw new ChronVerError(`Day must be between 1 and ${maxDays} for ${this.year}-${this.month.toString().padStart(2, "0")}`); - if (this.changeset < 0) - throw new ChronVerError("Changeset cannot be negative"); + if (!Number.isSafeInteger(this.changeset) || this.changeset < 0) + throw new ChronVerError("Changeset must be a non-negative integer"); - if (this.feature && !/^[a-zA-Z0-9-]+$/.test(this.feature)) - throw new ChronVerError("Feature name must contain only alphanumeric characters and hyphens"); + if (this.feature !== undefined && (!ChronVer.FEATURE_REGEX.test(this.feature) || this.feature === "break")) + throw new ChronVerError(`Feature name must contain only alphanumeric characters and hyphens (at least one alphanumeric) and cannot be "break"`); } //// public methods @@ -135,20 +131,20 @@ export class ChronVer { } getDateString(): string { - return `${this.year}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}`; + return `${this.year.toString().padStart(4, "0")}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}`; } increment(): ChronVer { - const today = new Date(); + const today = new ChronVer(); + const dateDiff = (this.year - today.year) || (this.month - today.month) || (this.day - today.day); - const isToday = this.year === today.getFullYear() && - this.month === (today.getMonth() + 1) && - this.day === today.getDate(); + if (dateDiff > 0) + throw new ChronVerError(`Cannot increment ${this.toString()}: its date is after today (${today.getDateString()})`); - if (isToday) /*** already ran today, increment changeset ***/ - return new ChronVer(`${this.year}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}.${this.changeset + 1}`); + if (dateDiff === 0) /*** already ran today, increment changeset (drops feature and -break) ***/ + return new ChronVer(`${this.getDateString()}.${this.changeset + 1}`); - return new ChronVer(); + return today; } isNewerThan(other: ChronVer): boolean { @@ -160,8 +156,7 @@ export class ChronVer { } toString(): string { - const base = `${this.year}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}`; - let result = base; + let result = this.getDateString(); if (this.changeset > 0) result += `.${this.changeset}`; @@ -189,7 +184,16 @@ export class ChronVer { } static fromDate(date: Date, changeset: number = 0): ChronVer { - const dateStr = `${date.getFullYear()}.${(date.getMonth() + 1).toString().padStart(2, "0")}.${date.getDate().toString().padStart(2, "0")}`; + if (Number.isNaN(date.getTime())) + throw new ChronVerError("Invalid date"); + + if (date.getFullYear() < 1 || date.getFullYear() > 9999) + throw new ChronVerError("Year must be between 1 and 9999"); + + if (!Number.isSafeInteger(changeset) || changeset < 0) + throw new ChronVerError("Changeset must be a non-negative integer"); + + const dateStr = `${date.getFullYear().toString().padStart(4, "0")}.${(date.getMonth() + 1).toString().padStart(2, "0")}.${date.getDate().toString().padStart(2, "0")}`; return new ChronVer(changeset > 0 ? `${dateStr}.${changeset}` : dateStr); } @@ -254,10 +258,14 @@ export class ChronVer { } static sort(versions: string[], descending: boolean = false): string[] { - return versions.slice().sort((a, b) => { - const result = ChronVer.compare(a, b); - return descending ? -result : result; - }); + const parsed = versions.map(version => ({ version, chronVer: new ChronVer(version) })); /*** validate every input up front ***/ + + return parsed + .sort((a, b) => { + const result = a.chronVer.compare(b.chronVer); + return descending ? -result : result; + }) + .map(({ version }) => version); } } |