quantique-converter
Version:
Converters for all kind of data
22 lines (21 loc) • 718 B
JavaScript
const convertToArray = (input) => {
if (Array.isArray(input))
return { isValid: true, error: '', convertedValue: input };
if (typeof input === 'string') {
try {
const parsed = JSON.parse(input);
if (Array.isArray(parsed))
return { isValid: true, error: '', convertedValue: parsed };
} catch (e) {
return { isValid: false, error: 'Not a valid value', convertedValue: null };
// Not JSON, continue to other methods
}
return {
isValid: true,
error: '',
convertedValue: input.split(',').map((item) => item.trim()),
};
}
return { isValid: true, error: '', convertedValue: [input] };
};
module.exports = convertToArray;