Path: blob/main/plugins/default-browser-emulator/lib/VersionUtils.ts
1029 views
import * as macOsVersionAliasMap from '../data/macOsVersionAliasMap.json';12export function convertMacOsVersionString(versionString: string) {3let newVersionString = macOsVersionAliasMap[versionString];4if (!newVersionString) {5const [majorVersion] = versionString.split('.');6newVersionString = macOsVersionAliasMap[`${majorVersion}.*`];7}8return newVersionString || versionString;9}1011export function findClosestVersionMatch(versionToMatch: string, versions: string[]) {12if (versions.length === 1 && versions[0] === 'ALL') return 'ALL';13if (!versions.length) return null;1415// there is no guarantee we have an exact match, so let's get the closest16const versionTree = convertVersionsToTree(versions);17const [major, minor] = versionToMatch.split('-').map(x => Number(x));1819const majors = Object.keys(versionTree).map(x => Number(x));20const majorMatch = getClosestNumberMatch(major, majors);21let versionMatch = `${majorMatch}`;2223if (minor) {24const minors = Object.keys(versionTree[majorMatch]).map(x => Number(x));25const minorMatch = getClosestNumberMatch(minor, minors);26if (minorMatch !== undefined) versionMatch += `-${minorMatch}`;27} else if (!versions.includes(versionMatch)) {28const minors = Object.keys(versionTree[majorMatch]).map(x => Number(x));29if (minors.length) {30const minorMatch = major > majorMatch ? Math.max(...minors) : Math.min(...minors);31versionMatch += `-${minorMatch}`;32}33}3435return versions.includes(versionMatch) ? versionMatch : null;36}3738export function getClosestNumberMatch(numToMatch: number, nums: number[]) {39const sortedNums = nums.sort((a, b) => a - b);40let closest = sortedNums[0];41for (const num of sortedNums) {42if (num === numToMatch) {43return num;44}45if (num < numToMatch) {46closest = num;47} else if (num > numToMatch) {48break;49}50}51return closest;52}5354export function convertVersionsToTree(versions: string[]): IVersionTree {55return versions.reduce((tree: any, version: string) => {56const [major, minor, build] = version.split(/\.|-/);57tree[major] = tree[major] || {};58if (minor === undefined) return tree;59tree[major][minor] = tree[major][minor] || [];60if (build) tree[major][minor].push(build);61return tree;62}, {});63}6465export interface IVersionTree {66[major: number]: {67[minor: number]: number[];68};69}707172