Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

semver.ts

223 lines6.4 KB

1interface ParsedSemver {
2 readonly major: number;
3 readonly minor: number;
4 readonly patch: number;
5 readonly prerelease: ReadonlyArray<string>;
6}
7​
8const SEMVER_NUMBER_SEGMENT = /^\d+$/;
9​
10export function normalizeSemverVersion(version: string): string {
11 const [main, prerelease] = version.trim().split("-", 2);
12 const segments: string[] = [];
13 for (const segment of (main ?? "").split(".")) {
14 const trimmed = segment.trim();
15 if (trimmed.length > 0) {
16 segments.push(trimmed);
17 }
18 }
19​
20 while (segments.length > 0 && segments.length < 3) {
21 segments.push("0");
22 }
23​
24 return prerelease ? `${segments.join(".")}-${prerelease}` : segments.join(".");
26​
27export function parseSemver(value: string): ParsedSemver | null {
28 const normalized = normalizeSemverVersion(value).replace(/^v/, "");
29 const [main = "", prerelease] = normalized.split("-", 2);
30 const segments = main.split(".");
31 if (segments.length !== 3) {
32 return null;
33 }
34​
35 const [majorSegment, minorSegment, patchSegment] = segments;
36 if (majorSegment === undefined || minorSegment === undefined || patchSegment === undefined) {
37 return null;
38 }
39 if (
40 !SEMVER_NUMBER_SEGMENT.test(majorSegment) ||
41 !SEMVER_NUMBER_SEGMENT.test(minorSegment) ||
42 !SEMVER_NUMBER_SEGMENT.test(patchSegment)
43 ) {
44 return null;
45 }
46​
47 const major = Number.parseInt(majorSegment, 10);
48 const minor = Number.parseInt(minorSegment, 10);
49 const patch = Number.parseInt(patchSegment, 10);
50 if (![major, minor, patch].every(Number.isInteger)) {
51 return null;
52 }
53​
54 return {
55 major,
56 minor,
57 patch,
58 prerelease: parsePrereleaseSegments(prerelease),
59 };
61​
62function parsePrereleaseSegments(prerelease: string | undefined): ReadonlyArray<string> {
63 if (prerelease === undefined) {
64 return [];
65 }
66 const segments: string[] = [];
67 for (const segment of prerelease.split(".")) {
68 const trimmed = segment.trim();
69 if (trimmed.length > 0) {
70 segments.push(trimmed);
71 }
72 }
73 return segments;
75​
76function comparePrereleaseIdentifier(left: string, right: string): number {
77 const leftNumeric = SEMVER_NUMBER_SEGMENT.test(left);
78 const rightNumeric = SEMVER_NUMBER_SEGMENT.test(right);
79​
80 if (leftNumeric && rightNumeric) {
81 return Number.parseInt(left, 10) - Number.parseInt(right, 10);
82 }
83 if (leftNumeric) {
84 return -1;
85 }
86 if (rightNumeric) {
87 return 1;
88 }
89 return left.localeCompare(right);
91​
92export function compareSemverVersions(left: string, right: string): number {
93 const parsedLeft = parseSemver(left);
94 const parsedRight = parseSemver(right);
95 if (!parsedLeft || !parsedRight) {
96 return left.localeCompare(right);
97 }
98​
99 if (parsedLeft.major !== parsedRight.major) {
100 return parsedLeft.major - parsedRight.major;
101 }
102 if (parsedLeft.minor !== parsedRight.minor) {
103 return parsedLeft.minor - parsedRight.minor;
104 }
105 if (parsedLeft.patch !== parsedRight.patch) {
106 return parsedLeft.patch - parsedRight.patch;
107 }
108​
109 if (parsedLeft.prerelease.length === 0 && parsedRight.prerelease.length === 0) {
110 return 0;
111 }
112 if (parsedLeft.prerelease.length === 0) {
113 return 1;
114 }
115 if (parsedRight.prerelease.length === 0) {
116 return -1;
117 }
118​
119 const length = Math.max(parsedLeft.prerelease.length, parsedRight.prerelease.length);
120 for (let index = 0; index < length; index += 1) {
121 const leftIdentifier = parsedLeft.prerelease[index];
122 const rightIdentifier = parsedRight.prerelease[index];
123 if (leftIdentifier === undefined) {
124 return -1;
125 }
126 if (rightIdentifier === undefined) {
127 return 1;
128 }
129 const comparison = comparePrereleaseIdentifier(leftIdentifier, rightIdentifier);
130 if (comparison !== 0) {
131 return comparison;
132 }
133 }
134​
135 return 0;
137​
138/**
139 * Keep the function body valid plain JavaScript: SSH startup stringifies this
140 * function and runs it on remote Node versions before TypeScript support is known.
141 */
142export const satisfiesSemverRange: (rawVersion: string, range: string) => boolean =
143 function satisfiesSemverRange(rawVersion, range) {
144 const normalizedVersion = String(rawVersion).trim().replace(/^v/, "");
145 const versionMatch = normalizedVersion.match(
146 /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-[0-9A-Za-z.-]+)?$/,
147 );
148 if (!versionMatch) {
149 return false;
150 }
151​
152 const version = {
153 major: Number(versionMatch[1]),
154 minor: Number(versionMatch[2] || 0),
155 patch: Number(versionMatch[3] || 0),
156 };
157​
158 return range.split("||").some((group) => {
159 const comparators = group.trim().split(/\s+/).filter(Boolean);
160 if (comparators.length === 0) {
161 return false;
162 }
163 return comparators.every((comparator) => {
164 const match = comparator.trim().match(/^(\^|>=|>|<=|<|=)?\s*v?(\d+(?:\.\d+){0,2})$/);
165 if (!match) {
166 return false;
167 }
168 const targetVersion = match[2];
169 if (targetVersion === undefined) {
170 return false;
171 }
172 const targetMatch = targetVersion.match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?$/);
173 if (!targetMatch) {
174 return false;
175 }
176 const target = {
177 major: Number(targetMatch[1]),
178 minor: Number(targetMatch[2] || 0),
179 patch: Number(targetMatch[3] || 0),
180 };
181 const compared =
182 version.major !== target.major
183 ? version.major > target.major
184 ? 1
185 : -1
186 : version.minor !== target.minor
187 ? version.minor > target.minor
188 ? 1
189 : -1
190 : version.patch !== target.patch
191 ? version.patch > target.patch
192 ? 1
193 : -1
194 : 0;
195 const operator = match[1] || "=";
196 switch (operator) {
197 case "^":
198 if (compared < 0) {
199 return false;
200 }
201 if (target.major > 0) {
202 return version.major === target.major;
203 }
204 if (target.minor > 0) {
205 return version.major === 0 && version.minor === target.minor;
206 }
207 return version.major === 0 && version.minor === 0 && version.patch === target.patch;
208 case ">=":
209 return compared >= 0;
210 case ">":
211 return compared > 0;
212 case "<=":
213 return compared <= 0;
214 case "<":
215 return compared < 0;
216 case "=":
217 return compared === 0;
218 default:
219 return false;
220 }
221 });
222 });
223 };