Skip to content

Commit 984491a

Browse files
committed
add minSatisfying
1 parent 4bdb8f4 commit 984491a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ strings that they parse.
327327
range.
328328
* `maxSatisfying(versions, range)`: Return the highest version in the list
329329
that satisfies the range, or `null` if none of them do.
330+
* `minSatisfying(versions, range)`: Return the lowest version in the list
331+
that satisfies the range, or `null` if none of them do.
330332
* `gtr(version, range)`: Return `true` if version is greater than all the
331333
versions possible in the range.
332334
* `ltr(version, range)`: Return `true` if version is less than all the

semver.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,15 @@ function maxSatisfying(versions, range, loose) {
10941094
})[0] || null;
10951095
}
10961096

1097+
exports.minSatisfying = minSatisfying;
1098+
function minSatisfying(versions, range, loose) {
1099+
return versions.filter(function(version) {
1100+
return satisfies(version, range, loose);
1101+
}).sort(function(a, b) {
1102+
return compare(a, b, loose);
1103+
})[0] || null;
1104+
}
1105+
10971106
exports.validRange = validRange;
10981107
function validRange(range, loose) {
10991108
try {

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,19 @@ test('\nmax satisfying', function(t) {
696696
});
697697
t.end();
698698
});
699+
700+
test('\nmin satisfying', function(t) {
701+
[[['1.2.3', '1.2.4'], '1.2', '1.2.3'],
702+
[['1.2.4', '1.2.3'], '1.2', '1.2.3'],
703+
[['1.2.3', '1.2.4', '1.2.5', '1.2.6'], '~1.2.3', '1.2.3'],
704+
[['1.1.0', '1.2.0', '1.2.1', '1.3.0', '2.0.0b1', '2.0.0b2', '2.0.0b3', '2.0.0', '2.1.0'], '~2.0.0', '2.0.0', true]
705+
].forEach(function(v) {
706+
var versions = v[0];
707+
var range = v[1];
708+
var expect = v[2];
709+
var loose = v[3];
710+
var actual = semver.minSatisfying(versions, range, loose);
711+
t.equal(actual, expect);
712+
});
713+
t.end();
714+
});

0 commit comments

Comments
 (0)