diff --git a/lib/box/index.js b/lib/box/index.js index 03a2ffd952..7519200df9 100644 --- a/lib/box/index.js +++ b/lib/box/index.js @@ -106,12 +106,8 @@ Box.prototype._readDir = function(base, fn, prefix = '') { const self = this; const { ignore } = self; - if (base && ignore && ignore.length) { - for (let i = 0, len = ignore.length; i < len; i++) { - if (minimatch(base, ignore[i])) { - return Promise.resolve('Ignoring dir.'); - } - } + if (base && ignore && ignore.length && ignore.some(item => minimatch(base, item))) { + return Promise.resolve('Ignoring dir.'); } return fs.readdir(base).map(path => fs.stat(join(base, path)).then(stats => { diff --git a/lib/extend/filter.js b/lib/extend/filter.js index b8ddc42889..e7b50522df 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -45,12 +45,9 @@ Filter.prototype.unregister = function(type, fn) { const list = this.list(type); if (!list || !list.length) return; - for (let i = 0, len = list.length; i < len; i++) { - if (list[i] === fn) { - list.splice(i, 1); - break; - } - } + const index = list.findIndex(item => item === fn); + + if (index != null) list.splice(index, 1); }; Filter.prototype.exec = function(type, data, options = {}) { diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 12e8c06549..1ce52022de 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -33,12 +33,10 @@ Tag.prototype.register = function(name, fn, options) { } else { tag = new NunjucksAsyncTag(name, fn); } + } else if (options.ends) { + tag = new NunjucksBlock(name, fn); } else { - if (options.ends) { - tag = new NunjucksBlock(name, fn); - } else { - tag = new NunjucksTag(name, fn); - } + tag = new NunjucksTag(name, fn); } this.env.addExtension(name, tag); @@ -77,11 +75,11 @@ NunjucksTag.prototype.parse = function(parser, nodes, lexer) { NunjucksTag.prototype._parseArgs = (parser, nodes, lexer) => { const tag = parser.nextToken(); const node = new nodes.NodeList(tag.lineno, tag.colno); - let token; - const argarray = new nodes.Array(tag.lineno, tag.colno); + let token; let argitem = ''; + while ((token = parser.nextToken(true))) { if (token.type === lexer.TOKEN_WHITESPACE || token.type === lexer.TOKEN_BLOCK_END) { if (argitem !== '') { diff --git a/lib/hexo/index.js b/lib/hexo/index.js index bab96173eb..20ccae0cfe 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -350,19 +350,14 @@ Hexo.prototype._generate = function(options = {}) { const path = route.format(item.path); const { data } = item; - let { layout } = item; newRouteList.push(path); - if (!layout) { + if (!item.layout) { return route.set(path, data); } - if (Array.isArray(layout)) { - layout = layout.filter((item, index, self) => self.indexOf(item) === index); - } else { - layout = [layout]; - } + const layout = Array.isArray(item.layout) ? item.layout.filter((item, index, self) => self.indexOf(item) === index) : [item.layout]; const locals = new Locals(path, data); const layoutLength = layout.length; @@ -378,11 +373,9 @@ Hexo.prototype._generate = function(options = {}) { route.set(path, () => { if (options.cache && cache != null) return cache; - let view, name; - for (let i = 0; i < layoutLength; i++) { - name = layout[i]; - view = theme.getView(name); + const name = layout[i]; + const view = theme.getView(name); if (view) { log.debug('Rendering %s: %s', name, chalk.magenta(path)); diff --git a/lib/hexo/load_config.js b/lib/hexo/load_config.js index 629733b78a..6d9d3f890b 100644 --- a/lib/hexo/load_config.js +++ b/lib/hexo/load_config.js @@ -49,14 +49,7 @@ function findConfigPath(path) { const { dir, name } = parse(path); return fs.readdir(dir).then(files => { - let item; - - for (let i = 0, len = files.length; i < len; i++) { - item = files[i]; - - if (item.startsWith(name)) { - return join(dir, item); - } - } + const item = files.find(item => item.startsWith(name)); + if (item != null) return join(dir, item); }); } diff --git a/lib/hexo/locals.js b/lib/hexo/locals.js index 6c127d69fe..0ae994dc45 100644 --- a/lib/hexo/locals.js +++ b/lib/hexo/locals.js @@ -24,13 +24,7 @@ Locals.prototype.set = function(name, value) { if (typeof name !== 'string') throw new TypeError('name must be a string!'); if (value == null) throw new TypeError('value is required!'); - let getter; - - if (typeof value === 'function') { - getter = value; - } else { - getter = () => value; - } + const getter = typeof value === 'function' ? value : () => value; this.getters[name] = getter; this.cache[name] = null; @@ -56,12 +50,10 @@ Locals.prototype.invalidate = function() { Locals.prototype.toObject = function() { const result = {}; const keys = Object.keys(this.getters); - let key = ''; - let item; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - item = this.get(key); + const key = keys[i]; + const item = this.get(key); if (item != null) result[key] = item; } diff --git a/lib/hexo/multi_config_path.js b/lib/hexo/multi_config_path.js index 65bf1205c1..afc3b40b00 100644 --- a/lib/hexo/multi_config_path.js +++ b/lib/hexo/multi_config_path.js @@ -7,15 +7,14 @@ const yml = require('js-yaml'); module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const { log } = ctx; - const defaultPath = join(base, '_config.yml'); - let paths; if (!configPaths) { log.w('No config file entered.'); return join(base, '_config.yml'); } + let paths; // determine if comma or space separated if (configPaths.includes(',')) { paths = configPaths.replace(' ', '').split(','); @@ -37,13 +36,7 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const combinedConfig = {}; let count = 0; for (let i = 0; i < numPaths; i++) { - let configPath = ''; - - if (isAbsolute(paths[i])) { - configPath = paths[i]; - } else { - configPath = join(base, paths[i]); - } + const configPath = isAbsolute(paths[i]) ? paths[i] : join(base, paths[i]); if (!fs.existsSync(configPath)) { log.w(`Config file ${paths[i]} not found.`); diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 6549be0435..2db4c43a4e 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -119,14 +119,9 @@ Post.prototype._renderScaffold = function(data) { }).then(frontMatter => { const { separator } = yfmSplit; const jsonMode = separator[0] === ';'; - let obj; // Parse front-matter - if (jsonMode) { - obj = JSON.parse(`{${frontMatter}}`); - } else { - obj = yaml.load(frontMatter); - } + const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : yaml.load(frontMatter); // Add data which are not in the front-matter for (const key of Object.keys(data)) { diff --git a/lib/hexo/register_models.js b/lib/hexo/register_models.js index 54073da006..6245e122a1 100644 --- a/lib/hexo/register_models.js +++ b/lib/hexo/register_models.js @@ -6,10 +6,9 @@ module.exports = ctx => { const db = ctx.database; const keys = Object.keys(models); - let key = ''; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; db.model(key, models[key](ctx)); } }; diff --git a/lib/hexo/router.js b/lib/hexo/router.js index 091eeceba2..8339bf2d94 100644 --- a/lib/hexo/router.js +++ b/lib/hexo/router.js @@ -33,16 +33,7 @@ Router.format = Router.prototype.format = path => { Router.prototype.list = function() { const { routes } = this; - const keys = Object.keys(routes); - const arr = []; - let key; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - if (routes[key]) arr.push(key); - } - - return arr; + return Object.keys(routes).filter(key => routes[key]); }; Router.prototype.get = function(path) { diff --git a/lib/hexo/scaffold.js b/lib/hexo/scaffold.js index b7c37ff241..0d1428470e 100644 --- a/lib/hexo/scaffold.js +++ b/lib/hexo/scaffold.js @@ -35,14 +35,7 @@ Scaffold.prototype._listDir = function() { }; Scaffold.prototype._getScaffold = function(name) { - return this._listDir().then(list => { - let item; - - for (let i = 0, len = list.length; i < len; i++) { - item = list[i]; - if (item.name === name) return item; - } - }); + return this._listDir().then(list => list.find(item => item.name === name)); }; Scaffold.prototype.get = function(name, callback) { diff --git a/lib/models/cache.js b/lib/models/cache.js index e855f32a58..f4cef7d94d 100644 --- a/lib/models/cache.js +++ b/lib/models/cache.js @@ -13,7 +13,6 @@ module.exports = ctx => { Cache.static('compareFile', function(id, hashFn, statFn) { const cache = this.findById(id); const self = this; - let mtime; // If cache does not exist, then it must be a new file. We have to get both // file hash and stats. @@ -27,6 +26,8 @@ module.exports = ctx => { }); } + let mtime; + // Get file stats return statFn(id).then(stats => { mtime = stats.mtime; diff --git a/lib/models/category.js b/lib/models/category.js index 0de0e5800d..a025b5316a 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -10,17 +10,19 @@ module.exports = ctx => { }); Category.virtual('slug').get(function() { - const map = ctx.config.category_map || {}; let name = this.name; - let str = ''; if (!name) return; + let str = ''; + if (this.parent) { const parent = ctx.model('Category').findById(this.parent); str += `${parent.slug}/`; } + const map = ctx.config.category_map || {}; + name = map[name] || name; str += slugize(name, {transform: ctx.config.filename_case}); diff --git a/lib/plugins/console/config.js b/lib/plugins/console/config.js index 3561f14676..e0183b9c3b 100644 --- a/lib/plugins/console/config.js +++ b/lib/plugins/console/config.js @@ -30,15 +30,9 @@ function configConsole(args) { }).then(config => { if (!config) config = {}; - let result = ''; - setProperty(config, key, castValue(value)); - if (ext === '.json') { - result = JSON.stringify(config); - } else { - result = yaml.dump(config); - } + const result = ext === '.json' ? JSON.stringify(config) : yaml.dump(config); return fs.writeFile(configPath, result); }); @@ -58,11 +52,10 @@ function getProperty(obj, key) { function setProperty(obj, key, value) { const split = key.split('.'); let cursor = obj; - let name = ''; const lastKey = split.pop(); for (let i = 0, len = split.length; i < len; i++) { - name = split[i]; + const name = split[i]; cursor = cursor[name] = cursor[name] || {}; } diff --git a/lib/plugins/console/list/route.js b/lib/plugins/console/list/route.js index 912e008233..389dd59457 100644 --- a/lib/plugins/console/list/route.js +++ b/lib/plugins/console/list/route.js @@ -17,14 +17,14 @@ function listRoute() { function buildTree(routes) { const obj = {}; - let item, j, lenj, seg, cursor; + let cursor; for (let i = 0, len = routes.length; i < len; i++) { - item = routes[i].split('/'); + const item = routes[i].split('/'); cursor = obj; - for (j = 0, lenj = item.length; j < lenj; j++) { - seg = item[j]; + for (let j = 0, lenj = item.length; j < lenj; j++) { + const seg = item[j]; cursor = cursor[seg] = cursor[seg] || {}; } } @@ -35,11 +35,10 @@ function buildTree(routes) { function buildNodes(tree) { const keys = Object.keys(tree); const nodes = []; - let key, item; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - item = tree[key]; + const key = keys[i]; + const item = tree[key]; if (Object.keys(item).length) { nodes.push({ diff --git a/lib/plugins/console/new.js b/lib/plugins/console/new.js index daa6871267..bc33082fe3 100644 --- a/lib/plugins/console/new.js +++ b/lib/plugins/console/new.js @@ -31,11 +31,10 @@ function newConsole(args) { }; const keys = Object.keys(args); - let key = ''; const self = this; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (!reservedKeys[key]) data[key] = args[key]; } diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/plugins/filter/before_post_render/backtick_code_block.js index 68b20dd7d5..06dec6739a 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -34,13 +34,8 @@ function backtickCodeBlock(data) { } if (args) { - let match; + const match = rAllOptions.exec(args) || rLangCaption.exec(args); - if (rAllOptions.test(args)) { - match = args.match(rAllOptions); - } else if (rLangCaption.test(args)) { - match = args.match(rLangCaption); - } if (match) { options.lang = match[1]; diff --git a/lib/plugins/filter/new_post_path.js b/lib/plugins/filter/new_post_path.js index a0fe544e9f..4773c0cca1 100644 --- a/lib/plugins/filter/new_post_path.js +++ b/lib/plugins/filter/new_post_path.js @@ -25,12 +25,13 @@ function newPostPathFilter(data = {}, replace) { const newPostName = config.new_post_name; const permalinkDefaults = config.permalink_defaults; const { path, layout, slug } = data; - let target = ''; if (!permalink || permalink.rule !== newPostName) { permalink = new Permalink(newPostName); } + let target = ''; + if (path) { switch (layout) { case 'page': @@ -57,7 +58,6 @@ function newPostPathFilter(data = {}, replace) { default: { const date = moment(data.date || Date.now()); const keys = Object.keys(data); - let key = ''; const filenameData = { year: date.format('YYYY'), @@ -69,7 +69,7 @@ function newPostPathFilter(data = {}, replace) { }; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (!reservedKeys[key]) filenameData[key] = data[key]; } diff --git a/lib/plugins/filter/post_permalink.js b/lib/plugins/filter/post_permalink.js index 39980897ae..7a958f9a3c 100644 --- a/lib/plugins/filter/post_permalink.js +++ b/lib/plugins/filter/post_permalink.js @@ -32,10 +32,9 @@ function postPermalinkFilter(data) { } const keys = Object.keys(data); - let key = ''; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (meta.hasOwnProperty(key)) continue; // Use Object.getOwnPropertyDescriptor to copy getters to avoid "Maximum call diff --git a/lib/plugins/generator/asset.js b/lib/plugins/generator/asset.js index 1ff14359be..48cf3054fa 100644 --- a/lib/plugins/generator/asset.js +++ b/lib/plugins/generator/asset.js @@ -9,9 +9,8 @@ function assetGenerator(locals) { const self = this; function process(name) { - return Promise.filter(self.model(name).toArray(), asset => fs.exists(asset.source).then(exist => { - if (exist) return exist; - return asset.remove().thenReturn(exist); + return Promise.filter(self.model(name).toArray(), asset => fs.exists(asset.source).tap(exist => { + if (!exist) return asset.remove(); })).map(asset => { const { source } = asset; let { path } = asset; diff --git a/lib/plugins/helper/css.js b/lib/plugins/helper/css.js index 9383c4ed3f..0fcd0ab26c 100644 --- a/lib/plugins/helper/css.js +++ b/lib/plugins/helper/css.js @@ -1,22 +1,18 @@ 'use strict'; function cssHelper(...args) { - let result = ''; - - for (let i = 0, len = args.length; i < len; i++) { - let path = args[i]; - - if (i) result += '\n'; + return args.reduce((_result, path, i) => { + if (i) _result += '\n'; if (Array.isArray(path)) { - result += Reflect.apply(cssHelper, this, path); + _result += Reflect.apply(cssHelper, this, path); + } else { if (!path.includes('?') && !path.endsWith('.css')) path += '.css'; - result += ``; + _result += ``; } - } - - return result; + return _result; + }, ''); } module.exports = cssHelper; diff --git a/lib/plugins/helper/image_tag.js b/lib/plugins/helper/image_tag.js index b5b9bebb84..8fc8d92da9 100644 --- a/lib/plugins/helper/image_tag.js +++ b/lib/plugins/helper/image_tag.js @@ -3,17 +3,9 @@ const { htmlTag } = require('hexo-util'); function imageTagHelper(path, options = {}) { - const attrs = { + const attrs = Object.assign({ src: this.url_for(path) - }; - - const keys = Object.keys(options); - let key = ''; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - attrs[key] = options[key]; - } + }, options); if (attrs.class && Array.isArray(attrs.class)) { attrs.class = attrs.class.join(' '); diff --git a/lib/plugins/helper/link_to.js b/lib/plugins/helper/link_to.js index c0c52c1c61..ed9605c9f6 100644 --- a/lib/plugins/helper/link_to.js +++ b/lib/plugins/helper/link_to.js @@ -7,18 +7,10 @@ function linkToHelper(path, text, options = {}) { if (!text) text = path.replace(/^https?:\/\/|\/$/g, ''); - const attrs = { + const attrs = Object.assign({ href: this.url_for(path), title: text - }; - - const keys = Object.keys(options); - let key = ''; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - attrs[key] = options[key]; - } + }, options); if (attrs.external) { attrs.target = '_blank'; diff --git a/lib/plugins/helper/list_archives.js b/lib/plugins/helper/list_archives.js index 0be4a18282..5923ec871a 100644 --- a/lib/plugins/helper/list_archives.js +++ b/lib/plugins/helper/list_archives.js @@ -59,13 +59,11 @@ function listArchivesHelper(options = {}) { return self.url_for(url); } - let item, i, len; - if (style === 'list') { result += `'; } else { - for (i = 0, len = data.length; i < len; i++) { - item = data[i]; + for (let i = 0, len = data.length; i < len; i++) { + const item = data[i]; if (i) result += separator; diff --git a/lib/plugins/helper/list_categories.js b/lib/plugins/helper/list_categories.js index 458ee03787..ecf1114406 100644 --- a/lib/plugins/helper/list_categories.js +++ b/lib/plugins/helper/list_categories.js @@ -17,7 +17,6 @@ function listCategoriesHelper(categories, options) { const order = options.order || 1; const showCurrent = options.show_current || false; const childrenIndicator = options.hasOwnProperty('children_indicator') ? options.children_indicator : false; - let result = ''; const self = this; function prepareQuery(parent) { @@ -52,17 +51,10 @@ function listCategoriesHelper(categories, options) { } // special case: category page - if (!isCurrent && self.page.base) { - if (self.page.base.startsWith(cat.path)) { - isCurrent = true; - } - } + isCurrent = isCurrent || (self.page.base && self.page.base.startsWith(cat.path)); } - let additionalClassName = ''; - if (child && childrenIndicator) { - additionalClassName = ` ${childrenIndicator}`; - } + const additionalClassName = child && childrenIndicator ? ` ${childrenIndicator}` : ''; result += `
  • `; @@ -108,12 +100,10 @@ function listCategoriesHelper(categories, options) { } if (style === 'list') { - result += ``; - } else { - result += flatList(0); + return ``; } - return result; + return flatList(0); } module.exports = listCategoriesHelper; diff --git a/lib/plugins/helper/list_posts.js b/lib/plugins/helper/list_posts.js index b461ab9987..20cc21d48d 100644 --- a/lib/plugins/helper/list_posts.js +++ b/lib/plugins/helper/list_posts.js @@ -13,7 +13,6 @@ function listPostsHelper(posts, options) { const order = options.order || -1; const className = options.class || 'post'; const amount = options.amount || 6; - let result = ''; const self = this; // Sort the posts @@ -22,6 +21,8 @@ function listPostsHelper(posts, options) { // Limit the number of posts if (amount) posts = posts.limit(amount); + let result = ''; + if (style === 'list') { result += `