本文整理汇总了TypeScript中flat.flatten函数的典型用法代码示例。如果您正苦于以下问题:TypeScript flatten函数的具体用法?TypeScript flatten怎么用?TypeScript flatten使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了flatten函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: writeTranslations
async function writeTranslations(
flatMissingTranslations: TranslationCache,
basePath: string = '../src/Translation/locales'
) {
const mainTranslations = await new Translation().fetch(DEFAULT_LOCALE)
const availableLocales = await getAvailableLocales()
const flatMainTranslations = flat.flatten<TranslationData, TranslationData>(
mainTranslations
)
for (const locale of availableLocales) {
const localePath = path.resolve(__dirname, `${basePath}/${locale}.json`)
const flatCurrentTranslations = flatAndRemoveObsoleteKeys(
require(localePath),
flatMainTranslations
)
let updatedTranslations = Object.assign(
{},
flatCurrentTranslations,
flatMissingTranslations[locale]
)
updatedTranslations = flat.unflatten(updatedTranslations)
await utils.promisify(fs.writeFile)(
localePath,
JSON.stringify(updatedTranslations, null, 2),
'utf8'
)
}
}
示例2: flatAndRemoveObsoleteKeys
function flatAndRemoveObsoleteKeys(
translations: TranslationData,
flatBaseTranslations: TranslationData
): TranslationData {
const currentTranslations = flat.flatten(translations)
const cleanedTranslations = Object.keys(currentTranslations)
.filter(key => key in flatBaseTranslations)
.reduce((clean, key) => ({ ...clean, [key]: currentTranslations[key] }), {})
return cleanedTranslations
}
示例3:
a: {
b: number;
},
c: boolean[][];
};
let target: Target;
type Result = {
'a.b': number;
'c.0.0': boolean;
};
let result: Result;
result = flatten<Target, Result>(target);
result = flatten<Target, Result>(target, options);
}
namespace TestUnflatten {
let options: {
delimiter?: string;
object?: boolean;
overwrite?: boolean;
};
type Target = {
'a.b': number;
'c.0.0': boolean;
};