Skip to content

Latest commit

 

History

History
53 lines (44 loc) · 2.22 KB

README.md

File metadata and controls

53 lines (44 loc) · 2.22 KB

Type-safe ISO 639-1 language code for TypeScript

deno.land/x/iso639_1 MIT License GitHub Actions Codecov

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 "https://deno.land/x/iso639_1/mod.ts";

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.