Skip to content

Commit

Permalink
Allow locale to contain a list of BCP47 tags (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Turbo87 and sindresorhus committed Dec 23, 2020
1 parent f5c147c commit c7ea91a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
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 | readonly 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
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
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -81,6 +81,7 @@ Default: `false` *(No localization)*

- 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. [Node.js 13](https://nodejs.org/en/blog/release/v13.0.0/) and later ships with ICU by default.

Expand Down
6 changes: 6 additions & 0 deletions test.js
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 c7ea91a

Please sign in to comment.