Created
July 30, 2017 12:27
-
-
Save ayoola-solomon/81066401d50301eec1e43b2ed1000b54 to your computer and use it in GitHub Desktop.
Date.prototype.strftime test with YUI created by Soulman - https://repl.it/Josn/12
This file contains 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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Date.prototype.strftime test with YUI</title> | |
</head> | |
<body class="yui-skin-sam"> | |
<div id="yui-main"> | |
<div id="testReport"></div> | |
</div> | |
<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script> | |
<script src="strftime.js"></script> | |
<script src="strftime_test.js"></script> | |
</body> | |
</html> |
This file contains 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
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 = { | |
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; | |
}()); |
This file contains 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
YUI({ | |
combine: true, | |
timeout: 10000, | |
}).use("node", "console", "test", function(Y) { | |
var strftimeTestCase = new Y.Test.Case({ | |
name: "Date.prototype.strftime Tests", | |
setUp: function () { | |
this.date = new Date(2009, 9, 2, 22, 14, 45); | |
}, | |
tearDown: function () { | |
delete this.date; | |
}, | |
"test %Y should return full year": function () { | |
var year = Date.formats.Y(this.date); | |
assert.isNumber(year); | |
assert.areEqual(2009, year); | |
}, | |
"test %m should return month": function () { | |
var month = Date.formats.m(this.date); | |
assert.isString(month); | |
assert.areEqual("10", month); | |
}, | |
"%d should return day of the month": function () { | |
assert.areEqual("02", Date.formats.d(this.date)); | |
}, | |
"%y should return year as two digits": function () { | |
assert.areEqual("09", Date.formats.y(this.date)); | |
}, | |
"test format specifier %F": function () { | |
assert.areEqual("%Y-%m-%d", Date.formats.F === "%Y-%m-%d"); | |
} | |
}); | |
// create the console | |
var r = new Y.console({ | |
newestOnTop: false, | |
style: 'block' | |
}); | |
r.render("#testReport"); | |
Y.Test.Runner.add(strftimeTestCase); | |
Y.Test.Runner.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment