From 66849ed309e29db17681beab84a68bdbafbe106e Mon Sep 17 00:00:00 2001 From: Pascal Duez Date: Mon, 9 Sep 2024 10:12:15 +0200 Subject: [PATCH] Add Flow declarations --- CHANGELOG.md | 3 +++ index.js.flow | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 75 insertions(+) create mode 100644 index.js.flow diff --git a/CHANGELOG.md b/CHANGELOG.md index 518e8d0..c6a8909 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + - Add local Flow declarations (shipped with the package). + We don't necessarily want to contribute the fork definitions to flow-typed. ## [1.0.0] - 2024-09-06 ### Changed diff --git a/index.js.flow b/index.js.flow new file mode 100644 index 0000000..82b5f37 --- /dev/null +++ b/index.js.flow @@ -0,0 +1,71 @@ +// @flow + +export type Options = { + locale?: string, + fallbackLocale?: string, + separator?: string, + fallback?: string, + scope?: string | string[], + interpolate?: boolean, + format?: 'default' | string, + type?: 'datetime' | string, + // @TODO: check if other types than number are really allowed. + count?: number | string | null, + ... +}; + +export type NotFoundHandler = (evt: { + ...CustomEvent, + detail: { locale: string, key: string, fallback: string, scope: string }, + ... +}) => void; + +export type LocaleChangeHandler = (evt: { + ...CustomEvent, + detail: { + locale: string, + previous: string, + }, + ... +}) => void; + +export type ErrorHandler = (evt: { + ...CustomEvent, + detail: { + error: Error, + entry: string, + values: Options, + }, + ... +}) => void; + +declare function translate(key: string | string[], options?: Options): string; + +declare class Counterpart { + constructor(): this; + + translate: typeof translate; + localize(date: Date, options: { ... }): string; + setSeparator(value: string): string; + setMissingEntryGenerator(callback: (value: string) => void): void; + getLocale(): string; + setLocale(value: string): string; + setFallbackLocale(value: string | string[]): void; + registerTranslations(locale: string, data: { ... }): void; + registerInterpolations(data: { ... }): void; + setKeyTransformer( + callback: (value: string, options: { ... }) => string, + ): string; + onLocaleChange(callback: LocaleChangeHandler): void; + offLocaleChange(callback: LocaleChangeHandler): void; + onTranslationNotFound(callback: NotFoundHandler): void; + offTranslationNotFound(callback: NotFoundHandler): void; + onError(callback: ErrorHandler): void; + offError(callback: ErrorHandler): void; +} + +declare module.exports: { + Instance: Class, + Translator: Class, +} & Counterpart & + typeof translate; diff --git a/package.json b/package.json index acef229..802a293 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "files": [ "locales", "index.js", + "index.js.flow", "strftime.js", "README.md", "LICENSE"