@@ -7,74 +7,81 @@ const DEFAULTS = {
77 data : false
88} ;
99
10- // One centralized parser
11- export function parseUrlParams ( url ) {
12- const params = new URLSearchParams (
13- new URL ( url , 'https://dummy.origin' ) . search
14- ) ;
15-
16- return {
17- version : validateVersion ( params . get ( 'version' ) ) , // string
18- sound : validateBool ( params . get ( 'sound' ) , DEFAULTS . sound ) , // bool
19- preload : validateBool ( params . get ( 'preload' ) , DEFAULTS . preload ) , // bool
20- shapes : validateBool ( params . get ( 'shapes' ) , DEFAULTS . shapes ) , // bool
21- data : validateBool ( params . get ( 'data' ) , DEFAULTS . data ) // bool
22- // Easy to add more params here
23- } ;
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 ] ;
2426}
2527
2628function validateVersion ( version ) {
27- if ( ! version ) {
28- return currentP5Version ;
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+ }
2946 }
30- const v = String ( version ) . trim ( ) ;
31- if ( v . toLowerCase ( ) === 'latest' ) {
32- const newest = getNewestVersion ( p5Versions ) ;
33- return newest ?? currentP5Version ; //The ?? operator means: “if newest is null or undefined, use currentP5Version
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+ }
3456 }
35- if ( v . toLowerCase ( ) === 'current' ) return currentP5Version ;
36- if ( p5Versions . includes ( v ) ) return v ;
37- const normalized = v . replace ( / ^ v / i, '' ) ;
38- if ( p5Versions . includes ( normalized ) ) return normalized ;
39- //This line strips that leading v using a regular expression ^v (meaning “v at the start”) and then rechecks.
4057
41- // if valid return version
4258 return currentP5Version ;
4359}
4460
45- //picks highest version number from array
46- function getNewestVersion ( list ) {
47- // Defensive copy + semver sort (major.minor.patch)
48- const parts = ( s ) => s . split ( '.' ) . map ( ( n ) => parseInt ( n , 10 ) || 0 ) ;
49- return [ ...list ] . sort ( ( a , b ) => {
50- const [ am , an , ap ] = parts ( a ) ;
51- const [ bm , bn , bp ] = parts ( b ) ;
52- if ( am !== bm ) return bm - am ;
53- if ( an !== bn ) return bn - an ;
54- return bp - ap ;
55- } ) [ 0 ] ;
56- }
57-
5861function validateBool ( value , defaultValue ) {
59- if ( value ) return defaultValue ; // param absent
60- //if (value === '') return true; // bare flag: ?flag
62+ if ( ! value ) return defaultValue ;
6163
6264 const v = String ( value ) . trim ( ) . toLowerCase ( ) ;
6365
64- const TRUTHY = new Set ( [ 'on' , 'true' , '1' , 'yes' , 'y' , 'enable' , 'enabled' ] ) ;
65- const FALSY = new Set ( [
66- 'off' ,
67- 'false' ,
68- '0' ,
69- //'no',
70- //'n',
71- //'disable',
72- //'disabled'
73- ] ) ;
66+ const TRUTHY = new Set ( [ 'on' , 'true' , '1' ] ) ;
67+ const FALSY = new Set ( [ 'off' , 'false' , '0' ] ) ;
7468
7569 if ( TRUTHY . has ( v ) ) return true ;
7670 if ( FALSY . has ( v ) ) return false ;
7771
78- return defaultValue ; // unrecognized → fall back safely
72+ return defaultValue ;
7973}
8074
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