intl-schematic
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -15,3 +15,3 @@ import { callPlugins } from './plugins/core'; | ||
const doc = getLocaleDocument(); | ||
const callHook = (hook, value, processor, _input = input) => callPluginsHook(hook, value, _input, parameter, currentLocaleId, key, doc, processor) ?? key; | ||
const callHook = (hook, value, _input = input) => callPluginsHook(hook, value, _input, parameter, currentLocaleId, key, doc) ?? key; | ||
if (!doc) { | ||
@@ -21,8 +21,8 @@ return callHook('docNotFound'); | ||
const currentKey = doc[key]; | ||
if (typeof currentKey === 'undefined') { | ||
return callHook('keyNotFound'); | ||
if (currentKey == null) { | ||
return callHook('keyNotFound', key); | ||
} | ||
// Process a plain-string | ||
if (typeof currentKey !== 'object' && typeof currentKey !== 'function') { | ||
return currentKey ? callHook('keyProcessed', currentKey) : callHook('keyNotFound'); | ||
if (typeof currentKey === 'string') { | ||
return callHook('keyProcessed', currentKey); | ||
} | ||
@@ -29,0 +29,0 @@ // Process a function record |
@@ -6,4 +6,4 @@ export declare const ArrayRecordPlugin: { | ||
callHook: (hook: "initPlugin" | "docNotFound" | "keyNotFound" | "keyFound" | "processorFound" | "processorNotFound" | "keyProcessed" | "keyNotProcessed", value?: unknown) => string | undefined; | ||
}, key: unknown, input: import("..").LocaleInputParameter<any, string, any> | undefined, parameter: import("..").LocaleOptionsParameter<any, string, any> | undefined): string | undefined; | ||
}, key: unknown, input: import("..").LocaleInputParameter<any, string, any> | undefined, parameter: import("..").LocaleOptionsParameter<any, string, any> | undefined, _: () => Intl.Locale | undefined, recKey: string): string | undefined; | ||
}; | ||
//# sourceMappingURL=array-record.d.ts.map |
import { createPlugin } from './core'; | ||
export const ArrayRecordPlugin = createPlugin({ | ||
name: 'ArrayRecord', | ||
keyFound(key, input, parameter) { | ||
keyFound(key, input, parameter, _, recKey) { | ||
// Process an array record (["Some text", "translation-key"]) | ||
@@ -6,0 +6,0 @@ if (Array.isArray(key)) { |
@@ -5,3 +5,3 @@ import { Translation, LocaleInputParameter, LocaleKey, TranslationProxy, LocaleOptionsParameter } from '../ts.schema'; | ||
callHook: (hook: PluginHooks, value?: unknown) => string | undefined; | ||
}, value: unknown | undefined, input: LocaleInputParameter<Locale, LocaleKey<Locale>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, LocaleKey<Locale>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin: string | undefined) => string | undefined; | ||
}, value: unknown | undefined, input: LocaleInputParameter<Locale, LocaleKey<Locale>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, LocaleKey<Locale>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin?: string | undefined) => string | undefined; | ||
export interface Plugin<Locale extends Translation, Processors> { | ||
@@ -19,5 +19,5 @@ name: string; | ||
type PluginHooks = keyof Omit<Plugin<any, any>, 'name'>; | ||
export declare const callPlugins: <Locale extends import("../ts.schema").TranslationModule, Processors>(translate: TranslationProxy<Locale, Processors>, plugins?: Plugin<Locale, Processors>[]) => (hook: PluginHooks, value: unknown, input: LocaleInputParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin: string | undefined) => string | undefined; | ||
export declare const callPlugins: <Locale extends import("../ts.schema").TranslationModule, Processors>(translate: TranslationProxy<Locale, Processors>, plugins?: Plugin<Locale, Processors>[]) => (hook: PluginHooks, value: unknown, input: LocaleInputParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin?: string | undefined) => string | undefined; | ||
export declare const createPlugin: <T extends Plugin<any, any>>(plugin: T) => T; | ||
export {}; | ||
//# sourceMappingURL=core.d.ts.map |
@@ -16,11 +16,29 @@ export const callPlugins = (translate, plugins = []) => { | ||
}, {}); | ||
console.log(pluginsPerHook); | ||
const callPluginsForHook = (hook, ...[value, input, parameter, key, currentLocaleId, doc, initiatorPlugin]) => String(pluginsPerHook[hook]?.reduce((val, pluginHook) => (pluginHook?.call({ | ||
callHook(hook, value) { | ||
return callPluginsForHook(hook, value, input, parameter, key, currentLocaleId, doc, pluginHook.name); | ||
}, | ||
translate | ||
}, val, input, parameter, key, currentLocaleId, doc, initiatorPlugin) ?? val), value) ?? value); | ||
const callPluginsForHook = (hook, ...[value, input, parameter, currentLocaleId, key, doc, initiatorPlugin]) => { | ||
if (!pluginsPerHook[hook]) { | ||
return !value ? undefined : String(value); | ||
} | ||
let val = value; | ||
for (const pluginHook of pluginsPerHook[hook]) { | ||
const pluginResult = pluginHook.call({ | ||
callHook(_hook, value) { | ||
if (hook === _hook) { | ||
// Prevent recursion | ||
return; | ||
} | ||
return callPluginsForHook(_hook, value, input, parameter, currentLocaleId, key, doc, pluginHook.name); | ||
}, | ||
translate | ||
}, val, input, parameter, currentLocaleId, key, doc, initiatorPlugin); | ||
if (typeof pluginResult === 'string') { | ||
return pluginResult; | ||
} | ||
if (pluginResult != null) { | ||
val = pluginResult; | ||
} | ||
} | ||
return !val ? undefined : String(val); | ||
}; | ||
return callPluginsForHook; | ||
}; | ||
export const createPlugin = (plugin) => plugin; |
@@ -15,3 +15,3 @@ import { callPlugins } from './plugins/core'; | ||
const doc = getLocaleDocument(); | ||
const callHook = (hook, value, processor, _input = input) => callPluginsHook(hook, value, _input, parameter, currentLocaleId, key, doc, processor) ?? key; | ||
const callHook = (hook, value, _input = input) => callPluginsHook(hook, value, _input, parameter, currentLocaleId, key, doc) ?? key; | ||
if (!doc) { | ||
@@ -21,8 +21,8 @@ return callHook('docNotFound'); | ||
const currentKey = doc[key]; | ||
if (typeof currentKey === 'undefined') { | ||
return callHook('keyNotFound'); | ||
if (currentKey == null) { | ||
return callHook('keyNotFound', key); | ||
} | ||
// Process a plain-string | ||
if (typeof currentKey !== 'object' && typeof currentKey !== 'function') { | ||
return currentKey ? callHook('keyProcessed', currentKey) : callHook('keyNotFound'); | ||
if (typeof currentKey === 'string') { | ||
return callHook('keyProcessed', currentKey); | ||
} | ||
@@ -29,0 +29,0 @@ // Process a function record |
@@ -6,4 +6,4 @@ export declare const ArrayRecordPlugin: { | ||
callHook: (hook: "initPlugin" | "docNotFound" | "keyNotFound" | "keyFound" | "processorFound" | "processorNotFound" | "keyProcessed" | "keyNotProcessed", value?: unknown) => string | undefined; | ||
}, key: unknown, input: import("..").LocaleInputParameter<any, string, any> | undefined, parameter: import("..").LocaleOptionsParameter<any, string, any> | undefined): string | undefined; | ||
}, key: unknown, input: import("..").LocaleInputParameter<any, string, any> | undefined, parameter: import("..").LocaleOptionsParameter<any, string, any> | undefined, _: () => Intl.Locale | undefined, recKey: string): string | undefined; | ||
}; | ||
//# sourceMappingURL=array-record.d.ts.map |
import { createPlugin } from './core'; | ||
export const ArrayRecordPlugin = createPlugin({ | ||
name: 'ArrayRecord', | ||
keyFound(key, input, parameter) { | ||
keyFound(key, input, parameter, _, recKey) { | ||
// Process an array record (["Some text", "translation-key"]) | ||
@@ -6,0 +6,0 @@ if (Array.isArray(key)) { |
@@ -5,3 +5,3 @@ import { Translation, LocaleInputParameter, LocaleKey, TranslationProxy, LocaleOptionsParameter } from '../ts.schema'; | ||
callHook: (hook: PluginHooks, value?: unknown) => string | undefined; | ||
}, value: unknown | undefined, input: LocaleInputParameter<Locale, LocaleKey<Locale>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, LocaleKey<Locale>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin: string | undefined) => string | undefined; | ||
}, value: unknown | undefined, input: LocaleInputParameter<Locale, LocaleKey<Locale>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, LocaleKey<Locale>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin?: string | undefined) => string | undefined; | ||
export interface Plugin<Locale extends Translation, Processors> { | ||
@@ -19,5 +19,5 @@ name: string; | ||
type PluginHooks = keyof Omit<Plugin<any, any>, 'name'>; | ||
export declare const callPlugins: <Locale extends import("../ts.schema").TranslationModule, Processors>(translate: TranslationProxy<Locale, Processors>, plugins?: Plugin<Locale, Processors>[]) => (hook: PluginHooks, value: unknown, input: LocaleInputParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin: string | undefined) => string | undefined; | ||
export declare const callPlugins: <Locale extends import("../ts.schema").TranslationModule, Processors>(translate: TranslationProxy<Locale, Processors>, plugins?: Plugin<Locale, Processors>[]) => (hook: PluginHooks, value: unknown, input: LocaleInputParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, parameter: LocaleOptionsParameter<Locale, Extract<Exclude<keyof Locale, "$schema">, string>, Processors> | undefined, currentLocaleId: () => Intl.Locale | undefined, key: string, translationDocument: Locale | undefined, initiatorPlugin?: string | undefined) => string | undefined; | ||
export declare const createPlugin: <T extends Plugin<any, any>>(plugin: T) => T; | ||
export {}; | ||
//# sourceMappingURL=core.d.ts.map |
@@ -16,11 +16,29 @@ export const callPlugins = (translate, plugins = []) => { | ||
}, {}); | ||
console.log(pluginsPerHook); | ||
const callPluginsForHook = (hook, ...[value, input, parameter, key, currentLocaleId, doc, initiatorPlugin]) => String(pluginsPerHook[hook]?.reduce((val, pluginHook) => (pluginHook?.call({ | ||
callHook(hook, value) { | ||
return callPluginsForHook(hook, value, input, parameter, key, currentLocaleId, doc, pluginHook.name); | ||
}, | ||
translate | ||
}, val, input, parameter, key, currentLocaleId, doc, initiatorPlugin) ?? val), value) ?? value); | ||
const callPluginsForHook = (hook, ...[value, input, parameter, currentLocaleId, key, doc, initiatorPlugin]) => { | ||
if (!pluginsPerHook[hook]) { | ||
return !value ? undefined : String(value); | ||
} | ||
let val = value; | ||
for (const pluginHook of pluginsPerHook[hook]) { | ||
const pluginResult = pluginHook.call({ | ||
callHook(_hook, value) { | ||
if (hook === _hook) { | ||
// Prevent recursion | ||
return; | ||
} | ||
return callPluginsForHook(_hook, value, input, parameter, currentLocaleId, key, doc, pluginHook.name); | ||
}, | ||
translate | ||
}, val, input, parameter, currentLocaleId, key, doc, initiatorPlugin); | ||
if (typeof pluginResult === 'string') { | ||
return pluginResult; | ||
} | ||
if (pluginResult != null) { | ||
val = pluginResult; | ||
} | ||
} | ||
return !val ? undefined : String(val); | ||
}; | ||
return callPluginsForHook; | ||
}; | ||
export const createPlugin = (plugin) => plugin; |
{ | ||
"name": "intl-schematic", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -71,1 +71,33 @@ # intl-schematic (WIP) | ||
``` | ||
### Add default plugins and processors | ||
These allow to use standard [Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) features, | ||
like [`DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) | ||
or [`PluralRules`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules). | ||
```js | ||
import { createTranslator } from 'intl-schematic'; | ||
import { defaultPlugins } from 'intl-schematic/plugins'; | ||
import { defaultProcessors } from 'intl-schematic/processors'; | ||
const getDocument = () => ({ | ||
price: { | ||
processor: { number: "" }, | ||
parameter: { // Intl.NumberFormat options | ||
style: "currency", | ||
currency: "USD", | ||
currencyDisplay: "symbol", | ||
trailingZeroDisplay: "stripIfInteger" | ||
}, | ||
input: 0 // fallback | ||
} | ||
}); | ||
const t = createTranslator(getDocument, getLocale, { | ||
plugins: defaultPlugins, | ||
processors: defaultProcessors, | ||
}); | ||
console.log(t('price', 123)); // "US$123" | ||
``` |
@@ -54,5 +54,4 @@ import type { LocaleInputParameter, LocaleKey, LocaleOptionsParameter, Translation, TranslationProxy } from './ts.schema'; | ||
value?: unknown, | ||
processor?: string, | ||
_input: typeof input = input, | ||
) => callPluginsHook(hook, value, _input, parameter, currentLocaleId, key, doc, processor) ?? key; | ||
) => callPluginsHook(hook, value, _input, parameter, currentLocaleId, key, doc) ?? key; | ||
@@ -65,9 +64,9 @@ if (!doc) { | ||
if (typeof currentKey === 'undefined') { | ||
return callHook('keyNotFound'); | ||
if (currentKey == null) { | ||
return callHook('keyNotFound', key); | ||
} | ||
// Process a plain-string | ||
if (typeof currentKey !== 'object' && typeof currentKey !== 'function') { | ||
return currentKey ? callHook('keyProcessed', currentKey) : callHook('keyNotFound'); | ||
if (typeof currentKey === 'string') { | ||
return callHook('keyProcessed', currentKey); | ||
} | ||
@@ -74,0 +73,0 @@ |
@@ -5,3 +5,3 @@ import { createPlugin } from './core'; | ||
name: 'ArrayRecord', | ||
keyFound(key, input, parameter) { | ||
keyFound(key, input, parameter, _, recKey) { | ||
// Process an array record (["Some text", "translation-key"]) | ||
@@ -8,0 +8,0 @@ if (Array.isArray(key)) { |
@@ -17,3 +17,3 @@ import { Translation, LocaleInputParameter, LocaleKey, TranslationProxy, LocaleOptionsParameter } from '../ts.schema'; | ||
translationDocument: Locale | undefined, | ||
initiatorPlugin: string | undefined | ||
initiatorPlugin?: string | undefined | ||
) => string | undefined; | ||
@@ -54,5 +54,2 @@ | ||
console.log(pluginsPerHook); | ||
const callPluginsForHook = (hook: PluginHooks, ...[ | ||
@@ -62,20 +59,38 @@ value, | ||
parameter, | ||
currentLocaleId, | ||
key, | ||
currentLocaleId, | ||
doc, | ||
initiatorPlugin | ||
]: Parameters<PluginHook<Locale, Processors>>): string | undefined => String( | ||
pluginsPerHook[hook]?.reduce( | ||
(val, pluginHook) => ( | ||
pluginHook?.call({ | ||
callHook(hook, value) { | ||
return callPluginsForHook(hook, value, input, parameter, key, currentLocaleId, doc, pluginHook.name); | ||
}, | ||
translate | ||
}, val, input, parameter, key, currentLocaleId, doc, initiatorPlugin) ?? val | ||
), | ||
value | ||
) ?? value | ||
); | ||
]: Parameters<PluginHook<Locale, Processors>>): string | undefined => { | ||
if (!pluginsPerHook[hook]) { | ||
return !value ? undefined : String(value); | ||
} | ||
let val = value; | ||
for (const pluginHook of pluginsPerHook[hook]) { | ||
const pluginResult = pluginHook.call({ | ||
callHook(_hook, value) { | ||
if (hook === _hook) { | ||
// Prevent recursion | ||
return; | ||
} | ||
return callPluginsForHook(_hook, value, input, parameter, currentLocaleId, key, doc, pluginHook.name); | ||
}, | ||
translate | ||
}, val, input, parameter, currentLocaleId, key, doc, initiatorPlugin); | ||
if (typeof pluginResult === 'string') { | ||
return pluginResult; | ||
} | ||
if (pluginResult != null) { | ||
val = pluginResult; | ||
} | ||
} | ||
return !val ? undefined : String(val); | ||
}; | ||
return callPluginsForHook; | ||
@@ -82,0 +97,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
101995
1982
103
0