source: frontend/node_modules/semver/internal/re.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 7.9 KB
Line 
1'use strict'
2
3const {
4 MAX_SAFE_COMPONENT_LENGTH,
5 MAX_SAFE_BUILD_LENGTH,
6 MAX_LENGTH,
7} = require('./constants')
8const debug = require('./debug')
9exports = module.exports = {}
10
11// The actual regexps go on exports.re
12const re = exports.re = []
13const safeRe = exports.safeRe = []
14const src = exports.src = []
15const safeSrc = exports.safeSrc = []
16const t = exports.t = {}
17let R = 0
18
19const LETTERDASHNUMBER = '[a-zA-Z0-9-]'
20
21// Replace some greedy regex tokens to prevent regex dos issues. These regex are
22// used internally via the safeRe object since all inputs in this library get
23// normalized first to trim and collapse all extra whitespace. The original
24// regexes are exported for userland consumption and lower level usage. A
25// future breaking change could export the safer regex only with a note that
26// all input should have extra whitespace removed.
27const safeRegexReplacements = [
28 ['\\s', 1],
29 ['\\d', MAX_LENGTH],
30 [LETTERDASHNUMBER, MAX_SAFE_BUILD_LENGTH],
31]
32
33const makeSafeRegex = (value) => {
34 for (const [token, max] of safeRegexReplacements) {
35 value = value
36 .split(`${token}*`).join(`${token}{0,${max}}`)
37 .split(`${token}+`).join(`${token}{1,${max}}`)
38 }
39 return value
40}
41
42const createToken = (name, value, isGlobal) => {
43 const safe = makeSafeRegex(value)
44 const index = R++
45 debug(name, index, value)
46 t[name] = index
47 src[index] = value
48 safeSrc[index] = safe
49 re[index] = new RegExp(value, isGlobal ? 'g' : undefined)
50 safeRe[index] = new RegExp(safe, isGlobal ? 'g' : undefined)
51}
52
53// The following Regular Expressions can be used for tokenizing,
54// validating, and parsing SemVer version strings.
55
56// ## Numeric Identifier
57// A single `0`, or a non-zero digit followed by zero or more digits.
58
59createToken('NUMERICIDENTIFIER', '0|[1-9]\\d*')
60createToken('NUMERICIDENTIFIERLOOSE', '\\d+')
61
62// ## Non-numeric Identifier
63// Zero or more digits, followed by a letter or hyphen, and then zero or
64// more letters, digits, or hyphens.
65
66createToken('NONNUMERICIDENTIFIER', `\\d*[a-zA-Z-]${LETTERDASHNUMBER}*`)
67
68// ## Main Version
69// Three dot-separated numeric identifiers.
70
71createToken('MAINVERSION', `(${src[t.NUMERICIDENTIFIER]})\\.` +
72 `(${src[t.NUMERICIDENTIFIER]})\\.` +
73 `(${src[t.NUMERICIDENTIFIER]})`)
74
75createToken('MAINVERSIONLOOSE', `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
76 `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
77 `(${src[t.NUMERICIDENTIFIERLOOSE]})`)
78
79// ## Pre-release Version Identifier
80// A numeric identifier, or a non-numeric identifier.
81// Non-numeric identifiers include numeric identifiers but can be longer.
82// Therefore non-numeric identifiers must go first.
83
84createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NONNUMERICIDENTIFIER]
85}|${src[t.NUMERICIDENTIFIER]})`)
86
87createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NONNUMERICIDENTIFIER]
88}|${src[t.NUMERICIDENTIFIERLOOSE]})`)
89
90// ## Pre-release Version
91// Hyphen, followed by one or more dot-separated pre-release version
92// identifiers.
93
94createToken('PRERELEASE', `(?:-(${src[t.PRERELEASEIDENTIFIER]
95}(?:\\.${src[t.PRERELEASEIDENTIFIER]})*))`)
96
97createToken('PRERELEASELOOSE', `(?:-?(${src[t.PRERELEASEIDENTIFIERLOOSE]
98}(?:\\.${src[t.PRERELEASEIDENTIFIERLOOSE]})*))`)
99
100// ## Build Metadata Identifier
101// Any combination of digits, letters, or hyphens.
102
103createToken('BUILDIDENTIFIER', `${LETTERDASHNUMBER}+`)
104
105// ## Build Metadata
106// Plus sign, followed by one or more period-separated build metadata
107// identifiers.
108
109createToken('BUILD', `(?:\\+(${src[t.BUILDIDENTIFIER]
110}(?:\\.${src[t.BUILDIDENTIFIER]})*))`)
111
112// ## Full Version String
113// A main version, followed optionally by a pre-release version and
114// build metadata.
115
116// Note that the only major, minor, patch, and pre-release sections of
117// the version string are capturing groups. The build metadata is not a
118// capturing group, because it should not ever be used in version
119// comparison.
120
121createToken('FULLPLAIN', `v?${src[t.MAINVERSION]
122}${src[t.PRERELEASE]}?${
123 src[t.BUILD]}?`)
124
125createToken('FULL', `^${src[t.FULLPLAIN]}$`)
126
127// like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
128// also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
129// common in the npm registry.
130createToken('LOOSEPLAIN', `[v=\\s]*${src[t.MAINVERSIONLOOSE]
131}${src[t.PRERELEASELOOSE]}?${
132 src[t.BUILD]}?`)
133
134createToken('LOOSE', `^${src[t.LOOSEPLAIN]}$`)
135
136createToken('GTLT', '((?:<|>)?=?)')
137
138// Something like "2.*" or "1.2.x".
139// Note that "x.x" is a valid xRange identifier, meaning "any version"
140// Only the first item is strictly required.
141createToken('XRANGEIDENTIFIERLOOSE', `${src[t.NUMERICIDENTIFIERLOOSE]}|x|X|\\*`)
142createToken('XRANGEIDENTIFIER', `${src[t.NUMERICIDENTIFIER]}|x|X|\\*`)
143
144createToken('XRANGEPLAIN', `[v=\\s]*(${src[t.XRANGEIDENTIFIER]})` +
145 `(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
146 `(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
147 `(?:${src[t.PRERELEASE]})?${
148 src[t.BUILD]}?` +
149 `)?)?`)
150
151createToken('XRANGEPLAINLOOSE', `[v=\\s]*(${src[t.XRANGEIDENTIFIERLOOSE]})` +
152 `(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
153 `(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
154 `(?:${src[t.PRERELEASELOOSE]})?${
155 src[t.BUILD]}?` +
156 `)?)?`)
157
158createToken('XRANGE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAIN]}$`)
159createToken('XRANGELOOSE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAINLOOSE]}$`)
160
161// Coercion.
162// Extract anything that could conceivably be a part of a valid semver
163createToken('COERCEPLAIN', `${'(^|[^\\d])' +
164 '(\\d{1,'}${MAX_SAFE_COMPONENT_LENGTH}})` +
165 `(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
166 `(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?`)
167createToken('COERCE', `${src[t.COERCEPLAIN]}(?:$|[^\\d])`)
168createToken('COERCEFULL', src[t.COERCEPLAIN] +
169 `(?:${src[t.PRERELEASE]})?` +
170 `(?:${src[t.BUILD]})?` +
171 `(?:$|[^\\d])`)
172createToken('COERCERTL', src[t.COERCE], true)
173createToken('COERCERTLFULL', src[t.COERCEFULL], true)
174
175// Tilde ranges.
176// Meaning is "reasonably at or greater than"
177createToken('LONETILDE', '(?:~>?)')
178
179createToken('TILDETRIM', `(\\s*)${src[t.LONETILDE]}\\s+`, true)
180exports.tildeTrimReplace = '$1~'
181
182createToken('TILDE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAIN]}$`)
183createToken('TILDELOOSE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAINLOOSE]}$`)
184
185// Caret ranges.
186// Meaning is "at least and backwards compatible with"
187createToken('LONECARET', '(?:\\^)')
188
189createToken('CARETTRIM', `(\\s*)${src[t.LONECARET]}\\s+`, true)
190exports.caretTrimReplace = '$1^'
191
192createToken('CARET', `^${src[t.LONECARET]}${src[t.XRANGEPLAIN]}$`)
193createToken('CARETLOOSE', `^${src[t.LONECARET]}${src[t.XRANGEPLAINLOOSE]}$`)
194
195// A simple gt/lt/eq thing, or just "" to indicate "any version"
196createToken('COMPARATORLOOSE', `^${src[t.GTLT]}\\s*(${src[t.LOOSEPLAIN]})$|^$`)
197createToken('COMPARATOR', `^${src[t.GTLT]}\\s*(${src[t.FULLPLAIN]})$|^$`)
198
199// An expression to strip any whitespace between the gtlt and the thing
200// it modifies, so that `> 1.2.3` ==> `>1.2.3`
201createToken('COMPARATORTRIM', `(\\s*)${src[t.GTLT]
202}\\s*(${src[t.LOOSEPLAIN]}|${src[t.XRANGEPLAIN]})`, true)
203exports.comparatorTrimReplace = '$1$2$3'
204
205// Something like `1.2.3 - 1.2.4`
206// Note that these all use the loose form, because they'll be
207// checked against either the strict or loose comparator form
208// later.
209createToken('HYPHENRANGE', `^\\s*(${src[t.XRANGEPLAIN]})` +
210 `\\s+-\\s+` +
211 `(${src[t.XRANGEPLAIN]})` +
212 `\\s*$`)
213
214createToken('HYPHENRANGELOOSE', `^\\s*(${src[t.XRANGEPLAINLOOSE]})` +
215 `\\s+-\\s+` +
216 `(${src[t.XRANGEPLAINLOOSE]})` +
217 `\\s*$`)
218
219// Star ranges basically just allow anything at all.
220createToken('STAR', '(<|>)?=?\\s*\\*')
221// >=0.0.0 is like a star
222createToken('GTE0', '^\\s*>=\\s*0\\.0\\.0\\s*$')
223createToken('GTE0PRE', '^\\s*>=\\s*0\\.0\\.0-0\\s*$')
Note: See TracBrowser for help on using the repository browser.