Skip to content

Commit

Permalink
Include all built-in locales by default.
Browse files Browse the repository at this point in the history
Fixes #8. Now you only need a custom build to optimize file size, rather than to
add functionality.
  • Loading branch information
mbostock committed Jul 30, 2015
1 parent c31306b commit 524a620
Show file tree
Hide file tree
Showing 23 changed files with 126 additions and 99 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ If you use NPM, `npm install d3-format`. Otherwise, download the [latest release

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

An alias for [*locale*.format](#locale_format) on the default locale. While this method generates output for [U.S. English](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js)-speaking humans by default, humans in other locales may be served using [localeFormat](#localeFormat) or by editing [index.js](https://github.com/d3/d3-format/tree/master/index.js) and rebuilding.
An alias for [*locale*.format](#locale_format) on the default [U.S. English](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js) locale. Use [localeFormat](#localeFormat) for a different built-in locale or to define a new locale.

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

An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the default locale. While this method generates output for [U.S. English](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js)-speaking humans by default, humans in other locales may be served using [localeFormat](#localeFormat) or by editing [index.js](https://github.com/d3/d3-format/tree/master/index.js) and rebuilding.
An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the default [U.S. English](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js) locale. Use [localeFormat](#localeFormat) for a different built-in locale or 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 @@ -170,34 +170,34 @@ This method is useful when formatting multiple numbers in the same units for eas

<a name="localeFormat" href="#localeFormat">#</a> <b>localeFormat</b>(<i>definition</i>)

Returns a *locale* object for the specified *definition*, with [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix) methods. The locale *definition* must include the following properties:
Returns a *locale* object for the specified *definition* with [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix) methods. If *definition* is a string, it is the name of a built-in locale:

* `"ca-ES"` - [Catalan (Spain)](https://github.com/d3/d3-format/tree/master/src/locale/ca-ES.js)
* `"de-DE"` - [German (Germany)](https://github.com/d3/d3-format/tree/master/src/locale/de-DE.js)
* `"en-CA"` - [English (Canada)](https://github.com/d3/d3-format/tree/master/src/locale/en-CA.js)
* `"en-GB"` - [English (United Kingdom)](https://github.com/d3/d3-format/tree/master/src/locale/en-GB.js)
* `"en-US"` - [English (United States)](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js)
* `"es-ES"` - [Spanish (Spain)](https://github.com/d3/d3-format/tree/master/src/locale/es-ES.js)
* `"fi-FI"` - [Finnish (Finland)](https://github.com/d3/d3-format/tree/master/src/locale/fi-FI.js)
* `"fr-CA"` - [French (Canada)](https://github.com/d3/d3-format/tree/master/src/locale/fr-CA.js)
* `"fr-FR"` - [French (France)](https://github.com/d3/d3-format/tree/master/src/locale/fr-FR.js)
* `"he-IL"` - [Hebrew (Israel)](https://github.com/d3/d3-format/tree/master/src/locale/he-IL.js)
* `"it-IT"` - [Italian (Italy)](https://github.com/d3/d3-format/tree/master/src/locale/it-IT.js)
* `"mk-MK"` - [Macedonian (Macedonia)](https://github.com/d3/d3-format/tree/master/src/locale/mk-MK.js)
* `"nl-NL"` - [Dutch (Netherlands)](https://github.com/d3/d3-format/tree/master/src/locale/nl-NL.js)
* `"pl-PL"` - [Polish (Poland)](https://github.com/d3/d3-format/tree/master/src/locale/pl-PL.js)
* `"pt-BR"` - [Portuguese (Brazil)](https://github.com/d3/d3-format/tree/master/src/locale/pt-BR.js)
* `"ru-RU"` - [Russian (Russia)](https://github.com/d3/d3-format/tree/master/src/locale/ru-RU.js)
* `"zh-CN"` - [Chinese (China)](https://github.com/d3/d3-format/tree/master/src/locale/zh-CN.js)

Otherwise, the locale *definition* must include the following properties:

* `decimal` - the decimal point (e.g., `"."`).
* `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., `["$", ""]`).

(Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands.) The following locale definitions are available in the source:

* [Catalan (Spain)](https://github.com/d3/d3-format/tree/master/src/locale/ca-ES.js)
* [German (Germany)](https://github.com/d3/d3-format/tree/master/src/locale/de-DE.js)
* [English (Canada)](https://github.com/d3/d3-format/tree/master/src/locale/en-CA.js)
* [English (United Kingdom)](https://github.com/d3/d3-format/tree/master/src/locale/en-GB.js)
* [English (United States)](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js)
* [Spanish (Spain)](https://github.com/d3/d3-format/tree/master/src/locale/es-ES.js)
* [Finnish (Finland)](https://github.com/d3/d3-format/tree/master/src/locale/fi-FI.js)
* [French (Canada)](https://github.com/d3/d3-format/tree/master/src/locale/fr-CA.js)
* [French (France)](https://github.com/d3/d3-format/tree/master/src/locale/fr-FR.js)
* [Hebrew (Israel)](https://github.com/d3/d3-format/tree/master/src/locale/he-IL.js)
* [Italian (Italy)](https://github.com/d3/d3-format/tree/master/src/locale/it-IT.js)
* [Macedonian (Macedonia)](https://github.com/d3/d3-format/tree/master/src/locale/mk-MK.js)
* [Dutch (Netherlands)](https://github.com/d3/d3-format/tree/master/src/locale/nl-NL.js)
* [Polish (Poland)](https://github.com/d3/d3-format/tree/master/src/locale/pl-PL.js)
* [Portuguese (Brazil)](https://github.com/d3/d3-format/tree/master/src/locale/pt-BR.js)
* [Russian (Russia)](https://github.com/d3/d3-format/tree/master/src/locale/ru-RU.js)
* [Chinese (China)](https://github.com/d3/d3-format/tree/master/src/locale/zh-CN.js)

To change the default locale, edit [index.js](https://github.com/d3/d3-format/tree/master/index.js) and run `npm run prepublish`.
Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands.

<a name="formatSpecifier" href="#formatSpecifier">#</a> <b>formatSpecifier</b>(<i>specifier</i>)

Expand Down
54 changes: 49 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
import formatSpecifier from "./src/formatSpecifier";
import locale from "./src/locale/en-US";
import localeFormat from "./src/localeFormat";
import locale from "./src/locale";
import precisionFixed from "./src/precisionFixed";
import precisionPrefix from "./src/precisionPrefix";
import precisionRound from "./src/precisionRound";

export var format = locale.format;
export var formatPrefix = locale.formatPrefix;
import caEs from "./src/locale/ca-ES";
import deDe from "./src/locale/de-DE";
import enCa from "./src/locale/en-CA";
import enGb from "./src/locale/en-GB";
import enUs from "./src/locale/en-US";
import esEs from "./src/locale/es-ES";
import fiFi from "./src/locale/fi-FI";
import frCa from "./src/locale/fr-CA";
import frFr from "./src/locale/fr-FR";
import heIl from "./src/locale/he-IL";
import itIt from "./src/locale/it-IT";
import mkMk from "./src/locale/mk-MK";
import nlNl from "./src/locale/nl-NL";
import plPl from "./src/locale/pl-PL";
import ptBr from "./src/locale/pt-BR";
import ruRu from "./src/locale/ru-RU";
import zhCn from "./src/locale/zh-CN";

var localeDefinitions = (new Map)
.set("ca-ES", caEs)
.set("de-DE", deDe)
.set("en-CA", enCa)
.set("en-GB", enGb)
.set("en-US", enUs)
.set("es-ES", esEs)
.set("fi-FI", fiFi)
.set("fr-CA", frCa)
.set("fr-FR", frFr)
.set("he-IL", heIl)
.set("it-IT", itIt)
.set("mk-MK", mkMk)
.set("nl-NL", nlNl)
.set("pl-PL", plPl)
.set("pt-BR", ptBr)
.set("ru-RU", ruRu)
.set("zh-CN", zhCn);

var defaultLocale = locale(enUs);
export var format = defaultLocale.format;
export var formatPrefix = defaultLocale.formatPrefix;

export function localeFormat(definition) {
if (typeof definition === "string") {
definition = localeDefinitions.get(definition);
if (!definition) return null;
}
return locale(definition);
};

export {
formatSpecifier,
localeFormat,
precisionFixed,
precisionPrefix,
precisionRound
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-format",
"version": "0.2.3",
"version": "0.3.0",
"description": "Format numbers for human consumption.",
"keywords": [
"d3",
Expand All @@ -19,7 +19,7 @@
"url": "https://github.com/d3/d3-format.git"
},
"scripts": {
"pretest": "mkdir -p build && d3-bundler --format=umd --name=format -- index.js > build/format.js",
"pretest": "mkdir -p build && d3-bundler --polyfill-map --format=umd --name=format -- index.js > build/format.js",
"test": "faucet `find test -name '*-test.js'`",
"prepublish": "npm run test && uglifyjs build/format.js -c -m -o build/format.min.js && rm -f build/format.zip && zip -j build/format.zip -- LICENSE README.md build/format.js build/format.min.js"
},
Expand Down
File renamed without changes.
6 changes: 2 additions & 4 deletions src/locale/ca-ES.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "\xa0€"]
});
};
6 changes: 2 additions & 4 deletions src/locale/de-DE.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "\xa0€"]
});
};
6 changes: 2 additions & 4 deletions src/locale/en-CA.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["$", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/en-GB.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["£", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/en-US.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["$", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/es-ES.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "\xa0€"]
});
};
6 changes: 2 additions & 4 deletions src/locale/fi-FI.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: "\xa0",
grouping: [3],
currency: ["", "\xa0€"]
});
};
6 changes: 2 additions & 4 deletions src/locale/fr-CA.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: "\xa0",
grouping: [3],
currency: ["", "$"]
});
};
6 changes: 2 additions & 4 deletions src/locale/fr-FR.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "\xa0€"]
});
};
6 changes: 2 additions & 4 deletions src/locale/he-IL.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["₪", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/it-IT.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["€", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/mk-MK.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "\xa0ден."]
});
};
6 changes: 2 additions & 4 deletions src/locale/nl-NL.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["€\xa0", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/pl-PL.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "zł"]
});
};
6 changes: 2 additions & 4 deletions src/locale/pt-BR.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["R$", ""]
});
};
6 changes: 2 additions & 4 deletions src/locale/ru-RU.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ",",
thousands: "\xa0",
grouping: [3],
currency: ["", "\xa0руб."]
});
};
6 changes: 2 additions & 4 deletions src/locale/zh-CN.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import localeFormat from "../localeFormat";

export default localeFormat({
export default {
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["¥", ""]
});
};
Empty file added test/locale-test.js
Empty file.
17 changes: 17 additions & 0 deletions test/localeFormat-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
var tape = require("tape"),
format = require("../");

tape("localeFormat(\"en-US\") returns a U.S. English format", function(test) {
var locale = format.localeFormat("en-US");
test.equal(locale.format("$,.2f")(1234.56), "$1,234.56");
test.end();
});

tape("localeFormat(\"fr-FR\") returns a French format", function(test) {
var locale = format.localeFormat("fr-FR");
test.equal(locale.format("$,.2f")(1234.56), "1.234,56 €");
test.end();
});

tape("localeFormat(\"invalid\") returns null", function(test) {
test.equal(format.localeFormat("invalid"), null);
test.end();
});

tape("localeFormat({decimal: decimal}) observes the specified decimal point", function(test) {
test.equal(format.localeFormat({decimal: "|"}).format("06.2f")(2), "002|00");
test.equal(format.localeFormat({decimal: "/"}).format("06.2f")(2), "002/00");
Expand Down

0 comments on commit 524a620

Please sign in to comment.