Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added date-string translation ability and test cases #1

Merged
merged 1 commit into from
Feb 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ exports.init = function (options) {
_config.filters = _.extend(filters, options.filters);
_config.tags = _.extend(tags, options.tags);

if(_config.dateStrings) {
dateformat._months = _config.dateStrings.months || dateformat._months;
dateformat._days = _config.dateStrings.days || dateformat._days;
dateformat._ordinals = _config.dateStrings.ordinals || dateformat._ordinals;
dateformat._ampm = _config.dateStrings.ampm || dateformat._ampm;
dateformat._AMPM = _config.dateStrings.AMPM || dateformat._AMPM;
}

dateformat.defaultTZOffset = _config.tzOffset;
};

Expand Down
68 changes: 51 additions & 17 deletions lib/dateformat.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
var _ = require('underscore'),
_months = {
full: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
abbr: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
_days = {
full: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
abbr: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
alt: {'-1': 'Yesterday', 0: 'Today', 1: 'Tomorrow'}
};
var _ = require('underscore');

/*
DateZ is licensed under the MIT License:
Expand All @@ -17,6 +8,29 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
exports.defaultTZOffset = 0;
exports._months = {
full: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
abbr: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
};
exports._days = {
full: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
abbr: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
alt: {'-1': 'Yesterday', 0: 'Today', 1: 'Tomorrow'}
};
exports._ampm = [
{ comparator:function(hour){ return hour < 12; }, result: "am"},
{ comparator:function(hour){ return hour >= 12; }, result: "pm"},
];
exports._AMPM = [
{ comparator:function(hour){ return hour < 12; }, result: "AM"},
{ comparator:function(hour){ return hour >= 12; }, result: "PM"},
];
exports._ordinals = [
{ comparator:function(day){ return day % 10 === 1 && day !== 11; }, result: "st"},
{ comparator:function(day){ return day % 10 === 2 && day !== 12; }, result: "nd"},
{ comparator:function(day){ return day % 10 === 3 && day !== 13; }, result: "rd"},
{ comparator:function(day){ return true; }, result: "th"} // default
];
exports.DateZ = function () {
var members = {
'default': ['getUTCDate', 'getUTCDay', 'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth', 'getUTCSeconds', 'toISOString', 'toGMTString', 'toUTCString', 'valueOf', 'getTime'],
Expand Down Expand Up @@ -103,20 +117,26 @@ exports.d = function (input) {
return (input.getDate() < 10 ? '0' : '') + input.getDate();
};
exports.D = function (input) {
return _days.abbr[input.getDay()];
return exports._days.abbr[input.getDay()];
};
exports.j = function (input) {
return input.getDate();
};
exports.l = function (input) {
return _days.full[input.getDay()];
return exports._days.full[input.getDay()];
};
exports.N = function (input) {
return input.getDay() + 1;
};
exports.S = function (input) {
var d = input.getDate();
return (d % 10 === 1 && d !== 11 ? 'st' : (d % 10 === 2 && d !== 12 ? 'nd' : (d % 10 === 3 && d !== 13 ? 'rd' : 'th')));
var s;
_.each(exports._ordinals, function(element, index, list){
if(typeof s !== "undefined") return;
var comparator = element.comparator;
if(comparator(d)) s = element.result;
});
return s;
};
exports.w = function (input) {
return input.getDay();
Expand Down Expand Up @@ -149,13 +169,13 @@ exports.W = function (input) {

// Month
exports.F = function (input) {
return _months.full[input.getMonth()];
return exports._months.full[input.getMonth()];
};
exports.m = function (input) {
return (input.getMonth() < 9 ? '0' : '') + (input.getMonth() + 1);
};
exports.M = function (input) {
return _months.abbr[input.getMonth()];
return exports._months.abbr[input.getMonth()];
};
exports.n = function (input) {
return input.getMonth() + 1;
Expand All @@ -182,10 +202,24 @@ exports.y = function (input) {

// Time
exports.a = function (input) {
return input.getHours() < 12 ? 'am' : 'pm';
var s;
var d = input.getHours();
_.each(exports._ampm, function(element, index, list){
if(typeof s !== "undefined") return;
var comparator = element.comparator;
if(comparator(d)) s = element.result;
});
return s;
};
exports.A = function (input) {
return input.getHours() < 12 ? 'AM' : 'PM';
var s;
var d = input.getHours();
_.each(exports._AMPM, function(element, index, list){
if(typeof s !== "undefined") return;
var comparator = element.comparator;
if(comparator(d)) s = element.result;
});
return s;
};
exports.B = function (input) {
var hours = input.getUTCHours(), beats;
Expand Down
72 changes: 72 additions & 0 deletions lib/dates.spanish.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var swig = require('../index'),
swig_spanish = require('../index'),
DateZ = require('./dateformat').DateZ;

swig_spanish.init({
allowErrors: true,
dateStrings:{
days:{
full: ['lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo'],
abbr: ['lun', 'mar', 'mié', 'jue', 'vie', 'sab', 'dom'],
alt: {'-1': 'de ayer', 0: 'de hoy', 1: 'de mañana'}
},
months:{
full: ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'],
abbr: ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
},
ordinals: [
{ comparator : function(number){ return number % 10 === 1 || number % 10 === 3; }, result:"er" },
{ comparator : function(number){ return true; }, result:".o" }
]
// AM and PM are the same in spanish so use defaults
}
});

function testFilter(test, filter, input, output, message) {
var tpl = swig.compile('{{ v|' + filter + ' }}');
test.strictEqual(tpl(input), output, message);
}

// Create dates for a particular timezone offset.
// For example, specifying 480 (offset in minutes) for tzOffset creates a date
// in your local timezone that is the same date as the specified y,m,d,h,i,s in PST.
function makeDate(tzOffset, y, m, d, h, i, s) {
var date = new Date(y, m || 0, d || 0, h || 0, i || 0, s || 0),
offset = date.getTimezoneOffset();

if (offset !== tzOffset) { // timezone offset in PST for september
date = new Date(date.getTime() - ((offset * 60000) - (tzOffset * 60000)));
}

return date;
}

exports.date = function (test) {
var date = makeDate(420, 2011, 8, 6, 9, 5, 2),
tpl = swig.compile('{{ d|date("d", 420) }}');

function testFormat(format, expected) {
testFilter(test, 'date("' + format + '", 420)', { v: date }, expected, format);
}
// Day
testFormat('D', 'mié');
testFormat('l', 'miércoles');
testFormat('S', '.o');

// Month
testFormat('F', 'septiembre');
testFormat('M', 'sep');

// Time
testFormat('a', 'am');
testFormat('A', 'AM');

// Test teen ordinals
date = makeDate(420, 2011, 8, 11, 9, 5, 2),

testFormat('D', 'lun');
testFormat('l', 'lunes');
testFormat('S', 'er');

test.done();
};