You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

4 years ago
  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