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

Merge pull request #208 from thewhodidthis/bf#206--util-alt

util/Util: tweak toNumerical() to match JSDoc comment
This commit is contained in:
Alain Pitiot 2020-10-27 11:29:19 +01:00 committed by GitHub
commit 857b9cba96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -259,22 +259,26 @@ export function toNumerical(obj)
return obj;
}
const convertToNumber = (input) =>
{
const n = Number.parseFloat(input);
if (Number.isNaN(n))
{
throw `unable to convert ${input} to a number`;
}
return n;
}
if (typeof obj === 'string')
{
obj = [obj];
return convertToNumber(obj);
}
if (Array.isArray(obj))
{
return obj.map(e =>
{
let n = Number.parseFloat(e);
if (Number.isNaN(n))
{
throw `unable to convert ${e} to a number`;
}
return n;
});
return obj.map(convertToNumber);
}
throw 'unable to convert the object to a number';