2024-03-18 16:09:19 +01:00
|
|
|
import { Variable } from './types'
|
2023-03-15 12:21:52 +01:00
|
|
|
|
|
|
|
export const parseGuessedValueType = (
|
|
|
|
value: Variable['value']
|
2023-03-21 15:42:03 +01:00
|
|
|
): string | (string | null)[] | boolean | number | null | undefined => {
|
2023-03-15 12:21:52 +01:00
|
|
|
if (value === null) return null
|
|
|
|
if (value === undefined) return undefined
|
|
|
|
if (typeof value !== 'string') return value
|
2023-04-06 17:12:56 +02:00
|
|
|
const isStartingWithZero =
|
2023-03-15 12:21:52 +01:00
|
|
|
value.startsWith('0') && !value.startsWith('0.') && value.length > 1
|
2023-04-06 17:12:56 +02:00
|
|
|
if (typeof value === 'string' && isStartingWithZero) return value
|
|
|
|
const isStartingWithPlus = value.startsWith('+')
|
|
|
|
if (typeof value === 'string' && isStartingWithPlus) return value
|
2023-03-15 12:21:52 +01:00
|
|
|
if (typeof value === 'number') return value
|
|
|
|
if (value === 'true') return true
|
|
|
|
if (value === 'false') return false
|
|
|
|
if (value === 'null') return null
|
|
|
|
if (value === 'undefined') return undefined
|
|
|
|
// isNaN works with strings
|
|
|
|
if (isNaN(value as unknown as number)) return value
|
|
|
|
return Number(value)
|
|
|
|
}
|