1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-12 08:38:10 +00:00

util/Util: fix turnSquareBracketsIntoArrays for older browsers

Rely on String.prototype.match() instead of matchAll()
This commit is contained in:
Sotiri Bakagiannis 2020-11-27 14:07:50 +00:00
parent 85f77e1b39
commit 57ac49f649

View File

@ -981,20 +981,23 @@ export function turnSquareBracketsIntoArrays(input)
// Matches content within square brackets (using literal // Matches content within square brackets (using literal
// form is MDN's advice for patterns unlikely to change) // form is MDN's advice for patterns unlikely to change)
const regexp = /\[(.*?)\]/g; const matchesMaybe = input.match(/\[(.*?)\]/g);
// Find all matches (iterator)
const matchesFound = input.matchAll(regexp);
// Remap results
const matches = Array.from(matchesFound, (data) =>
{
// Out of all the information for each match, focus
// on substrings inside of square brackets
const [_, arrayLikeContent = ''] = data;
// Pass through if no array-like matches found
if (matchesMaybe === null)
{
return input;
}
// Reformat content for each match
const matches = matchesMaybe.map((data) =>
{
// Remove the square brackets
const arrayLikeContent = data.replace(/[\[\]]+/g, '');
// Eat up space after comma // Eat up space after comma
const commaSplitValues = arrayLikeContent.split(/[, ]+/); const commaSplitValues = arrayLikeContent.split(/[, ]+/);
// Type cast numeric values // Type cast numeric values
const output = commaSplitValues.map((value) => const result = commaSplitValues.map((value) =>
{ {
// Leave empty strings untouched // Leave empty strings untouched
const numberMaybe = value && Number(value); const numberMaybe = value && Number(value);
@ -1003,12 +1006,11 @@ export function turnSquareBracketsIntoArrays(input)
} }
); );
return output; return result;
} }
); );
// Pass through if no array-like matches return matches;
return matches.length ? matches : input;
} }