# classifyEdifactDtmFormat

Industry: Intermodal

## Signature

```ts
classifyEdifactDtmFormat(format: string): EdifactDtmFormatClass | null
```

Name 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 `DTM` reader holds the code as a plain string: it calls this function, tests `kind`, and passes `format` on. Testing `kind` narrows `format` to that kind's union, so the call needs no cast. ### Kinds

  | `kind` | Codes | Read by |
  |---|---|---|
  | `"date"` | `102` | `parseEdifactDate` |
  | `"time"` | `401`, `402` | `parseEdifactTime` |
  | `"dateTime"` | `203`, `204` | `parseEdifactDateTime` |
  | `"offsetDateTime"` | `205`, `208`, `303`, `304` | `parseEdifactOffsetDateTime` |
  | `"datePeriod"` | `718` | `parseEdifactDatePeriod` |
  | `"dateTimePeriod"` | `719` | `parseEdifactDateTimePeriod` |

- **The narrowing pattern.** With `const classified = classifyEdifactDtmFormat(code)`, the line `if (classified?.kind === "date") parseEdifactDate(value, classified.format)` compiles with no cast: inside the test, `classified.format` is `EdifactDateFormat`.
- Both members are always present, and each call returns a new object.
- Returns `null` for every code no function reads: the two-digit-year codes (`101`, `201`, `202`, `206`, `207`, `301`, `302`, `713`, `717`), `209`, `404`, `406` and every other 2379 code.
- Matching is exact: `" 203"`, `"0203"` and the number `203` are not codes.
- Returns a result exactly when `isValidEdifactDtmFormat` is true.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `format` | `string` | The data element 2379 format code, as it arrives (e.g. "203") |

## Returns

`{ kind, format }`, or null on invalid input

## Types

### EdifactDtmFormatClass

A UN/EDIFACT data element 2379 format code with the kind of value it states: the result of `classifyEdifactDtmFormat`.

```ts
import type { EdifactDtmFormatClass } from "@northguild/gmt";
```

```ts
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

- [`EdifactDateFormat`](/gmt/reference/types/EdifactDateFormat/)
- [`EdifactTimeFormat`](/gmt/reference/types/EdifactTimeFormat/)
- [`EdifactDateTimeFormat`](/gmt/reference/types/EdifactDateTimeFormat/)
- [`EdifactOffsetDateTimeFormat`](/gmt/reference/types/EdifactOffsetDateTimeFormat/)
- [`EdifactDatePeriodFormat`](/gmt/reference/types/EdifactDatePeriodFormat/)
- [`EdifactDateTimePeriodFormat`](/gmt/reference/types/EdifactDateTimePeriodFormat/)

## Examples

```ts
import { classifyEdifactDtmFormat } from "@northguild/gmt/intermodal/parse";

classifyEdifactDtmFormat("102") // { kind: "date", format: "102" }
```

## Source

[intermodal/parse/classifyEdifactDtmFormat.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/intermodal/parse/classifyEdifactDtmFormat.ts)
