|
| 1 | +import { p5Versions, currentP5Version } from '../../common/p5Versions'; |
| 2 | + |
| 3 | +const DEFAULTS = { |
| 4 | + sound: true, |
| 5 | + preload: false, |
| 6 | + shapes: false, |
| 7 | + data: false |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Sorts version strings in descending order and returns the highest version |
| 12 | + * @param {string[]} versions - Array of version strings (e.g., ['1.11.2', '1.11.1']) |
| 13 | + * @returns {string} The highest version from the array |
| 14 | + */ |
| 15 | +function getNewestVersion(versions) { |
| 16 | + return versions.sort((a, b) => { |
| 17 | + const pa = a.split('.').map((n) => parseInt(n, 10)); |
| 18 | + const pb = b.split('.').map((n) => parseInt(n, 10)); |
| 19 | + for (let i = 0; i < 3; i++) { |
| 20 | + const na = pa[i] || 0; |
| 21 | + const nb = pb[i] || 0; |
| 22 | + if (na !== nb) return nb - na; |
| 23 | + } |
| 24 | + return 0; |
| 25 | + })[0]; |
| 26 | +} |
| 27 | + |
| 28 | +function validateVersion(version) { |
| 29 | + if (!version) return currentP5Version; |
| 30 | + |
| 31 | + const ver = String(version).trim(); |
| 32 | + |
| 33 | + if (p5Versions.includes(ver)) return ver; |
| 34 | + |
| 35 | + // if only major.minor provided like "1.11" |
| 36 | + const majorMinorMatch = /^(\d+)\.(\d+)$/.exec(ver); |
| 37 | + if (majorMinorMatch) { |
| 38 | + const [, major, minor] = majorMinorMatch; |
| 39 | + const matches = p5Versions.filter((v) => { |
| 40 | + const parts = v.split('.'); |
| 41 | + return parts[0] === major && parts[1] === minor; |
| 42 | + }); |
| 43 | + if (matches.length) { |
| 44 | + return getNewestVersion(matches); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // if only major provided like "1" |
| 49 | + const majorOnlyMatch = /^(\d+)$/.exec(ver); |
| 50 | + if (majorOnlyMatch) { |
| 51 | + const [, major] = majorOnlyMatch; |
| 52 | + const matches = p5Versions.filter((v) => v.split('.')[0] === major); |
| 53 | + if (matches.length) { |
| 54 | + return getNewestVersion(matches); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return currentP5Version; |
| 59 | +} |
| 60 | + |
| 61 | +function validateBool(value, defaultValue) { |
| 62 | + if (!value) return defaultValue; |
| 63 | + |
| 64 | + const v = String(value).trim().toLowerCase(); |
| 65 | + |
| 66 | + const TRUTHY = new Set(['on', 'true', '1']); |
| 67 | + const FALSY = new Set(['off', 'false', '0']); |
| 68 | + |
| 69 | + if (TRUTHY.has(v)) return true; |
| 70 | + if (FALSY.has(v)) return false; |
| 71 | + |
| 72 | + return defaultValue; |
| 73 | +} |
| 74 | + |
| 75 | +export function parseUrlParams(url) { |
| 76 | + const params = new URLSearchParams( |
| 77 | + new URL(url, 'https://dummy.origin').search |
| 78 | + ); |
| 79 | + |
| 80 | + return { |
| 81 | + version: validateVersion(params.get('version')), |
| 82 | + sound: validateBool(params.get('sound'), DEFAULTS.sound), |
| 83 | + preload: validateBool(params.get('preload'), DEFAULTS.preload), |
| 84 | + shapes: validateBool(params.get('shapes'), DEFAULTS.shapes), |
| 85 | + data: validateBool(params.get('data'), DEFAULTS.data) |
| 86 | + }; |
| 87 | +} |
0 commit comments