All of my inputs are strings. I want to export them to a sort of DB, using the correct type.
So I can have inputs like e.g. "hello123"
, "123"
, or "123.0"
.
Which have the type string
, int
, and float
respectively.
I already have a decent function determining whether it's a number or not:
const isNumber = (input) => !isNaN(Number(input))
console.log(isNumber("123hello")) // false
console.log(isNumber("123")) // true
console.log(isNumber("123.0")) // true
But I would like something like:
const getType = (input) => {
if (/* check if int */) {
return 'int'
} else if (/* check if float */) {
return 'float'
} else {
return 'string'
}
}
console.log(getType("123hello")) // 'string'
console.log(getType("123")) // 'int'
console.log(getType("123.0")) // 'float' (preferably, but 'int' is also ok)