get-semver-range.js 729 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { Range } = require("semver")
  7. const cache = new Map()
  8. /**
  9. * Get the `semver.Range` object of a given range text.
  10. * @param {string} x The text expression for a semver range.
  11. * @returns {Range|null} The range object of a given range text.
  12. * It's null if the `x` is not a valid range text.
  13. */
  14. module.exports = function getSemverRange(x) {
  15. const s = String(x)
  16. let ret = cache.get(s) || null
  17. if (!ret) {
  18. try {
  19. ret = new Range(s)
  20. } catch (_error) {
  21. // Ignore parsing error.
  22. }
  23. cache.set(s, ret)
  24. }
  25. return ret
  26. }