Skip to content

Commit

Permalink
Add d3.formatDefaultLocale.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 22, 2016
1 parent 0b88f01 commit edb73d0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ d3.format("#x")(48879); // prefixed lowercase hexadecimal, "0xbeef"
d3.format(",.2r")(4223); // grouped thousands with two significant digits, "4,200"
```

See [*locale*.format](#locale_format) for a detailed specification, and try running [formatSpecifier](#formatSpecifier) on the above formats to decode their meaning.
See [*locale*.format](#locale_format) for a detailed specification, and try running [d3.formatSpecifier](#formatSpecifier) on the above formats to decode their meaning.

## Installing

Expand All @@ -84,11 +84,11 @@ var format = d3.format(".2s");

<a name="format" href="#format">#</a> d3.<b>format</b>(<i>specifier</i>)

An alias for [*locale*.format](#locale_format) on the U.S. English locale. See [formatLocale](#formatLocale) to define a new locale.
An alias for [*locale*.format](#locale_format) on the default locale. See [d3.formatDefaultLocale](#formatDefaultLocale) to set the default locale, and [d3.formatLocale](#formatLocale) to define a new locale.

<a name="formatPrefix" href="#formatPrefix">#</a> d3.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>)

An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the U.S. English locale. See [formatLocale](#formatLocale) to define a new locale.
An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the default locale. See [d3.formatDefaultLocale](#formatDefaultLocale) to set the default locale, and [d3.formatLocale](#formatLocale) to define a new locale.

<a name="locale_format" href="#locale_format">#</a> <i>locale</i>.<b>format</b>(<i>specifier</i>)

Expand Down Expand Up @@ -293,3 +293,7 @@ Returns a *locale* object for the specified *definition* with [*locale*.format](
* `currency` - the currency prefix and suffix (e.g., `["$", ""]`).

Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands.

<a name="formatDefaultLocale" href="#formatDefaultLocale">#</a> d3.<b>formatDefaultLocale</b>(<i>definition</i>)

Equivalent to [d3.formatLocale](#formatLocale), except it also redefines [d3.format](#format) and [d3.formatPrefix](#formatPrefix) to the new locale’s [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix). If you do not set a default locale, it defaults to [U.S. English](https://github.com/d3/d3-format/blob/master/locale/en-US.json).
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {default as formatLocale, format, formatPrefix} from "./src/locale";
export {default as formatDefaultLocale, format, formatPrefix} from "./src/defaultLocale";
export {default as formatLocale} from "./src/locale";
export {default as formatSpecifier} from "./src/formatSpecifier";
export {default as precisionFixed} from "./src/precisionFixed";
export {default as precisionPrefix} from "./src/precisionPrefix";
Expand Down
19 changes: 19 additions & 0 deletions src/defaultLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import formatLocale from "./locale";

var locale;
export var format;
export var formatPrefix;

defaultLocale({
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["$", ""]
});

export default function defaultLocale(definition) {
locale = formatLocale(definition);
format = locale.format;
formatPrefix = locale.formatPrefix;
return locale;
}
12 changes: 1 addition & 11 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ function identity(x) {
return x;
}

var defaultLocale = formatLocale({
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["$", ""]
});

export var format = defaultLocale.format;
export var formatPrefix = defaultLocale.formatPrefix;

export default function formatLocale(locale) {
export default function(locale) {
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity,
currency = locale.currency,
decimal = locale.decimal;
Expand Down
48 changes: 48 additions & 0 deletions test/defaultLocale-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var tape = require("tape"),
d3 = require("../");

var enUs = {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
};

var frFr = {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
};

tape("d3.formatDefaultLocale(definition) returns the new default locale", function(test) {
var locale = d3.formatDefaultLocale(frFr);
try {
test.equal(locale.format("$,.2f")(12345678.90), "12.345.678,90 €");
test.end();
} finally {
d3.formatDefaultLocale(enUs);
}
});

tape("d3.formatDefaultLocale(definition) affects d3.format", function(test) {
var locale = d3.formatDefaultLocale(frFr);
try {
test.equal(d3.format, locale.format);
test.equal(d3.format("$,.2f")(12345678.90), "12.345.678,90 €");
test.end();
} finally {
d3.formatDefaultLocale(enUs);
}
});

tape("d3.formatDefaultLocale(definition) affects d3.formatPrefix", function(test) {
var locale = d3.formatDefaultLocale(frFr);
try {
test.equal(d3.formatPrefix, locale.formatPrefix);
test.equal(d3.formatPrefix(",.2", 1e3)(12345678.90), "12.345,68k");
test.end();
} finally {
d3.formatDefaultLocale(enUs);
}
});

0 comments on commit edb73d0

Please sign in to comment.