Skip to content

Commit

Permalink
this sort of works for date filters, but i'm not very happy about the…
Browse files Browse the repository at this point in the history
… way i had to accomplish this.
  • Loading branch information
paularmstrong committed Aug 7, 2011
1 parent 90b3c24 commit e285cdd
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
88 changes: 87 additions & 1 deletion lib/filters.js
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,67 @@
var helpers = require('./helpers'), var helpers = require('./helpers'),
escape = helpers.escape, escape = helpers.escape,
_filters; _filters, _dateFormats;

_dateFormats = {
_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'}
},
// Day
d: "function () { return (this.getDate() < 10 ? '0' : '') + this.getDate(); }",
D: "function () { return _dateFormats._days.abbr[this.getDay()]; }",
j: "function () { return this.getDate(); }",
l: "function () { return _dateFormats._days.full[this.getDay()]; }",
N: "function () { return this.getDay(); }",
S: "function () { return (this.getDate() % 10 === 1 && this.getDate() !== 11 ? 'st' : (this.getDate() % 10 === 2 && this.getDate() !== 12 ? 'nd' : (this.getDate() % 10 === 3 && this.getDate() !== 13 ? 'rd' : 'th'))); }",
w: "function () { return this.getDay() - 1; }",
//z: function () { return ''; },

// Week
//W: function () { return ''; },

// Month
F: "function () { return _dateFormats._months.full[this.getMonth()]; }",
m: "function () { return (this.getMonth() < 8 ? '0' : '') + (this.getMonth() + 1); }",
M: "function () { return _dateFormats._months.abbr[this.getMonth()]; }",
n: "function () { return this.getMonth() + 1; }",
//t: function () { return ''; },

// Year
//L: function () { return ''; },
//o: function () { return ''; },
Y: "function () { return this.getFullYear(); }",
y: "function () { return ('' + this.getFullYear()).substr(2); }",

// Time
a: "function () { return this.getHours() < 12 ? 'am' : 'pm'; }",
A: "function () { return this.getHours() < 12 ? 'AM' : 'PM'; }",
//B: function () { return ''; },
g: "function () { return this.getHours() === 0 ? 12 : (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()); }",
G: "function () { return this.getHours(); }",
h: "function () { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? '0' : '') + (this.getHours() < 10 ? this.getHours() : this.getHours() - 12); }",
H: "function () { return (this.getHours() < 10 ? '0' : '') + this.getHours(); }",
i: "function () { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); }",
s: "function () { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); }",
//u: function () { return ''; },

// Timezone
//e: function () { return ''; },
//I: function () { return ''; },
O: "function () { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; }",
//T: function () { return ''; },
Z: "function () { return this.getTimezoneOffset() * 60; }",

// Full Date/Time
//c: function () { return ''; },
r: "function () { return this.toString(); }",
U: "function () { return this.getTime() / 1000; }"
};


