Skip to content

Commit

Permalink
feat: add formatDisplayNames and formatList methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 11, 2023
1 parent 7a4c653 commit 904a995
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/formatters/values_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,18 @@ export class Formatter {
formatPlural(value: string | number, options?: Intl.PluralRulesOptions): string {
return formatters.plural(this.locale, options).select(Number(value))
}

/**
* Format an array of strings to a sentence.
*/
formatList(list: Iterable<string>, options?: Intl.ListFormatOptions) {
return formatters.list(this.locale, options).format(list)
}

/**
* Format region, currency, language codes to their display names
*/
formatDisplayNames(code: string, options: Intl.DisplayNamesOptions) {
return formatters.displayNames(this.locale, options).of(code)
}
}
14 changes: 14 additions & 0 deletions tests/values_formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,18 @@ test.group('Formatter', () => {
const formatter = new Formatter('en-in')
assert.equal(formatter.formatPlural(3, { type: 'ordinal' }), 'few')
})

test('format a list', ({ assert }) => {
const formatter = new Formatter('en-in')
assert.equal(
formatter.formatList(['Me', 'myself', 'I'], { type: 'conjunction' }),
'Me, myself and I'
)
})

test('format display names', ({ assert }) => {
const formatter = new Formatter('en-in')
assert.equal(formatter.formatDisplayNames('INR', { type: 'currency' }), 'Indian Rupee')
assert.equal(formatter.formatDisplayNames('en-US', { type: 'language' }), 'American English')
})
})

0 comments on commit 904a995

Please sign in to comment.