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

New date helper using momentjs 'fromNow' method #1350

Merged
merged 6 commits into from
Nov 7, 2015
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
25 changes: 18 additions & 7 deletions lib/plugins/helper/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var moment = require('moment-timezone');
var isMoment = moment.isMoment;
var isDate = require('util').isDate;

function output(date, format, lang, timezone){
function getMoment(date, lang, timezone){
if (date == null) date = moment();
if (!isMoment(date)) date = moment(isDate(date) ? date : new Date(date));

if (lang) date = date.locale(lang);
if (timezone) date = date.tz(timezone);

return date.format(format);
return date;
}

function toISOString(date){
Expand All @@ -28,20 +28,30 @@ function toISOString(date){

function dateHelper(date, format){
/* jshint validthis: true */
var config = this.config;
return output(date, format || config.date_format, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.date_format);
}

function fromNowHelper(date){
/* jshint validthis: true */
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.fromNow();
}

function timeHelper(date, format){
/* jshint validthis: true */
var config = this.config;
return output(date, format || config.time_format, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.time_format);
}

function fullDateHelper(date, format){
/* jshint validthis: true */
if (format){
return output(date, format, getLanguage(this), this.config.timezone);
var moment = getMoment(date, getLanguage(this), this.config.timezone);
return moment.format(format);
} else {
return this.date(date) + ' ' + this.time(date);
}
Expand All @@ -58,6 +68,7 @@ function getLanguage(ctx){
}

exports.date = dateHelper;
exports.date_fromNow = fromNowHelper;
exports.date_xml = toISOString;
exports.time = timeHelper;
exports.full_date = fullDateHelper;
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function(ctx){
var date = require('./date');

helper.register('date', date.date);
helper.register('date_fromNow', date.date_fromNow);
helper.register('date_xml', date.date_xml);
helper.register('time', date.time);
helper.register('full_date', date.full_date);
Expand Down
21 changes: 21 additions & 0 deletions test/scripts/helpers/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ describe('date', function(){
dateXML(Date.now()).should.eql(moment().toISOString());
});

it('date_fromNow', function(){
var ctx = {
config: hexo.config,
page: {}
};

var dateFromNow = dateHelper.date_fromNow.bind(ctx);

// now
dateFromNow().should.eql(moment().fromNow());

// moment
dateFromNow(moment()).should.eql(moment().fromNow());

// date
dateFromNow(new Date()).should.eql(moment().fromNow());

// number
dateFromNow(Date.now()).should.eql(moment().fromNow());
});

it('time', function(){
var ctx = {
config: hexo.config,
Expand Down