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

Merge pull request #262 from thewhodidthis/bf#205--util

Allow for parsing of array-like strings when setting visual stimulus position or size
This commit is contained in:
Alain Pitiot 2021-02-18 15:32:34 +01:00 committed by GitHub
commit bed6ff240e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 30 deletions

View File

@ -481,9 +481,8 @@ export class TrialHandler extends PsychObject
// Keep the first match if more than one are found. If the // Keep the first match if more than one are found. If the
// input string looked like '[1, 2][3, 4]' for example, // input string looked like '[1, 2][3, 4]' for example,
// the resulting `value` would be [1, 2]. When `arrayMaybe` is // the resulting `value` would be [1, 2]. When `arrayMaybe` is
// empty, `value` turns `undefined`. At this point that might // empty, `value` turns `undefined`.
// only happen if `value` is an empty array to begin with. value = arrayMaybe;
value = arrayMaybe[0];
} }
// if value is a numerical string, convert it to a number: // if value is a numerical string, convert it to a number:

View File

@ -271,16 +271,23 @@ export function toNumerical(obj)
return n; return n;
}; };
if (typeof obj === 'string')
{
return convertToNumber(obj);
}
if (Array.isArray(obj)) if (Array.isArray(obj))
{ {
return obj.map(convertToNumber); return obj.map(convertToNumber);
} }
const arrayMaybe = turnSquareBracketsIntoArrays(obj);
if (Array.isArray(arrayMaybe))
{
return arrayMaybe.map(convertToNumber);
}
if (typeof obj === 'string')
{
return convertToNumber(obj);
}
throw 'unable to convert the object to a number'; throw 'unable to convert the object to a number';
} }
catch (error) catch (error)
@ -962,54 +969,52 @@ export function offerDataForDownload(filename, data, type)
/** /**
* To overcome built-in JSON parsing limitations when it comes to eg. floats missing the naught prefix, turn substrings contained within square brackets into arrays type casting numeric looking values in the process. * Convert a string representing a JSON array, e.g. "[1, 2]" into an array, e.g. ["1","2"].
* This approach overcomes the built-in JSON parsing limitations when it comes to eg. floats
* missing the naught prefix, and is able to process several arrays, e.g. "[1,2][3,4]".
* *
* @name module:util.turnSquareBracketsIntoArrays * @name module:util.turnSquareBracketsIntoArrays
* @function * @function
* @public * @public
* @param {string} input - string containing lists in square brackets * @param {string} input - string potentially containing JSON arrays
* @returns {array} an array of arrays found * @param {string} max - how many matches to return, unwrap resulting array if less than two
* @returns {array} an array if arrays were found, undefined otherwise
*/ */
export function turnSquareBracketsIntoArrays(input) export function turnSquareBracketsIntoArrays(input, max = 1)
{ {
// Only interested in strings // Only interested in strings
// https://stackoverflow.com/questions/4059147 // https://stackoverflow.com/questions/4059147
if (String(input) !== input) if (String(input) !== input)
{ {
return input; return;
} }
// 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 matchesMaybe = input.match(/\[(.*?)\]/g); const matchesMaybe = input.match(/\[(.*?)\]/g);
// Pass through if no array-like matches found // Exit if no array-like matches found
if (matchesMaybe === null) if (matchesMaybe === null)
{ {
return input; return;
} }
// Reformat content for each match // Reformat content for each match
const matches = matchesMaybe.map((data) => const matches = matchesMaybe.map((data) =>
{ {
// Remove the square brackets return data
const arrayLikeContent = data.replace(/[\[\]]+/g, ''); // Remove the square brackets
// Eat up space after comma .replace(/[\[\]]+/g, '')
const commaSplitValues = arrayLikeContent.split(/[, ]+/); // Eat up space after comma
// Type cast numeric values .split(/[, ]+/);
const result = commaSplitValues.map((value) =>
{
// Leave empty strings untouched
const numberMaybe = value && Number(value);
return Number.isNaN(numberMaybe) ? value : numberMaybe;
}
);
return result;
} }
); );
if (max < 2)
{
return matches[0];
}
return matches; return matches;
} }