UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

551 lines (550 loc) • 12.3 kB
// src/internal/ediGrammar.ts /** What each half of a value of each kind is: a period of dates is two dates. */ const VALUE_KIND = { date: "date", time: "time", dateTime: "dateTime", offsetDateTime: "offsetDateTime", datePeriod: "date", dateTimePeriod: "dateTime", dateRange: "date", dateTimeRange: "dateTime" }; /** Hour, 00–23. */ const HH = "([01][0-9]|2[0-3])"; /** Minute, 00–59. */ const MI = "([0-5][0-9])"; /** The regex source of each part: one capture group, the field's whole range of digits. */ const FRAGMENT = { CCYY: "(\\d{4})", MM: "(0[1-9]|1[0-2])", DD: "(0[1-9]|[12][0-9]|3[01])", HH, MI, SS: "([0-5][0-9])", SD: "(\\d)", SDD: "(\\d{2})", ZS: "([+-])", ZH: HH, ZM: MI, ZZZ: "([+-](?:[01][0-9]|2[0-3])|UTC|GMT)" }; /** The `ZHHMM` offset of 2379 codes 205 and 208: sign, hours, minutes. */ const ZHHMM = [ "ZS", "ZH", "ZM" ]; /** The parts' sources joined, anchored, flagless; a period's halves joined by `join`. */ function grammarOf(layout, join) { const source = (parts) => parts.map((part) => FRAGMENT[part]).join(""); const end = layout.end === void 0 ? "" : `${join}${source(layout.end)}`; return new RegExp(`^${source(layout.start)}${end}$`); } /** A grammar table from a layout table, in the layout table's key order. */ function grammarsOf(layouts, join) { const grammars = {}; for (const code of Object.keys(layouts)) grammars[code] = grammarOf(layouts[code], join); return grammars; } /** * The 11 UNTDID 2379 codes GMT reads, in directory order: a date (`102`), a date and time * (`203`, `204`), a date and time with a `ZHHMM` offset (`205`, `208`), a date and time with * three zone characters (`303`, `304`), a time (`401`, `402`) and a period (`718`, `719`). * * Every other 2379 code returns the sentinel; the file header lists the ones cut and why. */ const EDIFACT_DTM_FORMATS = [ "102", "203", "204", "205", "208", "303", "304", "401", "402", "718", "719" ]; /** * UNTDID 2379 code → its kind and its value's parts. Typed kind by kind from the public unions, * so a missing code, and a code filed under a kind its union does not name, both fail typecheck. */ const EDIFACT_DTM_LAYOUT = { "102": { kind: "date", start: [ "CCYY", "MM", "DD" ] }, "203": { kind: "dateTime", start: [ "CCYY", "MM", "DD", "HH", "MI" ] }, "204": { kind: "dateTime", start: [ "CCYY", "MM", "DD", "HH", "MI", "SS" ] }, "205": { kind: "offsetDateTime", start: [ "CCYY", "MM", "DD", "HH", "MI", ...ZHHMM ] }, "208": { kind: "offsetDateTime", start: [ "CCYY", "MM", "DD", "HH", "MI", "SS", ...ZHHMM ] }, "303": { kind: "offsetDateTime", start: [ "CCYY", "MM", "DD", "HH", "MI", "ZZZ" ] }, "304": { kind: "offsetDateTime", start: [ "CCYY", "MM", "DD", "HH", "MI", "SS", "ZZZ" ] }, "401": { kind: "time", start: ["HH", "MI"] }, "402": { kind: "time", start: [ "HH", "MI", "SS" ] }, "718": { kind: "datePeriod", start: [ "CCYY", "MM", "DD" ], end: [ "CCYY", "MM", "DD" ] }, "719": { kind: "dateTimePeriod", start: [ "CCYY", "MM", "DD", "HH", "MI" ], end: [ "CCYY", "MM", "DD", "HH", "MI" ] } }; /** * UNTDID 2379 code → the anchored, flagless grammar of its value. A period's halves are run * together with nothing between them: every directory read gives a period "without hyphen" (the * file header quotes each entry). */ const EDIFACT_DTM_GRAMMAR = grammarsOf(EDIFACT_DTM_LAYOUT, ""); /** * The 10 X12 1250 codes GMT reads: a date (`D8`, `DB`), a date and time (`DT`, `RTS`), a time * (`TM`, `TS`), a range of dates (`RD8`, `RD`) and a range of date-times (`RDT`, `DTS`). * * The other 32 of the 42 codes return the sentinel; the file header lists the ones cut and why. * `TS` is also a code of data element 623, with an unrelated meaning there. */ const X12_DATE_TIME_PERIOD_FORMATS = [ "D8", "DB", "DT", "RTS", "TM", "TS", "RD8", "RD", "RDT", "DTS" ]; /** * X12 1250 code → its kind and its value's parts. Typed kind by kind from the public unions, so * a missing code, and a code filed under a kind its union does not name, both fail typecheck. */ const X12_DATE_TIME_PERIOD_LAYOUT = { D8: { kind: "date", start: [ "CCYY", "MM", "DD" ] }, DB: { kind: "date", start: [ "MM", "DD", "CCYY" ] }, DT: { kind: "dateTime", start: [ "CCYY", "MM", "DD", "HH", "MI" ] }, RTS: { kind: "dateTime", start: [ "CCYY", "MM", "DD", "HH", "MI", "SS" ] }, TM: { kind: "time", start: ["HH", "MI"] }, TS: { kind: "time", start: [ "HH", "MI", "SS" ] }, RD8: { kind: "dateRange", start: [ "CCYY", "MM", "DD" ], end: [ "CCYY", "MM", "DD" ] }, RD: { kind: "dateRange", start: [ "MM", "DD", "CCYY" ], end: [ "MM", "DD", "CCYY" ] }, RDT: { kind: "dateTimeRange", start: [ "CCYY", "MM", "DD", "HH", "MI" ], end: [ "CCYY", "MM", "DD", "HH", "MI" ] }, DTS: { kind: "dateTimeRange", start: [ "CCYY", "MM", "DD", "HH", "MI", "SS" ], end: [ "CCYY", "MM", "DD", "HH", "MI", "SS" ] } }; /** * X12 1250 code → the anchored, flagless grammar of its value. A range's halves are joined by * exactly one hyphen, the one X12 transmits. */ const X12_DATE_TIME_PERIOD_GRAMMAR = grammarsOf(X12_DATE_TIME_PERIOD_LAYOUT, "-"); /** * X12 data element 337 (Time), the time element of `AT7`, `G62` and `DTM-03`: "Time expressed in * 24-hour clock time as follows: HHMM, or HHMMSS, or HHMMSSD, or HHMMSSDD, where H = hours * (00-23), M = minutes (00-59), S = integer seconds (00-59) and DD = decimal seconds; decimal * seconds are expressed as follows: D = tenths (0-9) and DD = hundredths (00-99)", length 4 to 8 * (release 005010, https://www.stedi.com/edi/x12-005010/element/337). * * Mask → its parts, keyed by the element's own four masks in the order the definition gives * them. Not a 1250 table: the element has no format qualifier, its four- and six-digit forms * coincide with `TM` and `TS`, and no 1250 code holds decimal seconds. Typed from the public * union, so a missing form fails typecheck. */ const X12_TIME_ELEMENT_LAYOUT = { HHMM: { kind: "time", start: ["HH", "MI"] }, HHMMSS: { kind: "time", start: [ "HH", "MI", "SS" ] }, HHMMSSD: { kind: "time", start: [ "HH", "MI", "SS", "SD" ] }, HHMMSSDD: { kind: "time", start: [ "HH", "MI", "SS", "SDD" ] } }; /** * The four forms of X12 data element 337, shortest first: the layout table's own keys, so the * list cannot name a form the table lacks. */ const X12_TIME_ELEMENT_FORMS = Object.keys(X12_TIME_ELEMENT_LAYOUT); /** * Element 337 form → the anchored, flagless grammar of its value. The four masks have four * lengths (4, 6, 7 and 8 digits), so a value fits one form or none, and the reader needs no * qualifier to tell them apart. */ const X12_TIME_ELEMENT_GRAMMAR = grammarsOf(X12_TIME_ELEMENT_LAYOUT, ""); /** * The milliseconds one unit of each decimal-seconds part is worth: a tenth of a second is 100 * and a hundredth is 10. The reader multiplies by it and the writer divides by it, so the two * agree on what `D` and `DD` mean. */ const MILLISECONDS_PER_DECIMAL_SECOND = { SD: 100, SDD: 10 }; /** * Look a form of X12 data element 337 up by its mask, for the writer * (`writeX12TimeElement`), whose caller names the form. The reader (`readX12Time`) is given no * form: it tries the grammar of each form in the same table. * * - Matching is exact and by own key, so `"__proto__"`, `" HHMM"` and `"hhmm"` are not forms. * - Null for a data element 1250 code (`TM`, `TS`): a qualifier is not a mask. * - Null for a non-string argument. * * @param form the element's mask: `HHMM`, `HHMMSS`, `HHMMSSD` or `HHMMSSDD` * @returns the form's parts in mask order, as a single time, or null * * @example x12TimeElementLayoutOf("HHMMSSDD") // { kind: "time", start: ["HH", "MI", "SS", "SDD"] } * @example x12TimeElementLayoutOf("TS") // null (a 1250 code, not a mask) * @example x12TimeElementLayoutOf("__proto__") // null */ function x12TimeElementLayoutOf(form) { return typeof form === "string" && Object.hasOwn(X12_TIME_ELEMENT_LAYOUT, form) ? X12_TIME_ELEMENT_LAYOUT[form] : null; } /** * The 56 X12 623 codes, release 008010, in dictionary order: `01`–`12` (UTC+1 … UTC+12), `13`–`24` * (UTC−12 … UTC−1, descending), `25`–`29` (the half-hour offsets, added in release 006010), then * the 27 letter codes. Release 005010 has the 51 codes other than `25`–`29`. Membership is by * this list, never by shape. `TS`, `TT`, `CD` and `MD` are also codes of data element 1250, with * unrelated meanings there. */ const X12_TIME_CODES = [ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "AD", "AS", "AT", "CD", "CS", "CT", "ED", "ES", "ET", "GM", "HD", "HS", "HT", "LT", "MD", "MS", "MT", "ND", "NS", "NT", "PD", "PS", "PT", "TD", "TS", "TT", "UT" ]; const EDI_CODE_TABLES = { edifact: { layouts: EDIFACT_DTM_LAYOUT, grammars: EDIFACT_DTM_GRAMMAR, rangeSeparator: "" }, x12: { layouts: X12_DATE_TIME_PERIOD_LAYOUT, grammars: X12_DATE_TIME_PERIOD_GRAMMAR, rangeSeparator: "-" } }; /** * Look a format code up in its standard's tables: the one lookup the classifier, the reader and * the writer share. * * - Matching is exact and by own key, so `"__proto__"`, `" 102"` and `"d8"` are not codes. * - Null for a code that is not read (`101`, `209`, `D6`, `UN`), like an unknown code. * - Null for a standard that is not `"edifact"` or `"x12"`, and for a non-string argument. * * @param standard which element the code belongs to * @param code the 2379 or 1250 format code * @returns the code's kind, layout, grammar and range separator, or null * * @example ediCodeOf("edifact", "718") // { kind: "datePeriod", valueKind: "date", layout: { kind: "datePeriod", start: ["CCYY", "MM", "DD"], end: [...] }, grammar: /^…$/, rangeSeparator: "" } * @example ediCodeOf("x12", "RD8") // { kind: "dateRange", valueKind: "date", layout: {...}, grammar: /^…$/, rangeSeparator: "-" } * @example ediCodeOf("edifact", "101") // null (a two-digit year: not read) * @example ediCodeOf("edifact", "D8") // null (an X12 code) */ function ediCodeOf(standard, code) { if (typeof standard !== "string" || typeof code !== "string") return null; if (!Object.hasOwn(EDI_CODE_TABLES, standard)) return null; const table = EDI_CODE_TABLES[standard]; if (!Object.hasOwn(table.layouts, code)) return null; const layout = table.layouts[code]; const grammar = table.grammars[code]; return { kind: layout.kind, valueKind: VALUE_KIND[layout.kind], layout, grammar, rangeSeparator: table.rangeSeparator }; } /** * Look a format code up as a code of one kind: null unless the code is in its standard's tables * under exactly that kind. Each public function names its own kind here, so a code of another * kind (`203` given to a date function) is refused before any value is read. * * @param standard which element the code belongs to * @param kind the kind of value the calling function reads or writes * @param code the 2379 or 1250 format code * @returns the code's entry, or null when the code is not one of that kind * * @example ediCodeOfKind("edifact", "date", "102") // { kind: "date", … } * @example ediCodeOfKind("edifact", "date", "203") // null (a date-time code) * @example ediCodeOfKind("x12", "dateRange", "RD8") // { kind: "dateRange", … } */ function ediCodeOfKind(standard, kind, code) { const entry = ediCodeOf(standard, code); return entry !== null && entry.kind === kind ? entry : null; } export { EDIFACT_DTM_FORMATS, EDIFACT_DTM_GRAMMAR, MILLISECONDS_PER_DECIMAL_SECOND, X12_DATE_TIME_PERIOD_FORMATS, X12_DATE_TIME_PERIOD_GRAMMAR, X12_TIME_CODES, X12_TIME_ELEMENT_FORMS, X12_TIME_ELEMENT_GRAMMAR, X12_TIME_ELEMENT_LAYOUT, ediCodeOf, ediCodeOfKind, x12TimeElementLayoutOf };