Skip to content

Commit

Permalink
Fix #14 - coerce inputs to the appropriate type.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 30, 2015
1 parent 5c41273 commit 5056e3d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export default function(locale) {
pad,
format;

if (!(date instanceof Date)) date = new Date(+date);

while (++i < n) {
if (specifier.charCodeAt(i) === 37) {
string.push(specifier.slice(j, i));
Expand All @@ -161,7 +163,7 @@ export default function(locale) {
function newParse(specifier, newDate) {
return function(string) {
var d = newYear(1900),
i = parseSpecifier(d, specifier, string, 0);
i = parseSpecifier(d, specifier, string += "", 0);
if (i != string.length) return null;

// The am-pm flag is 0 for AM, and 1 for PM.
Expand Down
12 changes: 12 additions & 0 deletions test/format-parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ var tape = require("tape"),
timeFormat = require("../"),
date = require("./date");

tape("parse(string) coerces the specified string to a string", function(test) {
var p = timeFormat.format("%c").parse;
test.deepEqual(p({toString: function() { return "Mon Jan 1 00:00:00 1990"; }}), date.local(1990, 0, 1));
test.deepEqual(p({toString: function() { return "Tue Jan 2 00:00:00 1990"; }}), date.local(1990, 0, 2));
test.deepEqual(p({toString: function() { return "Wed Jan 3 00:00:00 1990"; }}), date.local(1990, 0, 3));
test.deepEqual(p({toString: function() { return "Thu Jan 4 00:00:00 1990"; }}), date.local(1990, 0, 4));
test.deepEqual(p({toString: function() { return "Fri Jan 5 00:00:00 1990"; }}), date.local(1990, 0, 5));
test.deepEqual(p({toString: function() { return "Sat Jan 6 00:00:00 1990"; }}), date.local(1990, 0, 6));
test.deepEqual(p({toString: function() { return "Sun Jan 7 00:00:00 1990"; }}), date.local(1990, 0, 7));
test.end();
});

tape("format(\"%a %m/%d/%Y\").parse(date) parses abbreviated weekday and date", function(test) {
var p = timeFormat.format("%a %m/%d/%Y").parse;
test.deepEqual(p("Sun 01/01/1990"), date.local(1990, 0, 1));
Expand Down
12 changes: 12 additions & 0 deletions test/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ function multi(d) {
: formatYear)(d);
}

tape("format(date) coerces the specified date to a Date", function(test) {
var f = timeFormat.format("%c");
test.equal(f(+date.local(1990, 0, 1)), "Mon Jan 1 00:00:00 1990");
test.equal(f(+date.local(1990, 0, 2)), "Tue Jan 2 00:00:00 1990");
test.equal(f(+date.local(1990, 0, 3)), "Wed Jan 3 00:00:00 1990");
test.equal(f(+date.local(1990, 0, 4)), "Thu Jan 4 00:00:00 1990");
test.equal(f(+date.local(1990, 0, 5)), "Fri Jan 5 00:00:00 1990");
test.equal(f(+date.local(1990, 0, 6)), "Sat Jan 6 00:00:00 1990");
test.equal(f(+date.local(1990, 0, 7)), "Sun Jan 7 00:00:00 1990");
test.end();
});

tape("format(\"%a\")(date) formats abbreviated weekdays", function(test) {
var f = timeFormat.format("%a");
test.equal(f(date.local(1990, 0, 1)), "Mon");
Expand Down

0 comments on commit 5056e3d

Please sign in to comment.