Skip to content

Commit

Permalink
fix: Don't print undefined options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Skelton committed Mar 29, 2021
1 parent 525cf7b commit 197c5f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/printKeys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
)
})

0 comments on commit 197c5f7

Please sign in to comment.