Skip to content

Instantly share code, notes, and snippets.

@soomtong
Last active December 16, 2015 16:48
Show Gist options
  • Save soomtong/5465409 to your computer and use it in GitHub Desktop.
Save soomtong/5465409 to your computer and use it in GitHub Desktop.
Simple substring filter
/*
checkout this
http://paularmstrong.github.io/swig/docs/#filters-custom
setup like below
// set template engine
var swig = require('swig');
var swigFilter = require('./swigFilter');
var swigTag = require('./swigTag');
app.engine('html', swig.renderFile);
swig.setFilter('substring', swigFilter.substring);
swig.setFilter('printDate', swigFilter.printDate);
swig.setTag('iterate', swigTag.iterate.parse, swigTag.iterate.compile, swigTag.iterate.ends);
swig.setDefaults({
cache: false // default 'memory'
});
use :
{{ someString|substring(10) }}
*/
exports.substring = function (input, count) {
return input.toString().substr(0, count);
};
/*
use :
{{ myDate|printDate('d') }}
*/
exports.printDate = function (input, type) {
var str, year, month, day;
if (input.length == 8 && type != 'd') {
year = input.slice(0, 4);
month = input.slice(4, 6);
day = input.slice(6, 8);
if (month.length == 2 && month.slice(0,1) == '0') month = month.slice(1, 2);
if (day.length == 2 && day.slice(0,1) == '0') day = day.slice(1, 2);
str = month + '월 ' + day + '일';
} else if (type == 'D') {
day = input.slice(input.length - 2, input.length);
if (day.length == 2 && day.slice(0,1) == '0') day = day.slice(1, 2);
str = day + '일';
} else {
day = input.slice(input.length - 2, input.length);
if (day.length == 2 && day.slice(0,1) == '0') day = day.slice(1, 2);
str = day;
}
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment