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

fix(moment.locale): avoid lookup repeatedly with the wrong names. #4007

Merged
merged 1 commit into from
Dec 24, 2019
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
4 changes: 2 additions & 2 deletions lib/models/types/moment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const moment = require('moment-timezone');
const { SchemaType } = require('warehouse');
const { moment, toMomentLocale } = require('../../plugins/helper/date');

class SchemaTypeMoment extends SchemaType {
constructor(name, options = {}) {
Expand All @@ -23,7 +23,7 @@ SchemaTypeMoment.prototype.cast = function(value, data) {
const { options } = this;
value = toMoment(value);

if (options.language) value = value.locale(options.language);
if (options.language) value = value.locale(toMomentLocale(options.language));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options.language always is undefined.
This is a bug, or this is unnecessary code.

Copy link
Member

@SukkaW SukkaW Dec 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, models/types/moment is only used in Post and Page model.

type: Moment,

type: Moment,

What happened then is still a mystery. Only tommy351 knows.

if (options.timezone) value = value.tz(options.timezone);

return value;
Expand Down
31 changes: 25 additions & 6 deletions lib/plugins/helper/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
const moment = require('moment-timezone');
const { isMoment } = moment;

const isDate = value => {
if (typeof value === 'object') {
if (value instanceof Date) return !isNaN(value.getTime());
}
return false;
};
const isDate = value =>
typeof value === 'object' && value instanceof Date && !isNaN(value.getTime());

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

if (lang) date = date.locale(lang);
if (timezone) date = date.tz(timezone);
Expand Down Expand Up @@ -68,10 +65,32 @@ function getLanguage(ctx) {
return ctx.page.lang || ctx.page.language || ctx.config.language;
}

/**
* Convert Hexo language code to Moment locale code.
* examples:
* default => en
* zh-CN => zh-cn
*
* Moment defined locales: https://github.com/moment/moment/tree/master/locale
*/
function toMomentLocale(lang) {
if (lang === undefined) {
return undefined;
}

// moment.locale('') equals moment.locale('en')
// moment.locale(null) equals moment.locale('en')
if (!lang || lang === 'en' || lang === 'default') {
return 'en';
}
return lang.toLowerCase().replace('_', '-');
}

exports.date = dateHelper;
exports.date_xml = toISOString;
exports.time = timeHelper;
exports.full_date = fullDateHelper;
exports.relative_date = relativeDateHelper;
exports.time_tag = timeTagHelper;
exports.moment = moment;
exports.toMomentLocale = toMomentLocale;
4 changes: 3 additions & 1 deletion lib/plugins/helper/list_archives.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

const { toMomentLocale } = require('./date');

function listArchivesHelper(options = {}) {
const { config } = this;
const archiveDir = config.archive_dir;
const { timezone } = config;
const lang = this.page.lang || this.page.language || config.language;
const lang = toMomentLocale(this.page.lang || this.page.language || config.language);
let { format } = options;
const type = options.type || 'monthly';
const { style = 'list', transform, separator = ', ' } = options;
Expand Down
12 changes: 12 additions & 0 deletions test/scripts/helpers/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,16 @@ describe('date', () => {
timeTag(Date.now(), 'LLL').should.eql('<time datetime="' + moment().toISOString() + '">' + moment().tz('UTC').format('LLL') + '</time>');
ctx.config.timezone = '';
});

it('toMomentLocale', () => {
const toMomentLocale = dateHelper.toMomentLocale;

(toMomentLocale(undefined) === undefined).should.eql(true);
toMomentLocale(null).should.eql('en');
toMomentLocale('').should.eql('en');
toMomentLocale('en').should.eql('en');
toMomentLocale('default').should.eql('en');
toMomentLocale('zh-CN').should.eql('zh-cn');
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
toMomentLocale('zh_CN').should.eql('zh-cn');
});
});