index.js 541 B

1234567891011121314
  1. 'use strict';
  2. const semverRegex = require('semver-regex');
  3. module.exports = (stringWithVersions, {loose = false} = {}) => {
  4. if (typeof stringWithVersions !== 'string') {
  5. throw new TypeError(`Expected a string, got ${typeof stringWithVersions}`);
  6. }
  7. const regex = loose ? new RegExp(`(?:${semverRegex().source})|(?:v?(?:\\d+\\.\\d+)(?:\\.\\d+)?)`, 'g') : semverRegex();
  8. const matches = stringWithVersions.match(regex) || [];
  9. return [...new Set(matches.map(match => match.trim().replace(/^v/, '').replace(/^\d+\.\d+$/, '$&.0')))];
  10. };