@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.
156 lines • 9.09 kB
TypeScript
import { EdifactDtmFormat, EdifactDtmFormatClass, X12DateTimePeriodFormat, X12DateTimePeriodFormatClass, X12TimeCode, X12TimeElementForm } from "../types/edi.js";
/**
* One field of a mask, named as the standards print it. Each part is one capture group. The
* standards write `MM` for both month and minute; `MI` is this file's name for the minute. `ZS`,
* `ZH` and `ZM` are the sign, hours and minutes of the `ZHHMM` offset of codes 205 and 208.
* Element 337 writes `D` for tenths of a second and `DD` for hundredths, and `DD` is already the
* day of the month: `SD` and `SDD` are this file's names for the two.
*/
export type EdiPart = "CCYY" | "MM" | "DD" | "HH" | "MI" | "SS" | "SD" | "SDD" | "ZS" | "ZH" | "ZM" | "ZZZ";
/** The kind names `classifyEdifactDtmFormat` returns: one per UN/EDIFACT kind of value. */
export type EdifactDtmKind = EdifactDtmFormatClass["kind"];
/** The kind names `classifyX12DateTimePeriodFormat` returns: one per X12 kind of value. */
export type X12DateTimePeriodKind = X12DateTimePeriodFormatClass["kind"];
/** The kind of value a format code states, in either standard's own name for it. */
export type EdiKind = EdifactDtmKind | X12DateTimePeriodKind;
/**
* What one half of a value is: the kind of ISO 8601 string the reader returns for it and the
* writer takes. A single value has one half; a period or range has two of the same kind.
*/
export type EdiValueKind = "date" | "time" | "dateTime" | "offsetDateTime";
/** A code's value: its kind, its parts in mask order, and for a period or range the end's parts too. */
export interface EdiLayout<Kind extends EdiKind = EdiKind> {
readonly kind: Kind;
readonly start: readonly EdiPart[];
readonly end?: readonly EdiPart[];
}
/** The standard a format code belongs to. */
export type EdiStandard = "edifact" | "x12";
/** One format code of one standard, as the classifier, the reader and the writer all look it up. */
export interface EdiCode {
/** The kind of value the code states, as its standard's classifier names it. */
readonly kind: EdiKind;
/** What each half of the value is. */
readonly valueKind: EdiValueKind;
/** The code's parts in mask order. */
readonly layout: EdiLayout;
/** The anchored grammar of the code's value. */
readonly grammar: RegExp;
/** What the standard transmits between the halves of a period or range. */
readonly rangeSeparator: string;
}
/**
* 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.
*/
export declare const EDIFACT_DTM_FORMATS: readonly EdifactDtmFormat[];
/**
* 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).
*/
export declare const EDIFACT_DTM_GRAMMAR: Readonly<Record<EdifactDtmFormat, RegExp>>;
/**
* 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.
*/
export declare const X12_DATE_TIME_PERIOD_FORMATS: readonly X12DateTimePeriodFormat[];
/**
* X12 1250 code → the anchored, flagless grammar of its value. A range's halves are joined by
* exactly one hyphen, the one X12 transmits.
*/
export declare const X12_DATE_TIME_PERIOD_GRAMMAR: Readonly<Record<X12DateTimePeriodFormat, RegExp>>;
/**
* 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.
*/
export declare const X12_TIME_ELEMENT_LAYOUT: { readonly [Form in X12TimeElementForm]: EdiLayout<"time">; };
/**
* 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.
*/
export declare const X12_TIME_ELEMENT_FORMS: readonly X12TimeElementForm[];
/**
* 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.
*/
export declare const X12_TIME_ELEMENT_GRAMMAR: Readonly<Record<X12TimeElementForm, RegExp>>;
/**
* 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.
*/
export declare const MILLISECONDS_PER_DECIMAL_SECOND: Readonly<Record<"SD" | "SDD", number>>;
/**
* 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
*/
export declare function x12TimeElementLayoutOf(form: unknown): EdiLayout<"time"> | 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.
*/
export declare const X12_TIME_CODES: readonly X12TimeCode[];
/**
* 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)
*/
export declare function ediCodeOf(standard: unknown, code: unknown): EdiCode | null;
/**
* 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", … }
*/
export declare function ediCodeOfKind(standard: EdiStandard, kind: EdiKind, code: unknown): EdiCode | null;