Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support ejs include feature
  • Loading branch information
JacksonTian committed Apr 21, 2014
1 parent cba55b0 commit 85ae255
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 146 deletions.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -40,7 +40,8 @@ test-all-version:
test-coveralls:
@$(MAKE) test
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
-@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
-@$(MAKE) test MOCHA_OPTS='--require blanket' \
REPORTER=mocha-lcov-reporter | ./node_modules/.bin/coveralls

contributors: install
@./node_modules/.bin/contributors -f plain -o AUTHORS
Expand Down
53 changes: 0 additions & 53 deletions lib/filters.js
Expand Up @@ -10,8 +10,6 @@
* Module dependencies.
*/

var moment = require('moment');

var NO_ASCII_CHAR_RE = /[^\x00-\xff]/;

var TRUNCATE_PEDDING = '…';
Expand Down Expand Up @@ -60,54 +58,3 @@ exports.truncatechars = function (text, max) {
}
return text;
};

/**
* Show money format with decimal digits.
*
* e.g.:
*
* ```js
* fmoney("12345.675910", 3); // => 12,345.676
* ```
*
* @param {String|Number} s
* @param {Number} n, decimal digits.
* @return {String}
*/
exports.fmoney = function fmoney(s, n) {
s = parseFloat((s + "").replace(/[^\d\.\-]/g, ""));
if (isNaN(s)) {
return s;
}
var lr = (s.toFixed(n || 0) + '').split('.');
var l = lr[0].split('').reverse();
var t = [];
for (var i = 0, len = l.length, last = len - 1; i < len; i++) {
t.push(l[i]);
if ((i + 1) % 3 === 0 && i !== last) {
t.push(',');
}
}
t = t.reverse();
if (lr[1]) {
t.push('.');
t.push(lr[1]);
}
return t.join('');
};

/**
* Format DateTime to string.
*
* @see http://momentjs.com/docs/#/displaying/format/
* @param {Date} d
* @param {String} format, default is 'YYYY-MM-DD HH:mm:ss'.
* @return {String}
*/
exports.dateformat = function (d, format) {
var date = moment(d);
if (!d || !date || !date.isValid()) {
return '';
}
return date.format(format || 'YYYY-MM-DD HH:mm:ss');
};
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -23,8 +23,7 @@
}
},
"dependencies": {
"moment": "2.x.x",
"ejs": "0.8.x"
"ejs": "1.0.x"
},
"devDependencies": {
"contributors": "*",
Expand Down
88 changes: 0 additions & 88 deletions test/filters.test.js
Expand Up @@ -65,92 +65,4 @@ describe('filters.test.js', function () {
filters.truncatechars('hello2', 2).should.equal('he' + p);
});
});

