Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function coerceValue(
value: any,
fieldType: $Values
): Date | number | string | boolean | null | [number, number] {
const valueType = guessType(value)
if (valueType === fieldType) return value
// Attempt to coerce strings to a date
if (fieldType === fieldTypes.DATE) {
let parsedDate = null
if (valueType === fieldTypes.STRING) {
parsedDate = chrono.strict.parseDate(value)
}
return parsedDate
}
if (fieldType === fieldTypes.LOCATION) {
let parsedLocation = null
if (valueType === fieldTypes.ARRAY || valueType === fieldTypes.STRING) {
// We try to parse a location from a string
parsedLocation = parseLocation(value)
}
return parsedLocation
}
if (fieldType === fieldTypes.BOOLEAN) {
let parsedBoolean = null
if (valueType === fieldTypes.NUMBER) {
function pickMoment(
rawDate: string,
toCompare: Date,
getBetter: (moment1: Date, moment2: Date) => Date
) {
const parsed: Array =
rawDate && rawDate.length > 0 && chrono.strict.parse(rawDate);
if (parsed && parsed.length > 0 && parsed[0].start) {
const startDate = preferYears(parsed[0].start).date();
const betterDate = parsed[0].end
? getBetter(startDate, preferYears(parsed[0].end).date())
: startDate;
return getBetter(betterDate, toCompare);
} else {
return toCompare;
}
}