parse.js 660 B

123456789101112131415161718192021222324252627282930313233
  1. const {MAX_LENGTH} = require('../internal/constants')
  2. const { re, t } = require('../internal/re')
  3. const SemVer = require('../classes/semver')
  4. const parseOptions = require('../internal/parse-options')
  5. const parse = (version, options) => {
  6. options = parseOptions(options)
  7. if (version instanceof SemVer) {
  8. return version
  9. }
  10. if (typeof version !== 'string') {
  11. return null
  12. }
  13. if (version.length > MAX_LENGTH) {
  14. return null
  15. }
  16. const r = options.loose ? re[t.LOOSE] : re[t.FULL]
  17. if (!r.test(version)) {
  18. return null
  19. }
  20. try {
  21. return new SemVer(version, options)
  22. } catch (er) {
  23. return null
  24. }
  25. }
  26. module.exports = parse