describe('fmoney()', function () {

it('should throw RangeError', function () {
(function () {
filters.fmoney(1, -1);
filters.fmoney(1, 21);
filters.fmoney(1, -100);
filters.fmoney(1, 100);
filters.fmoney(1, Infinity);
filters.fmoney(1, Number.MAX_VALUE);
filters.fmoney(1, Number.MIN_VALUE);
}).should.throw();
});

it('should return NaN', function () {
isNaN(filters.fmoney()).should.ok;
isNaN(filters.fmoney(NaN)).should.ok;
isNaN(filters.fmoney(null)).should.ok;
isNaN(filters.fmoney(undefined)).should.ok;
isNaN(filters.fmoney('abc')).should.ok;
isNaN(filters.fmoney('')).should.ok;
});

it('should return right string', function () {
filters.fmoney(1, null).should.equal('1');
filters.fmoney(1, undefined).should.equal('1');
filters.fmoney(1, NaN).should.equal('1');
filters.fmoney(1, 'abc').should.equal('1');

filters.fmoney(1).should.equal('1');
filters.fmoney(1000).should.equal('1,000');
filters.fmoney(1000000).should.equal('1,000,000');
filters.fmoney(1, 0).should.equal('1');
filters.fmoney(1000, 0).should.equal('1,000');
filters.fmoney(1000000, 0).should.equal('1,000,000');
filters.fmoney(1, 1).should.equal('1.0');
filters.fmoney(1, 1.1).should.equal('1.0');
filters.fmoney(1, 1.4).should.equal('1.0');
filters.fmoney(1, 1.5).should.equal('1.0');
filters.fmoney(1, 1.6).should.equal('1.0');
filters.fmoney(1, 1.9).should.equal('1.0');
filters.fmoney(1000, 1).should.equal('1,000.0');
filters.fmoney(1000000, 1).should.equal('1,000,000.0');
filters.fmoney(1, 2).should.equal('1.00');
filters.fmoney(1, 2.1).should.equal('1.00');
filters.fmoney(1000, 2).should.equal('1,000.00');
filters.fmoney(1000000, 2).should.equal('1,000,000.00');
filters.fmoney(1, 20).should.equal('1.00000000000000000000');
filters.fmoney(1000, 20).should.equal('1,000.00000000000000000000');
filters.fmoney(1000000, 20).should.equal('1,000,000.00000000000000000000');

filters.fmoney("12345.675910", 1).should.equal('12,345.7');
filters.fmoney("12345.675910", 2).should.equal('12,345.68');
filters.fmoney("12345.675910", 3).should.equal('12,345.676');

filters.fmoney("12345.1234", 1).should.equal('12,345.1');
filters.fmoney("12345.1234", 2).should.equal('12,345.12');
filters.fmoney("12345.1234", 3).should.equal('12,345.123');

filters.fmoney("12345.675910", 3).should.equal('12,345.676');

for (var i = 1; i <= 20; i++) {
var wanted = i + '.' + new Array(i + 1).join('0');
filters.fmoney(i, i).should.equal(wanted);
filters.fmoney(i, i + '.' + i).should.equal(wanted);
}
});

});

describe('dateformat()', function () {
it('should reutrn empty string when input error', function () {
filters.dateformat().should.equal('');
filters.dateformat(null).should.equal('');
filters.dateformat(0).should.equal('');
filters.dateformat('').should.equal('');
filters.dateformat('foo').should.equal('');
});

it('should work', function () {
filters.dateformat('2012-11-11').should.equal('2012-11-11 00:00:00');
filters.dateformat('2012-11-11 00').should.equal('2012-11-11 00:00:00');
filters.dateformat(new Date()).should.match(/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/);
filters.dateformat(new Date('2012-11-11 00:00:01')).should.equal('2012-11-11 00:00:01');
});
});

});
13 changes: 12 additions & 1 deletion test/render.test.js
Expand Up @@ -57,6 +57,9 @@ app.use(function (req, res) {
if (req.url === '/partial_not_exists') {
return res.render('partial_not_exists.html', { layout: false });
}
if (req.url === '/include') {
return res.render('include.html', { layout: false, name: 'fengmk2', index: 'index' });
}
if (req.url === '/error') {
return res.render('error.html');
}
Expand Down Expand Up @@ -182,7 +185,7 @@ describe('render.test.js', function () {
it('should render with filters', function (done) {
request(app).get('/filters')
.expect(200)
.expect('你… | 12,345.12 | 2012-11-11 00:00:00 | /filters', done);
.expect('你… | /filters\n', done);
});

it('should overwrite:`default` to overwrite:`success`', function (done) {
Expand Down Expand Up @@ -223,4 +226,12 @@ describe('render.test.js', function () {
});
});
});

describe('include with ejs', function () {
it('should support include', function (done) {
request(app).get('/include')
.expect(200)
.expect('Hi, I will include "be_included".\nI should be included\n\n', done);
});
});
});
1 change: 1 addition & 0 deletions test/views/be_included.html
@@ -0,0 +1 @@
<%="I should be included"%>
2 changes: 1 addition & 1 deletion test/views/filters.html
@@ -1 +1 @@
<%- truncatechars('你好啊', 2) %> | <%- fmoney(12345.1234, 2) %> | <%- dateformat('2012-11-11') %> | <%- query(locals.request.url).pathname %>
<%- truncatechars('你好啊', 2) %> | <%- query(locals.request.url).pathname %>
2 changes: 2 additions & 0 deletions test/views/include.html
@@ -0,0 +1,2 @@
Hi, I will include "be_included".
<% include be_included.html %>

0 comments on commit 85ae255

Please sign in to comment.