quantique-converter
Version:
Converters for all kind of data
20 lines (18 loc) • 521 B
JavaScript
// Convert to Number
const convertToNumber = (input) => {
if (typeof input === 'boolean') {
return { isValid: true, error: '', convertedValue: input ? 1 : 0 };
}
const num = Number(input);
if (isNaN(num)) {
return {
isValid: false,
error:
`Cannot convert ${input} to number` ||
defaultErrorMessages(rules, type).minLength,
convertedValue: '',
};
}
return { isValid: true, error: '', convertedValue: num };
};
module.exports = convertToNumber;