From 197c5f705373707d236208c2a0f91cc0e2e1ff3d Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Mon, 29 Mar 2021 09:44:31 -0500 Subject: [PATCH] fix: Don't print undefined options --- src/index.ts | 12 ++++++------ test/printKeys.spec.ts | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3875354..ba8ada8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,12 +6,12 @@ export default { name: 'printKeys', type: 'postProcessor', process(_, key, 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) - ) + // Filter out internal and undefined 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) => options[key] !== undefined) + .filter((key) => !ignore.includes(key)) return optionKeys.length ? key + ' ' + JSON.stringify(options, optionKeys) diff --git a/test/printKeys.spec.ts b/test/printKeys.spec.ts index dbf3244..cb8a023 100644 --- a/test/printKeys.spec.ts +++ b/test/printKeys.spec.ts @@ -34,3 +34,11 @@ it('should not print the internal options', () => { expect(i18next.t('key', { lngs: ['en', 'es'] })).toBe('key') expect(i18next.t('key', { interpolation: {} })).toBe('key') }) + +it('should not print undefined options', () => { + expect(i18next.t('basic', { count: undefined })).toBe('basic') + expect(i18next.t('basic', { context: undefined })).toBe('basic') + expect(i18next.t('basic', { count: 1, context: undefined })).toBe( + 'basic {"count":1}' + ) +})