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.

37 lines
698 B

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