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

refactor(list_archives): Reduce calls to date.format() #4011

Merged
merged 1 commit into from
Dec 27, 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
10 changes: 7 additions & 3 deletions lib/plugins/helper/list_archives.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function listArchivesHelper(options = {}) {
const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count') ? options.show_count : true;
const className = options.class || 'archive';
const order = options.order || -1;
const compareFunc = type === 'monthly'
? (yearA, monthA, yearB, monthB) => yearA === yearB && monthA === monthB
: (yearA, monthA, yearB, monthB) => yearA === yearB;

let result = '';

if (!format) {
Expand All @@ -30,14 +34,14 @@ function listArchivesHelper(options = {}) {
let date = post.date.clone();

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

const year = date.year();
const month = date.month() + 1;
const name = date.format(format);
const lastData = data[length - 1];

if (!lastData || lastData.name !== name) {
if (!lastData || !compareFunc(lastData.year, lastData.month, year, month)) {
if (lang) date = date.locale(lang);
const name = date.format(format);
length = data.push({
name,
year,
Expand Down
28 changes: 28 additions & 0 deletions test/scripts/helpers/list_archives.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ describe('list_archives', () => {
].join(''));
});

it('show_count + style: false', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

for improve test coverage (#3984)

const result = listArchives({
style: false,
show_count: false
});

result.should.eql([
'<a class="archive-link" href="/archives/2014/02/">February 2014</a>',
'<a class="archive-link" href="/archives/2013/10/">October 2013</a>',
'<a class="archive-link" href="/archives/2013/06/">June 2013</a>'
].join(', '));
});

it('order', () => {
const result = listArchives({
order: 1
Expand Down Expand Up @@ -123,6 +136,21 @@ describe('list_archives', () => {
].join(''));
});

it('transform + style: false', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

for improve test coverage (#3984)

const result = listArchives({
style: false,
transform(str) {
return str.toUpperCase();
}
});

result.should.eql([
'<a class="archive-link" href="/archives/2014/02/">FEBRUARY 2014<span class="archive-count">1</span></a>',
'<a class="archive-link" href="/archives/2013/10/">OCTOBER 2013<span class="archive-count">1</span></a>',
'<a class="archive-link" href="/archives/2013/06/">JUNE 2013<span class="archive-count">2</span></a>'
].join(', '));
});

it('separator', () => {
const result = listArchives({
style: false,
Expand Down