From 525cf7b894b097c72dfde559f4af76131c13d826 Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Fri, 26 Mar 2021 11:23:12 -0500 Subject: [PATCH] fix: Don't print i18next internal options --- src/index.ts | 13 +++++++++++-- test/printKeys.spec.ts | 7 +++++++ tsconfig.json | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index b1db24f..3875354 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,20 @@ import { PostProcessorModule } from 'i18next' +const ignore = ['defaultValue', 'interpolation', 'lng', 'lngs', 'ns'] + export default { name: 'printKeys', type: 'postProcessor', process(_, key, options) { - return Object.keys(options).length - ? key + ' ' + JSON.stringify(options) + // Filter out internal i18next options when printing keys. The resulting + // array is passed to JSON.stringify which instructs it which keys should + // be printed. + const optionKeys = Object.keys(options).filter( + (key) => !ignore.includes(key) + ) + + return optionKeys.length + ? key + ' ' + JSON.stringify(options, optionKeys) : key.toString() }, } as PostProcessorModule diff --git a/test/printKeys.spec.ts b/test/printKeys.spec.ts index 37e15bf..dbf3244 100644 --- a/test/printKeys.spec.ts +++ b/test/printKeys.spec.ts @@ -27,3 +27,10 @@ it('should print interpolated values after the key', () => { ) expect(i18next.t('count', { count: 12 })).toBe('count {"count":12}') }) + +it('should not print the internal options', () => { + expect(i18next.t('key', { defaultValue: 'hi' })).toBe('key') + expect(i18next.t('key', { lng: 'en-US' })).toBe('key') + expect(i18next.t('key', { lngs: ['en', 'es'] })).toBe('key') + expect(i18next.t('key', { interpolation: {} })).toBe('key') +}) diff --git a/tsconfig.json b/tsconfig.json index 2be4412..f16a285 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "module": "CommonJS", "target": "ES6", + "lib": ["ESNext"], "moduleResolution": "node", "esModuleInterop": true, "removeComments": true,