From f85109684f453c1aea48d8b4448362362cf77f30 Mon Sep 17 00:00:00 2001 From: Raul Macarie Date: Thu, 28 Dec 2023 01:05:57 +0100 Subject: [PATCH 1/2] perf(t): create a type for the factory --- packages/t/source/factory.ts | 159 ++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 79 deletions(-) diff --git a/packages/t/source/factory.ts b/packages/t/source/factory.ts index 54669ee..bc1fa56 100644 --- a/packages/t/source/factory.ts +++ b/packages/t/source/factory.ts @@ -179,14 +179,75 @@ type WithLazyInit< } & Input : Input; -export const createTranslationsFactory = - < - SignalLike extends boolean, - LocaleGetterHookName extends string, - TranslationsGetterHookName extends string, - LocaleGetterFunctionName extends string | undefined = undefined, - TranslationsGetterFunctionName extends string | undefined = undefined, - >({ +export type CreateTranslationsFactory = < + SignalLike extends boolean, + LocaleGetterHookName extends string, + TranslationsGetterHookName extends string, + LocaleGetterFunctionName extends string | undefined = undefined, + TranslationsGetterFunctionName extends string | undefined = undefined, +>( + options: CreateTranslationsFactoryOptions< + SignalLike, + LocaleGetterFunctionName, + LocaleGetterHookName, + TranslationsGetterFunctionName, + TranslationsGetterHookName + >, +) => < + Loaders extends Record, + Locale extends keyof Loaders & string, + Translations extends Awaited>>, + Translator extends AnyTranslator, + Lazy extends boolean = false, +>( + translationLoaders: Loaders, + options: { + cache?: Partial>; + localeFrom: LocaleNegotiators; + translator: Translator; + }, + lazy?: Lazy, +) => Simplify< + WithLazyInit< + Lazy, + Locale, + CreateTranslationsInstance< + Locale, + Translations, + { + locale: { + function: LocaleGetterFunctionName extends undefined + ? undefined + : { + fn: LocaleGetter; + name: LocaleGetterFunctionName; + }; + hook: { + fn: LocaleGetter; + name: LocaleGetterHookName; + }; + setter: LocaleSetter; + }; + translations: { + function: TranslationsGetterFunctionName extends undefined + ? undefined + : { + fn: TranslationsPickerAsync; + name: TranslationsGetterFunctionName; + }; + hook: { + fn: TranslationsPicker; + name: TranslationsGetterHookName; + }; + }; + translator: Translator; + } + > + > +>; + +export const createTranslationsFactory: CreateTranslationsFactory = + ({ locale: { fn: localeFn, hook: { factory: localeHookFactory, name: localeHookName }, @@ -197,87 +258,27 @@ export const createTranslationsFactory = fn: translationsFn, hook: { factory: translationsHookFactory, name: translationsHookName }, }, - }: CreateTranslationsFactoryOptions< - SignalLike, - LocaleGetterFunctionName, - LocaleGetterHookName, - TranslationsGetterFunctionName, - TranslationsGetterHookName - >) => - < - Loaders extends Record, - Locale extends keyof Loaders & string, - Translations extends Awaited>>, - Translator extends AnyTranslator, - Lazy extends boolean = false, - >( - translationLoaders: Loaders, - { - cache: initialCache, - localeFrom: negotiators, - translator, - }: { - cache?: Partial>; - localeFrom: LocaleNegotiators; - translator: Translator; - }, - lazy?: Lazy, - ): Simplify< - WithLazyInit< - Lazy, - Locale, - CreateTranslationsInstance< - Locale, - Translations, - { - locale: { - function: LocaleGetterFunctionName extends undefined - ? undefined - : { - fn: LocaleGetter; - name: LocaleGetterFunctionName; - }; - hook: { - fn: LocaleGetter; - name: LocaleGetterHookName; - }; - setter: LocaleSetter; - }; - translations: { - function: TranslationsGetterFunctionName extends undefined - ? undefined - : { - fn: TranslationsPickerAsync; - name: TranslationsGetterFunctionName; - }; - hook: { - fn: TranslationsPicker; - name: TranslationsGetterHookName; - }; - }; - translator: Translator; - } - > - > - > => { - const initTranslator = (initNegotiators?: LocaleNegotiators) => { - for (const [locale, loader] of Object.entries(translationLoaders) as [ - Locale, - LazyLoader, - ][]) { + }) => + ( + translationLoaders, + { cache: initialCache, localeFrom: negotiators, translator }, + lazy, + ) => { + const initTranslator = (initNegotiators?: LocaleNegotiators) => { + for (const [locale, loader] of Object.entries(translationLoaders)) { loaders.set(locale, loader); } if (initialCache) { for (const [locale, translations] of Object.entries(initialCache) as [ - Locale, - Translations, + string, + AnyTranslations, ][]) { globalCache.set(locale, translations); } } - const availableLocales = Object.keys(translationLoaders) as Locale[]; + const availableLocales = Object.keys(translationLoaders); const activeNegotiators = initNegotiators !== undefined ? initNegotiators : negotiators; From fe9cf7f395dd0d7a62f26e3fefa1b3fda0e3759d Mon Sep 17 00:00:00 2001 From: Raul Macarie Date: Thu, 28 Dec 2023 01:06:21 +0100 Subject: [PATCH 2/2] perf(t): export types --- packages/t/source/factory.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/t/source/factory.ts b/packages/t/source/factory.ts index bc1fa56..0f84281 100644 --- a/packages/t/source/factory.ts +++ b/packages/t/source/factory.ts @@ -8,12 +8,12 @@ import type { } from "@wluwd/t-utils"; import type { Get, Simplify, ValueOf } from "type-fest"; -type ConcatKeys< +export type ConcatKeys< Previous extends string, New extends string, > = Previous extends "" ? New : `${Previous}.${New}`; -type PathsToBranches< +export type PathsToBranches< Branch extends Record, KeysFound extends string = "", > = Simplify< @@ -27,9 +27,9 @@ type PathsToBranches< > & string; -type AnyFunction = (...args: any) => any; +export type AnyFunction = (...args: any) => any; -interface Factory< +export interface Factory< Fn extends AnyFunction = AnyFunction, Name extends string | undefined = string, > { @@ -37,7 +37,7 @@ interface Factory< name: Name; } -interface Fn< +export interface Fn< Fn extends AnyFunction = AnyFunction, Name extends string | undefined = string, > { @@ -45,7 +45,7 @@ interface Fn< name: Name; } -type LocaleSetter = ( +export type LocaleSetter = ( locale: AllowedLocales, ) => void; @@ -94,14 +94,14 @@ export interface CreateTranslationsFactoryOptions< }; } -type AnyTranslator = (translation: string, data: any) => string; +export type AnyTranslator = (translation: string, data: any) => string; -type LocaleGetter< +export type LocaleGetter< Locale extends string, SignalLike extends boolean, > = () => SignalLike extends true ? () => Locale : Locale; -type TranslationsPicker< +export type TranslationsPicker< Translations extends AnyTranslations, SignalLike extends boolean, > = >( @@ -110,19 +110,19 @@ type TranslationsPicker< ? () => Get : Get; -type TranslationsPickerAsync = < +export type TranslationsPickerAsync = < Prefix extends PathsToBranches, >( prefix: Prefix, ) => Promise>; -type LocaleGetterHookBuilder< +export type LocaleGetterHookBuilder< Options extends Fn, string>, > = { [key in Options["name"]]: Options["fn"]; }; -type LocaleGetterFunctionBuilder< +export type LocaleGetterFunctionBuilder< Options extends Fn, string> | undefined, > = Options extends object ? { @@ -130,13 +130,13 @@ type LocaleGetterFunctionBuilder< } : object; -type TranslationsGetterHookBuilder< +export type TranslationsGetterHookBuilder< Options extends Fn, string>, > = { [key in Options["name"]]: Options["fn"]; }; -type TranslationsGetterFunctionBuilder< +export type TranslationsGetterFunctionBuilder< Options extends | Fn, string> | undefined, @@ -146,7 +146,7 @@ type TranslationsGetterFunctionBuilder< } : object; -type CreateTranslationsInstance< +export type CreateTranslationsInstance< Locale extends string, Translations extends AnyTranslations, Options extends { @@ -169,7 +169,7 @@ type CreateTranslationsInstance< t: Options["translator"]; }; -type WithLazyInit< +export type WithLazyInit< Lazy extends boolean, Locale extends string, Input extends object,