Permalink
Browse files

Merge pull request #3124 from segayuu/substring-to-startswith-or-ends…

…with

Replace String#substring() with String#startsWith() or String#endsWith()
  • Loading branch information...
JLHwung committed May 5, 2018
2 parents e5f16e2 + 654741f commit 0b26940f7e0a35f6937b944012468bbf0d844010
@@ -23,7 +23,7 @@ function Box(ctx, base, options) {
persistent: true
}, options);
if (base.substring(base.length - 1) !== sep) {
if (!base.endsWith(sep)) {
base += sep;
}
@@ -158,11 +158,11 @@ Box.prototype.process = function(callback) {
// Check existing files in cache
const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));
const cacheFiles = Cache.filter(item => item._id.substring(0, relativeBase.length) === relativeBase).map(item => item._id.substring(relativeBase.length));
const cacheFiles = Cache.filter(item => item._id.startsWith(relativeBase)).map(item => item._id.substring(relativeBase.length));
// Read files from directory
return self._readDir(base, file => self._processFile(file.type, file.path)).map(file => file.path).then(files => // Handle deleted files
Promise.filter(cacheFiles, path => !~files.indexOf(path)).map(path => self._processFile(File.TYPE_DELETE, path)));
Promise.filter(cacheFiles, path => !files.includes(path)).map(path => self._processFile(File.TYPE_DELETE, path)));
}).catch(err => {
if (err.cause && err.cause.code !== 'ENOENT') throw err;
}).asCallback(callback);
@@ -55,21 +55,12 @@ Tag.prototype.render = function(str, options, callback) {
const cache = [];
function escapeContent(str) {
return `<!--${placeholder}${cache.push(str) - 1}-->`;
}
const escapeContent = str => `<!--${placeholder}${cache.push(str) - 1}-->`;
const env = this.env;
str = str.replace(/<pre><code.*>[\s\S]*?<\/code><\/pre>/gm, escapeContent);
return new Promise((resolve, reject) => {
str = str.replace(/<pre><code.*>[\s\S]*?<\/code><\/pre>/gm, escapeContent);
env.renderString(str, options, (err, result) => {
if (err) return reject(err);
resolve(result.replace(rPlaceholder, function(...args) {
return cache[args[1]];
}));
});
});
return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); })
.then(result => result.replace(rPlaceholder, (_, index) => cache[index]));
};
function NunjucksTag(name, fn) {
@@ -53,12 +53,12 @@ function findConfigPath(path) {
const basename = pathFn.basename(path, extname);
return fs.readdir(dirname).then(files => {
let item = '';
let item;
for (let i = 0, len = files.length; i < len; i++) {
item = files[i];
if (item.substring(0, basename.length) === basename) {
if (item.startsWith(basename)) {
return pathFn.join(dirname, item);
}
}
@@ -17,9 +17,8 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) {
}
// determine if comma or space separated
if (configPaths.indexOf(',') > -1) {
if (configPaths.includes(',')) {
paths = configPaths.replace(' ', '').split(',');
} else {
// only one config
let configPath = pathFn.isAbsolute(configPaths) ? configPaths : pathFn.resolve(base, configPaths);
@@ -123,14 +123,14 @@ function generateConsole(args = {}) {
throw err;
}).then(() => {
const routeList = route.list();
const publicFiles = Cache.filter(item => item._id.substring(0, 7) === 'public/').map(item => item._id.substring(7));
const publicFiles = Cache.filter(item => item._id.startsWith('public/')).map(item => item._id.substring(7));
return Promise.all([
// Generate files
Promise.map(routeList, generateFile),
// Clean files
Promise.filter(publicFiles, path => !~routeList.indexOf(path)).map(deleteFile)
Promise.filter(publicFiles, path => !routeList.includes(path)).map(deleteFile)
]);
}).spread(result => {
const interval = prettyHrtime(process.hrtime(start));
@@ -17,7 +17,7 @@ function i18nLocalsFilter(locals) {
const pattern = new Pattern(`${i18nDir}/*path`);
const data = pattern.match(locals.path);
if (data && data.lang && ~i18nLanguages.indexOf(data.lang)) {
if (data && data.lang && i18nLanguages.includes(data.lang)) {
lang = data.lang;
page.canonical_path = data.path;
} else {
@@ -12,7 +12,7 @@ function cssHelper(...args) {
if (Array.isArray(path)) {
result += cssHelper.apply(this, path);
} else {
if (path.indexOf('?') < 0 && path.substring(path.length - 4, path.length) !== '.css') path += '.css';
if (!path.includes('?') && !path.endsWith('.css')) path += '.css';
result += `<link rel="stylesheet" href="${this.url_for(path)}">`;
}
}
@@ -1,13 +1,11 @@
'use strict';
function isCurrentHelper(path = '/', strict) {
const currentPath = this.path.replace(/^[^\/].*/, _ => // eslint-disable-line no-useless-escape
`/${_}`);
const currentPath = this.path.replace(/^[^/].*/, '/$&');
if (strict) {
if (path[path.length - 1] === '/') path += 'index.html';
path = path.replace(/^[^\/].*/, _ => // eslint-disable-line no-useless-escape
`/${_}`);
path = path.replace(/^[^/].*/, '/$&');
return currentPath === path;
}
@@ -16,10 +14,9 @@ function isCurrentHelper(path = '/', strict) {
if (path === '/') return currentPath === '/index.html';
path = path.replace(/^[^\/].*/, _ => // eslint-disable-line no-useless-escape
`/${_}`);
path = path.replace(/^[^/].*/, '/$&');
return currentPath.substring(0, path.length) === path;
return currentPath.startsWith(path);
}
function isHomeHelper() {
@@ -12,7 +12,7 @@ function jsHelper(...args) {
if (Array.isArray(path)) {
result += jsHelper.apply(this, path);
} else {
if (path.indexOf('?') < 0 && path.substring(path.length - 3, path.length) !== '.js') path += '.js';
if (!path.includes('?') && !path.endsWith('.js')) path += '.js';
result += `<script src="${this.url_for(path)}"></script>`;
}
}
@@ -56,7 +56,7 @@ function listCategoriesHelper(categories, options) {
// special case: category page
if (!isCurrent && self.page.base) {
if (self.page.base.indexOf(cat.path) === 0) {
if (self.page.base.startsWith(cat.path)) {
isCurrent = true;
}
}
@@ -204,7 +204,7 @@ function tagcloudHelper(tags, options) {
tags.sort('length').forEach(tag => {
const length = tag.length;
if (~sizes.indexOf(length)) return;
if (sizes.includes(length)) return;
sizes.push(length);
});
@@ -4,7 +4,7 @@ const url = require('url');
const _ = require('lodash');
function urlForHelper(path = '/', options) {
if (path[0] === '#' || path.substring(0, 2) === '//') {
if (path[0] === '#' || path.startsWith('//')) {
return path;
}
@@ -17,7 +17,7 @@ module.exports = ctx => {
const config = ctx.config;
function makeUrl(path) {
if (path[0] === '#' || path.substring(0, 2) === '//') {
if (path[0] === '#' || path.startsWith('//')) {
return path;
}
@@ -29,7 +29,7 @@ exports.pattern = new Pattern(path => {
if (!_.startsWith(path, 'source/')) return false;
path = path.substring(7);
if (common.isHiddenFile(path) || common.isTmpFile(path) || ~path.indexOf('node_modules')) return false;
if (common.isHiddenFile(path) || common.isTmpFile(path) || path.includes('node_modules')) return false;
return {path};
});

0 comments on commit 0b26940

Please sign in to comment.