Skip to content

Commit

Permalink
Allow locale to contain a list of BCP47 tags
Browse files Browse the repository at this point in the history
`toLocaleString()` allows the first argument to be a list of BCP47 tags (see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). This can be useful when passing in `navigator.languages`, so it should also be allowed for `pretty-bytes`. :)
  • Loading branch information
Turbo87 committed Oct 2, 2020
1 parent f5c147c commit 13c738d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ declare namespace prettyBytes {
- If `false`: Output won't be localized.
- If `true`: Localize the output using the system/browser locale.
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
__Note:__ Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime.
@default false
*/
readonly locale?: boolean | string;
readonly locale?: boolean | string | string[];

/**
Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Formats the given number using `Number#toLocaleString`.
*/
const toLocaleString = (number, locale) => {
let result = number;
if (typeof locale === 'string') {
if (typeof locale === 'string' || Array.isArray(locale)) {
result = number.toLocaleString(locale);
} else if (locale === true) {
result = number.toLocaleString();
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ test('locale option', t => {
t.is(prettyBytes(10.1, {locale: 'en'}), '10.1 B');
t.is(prettyBytes(1e30, {locale: 'en'}), '1,000,000 YB');

t.is(prettyBytes(-0.4, {locale: ['unknown', 'de', 'en']}), '-0,4 B');
t.is(prettyBytes(0.4, {locale: ['unknown', 'de', 'en']}), '0,4 B');
t.is(prettyBytes(1001, {locale: ['unknown', 'de', 'en']}), '1 kB');
t.is(prettyBytes(10.1, {locale: ['unknown', 'de', 'en']}), '10,1 B');
t.is(prettyBytes(1e30, {locale: ['unknown', 'de', 'en']}), '1.000.000 YB');

t.is(prettyBytes(-0.4, {locale: true}), '-0.4 B');
t.is(prettyBytes(0.4, {locale: true}), '0.4 B');
t.is(prettyBytes(1001, {locale: true}), '1 kB');
Expand Down

0 comments on commit 13c738d

Please sign in to comment.