deno.land / std@0.201.0 / semver / parse_range.ts

parse_range.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { ALL } from "./constants.ts";import type { SemVerRange } from "./types.ts";import { CARET, HYPHENRANGE, re, STAR, TILDE, XRANGE } from "./_shared.ts";import { parseComparator } from "./parse_comparator.ts";
// ~, ~> --> * (any, kinda silly)// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0function replaceTildes(comp: string): string { return comp .trim() .split(/\s+/) .map((comp) => replaceTilde(comp)) .join(" ");}
function replaceTilde(comp: string): string { const r: RegExp = re[TILDE]; return comp.replace( r, (_: string, M: string, m: string, p: string, pr: string) => { let ret: string;
if (isX(M)) { ret = ""; } else if (isX(m)) { ret = ">=" + M + ".0.0 <" + (+M + 1) + ".0.0"; } else if (isX(p)) { // ~1.2 == >=1.2.0 <1.3.0 ret = ">=" + M + "." + m + ".0 <" + M + "." + (+m + 1) + ".0"; } else if (pr) { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + M + "." + (+m + 1) + ".0"; } else { // ~1.2.3 == >=1.2.3 <1.3.0 ret = ">=" + M + "." + m + "." + p + " <" + M + "." + (+m + 1) + ".0"; }
return ret; }, );}
// ^ --> * (any, kinda silly)// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0// ^1.2.3 --> >=1.2.3 <2.0.0// ^1.2.0 --> >=1.2.0 <2.0.0function replaceCarets(comp: string): string { return comp .trim() .split(/\s+/) .map((comp) => replaceCaret(comp)) .join(" ");}
function replaceCaret(comp: string): string { const r: RegExp = re[CARET]; return comp.replace(r, (_: string, M, m, p, pr) => { let ret: string;
if (isX(M)) { ret = ""; } else if (isX(m)) { ret = ">=" + M + ".0.0 <" + (+M + 1) + ".0.0"; } else if (isX(p)) { if (M === "0") { ret = ">=" + M + "." + m + ".0 <" + M + "." + (+m + 1) + ".0"; } else { ret = ">=" + M + "." + m + ".0 <" + (+M + 1) + ".0.0"; } } else if (pr) { if (M === "0") { if (m === "0") { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + M + "." + m + "." + (+p + 1); } else { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + M + "." + (+m + 1) + ".0"; } } else { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + (+M + 1) + ".0.0"; } } else { if (M === "0") { if (m === "0") { ret = ">=" + M + "." + m + "." + p + " <" + M + "." + m + "." + (+p + 1); } else { ret = ">=" + M + "." + m + "." + p + " <" + M + "." + (+m + 1) + ".0"; } } else { ret = ">=" + M + "." + m + "." + p + " <" + (+M + 1) + ".0.0"; } }
return ret; });}
function replaceXRanges(comp: string): string { return comp .split(/\s+/) .map((comp) => replaceXRange(comp)) .join(" ");}
function replaceXRange(comp: string): string { comp = comp.trim(); const r: RegExp = re[XRANGE]; return comp.replace(r, (ret: string, gtlt, M, m, p, _pr) => { const xM: boolean = isX(M); const xm: boolean = xM || isX(m); const xp: boolean = xm || isX(p); const anyX: boolean = xp;
if (gtlt === "=" && anyX) { gtlt = ""; }
if (xM) { if (gtlt === ">" || gtlt === "<") { // nothing is allowed ret = "<0.0.0"; } else { // nothing is forbidden ret = "*"; } } else if (gtlt && anyX) { // we know patch is an x, because we have any x at all. // replace X with 0 if (xm) { m = 0; } p = 0;
if (gtlt === ">") { // >1 => >=2.0.0 // >1.2 => >=1.3.0 // >1.2.3 => >= 1.2.4 gtlt = ">="; if (xm) { M = +M + 1; m = 0; p = 0; } else { m = +m + 1; p = 0; } } else if (gtlt === "<=") { // <=0.7.x is actually <0.8.0, since any 0.7.x should // pass. Similarly, <=7.x is actually <8.0.0, etc. gtlt = "<"; if (xm) { M = +M + 1; } else { m = +m + 1; } }
ret = gtlt + M + "." + m + "." + p; } else if (xm) { ret = ">=" + M + ".0.0 <" + (+M + 1) + ".0.0"; } else if (xp) { ret = ">=" + M + "." + m + ".0 <" + M + "." + (+m + 1) + ".0"; }
return ret; });}
// Because * is AND-ed with everything else in the comparator,// and '' means "any version", just remove the *s entirely.function replaceStars(comp: string): string { return comp.trim().replace(re[STAR], "");}
// This function is passed to string.replace(re[HYPHENRANGE])// M, m, patch, prerelease, build// 1.2 - 3.4.5 -> >=1.2.0 <=3.4.5// 1.2.3 - 3.4 -> >=1.2.0 <3.5.0 Any 3.4.x will do// 1.2 - 3.4 -> >=1.2.0 <3.5.0function hyphenReplace( _$0: string, from: string, fM: string, fm: string, fp: string, _fpr: string, _fb: string, to: string, tM: string, tm: string, tp: string, tpr: string, _tb: string,) { if (isX(fM)) { from = ""; } else if (isX(fm)) { from = ">=" + fM + ".0.0"; } else if (isX(fp)) { from = ">=" + fM + "." + fm + ".0"; } else { from = ">=" + from; }
if (isX(tM)) { to = ""; } else if (isX(tm)) { to = "<" + (+tM + 1) + ".0.0"; } else if (isX(tp)) { to = "<" + tM + "." + (+tm + 1) + ".0"; } else if (tpr) { to = "<=" + tM + "." + tm + "." + tp + "-" + tpr; } else { to = "<=" + to; }
return (from + " " + to).trim();}
function isX(id: string): boolean { return !id || id.toLowerCase() === "x" || id === "*";}
/** * Parses a range string into a SemVerRange object or throws a TypeError. * @param range The range string * @returns A valid semantic version range */export function parseRange(range: string): SemVerRange { // handle spaces around and between comparator and version range = range.trim().replaceAll(/(?<=<|>|=) /g, "");
if (range === "") { return { ranges: [[ALL]] }; }
// Split into groups of comparators, these are considered OR'd together. const ranges = range .trim() .split(/\s*\|\|\s*/) .map((range) => { // convert `1.2.3 - 1.2.4` into `>=1.2.3 <=1.2.4` const hr: RegExp = re[HYPHENRANGE]; range = range.replace(hr, hyphenReplace); range = replaceCarets(range); range = replaceTildes(range); range = replaceXRanges(range); range = replaceStars(range);
// At this point, the range is completely trimmed and // ready to be split into comparators. // These are considered AND's if (range === "") { return [ALL]; } else { return range .split(" ") .filter((r) => r) .map((r) => parseComparator(r)); } });
return { ranges };}
std

Version Info

Tagged at
a year ago