blob: 945c517352cd2f8a81e888adfe06f4d98303da66 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const semverRegex = require('semver-regex');
3
4module.exports = (stringWithVersions, options = {}) => {
5 if (typeof stringWithVersions !== 'string') {
6 throw new TypeError(`Expected a string, got ${typeof stringWithVersions}`);
7 }
8
9 const reLoose = new RegExp(`(?:${semverRegex().source})|(?:v?(?:\\d+\\.\\d+)(?:\\.\\d+)?)`, 'g');
10 const matches = stringWithVersions.match(options.loose === true ? reLoose : semverRegex()) || [];
11
12 return [...new Set(matches.map(match => match.trim().replace(/^v/, '').replace(/^\d+\.\d+$/, '$&.0')))];
13};