classifyEdifactDtmFormat
Signature
Section titled “Signature”classifyEdifactDtmFormat(format: string): EdifactDtmFormatClass | nullName the kind of value a UN/EDIFACT data element 2379 format code states, and return the code narrowed to that kind, so a code that arrives as data reaches the function that reads it.
-
Each kind has its own parser, formatter and validator, and each takes only its own codes. A generic
DTMreader holds the code as a plain string: it calls this function, testskind, and passesformaton. Testingkindnarrowsformatto that kind’s union, so the call needs no cast. ### KindskindCodes Read by "date"102parseEdifactDate"time"401,402parseEdifactTime"dateTime"203,204parseEdifactDateTime"offsetDateTime"205,208,303,304parseEdifactOffsetDateTime"datePeriod"718parseEdifactDatePeriod"dateTimePeriod"719parseEdifactDateTimePeriod -
The narrowing pattern. With
const classified = classifyEdifactDtmFormat(code), the lineif (classified?.kind === "date") parseEdifactDate(value, classified.format)compiles with no cast: inside the test,classified.formatisEdifactDateFormat. -
Both members are always present, and each call returns a new object.
-
Returns
nullfor every code no function reads: the two-digit-year codes (101,201,202,206,207,301,302,713,717),209,404,406and every other 2379 code. -
Matching is exact:
" 203","0203"and the number203are not codes. -
Returns a result exactly when
isValidEdifactDtmFormatis true.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
format | string | The data element 2379 format code, as it arrives (e.g. “203”) |
Returns
Section titled “Returns”{ kind, format }, or null on invalid input
EdifactDtmFormatClass
Section titled “EdifactDtmFormatClass”A UN/EDIFACT data element 2379 format code with the kind of value it states: the result of classifyEdifactDtmFormat.
import type { EdifactDtmFormatClass } from "@northguild/gmt";type EdifactDtmFormatClass = | { /** The kind of value the code states: a calendar date, read by `parseEdifactDate`. */ kind: "date"; /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */ format: EdifactDateFormat; } | { /** The kind of value the code states: a time of day, read by `parseEdifactTime`. */ kind: "time"; /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */ format: EdifactTimeFormat; } | { /** The kind of value the code states: a local date and time, read by `parseEdifactDateTime`. */ kind: "dateTime"; /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */ format: EdifactDateTimeFormat; } | { /** The kind of value the code states: a date and time with an offset from UTC, read by `parseEdifactOffsetDateTime`. */ kind: "offsetDateTime"; /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */ format: EdifactOffsetDateTimeFormat; } | { /** The kind of value the code states: a period between two dates, read by `parseEdifactDatePeriod`. */ kind: "datePeriod"; /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */ format: EdifactDatePeriodFormat; } | { /** The kind of value the code states: a period between two local date-times, read by `parseEdifactDateTimePeriod`. */ kind: "dateTimePeriod"; /** The code itself, narrowed to the codes of that kind, so it fits the kind's functions. */ format: EdifactDateTimePeriodFormat; };Related types
Section titled “Related types”EdifactDateFormatEdifactTimeFormatEdifactDateTimeFormatEdifactOffsetDateTimeFormatEdifactDatePeriodFormatEdifactDateTimePeriodFormat
Examples
Section titled “Examples”import { classifyEdifactDtmFormat } from "@northguild/gmt/intermodal/parse";
classifyEdifactDtmFormat("102") // { kind: "date", format: "102" }