-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Enables URL parsing of p5.js version and add-on libraries #3715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hugo-RM
wants to merge
5
commits into
processing:develop
from
Enochteo:feat/url-parsing-p5.js-version
+170
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b72c387
Added unfinished URL parser and helper functions
Hugo-RM 07c9c71
Added test file: client/utils/parseURLParams.test.js
Enochteo f53f7b0
Refactor defaultHTML to accept options object
Hugo-RM a9076f4
added functions, defaults,
GoodKimchi e61bc35
Finish validator functions for URL parser
Hugo-RM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
client/utils/parseURLParams.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { p5Versions, currentP5Version } from '../../common/p5Versions'; | ||
|
|
||
| const DEFAULTS = { | ||
| sound: true, | ||
| preload: false, | ||
| shapes: false, | ||
| data: false | ||
| }; | ||
|
|
||
| /** | ||
| * Sorts version strings in descending order and returns the highest version | ||
| * @param {string[]} versions - Array of version strings (e.g., ['1.11.2', '1.11.1']) | ||
| * @returns {string} The highest version from the array | ||
| */ | ||
| function getNewestVersion(versions) { | ||
| return versions.sort((a, b) => { | ||
| const pa = a.split('.').map((n) => parseInt(n, 10)); | ||
| const pb = b.split('.').map((n) => parseInt(n, 10)); | ||
| for (let i = 0; i < 3; i++) { | ||
| const na = pa[i] || 0; | ||
| const nb = pb[i] || 0; | ||
| if (na !== nb) return nb - na; | ||
| } | ||
| return 0; | ||
| })[0]; | ||
| } | ||
|
|
||
| function validateVersion(version) { | ||
| if (!version) return currentP5Version; | ||
|
|
||
| const ver = String(version).trim(); | ||
|
|
||
| if (p5Versions.includes(ver)) return ver; | ||
|
|
||
| // if only major.minor provided like "1.11" | ||
| const majorMinorMatch = /^(\d+)\.(\d+)$/.exec(ver); | ||
| if (majorMinorMatch) { | ||
| const [, major, minor] = majorMinorMatch; | ||
| const matches = p5Versions.filter((v) => { | ||
| const parts = v.split('.'); | ||
| return parts[0] === major && parts[1] === minor; | ||
| }); | ||
| if (matches.length) { | ||
| return getNewestVersion(matches); | ||
| } | ||
| } | ||
|
|
||
| // if only major provided like "1" | ||
| const majorOnlyMatch = /^(\d+)$/.exec(ver); | ||
| if (majorOnlyMatch) { | ||
| const [, major] = majorOnlyMatch; | ||
| const matches = p5Versions.filter((v) => v.split('.')[0] === major); | ||
| if (matches.length) { | ||
| return getNewestVersion(matches); | ||
| } | ||
| } | ||
|
|
||
| return currentP5Version; | ||
| } | ||
|
|
||
| function validateBool(value, defaultValue) { | ||
| if (!value) return defaultValue; | ||
|
|
||
| const v = String(value).trim().toLowerCase(); | ||
|
|
||
| const TRUTHY = new Set(['on', 'true', '1']); | ||
| const FALSY = new Set(['off', 'false', '0']); | ||
|
|
||
| if (TRUTHY.has(v)) return true; | ||
| if (FALSY.has(v)) return false; | ||
|
|
||
| return defaultValue; | ||
| } | ||
|
|
||
| export function parseUrlParams(url) { | ||
| const params = new URLSearchParams( | ||
| new URL(url, 'https://dummy.origin').search | ||
| ); | ||
|
|
||
| return { | ||
| version: validateVersion(params.get('version')), | ||
| sound: validateBool(params.get('sound'), DEFAULTS.sound), | ||
| preload: validateBool(params.get('preload'), DEFAULTS.preload), | ||
| shapes: validateBool(params.get('shapes'), DEFAULTS.shapes), | ||
| data: validateBool(params.get('data'), DEFAULTS.data) | ||
| }; | ||
| } |
51 changes: 51 additions & 0 deletions
client/utils/parseURLParams.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { parseUrlParams } from './parseURLParams'; | ||
| import { currentP5Version } from '../../common/p5Versions'; | ||
|
|
||
| describe('parseUrlParams', () => { | ||
| test('returns defaults when no params are provided', () => { | ||
| const url = 'https://example.com'; | ||
| const result = parseUrlParams(url); | ||
|
|
||
| expect(result).toEqual({ | ||
| version: currentP5Version, | ||
| sound: true, | ||
| preload: false, | ||
| shapes: false, | ||
| data: false | ||
| }); | ||
| }); | ||
|
|
||
| test('parses a valid p5 version and falls back for invalid versions', () => { | ||
| const good = parseUrlParams('https://example.com?version=1.4.0'); | ||
| expect(good.version).toBe('1.4.0'); | ||
|
|
||
| const bad = parseUrlParams('https://example.com?version=9.9.9'); | ||
| expect(bad.version).toBe(currentP5Version); | ||
| }); | ||
|
|
||
| test('parses boolean-like params for sound/preload/shapes/data (true variants)', () => { | ||
| const trueVariants = ['on', 'true', '1', 'ON', 'True']; | ||
|
|
||
| trueVariants.forEach((v) => { | ||
| const url = `https://example.com?sound=${v}&preload=${v}&shapes=${v}&data=${v}`; | ||
| const result = parseUrlParams(url); | ||
| expect(result.sound).toBe(true); | ||
| expect(result.preload).toBe(true); | ||
| expect(result.shapes).toBe(true); | ||
| expect(result.data).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| test('parses boolean-like params for sound/preload/shapes/data (false variants)', () => { | ||
| const falseVariants = ['off', 'false', '0', 'OFF', 'False']; | ||
|
|
||
| falseVariants.forEach((v) => { | ||
| const url = `https://example.com?sound=${v}&preload=${v}&shapes=${v}&data=${v}`; | ||
| const result = parseUrlParams(url); | ||
| expect(result.sound).toBe(false); | ||
| expect(result.preload).toBe(false); | ||
| expect(result.shapes).toBe(false); | ||
| expect(result.data).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.