2
0
Files
bot/packages/variables/parseGuessedValueType.ts
Baptiste Arnaud 5e019bbb22 Introducing The Forge (#1072)
The Forge allows anyone to easily create their own Typebot Block.

Closes #380
2023-12-13 10:22:02 +01:00

23 lines
911 B
TypeScript

import { Variable } from '@typebot.io/schemas'
export const parseGuessedValueType = (
value: Variable['value']
): string | (string | null)[] | boolean | number | null | undefined => {
if (value === null) return null
if (value === undefined) return undefined
if (typeof value !== 'string') return value
const isStartingWithZero =
value.startsWith('0') && !value.startsWith('0.') && value.length > 1
if (typeof value === 'string' && isStartingWithZero) return value
const isStartingWithPlus = value.startsWith('+')
if (typeof value === 'string' && isStartingWithPlus) return value
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)
}