How to use the chrono-node.strict function in chrono-node

To help you get started, we’ve selected a few chrono-node examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github digidem / react-mapfilter / src / utils / field_types.js View on Github external
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) {
github magda-io / magda / magda-web-client / src / Components / Dataset / MetadataExtraction / extractExtents.ts View on Github external
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;
    }
}

chrono-node

A natural language date parser in Javascript

MIT
Latest version published 2 months ago

Package Health Score

83 / 100
Full package analysis

Similar packages