From c5f7c55a0e5f601751ad7a47c599f105d9c1cd42 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Wed, 11 Sep 2019 15:43:07 +0930 Subject: [PATCH] refactor: no unused argument, destructure path (#69) * refactor(test): no unused argument * style(test): chai should * refactor: destructure path module * refactor: destructure fs module --- index.js | 4 ++-- lib/template.js | 8 ++++---- test/index.js | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 8137ab6..a6f3c12 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ /* global hexo */ 'use strict'; -const pathFn = require('path'); +const { extname } = require('path'); const config = hexo.config.sitemap = Object.assign({ path: 'sitemap.xml' }, hexo.config.sitemap); -if (!pathFn.extname(config.path)) { +if (!extname(config.path)) { config.path += '.xml'; } diff --git a/lib/template.js b/lib/template.js index 02132d4..afb5373 100644 --- a/lib/template.js +++ b/lib/template.js @@ -1,7 +1,7 @@ 'use strict'; -const pathFn = require('path'); -const fs = require('fs'); +const { join } = require('path'); +const { readFileSync } = require('fs'); let sitemapTmpl; module.exports = function(config) { @@ -17,8 +17,8 @@ module.exports = function(config) { return encodeURI(str); }); - const sitemapSrc = config.sitemap.template || pathFn.join(__dirname, '../sitemap.xml'); - sitemapTmpl = nunjucks.compile(fs.readFileSync(sitemapSrc, 'utf8'), env); + const sitemapSrc = config.sitemap.template || join(__dirname, '../sitemap.xml'); + sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env); return sitemapTmpl; }; diff --git a/test/index.js b/test/index.js index 348eae9..316788f 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); const Hexo = require('hexo'); const cheerio = require('cheerio'); @@ -22,7 +22,7 @@ describe('Sitemap generator', () => { {source: 'bar', slug: 'bar', updated: 1e8 + 1}, {source: 'baz', slug: 'baz', updated: 1e8 - 1} ]).then(data => { - posts = Post.sort('-updated'); + posts = data.sort((a, b) => b.updated - a.updated); locals = hexo.locals.toObject(); }); }); @@ -34,14 +34,14 @@ describe('Sitemap generator', () => { result.path.should.eql('sitemap.xml'); result.data.should.eql(sitemapTmpl.render({ config: hexo.config, - posts: posts.toArray() + posts: posts })); const $ = cheerio.load(result.data); $('url').each((index, element) => { - $(element).children('loc').text().should.eql(posts.eq(index).permalink); - $(element).children('lastmod').text().should.eql(posts.eq(index).updated.toISOString()); + $(element).children('loc').text().should.eql(posts[index].permalink); + $(element).children('lastmod').text().should.eql(posts[index].updated.toISOString()); }); });