refactor(editor): Fix remaining FE type check errors (no-changelog) (#9607)

Co-authored-by: Alex Grozav <alex@grozav.com>
This commit is contained in:
Ricardo Espinoza
2024-06-10 09:23:06 -04:00
committed by GitHub
parent 1e15f73b0d
commit 22bdb0568e
84 changed files with 438 additions and 318 deletions

View File

@@ -6,6 +6,9 @@ const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;
* https://github.com/Matt-Esch/string-template/index.js
*/
export default function () {
const isReplacementGroup = (target: object, key: string): target is Record<string, unknown> =>
key in target;
function template(
value: string | ((...args: unknown[]) => string),
...args: Array<string | object>
@@ -15,21 +18,23 @@ export default function () {
}
const str = value;
let replacements: object = args;
if (args.length === 1 && typeof args[0] === 'object') {
args = args[0] as unknown as Array<string | object>;
replacements = args[0];
}
if (!args?.hasOwnProperty) {
args = {} as unknown as Array<string | object>;
if (!replacements?.hasOwnProperty) {
replacements = {};
}
return str.replace(RE_NARGS, (match, _, i, index: number) => {
let result: string | object | null;
return str.replace(RE_NARGS, (match, _, group: string, index: number): string => {
let result: string | null;
if (str[index - 1] === '{' && str[index + match.length] === '}') {
return i;
return `${group}`;
} else {
result = Object.hasOwn(args, i) ? args[i] : null;
result = isReplacementGroup(replacements, group) ? `${replacements[group]}` : null;
if (result === null || result === undefined) {
return '';
}

View File

@@ -26,7 +26,7 @@ export const t = function (
// only support flat keys
if (lang[path] !== undefined) {
return format(lang[path], options);
return format(lang[path], ...(options ? [options] : []));
}
return '';
@@ -44,8 +44,8 @@ export async function use(l: string) {
} catch (e) {}
}
export const i18n = function (fn: N8nLocaleTranslateFn) {
export function i18n(fn: N8nLocaleTranslateFn) {
i18nHandler = fn || i18nHandler;
};
}
export default { use, t, i18n };