Attributes
Includes Deno configuration
Repository
Current version released
10 months ago
Type-safe ISO 639-1 language code for TypeScript
This library provides a set of functions to work with ISO 639-1 language codes, a.k.a. two-letter language codes, in very type-safe way.
Although it does not introduce any new runtime types, it provides
the LanguageCode type, which is a narrowed-down version of
the string type that contains only valid two-letter language codes
defined in the ISO 639-1.
There are three ways to narrow down the string type to the
LanguageCode type: isLanguageCode, parseLanguageCode,
and validateLanguageCode. The following example shows how to narrow
down a string value to the LanguageCode type:
import { authoritativeLabels, isLanguageCode } from "@hongminhee/iso639-";
const code: string | null = prompt("Enter a two-letter language code:");
if (isLanguageCode(code)) {
// code is now narrowed down to LanguageCode
console.log("The language you chose is", authoritativeLabels[code].en);
} else {
console.error("Invalid language code:", code);
}For details, see the API references.