_filters = { _filters = {
lower: function () { lower: function () {
Expand Down Expand Up @@ -41,6 +102,31 @@ _filters = {


striptags: function () { striptags: function () {
return '(function () { return ' + this + '.toString().replace(/(<([^>]+)>)/ig, ""); })()'; return '(function () { return ' + this + '.toString().replace(/(<([^>]+)>)/ig, ""); })()';
},

date: function (format) {
var formatters = [], l = format.length, cur, t;
while (l--) {
cur = format.charAt(l);
formatters.push((_dateFormats.hasOwnProperty(cur)) ? 'b = ' + _dateFormats[cur] : cur);
}
formatters.shift();
formatters.pop();
return [
'(function () {'
, 'var out = "", i = 0, cur,'
, 'formatters = ' + JSON.stringify(formatters) + ','
, 'date = new Date(' + this + ');'
, 'for (i; i < ' + formatters.length + '; i += 1) {'
, 'if (typeof eval(formatters[i]) === "function") {'
, 'out += eval(formatters[i]).call(date);'
, '} else {'
, 'out += formatters[i];'
, '}'
, '}'
, 'return out;'
, '})()'
].join('');
} }
}; };


Expand Down
35 changes: 35 additions & 0 deletions tests/filters.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -76,3 +76,38 @@ exports.multiple = function (test) {
test.strictEqual('Awesome Sauce', eval(filters.wrap('input', ['join(\' \')', 'title']))); test.strictEqual('Awesome Sauce', eval(filters.wrap('input', ['join(\' \')', 'title'])));
test.done(); test.done();
}; };

exports.date = function (test) {
var input = 'Sat Aug 06 2011 09:05:02 GMT-0700 (PDT)';

test.strictEqual('06', eval(filters.wrap('input', ['date("d")'])), 'format: d http://www.php.net/date');
// test.strictEqual('Sat', eval(filters.wrap('input', ['date("D")'])), 'format: D http://www.php.net/date');
test.strictEqual('6', eval(filters.wrap('input', ['date("j")'])), 'format: j http://www.php.net/date');
// test.strictEqual('Saturday', eval(filters.wrap('input', ['date("l")'])), 'format: l http://www.php.net/date');
test.strictEqual('6', eval(filters.wrap('input', ['date("N")'])), 'format: N http://www.php.net/date');
test.strictEqual('th', eval(filters.wrap('input', ['date("S")'])), 'format: S http://www.php.net/date');
test.strictEqual('5', eval(filters.wrap('input', ['date("w")'])), 'format: w http://www.php.net/date');
// test.strictEqual('August', eval(filters.wrap('input', ['date("F")'])), 'format: F http://www.php.net/date');
test.strictEqual('08', eval(filters.wrap('input', ['date("m")'])), 'format: m http://www.php.net/date');
// test.strictEqual('Aug', eval(filters.wrap('input', ['date("M")'])), 'format: M http://www.php.net/date');
test.strictEqual('8', eval(filters.wrap('input', ['date("n")'])), 'format: n http://www.php.net/date');

test.strictEqual('2011', eval(filters.wrap('input', ['date("Y")'])), 'format: Y http://www.php.net/date');
test.strictEqual('11', eval(filters.wrap('input', ['date("y")'])), 'format: y http://www.php.net/date');
test.strictEqual('2011', eval(filters.wrap('input', ['date("Y")'])), 'format: Y http://www.php.net/date');
test.strictEqual('am', eval(filters.wrap('input', ['date("a")'])), 'format: a http://www.php.net/date');
test.strictEqual('AM', eval(filters.wrap('input', ['date("A")'])), 'format: A http://www.php.net/date');
test.strictEqual('9', eval(filters.wrap('input', ['date("g")'])), 'format: g http://www.php.net/date');
test.strictEqual('9', eval(filters.wrap('input', ['date("G")'])), 'format: G http://www.php.net/date');
test.strictEqual('09', eval(filters.wrap('input', ['date("h")'])), 'format: h http://www.php.net/date');
test.strictEqual('09', eval(filters.wrap('input', ['date("H")'])), 'format: H http://www.php.net/date');
test.strictEqual('05', eval(filters.wrap('input', ['date("i")'])), 'format: i http://www.php.net/date');
test.strictEqual('02', eval(filters.wrap('input', ['date("s")'])), 'format: s http://www.php.net/date');

test.strictEqual('+0700', eval(filters.wrap('input', ['date("O")'])), 'format: O http://www.php.net/date');
test.strictEqual('25200', eval(filters.wrap('input', ['date("Z")'])), 'format: Z http://www.php.net/date');
test.strictEqual('Sat Aug 06 2011 09:05:02 GMT-0700 (PDT)', eval(filters.wrap('input', ['date("r")'])), 'format: r http://www.php.net/date');
test.strictEqual('1312646702', eval(filters.wrap('input', ['date("U")'])), 'format: U http://www.php.net/date');

test.done();
};

0 comments on commit e285cdd

Please sign in to comment.