forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_shared.ts
289 lines (247 loc) · 7.14 KB
/
_shared.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { Operator } from "./types.ts";
export function compareNumber(
a: number,
b: number,
): 1 | 0 | -1 {
if (isNaN(a) || isNaN(b)) {
throw new Error("Comparison against non-numbers");
}
return a === b ? 0 : a < b ? -1 : 1;
}
export function checkIdentifier(
v1: ReadonlyArray<string | number>,
v2: ReadonlyArray<string | number>,
): 1 | 0 | -1 {
// NOT having a prerelease is > having one
// But NOT having a build is < having one
if (v1.length && !v2.length) {
return -1;
} else if (!v1.length && v2.length) {
return 1;
} else {
return 0;
}
}
export function compareIdentifier(
v1: ReadonlyArray<string | number>,
v2: ReadonlyArray<string | number>,
): 1 | 0 | -1 {
let i = 0;
do {
const a = v1[i];
const b = v2[i];
if (a === undefined && b === undefined) {
// same length is equal
return 0;
} else if (b === undefined) {
// longer > shorter
return 1;
} else if (a === undefined) {
// shorter < longer
return -1;
} else if (typeof a === "string" && typeof b === "number") {
// string > number
return 1;
} else if (typeof a === "number" && typeof b === "string") {
// number < string
return -1;
} else if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
// If they're equal, continue comparing segments.
continue;
}
} while (++i);
// It can't ever reach here, but typescript doesn't realize that so
// add this line so the return type is inferred correctly.
return 0;
}
// The actual regexps
const re: RegExp[] = [];
const src: string[] = [];
let R = 0;
// The following Regular Expressions can be used for tokenizing,
// validating, and parsing SemVer version strings.
// ## Numeric Identifier
// A single `0`, or a non-zero digit followed by zero or more digits.
const NUMERICIDENTIFIER: number = R++;
src[NUMERICIDENTIFIER] = "0|[1-9]\\d*";
// ## Non-numeric Identifier
// Zero or more digits, followed by a letter or hyphen, and then zero or
// more letters, digits, or hyphens.
const NONNUMERICIDENTIFIER: number = R++;
src[NONNUMERICIDENTIFIER] = "\\d*[a-zA-Z-][a-zA-Z0-9-]*";
// ## Main Version
// Three dot-separated numeric identifiers.
const MAINVERSION: number = R++;
const nid = src[NUMERICIDENTIFIER];
src[MAINVERSION] = `(${nid})\\.(${nid})\\.(${nid})`;
// ## Pre-release Version Identifier
// A numeric identifier, or a non-numeric identifier.
const PRERELEASEIDENTIFIER: number = R++;
src[PRERELEASEIDENTIFIER] = "(?:" + src[NUMERICIDENTIFIER] + "|" +
src[NONNUMERICIDENTIFIER] + ")";
// ## Pre-release Version
// Hyphen, followed by one or more dot-separated pre-release version
// identifiers.
const PRERELEASE: number = R++;
src[PRERELEASE] = "(?:-(" +
src[PRERELEASEIDENTIFIER] +
"(?:\\." +
src[PRERELEASEIDENTIFIER] +
")*))";
// ## Build Metadata Identifier
// Any combination of digits, letters, or hyphens.
const BUILDIDENTIFIER: number = R++;
src[BUILDIDENTIFIER] = "[0-9A-Za-z-]+";
// ## Build Metadata
// Plus sign, followed by one or more period-separated build metadata
// identifiers.
const BUILD: number = R++;
src[BUILD] = "(?:\\+(" + src[BUILDIDENTIFIER] + "(?:\\." +
src[BUILDIDENTIFIER] + ")*))";
// ## Full Version String
// A main version, followed optionally by a pre-release version and
// build metadata.
// Note that the only major, minor, patch, and pre-release sections of
// the version string are capturing groups. The build metadata is not a
// capturing group, because it should not ever be used in version
// comparison.
const FULL: number = R++;
const FULLPLAIN = "v?" + src[MAINVERSION] + src[PRERELEASE] + "?" + src[BUILD] +
"?";
src[FULL] = "^" + FULLPLAIN + "$";
const GTLT: number = R++;
src[GTLT] = "((?:<|>)?=?)";
// Something like "2.*" or "1.2.x".
// Note that "x.x" is a valid xRange identifier, meaning "any version"
// Only the first item is strictly required.
const XRANGEIDENTIFIER: number = R++;
src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + "|x|X|\\*";
const XRANGEPLAIN: number = R++;
src[XRANGEPLAIN] = "[v=\\s]*(" +
src[XRANGEIDENTIFIER] +
")" +
"(?:\\.(" +
src[XRANGEIDENTIFIER] +
")" +
"(?:\\.(" +
src[XRANGEIDENTIFIER] +
")" +
"(?:" +
src[PRERELEASE] +
")?" +
src[BUILD] +
"?" +
")?)?";
const XRANGE: number = R++;
src[XRANGE] = "^" + src[GTLT] + "\\s*" + src[XRANGEPLAIN] + "$";
// Tilde ranges.
// Meaning is "reasonably at or greater than"
const LONETILDE: number = R++;
src[LONETILDE] = "(?:~>?)";
const TILDE: number = R++;
src[TILDE] = "^" + src[LONETILDE] + src[XRANGEPLAIN] + "$";
// Caret ranges.
// Meaning is "at least and backwards compatible with"
const LONECARET: number = R++;
src[LONECARET] = "(?:\\^)";
const CARET: number = R++;
src[CARET] = "^" + src[LONECARET] + src[XRANGEPLAIN] + "$";
// A simple gt/lt/eq thing, or just "" to indicate "any version"
const COMPARATOR: number = R++;
src[COMPARATOR] = "^" + src[GTLT] + "\\s*(" + FULLPLAIN + ")$|^$";
// Something like `1.2.3 - 1.2.4`
const HYPHENRANGE: number = R++;
src[HYPHENRANGE] = "^\\s*(" +
src[XRANGEPLAIN] +
")" +
"\\s+-\\s+" +
"(" +
src[XRANGEPLAIN] +
")" +
"\\s*$";
// Star ranges basically just allow anything at all.
const STAR: number = R++;
src[STAR] = "(<|>)?=?\\s*\\*";
// Compile to actual regexp objects.
// All are flag-free, unless they were created above with a flag.
for (let i = 0; i < R; i++) {
if (!re[i]) {
re[i] = new RegExp(src[i]);
}
}
/**
* Returns true if the value is a valid SemVer number.
*
* Must be a number. Must not be NaN. Can be positive or negative infinity.
* Can be between 0 and MAX_SAFE_INTEGER.
* @param value The value to check
* @returns True if its a valid semver number
*/
export function isValidNumber(value: unknown): value is number {
return (
typeof value === "number" &&
!Number.isNaN(value) && (
!Number.isFinite(value) ||
(0 <= value && value <= Number.MAX_SAFE_INTEGER)
)
);
}
export const MAX_LENGTH = 256;
/**
* Returns true if the value is a valid semver pre-release or build identifier.
*
* Must be a string. Must be between 1 and 256 characters long. Must match
* the regular expression /[0-9A-Za-z-]+/.
* @param value The value to check
* @returns True if the value is a valid semver string.
*/
export function isValidString(value: unknown): value is string {
return (
typeof value === "string" &&
value.length > 0 &&
value.length <= MAX_LENGTH &&
!!value.match(/[0-9A-Za-z-]+/)
);
}
/**
* Checks to see if the value is a valid Operator string.
*
* Adds a type assertion if true.
* @param value The value to check
* @returns True if the value is a valid Operator string otherwise false.
*/
export function isValidOperator(value: unknown): value is Operator {
if (typeof value !== "string") return false;
switch (value) {
case "":
case "=":
case "==":
case "===":
case "!==":
case "!=":
case ">":
case ">=":
case "<":
case "<=":
return true;
default:
return false;
}
}
export {
CARET,
COMPARATOR,
FULL,
HYPHENRANGE,
NUMERICIDENTIFIER,
re,
src,
STAR,
TILDE,
XRANGE,
};