Skip to content

Commit

Permalink
Fix #15 - Use symbols, not strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 30, 2015
1 parent e40d151 commit 5c41273
Show file tree
Hide file tree
Showing 26 changed files with 210 additions and 136 deletions.
125 changes: 95 additions & 30 deletions README.md
Expand Up @@ -47,11 +47,11 @@ If you use NPM, `npm install d3-time-format`. Otherwise, download the [latest re

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

An alias for [*locale*.format](#locale_format) on the default [U.S. English](https://github.com/d3/d3-time-format/tree/master/src/locale/en-US.js) locale. Use [localeFormat](#localeFormat) for a different built-in locale or to define a new locale.
An alias for [*locale*.format](#locale_format) on the [U.S. English locale](#localeEnUs). See the other [locales](#locales), or use [locale](#locale) to define a new locale.

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

An alias for [*locale*.utcFormat](#locale_utcFormat) on the default [U.S. English](https://github.com/d3/d3-time-format/tree/master/src/locale/en-US.js) locale. Use [localeFormat](#localeFormat) for a different built-in locale or to define a new locale.
An alias for [*locale*.utcFormat](#locale_utcFormat) on the [U.S. English locale](#localeEnUs). See the other [locales](#locales), or use [locale](#locale) to define a new locale.

<a name="isoFormat" href="#isoFormat">#</a> <b>isoFormat</b>

Expand Down Expand Up @@ -129,34 +129,11 @@ The `%d` and `%e` directives are considered equivalent for parsing.

Returns this format’s specifier.

<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*.utcFormat](#locale_utcFormat) methods. If *definition* is a string, it is the name of a built-in locale:

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

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

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

Returns a *locale* object for the specified *definition* with [*locale*.format](#locale_format) and [*locale*.utcFormat](#locale_utcFormat) methods. The *definition* must include the following properties:

* `dateTime` - the date and time (`%c`) format specifier (<i>e.g.</i>, `"%a %b %e %X %Y"`).
* `date` - the date (`%x`) format specifier (<i>e.g.</i>, `"%m/%d/%Y"`).
Expand All @@ -167,6 +144,94 @@ Otherwise, the locale *definition* must include the following properties:
* `months` - the full names of the months (starting with January).
* `shortMonths` - the abbreviated names of the months (starting with January).

<a name="localeCaEs" href="#localeCaEs">#</a> <b>localeCaEs</b>

[Catalan (Spain)](https://github.com/d3/d3-time-format/tree/master/src/locale/ca-ES.js)

<a name="localeDeCh" href="#localeDeCh">#</a> <b>localeDeCh</b>

[German (Switzerland)](https://github.com/d3/d3-time-format/tree/master/src/locale/de-CH.js)

<a name="localeDeDe" href="#localeDeDe">#</a> <b>localeDeDe</b>

[German (Germany)](https://github.com/d3/d3-time-format/tree/master/src/locale/de-DE.js)

<a name="localeEnCa" href="#localeEnCa">#</a> <b>localeEnCa</b>

[English (Canada)](https://github.com/d3/d3-time-format/tree/master/src/locale/en-CA.js)

<a name="localeEnGb" href="#localeEnGb">#</a> <b>localeEnGb</b>

[English (United Kingdom)](https://github.com/d3/d3-time-format/tree/master/src/locale/en-GB.js)

<a name="localeEnUs" href="#localeEnUs">#</a> <b>localeEnUs</b>

[English (United States)](https://github.com/d3/d3-time-format/tree/master/src/locale/en-US.js)

<a name="localeEsEs" href="#localeEsEs">#</a> <b>localeEsEs</b>

[Spanish (Spain)](https://github.com/d3/d3-time-format/tree/master/src/locale/es-ES.js)

<a name="localeFiFi" href="#localeFiFi">#</a> <b>localeFiFi</b>

[Finnish (Finland)](https://github.com/d3/d3-time-format/tree/master/src/locale/fi-FI.js)

<a name="localeFrCa" href="#localeFrCa">#</a> <b>localeFrCa</b>

[French (Canada)](https://github.com/d3/d3-time-format/tree/master/src/locale/fr-CA.js)

<a name="localeFrFr" href="#localeFrFr">#</a> <b>localeFrFr</b>

[French (France)](https://github.com/d3/d3-time-format/tree/master/src/locale/fr-FR.js)

<a name="localeHeIl" href="#localeHeIl">#</a> <b>localeHeIl</b>

[Hebrew (Israel)](https://github.com/d3/d3-time-format/tree/master/src/locale/he-IL.js)

<a name="localeHuHu" href="#localeHuHu">#</a> <b>localeHuHu</b>

[Hungarian (Hungary)](https://github.com/d3/d3-time-format/tree/master/src/locale/hu-HU.js)

<a name="localeItIt" href="#localeItIt">#</a> <b>localeItIt</b>

[Italian (Italy)](https://github.com/d3/d3-time-format/tree/master/src/locale/it-IT.js)

<a name="localeJaJp" href="#localeJaJp">#</a> <b>localeJaJp</b>

[Japanese (Japan)](https://github.com/d3/d3-time-format/tree/master/src/locale/ja-JP.js)

<a name="localeKoKr" href="#localeKoKr">#</a> <b>localeKoKr</b>

[Korean (South Korea)](https://github.com/d3/d3-time-format/tree/master/src/locale/ko-KR.js)

<a name="localeMkMk" href="#localeMkMk">#</a> <b>localeMkMk</b>

[Macedonian (Macedonia)](https://github.com/d3/d3-time-format/tree/master/src/locale/mk-MK.js)

<a name="localeNlNl" href="#localeNlNl">#</a> <b>localeNlNl</b>

[Dutch (Netherlands)](https://github.com/d3/d3-time-format/tree/master/src/locale/nl-NL.js)

<a name="localePlPl" href="#localePlPl">#</a> <b>localePlPl</b>

[Polish (Poland)](https://github.com/d3/d3-time-format/tree/master/src/locale/pl-PL.js)

<a name="localePtBr" href="#localePtBr">#</a> <b>localePtBr</b>

[Portuguese (Brazil)](https://github.com/d3/d3-time-format/tree/master/src/locale/pt-BR.js)

<a name="localeRuRu" href="#localeRuRu">#</a> <b>localeRuRu</b>

[Russian (Russia)](https://github.com/d3/d3-time-format/tree/master/src/locale/ru-RU.js)

<a name="localeSvSe" href="#localeSvSe">#</a> <b>localeSvSe</b>

[Swedish (Sweden)](https://github.com/d3/d3-time-format/tree/master/src/locale/sv-SE.js)

<a name="localeZhCn" href="#localeZhCn">#</a> <b>localeZhCn</b>

[Chinese (China)](https://github.com/d3/d3-time-format/tree/master/src/locale/zh-CN.js)

## Changes from D3 3.x:

* Exposed built-in locales.
Expand Down
85 changes: 25 additions & 60 deletions index.js
@@ -1,62 +1,27 @@
import locale from "./src/locale";
import caEs from "./src/locale/ca-ES.js";
import deCh from "./src/locale/de-CH.js";
import deDe from "./src/locale/de-DE.js";
import enCa from "./src/locale/en-CA.js";
import enGb from "./src/locale/en-GB.js";
import enUs from "./src/locale/en-US.js";
import esEs from "./src/locale/es-ES.js";
import fiFi from "./src/locale/fi-FI.js";
import frCa from "./src/locale/fr-CA.js";
import frFr from "./src/locale/fr-FR.js";
import heIl from "./src/locale/he-IL.js";
import huHu from "./src/locale/hu-HU.js";
import itIt from "./src/locale/it-IT.js";
import jaJp from "./src/locale/ja-JP.js";
import koKr from "./src/locale/ko-KR.js";
import mkMk from "./src/locale/mk-MK.js";
import nlNl from "./src/locale/nl-NL.js";
import plPl from "./src/locale/pl-PL.js";
import ptBr from "./src/locale/pt-BR.js";
import ruRu from "./src/locale/ru-RU.js";
import svSe from "./src/locale/sv-SE.js";
import zhCn from "./src/locale/zh-CN.js";

var localeDefinitions = {
"ca-ES": caEs,
"de-CH": deCh,
"de-DE": deDe,
"en-CA": enCa,
"en-GB": enGb,
"en-US": enUs,
"es-ES": esEs,
"fi-FI": fiFi,
"fr-CA": frCa,
"fr-FR": frFr,
"he-IL": heIl,
"hu-HU": huHu,
"it-IT": itIt,
"ja-JP": jaJp,
"ko-KR": koKr,
"mk-MK": mkMk,
"nl-NL": nlNl,
"pl-PL": plPl,
"pt-BR": ptBr,
"ru-RU": ruRu,
"sv-SE": svSe,
"zh-CN": zhCn
};

var defaultLocale = locale(enUs);
import defaultLocale from "./src/locale/en-US";
export {default as locale} from "./src/locale";
export {default as localeCaEs} from "./src/locale/ca-ES";
export {default as localeDeCh} from "./src/locale/de-CH";
export {default as localeDeDe} from "./src/locale/de-DE";
export {default as localeEnCa} from "./src/locale/en-CA";
export {default as localeEnGb} from "./src/locale/en-GB";
export {default as localeEnUs} from "./src/locale/en-US";
export {default as localeEsEs} from "./src/locale/es-ES";
export {default as localeFiFi} from "./src/locale/fi-FI";
export {default as localeFrCa} from "./src/locale/fr-CA";
export {default as localeFrFr} from "./src/locale/fr-FR";
export {default as localeHeIl} from "./src/locale/he-IL";
export {default as localeHuHu} from "./src/locale/hu-HU";
export {default as localeItIt} from "./src/locale/it-IT";
export {default as localeJaJp} from "./src/locale/ja-JP";
export {default as localeKoKr} from "./src/locale/ko-KR";
export {default as localeMkMk} from "./src/locale/mk-MK";
export {default as localeNlNl} from "./src/locale/nl-NL";
export {default as localePlPl} from "./src/locale/pl-PL";
export {default as localePtBr} from "./src/locale/pt-BR";
export {default as localeRuRu} from "./src/locale/ru-RU";
export {default as localeSvSe} from "./src/locale/sv-SE";
export {default as localeZhCn} from "./src/locale/zh-CN";
export {default as isoFormat} from "./src/isoFormat";
export var format = defaultLocale.format;
export var utcFormat = defaultLocale.utcFormat;

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

export {default as isoFormat} from "./src/isoFormat";
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "d3-time-format",
"version": "0.1.5",
"version": "0.2.0",
"description": "A JavaScript time formatter and parser inspired by strftime and strptime.",
"keywords": [
"d3",
Expand Down
6 changes: 4 additions & 2 deletions src/locale/ca-ES.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, %e de %B de %Y, %X",
date: "%d/%m/%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["dg.", "dl.", "dt.", "dc.", "dj.", "dv.", "ds."],
months: ["gener", "febrer", "març", "abril", "maig", "juny", "juliol", "agost", "setembre", "octubre", "novembre", "desembre"],
shortMonths: ["gen.", "febr.", "març", "abr.", "maig", "juny", "jul.", "ag.", "set.", "oct.", "nov.", "des."]
};
});
6 changes: 4 additions & 2 deletions src/locale/de-CH.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, der %e. %B %Y, %X",
date: "%d.%m.%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
shortMonths: ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
};
});
6 changes: 4 additions & 2 deletions src/locale/de-DE.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, der %e. %B %Y, %X",
date: "%d.%m.%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
shortMonths: ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
};
});
6 changes: 4 additions & 2 deletions src/locale/en-CA.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%a %b %e %X %Y",
date: "%Y-%m-%d",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
};
});
6 changes: 4 additions & 2 deletions src/locale/en-GB.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%a %e %b %X %Y",
date: "%d/%m/%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
};
});
6 changes: 4 additions & 2 deletions src/locale/en-US.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%a %b %e %X %Y",
date: "%m/%d/%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
};
});
6 changes: 4 additions & 2 deletions src/locale/es-ES.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, %e de %B de %Y, %X",
date: "%d/%m/%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"],
months: ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"],
shortMonths: ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"]
};
});
6 changes: 4 additions & 2 deletions src/locale/fi-FI.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, %-d. %Bta %Y klo %X",
date: "%-d.%-m.%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["Su", "Ma", "Ti", "Ke", "To", "Pe", "La"],
months: ["tammikuu", "helmikuu", "maaliskuu", "huhtikuu", "toukokuu", "kesäkuu", "heinäkuu", "elokuu", "syyskuu", "lokakuu", "marraskuu", "joulukuu"],
shortMonths: ["Tammi", "Helmi", "Maalis", "Huhti", "Touko", "Kesä", "Heinä", "Elo", "Syys", "Loka", "Marras", "Joulu"]
};
});
6 changes: 4 additions & 2 deletions src/locale/fr-CA.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%a %e %b %Y %X",
date: "%Y-%m-%d",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["dim", "lun", "mar", "mer", "jeu", "ven", "sam"],
months: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
shortMonths: ["jan", "fév", "mar", "avr", "mai", "jui", "jul", "aoû", "sep", "oct", "nov", "déc"]
};
});
6 changes: 4 additions & 2 deletions src/locale/fr-FR.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, le %e %B %Y, %X",
date: "%d/%m/%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."],
months: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
shortMonths: ["janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."]
};
});
6 changes: 4 additions & 2 deletions src/locale/he-IL.js
@@ -1,4 +1,6 @@
export default {
import locale from "../locale";

export default locale({
dateTime: "%A, %e ב%B %Y %X",
date: "%d.%m.%Y",
time: "%H:%M:%S",
Expand All @@ -7,4 +9,4 @@ export default {
shortDays: ["א׳", "ב׳", "ג׳", "ד׳", "ה׳", "ו׳", "ש׳"],
months: ["ינואר", "פברואר", "מרץ", "אפריל", "מאי", "יוני", "יולי", "אוגוסט", "ספטמבר", "אוקטובר", "נובמבר", "דצמבר"],
shortMonths: ["ינו׳", "פבר׳", "מרץ", "אפר׳", "מאי", "יוני", "יולי", "אוג׳", "ספט׳", "אוק׳", "נוב׳", "דצמ׳"]
};
});

0 comments on commit 5c41273

Please sign in to comment.