min-version.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const SemVer = require('../classes/semver')
  2. const Range = require('../classes/range')
  3. const gt = require('../functions/gt')
  4. const minVersion = (range, loose) => {
  5. range = new Range(range, loose)
  6. let minver = new SemVer('0.0.0')
  7. if (range.test(minver)) {
  8. return minver
  9. }
  10. minver = new SemVer('0.0.0-0')
  11. if (range.test(minver)) {
  12. return minver
  13. }
  14. minver = null
  15. for (let i = 0; i < range.set.length; ++i) {
  16. const comparators = range.set[i]
  17. comparators.forEach((comparator) => {
  18. // Clone to avoid manipulating the comparator's semver object.
  19. const compver = new SemVer(comparator.semver.version)
  20. switch (comparator.operator) {
  21. case '>':
  22. if (compver.prerelease.length === 0) {
  23. compver.patch++
  24. } else {
  25. compver.prerelease.push(0)
  26. }
  27. compver.raw = compver.format()
  28. /* fallthrough */
  29. case '':
  30. case '>=':
  31. if (!minver || gt(minver, compver)) {
  32. minver = compver
  33. }
  34. break
  35. case '<':
  36. case '<=':
  37. /* Ignore maximum versions */
  38. break
  39. /* istanbul ignore next */
  40. default:
  41. throw new Error(`Unexpected operation: ${comparator.operator}`)
  42. }
  43. })
  44. }
  45. if (minver && range.test(minver)) {
  46. return minver
  47. }
  48. return null
  49. }
  50. module.exports = minVersion