Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more filters + more tests

  • Loading branch information...
commit b5c437e5b8c0823b8cd35fafa88a5c209bba25bf 1 parent 177519a
Anders Hellerup Madsen authored
View
28 template/template_defaults.js
@@ -7,12 +7,12 @@ var utils = require('../utils/utils');
var filters = exports.filters = {
- add: function (value, arg) { return value + arg; },
+ add: function (value, arg) { return (typeof value === 'number' && typeof arg === 'number') ? (value + arg) : ''; },
addslashes: function (value, arg) { return utils.string.add_slashes(value.toString()); },
capfirst: function (value, arg) { return utils.string.cap_first(value.toString()); },
center: function (value, arg) { return utils.string.center(value.toString(), arg); },
- cut: function (value, arg) { return value.toString().replace(arg, ""); },
- date: function (value, arg) { return utils.date.date(arg, new Date(value)); },
+ cut: function (value, arg) { return value.toString().replace(new RegExp(arg, 'g'), ""); },
+ date: function (value, arg) { return (value instanceof Date) ? utils.date.date(arg, value) : ''; },
'default': function (value, arg) { return value ? value : arg; },
default_if_none: function (value, arg) { return (value === null || value === undefined) ? arg : value; },
@@ -33,7 +33,7 @@ var filters = exports.filters = {
// TODO: Implement escaping/autoescaping correctly
throw "escape() filter is not implemented";
},
- escapejs: function (value, arg) { return escape(value); },
+ escapejs: function (value, arg) { return escape(value || ''); },
filesizeformat: function (value, arg) {
var bytes = Number(value);
if (isNaN(bytes)) { return "0 bytes"; }
@@ -60,7 +60,7 @@ var filters = exports.filters = {
}
return s;
},
- force_escape: function (value, arg) { return utils.string.html_escape(value.toString()); },
+ force_escape: function (value, arg) { return utils.html.escape(value.toString()); },
get_digit: function (value, arg) {
if (typeof value !== 'number' || typeof arg !== 'number' || typeof arg < 1) { return value; }
var s = value.toString();
@@ -74,6 +74,24 @@ var filters = exports.filters = {
last: function (value, arg) { return (value instanceof Array && value.length) ? value[value.length - 1] : ''; },
length: function (value, arg) { return value.hasOwnProperty('length') ? value.length : 0; },
length_is: function (value, arg) { return value.hasOwnProperty('length') && value.length === arg; },
+ linebreaks: function (value, arg) { return utils.html.linebreaks(value.toString()); },
+ linebreaksbr: function (value, arg) { return value.toString().replace(/\n/g, '<br />'); },
+ linenumbers: function (value, arg) {
+ var lines = value.toString().split('\n');
+ var zeroes = new Array(lines.length.toString().length + 1).join('0');
+ lines = lines.map( function (s, idx) {
+ var num = (idx + 1).toString();
+ return zeroes.slice(0, zeroes.length - num.length) + num + '. ' + s;
+ });
+ return lines.join('\n');
+ },
+ ljust: function (value, arg) {
+ if (typeof arg !== 'number') { return ''; }
+ if (arg <= value.length) { return value.slice(0, arg); }
+ var spaces = new Array(arg + 1).join(' ');
+ return value + spaces.slice(0, arg - value.length);
+ },
+ lower: function (value, arg) { return typeof value === 'string' ? value.toLowerCase() : ''; },
sub: function (value, arg) { return value - arg; }
};
View
107 template/template_defaults.test.js
@@ -2,6 +2,46 @@ var sys = require('sys');
process.mixin(GLOBAL, require('../utils/test').dsl);
process.mixin(GLOBAL, require('./template_defaults'));
+testcase('add')
+ test('should add correctly', function () {
+ assertEquals(6, filters.add(4, 2));
+ assertEquals('', filters.add('a', 2));
+ assertEquals('', filters.add(2, 'a'));
+ });
+testcase('addslashes')
+ test('should add slashes correctly', function () {
+ assertEquals('he said \\"she said\\"', filters.addslashes('he said \"she said\"'));
+ assertEquals('6', filters.addslashes(6));
+ });
+testcase('capfirst')
+ test('should capitalize first letter correctly', function () {
+ assertEquals('Somewhere over the rainbow', filters.capfirst('somewhere over the rainbow'));
+ assertEquals('6', filters.capfirst(6));
+ });
+testcase('center')
+ test('center value', function () {
+ assertEquals(' centered ', filters.center('centered', 18));
+ assertEquals(' 6 ', filters.center(6, 18));
+ })
+testcase('cut')
+ test('remove unwanted letters', function () {
+ assertEquals('SomewhereOverTheRainbow', filters.cut('Somewhere Over The Rainbow', ' '));
+ });
+testcase('date')
+ test('correctly format Britneys birthdate', function () {
+ assertEquals('December 2, 1981', filters.date(new Date('12-02-1981'), 'F j, Y'));
+ assertEquals('', filters.date('hest', 'F j, Y'));
+ });
+testcase('default')
+ test('work as expected', function () {
+ assertEquals(6, filters['default'](false, 6));
+ })
+testcase('default_if_none')
+ test('work as expected', function () {
+ assertEquals(false, filters.default_if_none(false, 6));
+ assertEquals(6, filters.default_if_none(null, 6));
+ assertEquals(6, filters.default_if_none(undefined, 6));
+ })
testcase('Test dictsort filter');
test('should sort correctly', function () {
var list = [
@@ -14,6 +54,7 @@ testcase('Test dictsort filter');
assertEquals(before, list);
});
+testcase('dictsortreversed filter');
test('should sort correctly with dictsortreversed', function () {
var list = [
{'name': 'zed', 'age': 19},
@@ -25,6 +66,22 @@ testcase('Test dictsort filter');
assertEquals(before, list);
});
+testcase('divisibleby filter')
+ test('correctly determine if value is divisible with arg', function () {
+ assertEquals(true, filters.divisibleby(4, 2));
+ assertEquals(false, filters.divisibleby(5, 2));
+ assertEquals(false, filters.divisibleby('hest', 2));
+ assertEquals(false, filters.divisibleby('hest'));
+ });
+
+testcase('escapejs filter')
+ test('correctly escape value', function () {
+ assertEquals(escape('æøå&&Ø ""\n'), filters.escapejs('æøå&&Ø ""\n'));
+ assertEquals('6', filters.escapejs(6));
+ assertEquals('', filters.escapejs());
+
+ });
+
testcase('filesizeformat filter');
test('should return correct readable filesizes', function () {
assertEquals('117.7MB', filters.filesizeformat(123456789));
@@ -102,6 +159,56 @@ testcase('length_is filter')
assertEquals(false, filters.length_is(16, 4));
assertEquals(false, filters.length_is('hest'));
});
+testcase('linebreaks')
+ test('linebreaks should be converted to <p> and <br /> tags.', function () {
+ assertEquals('<p>Joel<br />is a slug</p>', filters.linebreaks('Joel\nis a slug'));
+ });
+testcase('linebreaksbr')
+ test('linebreaks should be converted to <br /> tags.', function () {
+ assertEquals('Joel<br />is a slug.<br />For sure...', filters.linebreaksbr('Joel\nis a slug.\nFor sure...'));
+ });
+testcase('linenumbers')
+ test('should add linenumbers to text', function () {
+ var s = "But I must explain to you how all this mistaken idea of\n"
+ + "denouncing pleasure and praising pain was born and I will\n"
+ + "give you a complete account of the system, and expound the\n"
+ + "actual teachings of the great explorer of the truth, the \n"
+ + "aster-builder of human happiness. No one rejects, dislikes,\n"
+ + "or avoids pleasure itself, because it is pleasure, but because\n"
+ + "those who do not know how to pursue pleasure rationally\n"
+ + "encounter consequences that are extremely painful. Nor again\n"
+ + "is there anyone who loves or pursues or desires to obtain pain\n"
+ + "of itself, because it is pain, but because occasionally\n"
+ + "circumstances occur in which toil and pain can procure him\n"
+ + "some great pleasure. To take a trivial example, which of us"
+
+ var expected = "01. But I must explain to you how all this mistaken idea of\n"
+ + "02. denouncing pleasure and praising pain was born and I will\n"
+ + "03. give you a complete account of the system, and expound the\n"
+ + "04. actual teachings of the great explorer of the truth, the \n"
+ + "05. aster-builder of human happiness. No one rejects, dislikes,\n"
+ + "06. or avoids pleasure itself, because it is pleasure, but because\n"
+ + "07. those who do not know how to pursue pleasure rationally\n"
+ + "08. encounter consequences that are extremely painful. Nor again\n"
+ + "09. is there anyone who loves or pursues or desires to obtain pain\n"
+ + "10. of itself, because it is pain, but because occasionally\n"
+ + "11. circumstances occur in which toil and pain can procure him\n"
+ + "12. some great pleasure. To take a trivial example, which of us"
+
+ assertEquals(expected, filters.linenumbers(s));
+ });
+testcase('ljust')
+ test('should left justify value i correctly sized field', function () {
+ assertEquals('hest ', filters.ljust('hest', 10));
+ assertEquals('', filters.ljust('hest'));
+ assertEquals('he', filters.ljust('hest', 2));
+ });
+testcase('lower')
+ test('should lowercase value', function () {
+ assertEquals('somewhere over the rainbow', filters.lower('Somewhere Over the Rainbow'));
+ assertEquals('', filters.lower(19));
+ });
run();
+
View
9 utils/string.js
@@ -48,15 +48,6 @@ function center(s, width) {
}
exports.center = center;
-function html_escape(s) {
- return s
- .replace(/&/g, '&amp;')
- .replace(/</g, '&lt;')
- .replace(/>/g, '&gt;')
- .replace(/'/g, '&#39;')
- .replace(/"/g, '&qout;');
-};
-exports.html_escape = html_escape;
View
3  utils/utils.js
@@ -1,5 +1,6 @@
/*jslint laxbreak: true, eqeqeq: true, undef: true, regexp: false */
-/*global require, process, exports */
+/*global require, exports */
exports.string = require('./string');
exports.date = require('./date');
+exports.html = require('./html');
Please sign in to comment.
Something went wrong with that request. Please try again.