Skip to content
greek conversion

Converters, options, and errors

Options are given to each conversion, and checked strictly. Two objects can be made once and reused, and they do different things:

  • Prepared options fix how the text is written. prepareOptions() checks and resolves options once, preset included, and every conversion takes the result in their place.
  • A converter fixes which characters a conversion knows. createConverter() can bind a preset, whose style then applies to each call and whose standard’s characters it holds the text to; it can also add characters, or restrict them to a list or by exclusion. Its methods convert, with options given at each call.
Prepared options A converter
Made by prepareOptions() createConverter()
It fixes How the text is written Which characters are converted
Use it for Options of one’s own, over many calls A standard’s characters, or characters of one’s own
It throws When made When made, and at a call: for another preset, or a character it rejects

The two work together: a converter takes options at each call, prepared or not. With both a converter and options of one’s own, prepare the options with the converter’s preset, as Prepared options in a converter shows.

A misspelled option never falls back silently to a default. An unknown key, value, or preset throws an OptionError, a RangeError that names the option (path), what it received, what it accepts, and the current spelling of a key or value an earlier release knew; its message also names the option a key was likely meant for, such as transliteration.longVowels for transliteration.long_vowels. An unknown format throws a plain RangeError, and an option group of the wrong type a TypeError. This matters most when options come from a configuration file or a form:

import { convert, OptionError } from "@humanities/greek-conversion";
const options = JSON.parse('{ "transliteration": { "eta": "ē" } }');
try {
convert("λόγος", "greek", "transliteration", options);
} catch (error) {
if (!(error instanceof OptionError)) throw error;
error.message;
// → Invalid value for transliteration.eta: "ē"; expected one of "e", "i".
error.path; // → transliteration.eta
error.accepted; // → ["e","i"]
}

FORMATS and CONVERSION_OPTION_VALUES list everything that is accepted, which is convenient for building a user interface:

import {
CONVERSION_OPTION_VALUES,
FORMATS,
} from "@humanities/greek-conversion";
FORMATS; // → ["greek","beta-code","transliteration"]
CONVERSION_OPTION_VALUES.transliteration.eta; // → ["e","i"]

prepareOptions() checks and resolves options once, with the same errors, and returns prepared options, which every conversion function takes in their place:

import { convert, prepareOptions } from "@humanities/greek-conversion";
const options = prepareOptions({ transliteration: { chi: "kh" } });
const words = ["χάος", "ψυχή"];
words.map((word) => convert(word, "greek", "transliteration", options));
// → ["kháos","psykhḗ"]

The defaults and each preset named are resolved once anyway. Other options are checked and resolved at each call, which a word takes about twice as long to convert: prepare them where many short texts are converted, or where options come from a configuration file or a form, to check them once. Prepared options cannot be changed, nor spread into other options, which they would leave empty: spread, or given as source, they throw a TypeError that says what to write instead. A converter takes them too, as Prepared options in a converter shows.

createConverter() makes a converter from its configuration, which is not options: preset, the preset it binds; outOfScopeBehavior, what it does with the characters outside its scope; aliases and characters, the spellings and characters it adds; repertoire and exclude, the characters it keeps to or leaves out. Options given there throw an OptionError (“Unknown converter option: transliteration”), and prepared options a TypeError.

A converter has the conversion functions as methods, which take options as the functions do: convert(), reencode(), convertDetailed(), convertGreekPassages(), and convertGreekPassagesDetailed(). In the Greek passages of a text, its own Greek spellings, those of its aliases in Greek and the Greek forms of its characters, are Greek, whatever characters they are made of.

A preset plays one part in options, and two in a converter:

  • Named in options, as { preset: "sbl-general" }, a preset is a style: a set of options, which the defaults complete and the options given with it refine. Every conversion function takes it so, and prepareOptions() too: prepared options hold a preset’s style, never its scope.
  • Bound by a converter, as the preset of its configuration, it is that style and a scope: the characters that its standard provides for, as the preset repertoire audit records them. The converter preserves and reports the others, or rejects them. The options of a call refine the bound preset’s; another preset throws a RangeError.

The SBL Handbook makes no provision for the digamma. Named, the preset writes it as the library does; bound, the converter keeps it in Greek and says where:

import { convert, createConverter } from "@humanities/greek-conversion";
convert("ὁ ϝάναξ", "greek", "transliteration", { preset: "sbl-general" });
// → ho wanax
const sbl = createConverter({ preset: "sbl-general" });
const result = sbl.convertDetailed("ὁ ϝάναξ", "greek", "transliteration");
result.output; // → ho ϝanax
result.diagnostics[0].code; // → out-of-scope-character
result.diagnostics[0].offset; // → 2

With outOfScopeBehavior: "reject", the conversion throws instead, and the error’s diagnostics list each rejected occurrence; here, koppa, for which ISO 843 defines no transliteration:

import { createConverter } from "@humanities/greek-conversion";
const iso = createConverter({
preset: "iso-843-type-1",
outOfScopeBehavior: "reject",
});
iso.convert("ϟ", "greek", "transliteration");
// throws CharacterScopeError: Conversion rejected: 1 unsupported character.

source is the exception to the bound preset: it describes the input, is resolved on its own, and may name any preset, so that source: {} reads the library’s default style, not the bound preset’s. Name a preset for its style alone: convert() given its name costs less than a converter. Bind it where the text must keep to its standard’s characters. See character extensions and repertoires.

A converter can teach new spellings of a letter, aliases, or characters of its own, characters; keep to a list of characters, repertoire; or leave some out, exclude:

import { createConverter } from "@humanities/greek-conversion";
const converter = createConverter({
aliases: [
{ letter: "theta", format: "transliteration", spellings: ["þ"] },
],
exclude: ["stigma", "koppa", "sampi"],
});
converter.convert("þeos", "transliteration", "greek"); // → θεος

An alias inherits every rule of its built-in letter. A new character definition maps directly between formats and keeps its case, but does not join the contextual rules for diphthongs, breathings, or numerals. Excluded characters are kept as the source writes them in their word. Of the options, only the transformations of the whole text (text) still apply to them:

import { createConverter } from "@humanities/greek-conversion";
const noStigma = createConverter({ exclude: ["stigma"] });
noStigma.convert("λόγος ϛ", "greek", "transliteration", {
text: { letterCase: "uppercase" },
});
// → LÓGOS Ϛ

See character extensions and repertoires for the definitions of aliases and characters, and the identifiers of the characters.

A converter takes options at each call, as the functions do, and prepared options among them. Prepared options are already resolved: the values of a preset, or of the defaults, merged with the options given. Which values were given is lost, so they cannot be resolved again on another preset: a converter takes options prepared with its preset, or any when it binds none, and throws a RangeError for others. With a converter and options of one’s own, prepare the options once, with the converter’s preset, and give them to each call:

import {
createConverter,
prepareOptions,
} from "@humanities/greek-conversion";
const sbl = createConverter({
preset: "sbl-general",
outOfScopeBehavior: "reject",
});
const capitals = prepareOptions({
preset: "sbl-general",
text: { letterCase: "uppercase" },
});
["λόγος", "ψυχή"].map((word) =>
sbl.convert(word, "greek", "transliteration", capitals)
);
// → ["LOGOS","PSYCHĒ"]

Options prepared without the preset would throw: “Converter is bound to preset sbl-general; received options prepared without a preset.”

Each preset imported from @humanities/greek-conversion/presets is options prepared with that preset, which a converter bound to the same preset takes.