Created
February 24, 2012 22:30
-
-
Save cjohansen/1904218 to your computer and use it in GitHub Desktop.
A simple JavaScript project tested with Buster.JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = exports; // Vanity | |
config["Browser tests"] = { | |
environment: "browser", | |
sources: ["strftime.js"], | |
tests: ["strftime-test.js"] | |
}; | |
config["Server tests"] = { | |
extends: "Browser tests", | |
environment: "node" | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
"http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<title>strftime</title> | |
</head> | |
<body> | |
<script type="text/javascript" src="http://cdn.busterjs.org/releases/latest/buster-test.js"></script> | |
<script type="text/javascript" src="http://cdn.busterjs.org/examples/strftime/strftime.js"></script> | |
<script type="text/javascript" src="http://cdn.busterjs.org/examples/strftime/strftime-test.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof require == "function" && typeof module == "object") { | |
buster = require("buster"); | |
require("./strftime"); | |
} | |
var assert = buster.assert; | |
buster.testCase("Date strftime tests", { | |
setUp: function () { | |
this.date = new Date(2009, 11, 5); | |
}, | |
"%Y": { | |
setUp: function () { | |
this.year = this.date.strftime("%Y"); | |
}, | |
"should return full year": function () { | |
assert.equals(this.year, "2009"); | |
}, | |
"should return a string": function () { | |
assert.equals(typeof this.year, "string"); | |
} | |
}, | |
"%y should return two digit year": function () { | |
assert.equals(this.date.strftime("%y"), "09"); | |
}, | |
"%m should return month": function () { | |
assert.equals(this.date.strftime("%m"), "12"); | |
}, | |
"%d should return date": function () { | |
assert.equals(this.date.strftime("%d"), "05"); | |
}, | |
"//%j should return the day of the year": function () { | |
var date = new Date(2011, 0, 1); | |
assert.equals(date.strftime("%j"), 1); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is unfinished demo code | |
Date.prototype.strftime = (function () { | |
function strftime(format) { | |
var date = this; | |
return (format + "").replace(/%([a-zA-Z])/g, function (m, f) { | |
var formatter = Date.formats && Date.formats[f]; | |
if (typeof formatter == "function") { | |
return formatter.call(Date.formats, date); | |
} else if (typeof formatter == "string") { | |
return date.strftime(formatter); | |
} | |
return "%" + f; | |
}); | |
} | |
// Internal helper | |
function zeroPad(num) { | |
return (+num < 10 ? "0" : "") + num; | |
} | |
Date.formats = { | |
// Formatting methods | |
d: function (date) { | |
return zeroPad(date.getDate()); | |
}, | |
m: function (date) { | |
return zeroPad(date.getMonth() + 1); | |
}, | |
y: function (date) { | |
return zeroPad(date.getYear() % 100); | |
}, | |
Y: function (date) { | |
return date.getFullYear(); | |
}, | |
j: function (date) { | |
var jan1 = new Date(date.getFullYear(), 0, 1); | |
var diff = date.getTime() - jan1.getTime(); | |
// 86400000 == 60 * 60 * 24 * 1000 | |
return Math.ceil(diff / 86400000); | |
}, | |
// Format shorthands | |
F: "%Y-%m-%d", | |
D: "%m/%d/%y" | |
}; | |
return strftime; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment