From 68c0444771ad1a37f91ae380e6ac34bc8e2c634d Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 3 Jan 2024 20:29:33 +0100 Subject: [PATCH] Write declaration file for new lib --- src/@types/country-language.d.ts | 21 +++++++++++++++++++++ src/utils/language.ts | 32 +++++++------------------------- 2 files changed, 28 insertions(+), 25 deletions(-) create mode 100644 src/@types/country-language.d.ts diff --git a/src/@types/country-language.d.ts b/src/@types/country-language.d.ts new file mode 100644 index 00000000..c0badd74 --- /dev/null +++ b/src/@types/country-language.d.ts @@ -0,0 +1,21 @@ +declare module "@ladjs/country-language" { + export interface LanguageObj { + countries: Array<{ + code_2: string; + code_3: string; + numCode: string; + }>; + direction: "RTL" | "LTR"; + name: string[]; + nativeName: string[]; + iso639_1: string; + } + + type Callback = (err: null | string, result: null | T) => void; + + declare namespace lib { + function getLanguage(locale: string, cb: Callback): void; + } + + export = lib; +} diff --git a/src/utils/language.ts b/src/utils/language.ts index 420c8d93..bef4e212 100644 --- a/src/utils/language.ts +++ b/src/utils/language.ts @@ -1,4 +1,4 @@ -import countryLanguages from "@ladjs/country-language"; +import countryLanguages, { LanguageObj } from "@ladjs/country-language"; import { getTag } from "@sozialhelden/ietf-language-tags"; const languageOrder = ["en", "hi", "fr", "de", "nl", "pt"]; @@ -48,18 +48,6 @@ export interface LocaleInfo { isRtl?: boolean; } -interface LanguageObj { - countries: Array<{ - code_2: string; - code_3: string; - numCode: string; - }>; - direction: "RTL" | "LTR"; - name: string[]; - nativeName: string[]; - iso639_1: string; -} - const extraLanguages: Record = { pirate: { code: "pirate", @@ -136,12 +124,9 @@ export function getCountryCodeForLocale(locale: string): string | null { const tag = getTag(locale, true); if (!tag?.language?.Subtag) return null; // this function isnt async, so its garuanteed to work like this - countryLanguages.getLanguage( - tag.language.Subtag, - (_err: string, lang: LanguageObj) => { - if (lang) output = lang; - }, - ); + countryLanguages.getLanguage(tag.language.Subtag, (_err, lang) => { + if (lang) output = lang; + }); if (!output) return null; if (output.countries.length === 0) return null; const priority = countryPriority[output.iso639_1.toLowerCase()]; @@ -169,12 +154,9 @@ export function getLocaleInfo(locale: string): LocaleInfo | null { let output: LanguageObj | null = null as any as LanguageObj; // this function isnt async, so its garuanteed to work like this - countryLanguages.getLanguage( - tag.language.Subtag, - (_err: string, lang: LanguageObj) => { - if (lang) output = lang; - }, - ); + countryLanguages.getLanguage(tag.language.Subtag, (_err, lang) => { + if (lang) output = lang; + }); if (!output) return null; return {