From e03ec093eceae8b26b6c206bdad6d2557a952858 Mon Sep 17 00:00:00 2001 From: David Nowinsky Date: Fri, 29 Nov 2019 15:25:59 +0100 Subject: [PATCH] Some type K abbreviations can be omitted in locale --- README.md | 2 +- locale/fr-FR.json | 3 ++- src/formatPrefixAuto.js | 6 ++++-- src/formatTypes.js | 4 ++-- src/locale.js | 5 ++++- test/locale-test.js | 5 +++++ 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1f4d8ef..3f9415d 100644 --- a/README.md +++ b/README.md @@ -346,7 +346,7 @@ Returns a *locale* object for the specified *definition* with [*locale*.format]( * `thousands` - the group separator (e.g., `","`). * `grouping` - the array of group sizes (e.g., `[3]`), cycled as needed. * `currency` - the currency prefix and suffix (e.g., `["$", ""]`). -* `currencyAbbreviations` - the list of abbreviated suffixes for currency vales (array of 5 elements: units, thousands, millions, billions and trillions) (default `["", "K", "M", "B", "T"]`). +* `currencyAbbreviations` - the list of abbreviated suffixes for currency values; an array of elements for each: units, thousands, millions, billions and trillions; defaults to `["", "K", "M", "B", "T"]`. The number of elements can vary. * `numerals` - optional; an array of ten strings to replace the numerals 0-9. * `percent` - optional; the percent sign (defaults to `"%"`). * `minus` - optional; the minus sign (defaults to hyphen-minus, `"-"`). diff --git a/locale/fr-FR.json b/locale/fr-FR.json index e0cf89d..349868d 100644 --- a/locale/fr-FR.json +++ b/locale/fr-FR.json @@ -3,5 +3,6 @@ "thousands": "\u00a0", "grouping": [3], "currency": ["", "\u00a0€"], - "percent": "\u202f%" + "percent": "\u202f%", + "currencyAbbreviations": ["", "K", "Mio", "Mrd"] } diff --git a/src/formatPrefixAuto.js b/src/formatPrefixAuto.js index e3cc6bc..d11bce8 100644 --- a/src/formatPrefixAuto.js +++ b/src/formatPrefixAuto.js @@ -15,8 +15,10 @@ function formatSignificantDigitsForPrefixes(x, p, minPrefixOrder, maxPrefixOrder : "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than the smallest prefix } -export function formatCurrencyPrefixAuto(x, p) { - return formatSignificantDigitsForPrefixes(x, p, 0, 4); +export function createFormatCurrencyPrefixAutoForLocale(currencyAbbreviations) { + return function formatCurrencyPrefixAuto(x, p) { + return formatSignificantDigitsForPrefixes(x, p, 0, currencyAbbreviations.length - 1); + } } export default function(x, p) { diff --git a/src/formatTypes.js b/src/formatTypes.js index 5327455..fd81632 100644 --- a/src/formatTypes.js +++ b/src/formatTypes.js @@ -1,4 +1,4 @@ -import formatPrefixAuto, { formatCurrencyPrefixAuto } from "./formatPrefixAuto.js"; +import formatPrefixAuto, { createFormatCurrencyPrefixAutoForLocale } from "./formatPrefixAuto.js"; import formatRounded from "./formatRounded.js"; export default { @@ -9,7 +9,7 @@ export default { "e": function(x, p) { return x.toExponential(p); }, "f": function(x, p) { return x.toFixed(p); }, "g": function(x, p) { return x.toPrecision(p); }, - "K": formatCurrencyPrefixAuto, + "K": createFormatCurrencyPrefixAutoForLocale, // depends of the current locale "o": function(x) { return Math.round(x).toString(8); }, "p": function(x, p) { return formatRounded(x * 100, p); }, "r": formatRounded, diff --git a/src/locale.js b/src/locale.js index afc2049..3a97be7 100644 --- a/src/locale.js +++ b/src/locale.js @@ -56,6 +56,9 @@ export default function(locale) { var formatType = formatTypes[type], maybeSuffix = /[defgKprs%]/.test(type); + if (type === 'K') + formatType = formatType(currencyAbbreviations); + // Set the default precision if not specified, // or clamp the specified precision to the supported range. // For significant precision, it must be in [1, 21]. @@ -151,7 +154,7 @@ export default function(locale) { } var formatPrefix = createFormatPrefix(SIprefixes, -8, 8); - var formatCurrencyPrefix = createFormatPrefix(currencyAbbreviations, 0, 4); + var formatCurrencyPrefix = createFormatPrefix(currencyAbbreviations, 0, currencyAbbreviations.length - 1); return { format: newFormat, diff --git a/test/locale-test.js b/test/locale-test.js index 494a09c..48c21dd 100644 --- a/test/locale-test.js +++ b/test/locale-test.js @@ -26,6 +26,11 @@ tape("formatLocale({currencyAbbreviations: [list of abbreviations]}) should abbr test.end(); }); +tape("formatLocale({currencyAbbreviations: [list of abbreviations]}) should abbreviate only specified levels", function (test) { + test.equal(d3.formatLocale({ currencyAbbreviations: ["", "M", "Mio", "Mrd"] }).format("$.3K")(1.2e12), "1200Mrd"); + test.end(); +}); + tape("formatLocale({grouping: undefined}) does not perform any grouping", function(test) { test.equal(d3.formatLocale({decimal: "."}).format("012,.2f")(2), "000000002.00"); test.end();