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

util/Util: add turnSquareBracketsIntoArrays helper

This commit is contained in:
Sotiri Bakagiannis 2020-07-24 09:51:00 +01:00
parent 08972dae90
commit 2a6138e4da

View File

@ -903,3 +903,53 @@ export function offerDataForDownload(filename, data, type)
document.body.removeChild(elem);
}
}
/**
* 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.
*
* @name module:util.turnSquareBracketsIntoArrays
* @function
* @public
* @param {string} input - string containing lists in square brackets
* @returns {array} an array of arrays found
*/
function turnSquareBracketsIntoArrays(input) {
// Only interested in strings
// https://stackoverflow.com/questions/4059147
if (String(input) !== input)
{
return input
}
// Matches content within square brackets (using literal
// form is MDN's advice for patterns unlikely to change)
const regexp = /\[(.*?)\]/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
// Eat up space after comma
const commaSplitValues = arrayLikeContent.split(/[, ]+/)
// Type cast numeric values
const output = commaSplitValues.map((value) =>
{
// Leave empty strings untouched
const numberMaybe = value && Number(value)
return Number.isNaN(numberMaybe) ? value : numberMaybe
}
)
return output
}
)
// Pass through if no array-like matches
return matches.length ? matches : input
}