Skip to content

Commit 3b55455

Browse files
committed
improve parseRange, add test cases
- parse "NaN" as string part - allow more whitespaces in hyphen
1 parent 1ab1d01 commit 3b55455

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

lib/util/semver.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ exports.versionLt = versionLt;
8989
*/
9090
exports.parseRange = str => {
9191
const splitAndConvert = str => {
92-
return str.split(".").map(item => (`${+item}` === item ? +item : item));
92+
return str
93+
.split(".")
94+
.map(item => (item !== "NaN" && `${+item}` === item ? +item : item));
9395
};
9496
// see https://docs.npmjs.com/misc/semver#range-grammar for grammar
9597
const parsePartial = str => {
@@ -131,13 +133,15 @@ exports.parseRange = str => {
131133
return [-range[0] - 1, ...range.slice(1)];
132134
};
133135
const parseSimple = str => {
134-
// simple ::= primitive | partial | tilde | caret
135-
// primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
136-
// tilde ::= '~' partial
137-
// caret ::= '^' partial
136+
// simple ::= primitive | partial | tilde | caret
137+
// primitive ::= ( '<' | '>' | '>=' | '<=' | '=' | '!' ) ( ' ' ) * partial
138+
// tilde ::= '~' ( ' ' ) * partial
139+
// caret ::= '^' ( ' ' ) * partial
138140
const match = /^(\^|~|<=|<|>=|>|=|v|!)/.exec(str);
139141
const start = match ? match[0] : "";
140-
const remainder = parsePartial(str.slice(start.length));
142+
const remainder = parsePartial(
143+
start.length ? str.slice(start.length).trim() : str.trim()
144+
);
141145
switch (start) {
142146
case "^":
143147
if (remainder.length > 1 && remainder[1] === 0) {
@@ -190,18 +194,14 @@ exports.parseRange = str => {
190194
// eslint-disable-next-line no-sparse-arrays
191195
return [, ...arr, ...items.slice(1).map(() => fn)];
192196
};
193-
// remove whitespace characters after ^, ~, <=, <, >=, >, =
194-
const trimRangeOperators = str => {
195-
return str.replace(/(\^|~|<=|<|>=|>|=)\s*/g, "$1");
196-
};
197197
const parseRange = str => {
198-
// range ::= hyphen | simple ( ' ' simple ) * | ''
199-
// hyphen ::= partial ' - ' partial
200-
const items = str.split(" - ");
198+
// range ::= hyphen | simple ( ' ' ( ' ' ) * simple ) * | ''
199+
// hyphen ::= partial ( ' ' ) * ' - ' ( ' ' ) * partial
200+
const items = str.split(/\s+-\s+/);
201201
if (items.length === 1) {
202-
const items = trimRangeOperators(str)
202+
const items = str
203203
.trim()
204-
.split(/\s+/g)
204+
.split(/(?<=[-0-9A-Za-z])\s+/g)
205205
.map(parseSimple);
206206
return combine(items, 2);
207207
}

test/SemVer.unittest.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ describe("SemVer", () => {
129129

130130
describe("parseRange", () => {
131131
const cases = {
132+
"5 || 6 || 7.x.x": ["5.x.x || 6.x || 7"],
133+
"1 - 2": ["1 - 2"],
132134
"=3": [
133135
"3",
134136
"v3",
@@ -145,7 +147,9 @@ describe("SemVer", () => {
145147
"^3.4": ["^3.4.*", "^ 3.4"],
146148
"3.4 - 6.5": [">=3.4 <=6.5", ">= 3.4 <= 6.5"],
147149
"<=3.4": ["<3.4 || =3.4", "<= 3.4"],
148-
">3.4": [">=3.4 !3.4", "> 3.4"]
150+
">3.4": [">=3.4 !3.4", "> 3.4"],
151+
"1.2.3-alpha.x.x": ["1.2.3-alpha", "1.2.3-alpha+build.25"],
152+
"1.2.3-NaN": ["1.2.3-NaN"]
149153
};
150154
for (const key of Object.keys(cases)) {
151155
describe(key, () => {

0 commit comments

Comments
 (0)