Skip to content

Commit

Permalink
Separate parsing from formatting.
Browse files Browse the repository at this point in the history
Also prefix exported symbols with “time” and update dependencies.
  • Loading branch information
mbostock committed Jan 7, 2016
1 parent e264157 commit d2e589e
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 274 deletions.
148 changes: 78 additions & 70 deletions README.md

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
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 timeFormatLocale} from "./src/locale";
export {default as timeFormatCaEs} from "./src/locale/ca-ES";
export {default as timeFormatDeCh} from "./src/locale/de-CH";
export {default as timeFormatDeDe} from "./src/locale/de-DE";
export {default as timeFormatEnCa} from "./src/locale/en-CA";
export {default as timeFormatEnGb} from "./src/locale/en-GB";
export {default as timeFormatEnUs} from "./src/locale/en-US";
export {default as timeFormatEsEs} from "./src/locale/es-ES";
export {default as timeFormatFiFi} from "./src/locale/fi-FI";
export {default as timeFormatFrCa} from "./src/locale/fr-CA";
export {default as timeFormatFrFr} from "./src/locale/fr-FR";
export {default as timeFormatHeIl} from "./src/locale/he-IL";
export {default as timeFormatHuHu} from "./src/locale/hu-HU";
export {default as timeFormatItIt} from "./src/locale/it-IT";
export {default as timeFormatJaJp} from "./src/locale/ja-JP";
export {default as timeFormatKoKr} from "./src/locale/ko-KR";
export {default as timeFormatMkMk} from "./src/locale/mk-MK";
export {default as timeFormatNlNl} from "./src/locale/nl-NL";
export {default as timeFormatPlPl} from "./src/locale/pl-PL";
export {default as timeFormatPtBr} from "./src/locale/pt-BR";
export {default as timeFormatRuRu} from "./src/locale/ru-RU";
export {default as timeFormatSvSe} from "./src/locale/sv-SE";
export {default as timeFormatZhCn} from "./src/locale/zh-CN";
export {default as isoFormat} from "./src/isoFormat";
export var format = defaultLocale.format;
export {default as isoParse} from "./src/isoParse";
export var timeFormat = defaultLocale.format;
export var timeParse = defaultLocale.parse;
export var utcFormat = defaultLocale.utcFormat;
export var utcParse = defaultLocale.utcParse;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-time-format",
"version": "0.2.1",
"version": "0.3.0",
"description": "A JavaScript time formatter and parser inspired by strftime and strptime.",
"keywords": [
"d3",
Expand All @@ -27,7 +27,7 @@
"prepublish": "npm run test && uglifyjs build/d3-time-format.js -c -m -o build/d3-time-format.min.js && rm -f build/d3-time-format.zip && zip -j build/d3-time-format.zip -- LICENSE README.md build/d3-time-format.js build/d3-time-format.min.js"
},
"dependencies": {
"d3-time": "~0.1.1"
"d3-time": "~0.2.0"
},
"devDependencies": {
"faucet": "0.0",
Expand Down
13 changes: 2 additions & 11 deletions src/isoFormat.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import locale from "./locale/en-US";

var isoSpecifier = "%Y-%m-%dT%H:%M:%S.%LZ";
export var isoSpecifier = "%Y-%m-%dT%H:%M:%S.%LZ";

function formatIsoNative(date) {
return date.toISOString();
}

formatIsoNative.parse = function(string) {
var date = new Date(string);
return isNaN(date) ? null : date;
};

formatIsoNative.toString = function() {
return isoSpecifier;
};

var formatIso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z")
var formatIso = Date.prototype.toISOString
? formatIsoNative
: locale.utcFormat(isoSpecifier);

Expand Down
13 changes: 13 additions & 0 deletions src/isoParse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import locale from "./locale/en-US";
import {isoSpecifier} from "./isoFormat";

function parseIsoNative(string) {
var date = new Date(string);
return isNaN(date) ? null : date;
}

var parseIso = +new Date("2000-01-01T00:00:00.000Z")
? parseIsoNative
: locale.utcParse(isoSpecifier);

export default parseIso;
20 changes: 14 additions & 6 deletions src/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {day, sunday, monday, year, utcDay, utcSunday, utcMonday, utcYear} from "d3-time";
import {timeDay, timeSunday, timeMonday, timeYear, utcDay, utcSunday, utcMonday, utcYear} from "d3-time";

function localDate(d) {
if (0 <= d.y && d.y < 100) {
Expand Down Expand Up @@ -292,15 +292,23 @@ export default function(locale) {
return {
format: function(specifier) {
var f = newFormat(specifier += "", formats);
f.parse = newParse(specifier, localDate);
f.toString = function() { return specifier; };
return f;
},
parse: function(specifier) {
var p = newParse(specifier += "", localDate);
p.toString = function() { return specifier; };
return p;
},
utcFormat: function(specifier) {
var f = newFormat(specifier += "", utcFormats);
f.parse = newParse(specifier, utcDate);
f.toString = function() { return specifier; };
return f;
},
utcParse: function(specifier) {
var p = newParse(specifier, utcDate);
p.toString = function() { return specifier; };
return p;
}
};
};
Expand Down Expand Up @@ -414,7 +422,7 @@ function formatHour12(d, p) {
}

function formatDayOfYear(d, p) {
return pad(1 + day.count(year(d), d), p, 3);
return pad(1 + timeDay.count(timeYear(d), d), p, 3);
}

function formatMilliseconds(d, p) {
Expand All @@ -434,15 +442,15 @@ function formatSeconds(d, p) {
}

function formatWeekNumberSunday(d, p) {
return pad(sunday.count(year(d), d), p, 2);
return pad(timeSunday.count(timeYear(d), d), p, 2);
}

function formatWeekdayNumber(d) {
return d.getDay();
}

function formatWeekNumberMonday(d, p) {
return pad(monday.count(year(d), d), p, 2);
return pad(timeMonday.count(timeYear(d), d), p, 2);
}

function formatYear(d, p) {
Expand Down
Loading

0 comments on commit d2e589e

Please sign in to comment.