UNPKG

@flatfile/plugin-validate-date

Version:

A Flatfile plugin for normalizing date formats

1 lines 2.96 kB
{"version":3,"sources":["../src/validate.date.utils.ts","../src/validate.date.plugin.ts"],"names":["normalizeDate","dateString","config","parsedDate","n","formattedDate","format","enUS","error","validateDate","recordHook","record","event","field","dateValue","normalizedDate"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAKO,SAASA,CACdC,CAAAA,CAAAA,CACAC,CACe,CAAA,CACf,GAAI,CACF,IAAMC,CAAoB,CAAAC,YAAA,CAAA,SAAA,CAAUH,CAAU,CAAA,CAC9C,GAAIE,CAAY,CAAA,CACd,IAAME,CAAAA,CAAgBC,cAAOH,CAAAA,CAAAA,CAAYD,EAAO,YAAc,CAAA,CAC5D,MAAQK,CAAAA,WACV,CAAC,CAAA,CAED,OAAKL,CAAO,CAAA,WAAA,CAKLG,CAHEA,CAAAA,CAAAA,CAAc,KAAM,CAAA,GAAG,CAAE,CAAA,CAAC,CAIrC,CACA,OAAO,IACT,CAASG,MAAAA,CAAAA,CAAO,CACd,OAAQ,OAAA,CAAA,KAAA,CAAMA,CAAK,CAAA,CACZ,IACT,CACF,CChBO,SAASC,CAAAA,CAAaP,CAAoC,CAAA,CAC/D,OAAOQ,2BAAAA,CACLR,EAAO,SAAa,EAAA,IAAA,CACpB,CAACS,CAAAA,CAAwBC,CACvBV,IAAAA,CAAAA,CAAO,WAAW,OAASW,CAAAA,CAAAA,EAAU,CACnC,IAAMC,CAAYH,CAAAA,CAAAA,CAAO,IAAIE,CAAK,CAAA,CAClC,GAAIC,CAAAA,CAAW,CACb,IAAMC,EAAiBf,CAAcc,CAAAA,CAAAA,CAAWZ,CAAM,CAAA,CAClDa,CACFJ,CAAAA,CAAAA,CAAO,IAAIE,CAAOE,CAAAA,CAAc,CAEhCJ,CAAAA,CAAAA,CAAO,QAASE,CAAAA,CAAAA,CAAO,6BAA6B,EAExD,CACF,CAAC,CACMF,CAAAA,CAAAA,CAEX,CACF","file":"index.cjs","sourcesContent":["import * as chrono from 'chrono-node'\nimport { format } from 'date-fns'\nimport { enUS } from 'date-fns/locale'\nimport { DateFormatNormalizerConfig } from './validate.date.plugin'\n\nexport function normalizeDate(\n dateString: string,\n config: DateFormatNormalizerConfig\n): string | null {\n try {\n const parsedDate = chrono.parseDate(dateString)\n if (parsedDate) {\n const formattedDate = format(parsedDate, config.outputFormat, {\n locale: enUS,\n })\n\n if (!config.includeTime) {\n // If time should not be included, truncate the formatted date to just the date part\n return formattedDate.split(' ')[0]\n }\n\n return formattedDate\n }\n return null\n } catch (error) {\n console.error(error)\n return null\n }\n}\n","import { FlatfileEvent } from '@flatfile/listener'\nimport { FlatfileRecord, recordHook } from '@flatfile/plugin-record-hook'\nimport { normalizeDate } from './validate.date.utils'\n\nexport interface DateFormatNormalizerConfig {\n sheetSlug?: string\n dateFields: string[]\n outputFormat: string\n includeTime: boolean\n locale?: string\n}\n\nexport function validateDate(config: DateFormatNormalizerConfig) {\n return recordHook(\n config.sheetSlug || '**',\n (record: FlatfileRecord, event: FlatfileEvent) => {\n config.dateFields.forEach((field) => {\n const dateValue = record.get(field) as string\n if (dateValue) {\n const normalizedDate = normalizeDate(dateValue, config)\n if (normalizedDate) {\n record.set(field, normalizedDate)\n } else {\n record.addError(field, 'Unable to parse date string')\n }\n }\n })\n return record\n }\n )\n}\n"]}