mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-21 16:00:26 +01:00
refactor: var to const and let (#9885)
* refactor: var to const and let * fix: missed global bootbox usage * refactor: align with eslint expectations
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
(function (factory) {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
var winston = require('winston');
|
||||
const winston = require('winston');
|
||||
|
||||
module.exports = factory(require('xregexp'), winston);
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
};
|
||||
|
||||
process.elapsedTimeSince = function (start) {
|
||||
var diff = process.hrtime(start);
|
||||
const diff = process.hrtime(start);
|
||||
return (diff[0] * 1e3) + (diff[1] / 1e6);
|
||||
};
|
||||
} else {
|
||||
@@ -19,10 +19,10 @@
|
||||
}
|
||||
// eslint-disable-next-line
|
||||
}(function (XRegExp, console) {
|
||||
var freeze = Object.freeze || function (obj) { return obj; };
|
||||
const freeze = Object.freeze || function (obj) { return obj; };
|
||||
|
||||
// add default escape function for escaping HTML entities
|
||||
var escapeCharMap = freeze({
|
||||
const escapeCharMap = freeze({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
@@ -34,9 +34,9 @@
|
||||
function replaceChar(c) {
|
||||
return escapeCharMap[c];
|
||||
}
|
||||
var escapeChars = /[&<>"'`=]/g;
|
||||
const escapeChars = /[&<>"'`=]/g;
|
||||
|
||||
var HTMLEntities = freeze({
|
||||
const HTMLEntities = freeze({
|
||||
amp: '&',
|
||||
gt: '>',
|
||||
lt: '<',
|
||||
@@ -292,12 +292,12 @@
|
||||
'diams;': 9830,
|
||||
});
|
||||
|
||||
var utils = {
|
||||
const utils = {
|
||||
generateUUID: function () {
|
||||
/* eslint-disable no-bitwise */
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
var r = Math.random() * 16 | 0;
|
||||
var v = c === 'x' ? r : ((r & 0x3) | 0x8);
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : ((r & 0x3) | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
/* eslint-enable no-bitwise */
|
||||
@@ -312,8 +312,8 @@
|
||||
return String.fromCharCode(parseInt(hex, 16));
|
||||
})
|
||||
.replace(/&([^;\W]+;?)/g, function (m, e) {
|
||||
var ee = e.replace(/;$/, '');
|
||||
var target = HTMLEntities[e] || (e.match(/;$/) && HTMLEntities[ee]);
|
||||
const ee = e.replace(/;$/, '');
|
||||
const target = HTMLEntities[e] || (e.match(/;$/) && HTMLEntities[ee]);
|
||||
|
||||
if (typeof target === 'number') {
|
||||
return String.fromCharCode(target);
|
||||
@@ -326,7 +326,7 @@
|
||||
},
|
||||
// https://github.com/jprichardson/string.js/blob/master/lib/string.js
|
||||
stripHTMLTags: function (str, tags) {
|
||||
var pattern = (tags || ['']).join('|');
|
||||
const pattern = (tags || ['']).join('|');
|
||||
return String(str).replace(new RegExp('<(\\/)?(' + (pattern || '[^\\s>]+') + ')(\\s+[^<>]*?)?\\s*(\\/)?>', 'gi'), '');
|
||||
},
|
||||
|
||||
@@ -340,7 +340,7 @@
|
||||
tag = tag.replace(/\u202E/gi, '');
|
||||
tag = tag.replace(/[,/#!$%^*;:{}=_`<>'"~()?|]/g, '');
|
||||
tag = tag.substr(0, maxLength || 15).trim();
|
||||
var matches = tag.match(/^[.-]*(.+?)[.-]*$/);
|
||||
const matches = tag.match(/^[.-]*(.+?)[.-]*$/);
|
||||
if (matches && matches.length > 1) {
|
||||
tag = matches[1];
|
||||
}
|
||||
@@ -373,7 +373,7 @@
|
||||
return utils.languageKeyRegex.test(input);
|
||||
},
|
||||
userLangToTimeagoCode: function (userLang) {
|
||||
var mapping = {
|
||||
const mapping = {
|
||||
'en-GB': 'en',
|
||||
'en-US': 'en',
|
||||
'fa-IR': 'fa',
|
||||
@@ -384,13 +384,13 @@
|
||||
},
|
||||
// shallow objects merge
|
||||
merge: function () {
|
||||
var result = {};
|
||||
var obj;
|
||||
var keys;
|
||||
for (var i = 0; i < arguments.length; i += 1) {
|
||||
const result = {};
|
||||
let obj;
|
||||
let keys;
|
||||
for (let i = 0; i < arguments.length; i += 1) {
|
||||
obj = arguments[i] || {};
|
||||
keys = Object.keys(obj);
|
||||
for (var j = 0; j < keys.length; j += 1) {
|
||||
for (let j = 0; j < keys.length; j += 1) {
|
||||
result[keys[j]] = obj[keys[j]];
|
||||
}
|
||||
}
|
||||
@@ -441,11 +441,11 @@
|
||||
},
|
||||
|
||||
promiseParallel: function (obj) {
|
||||
var keys = Object.keys(obj);
|
||||
const keys = Object.keys(obj);
|
||||
return Promise.all(
|
||||
keys.map(function (k) { return obj[k]; })
|
||||
).then(function (results) {
|
||||
var data = {};
|
||||
const data = {};
|
||||
keys.forEach(function (k, i) {
|
||||
data[k] = results[i];
|
||||
});
|
||||
@@ -476,7 +476,7 @@
|
||||
},
|
||||
|
||||
makeNumberHumanReadable: function (num) {
|
||||
var n = parseInt(num, 10);
|
||||
const n = parseInt(num, 10);
|
||||
if (!n) {
|
||||
return num;
|
||||
}
|
||||
@@ -524,7 +524,7 @@
|
||||
'map', 'mark', 'menu', 'meta', 'meter', 'nav', 'noframes', 'noscript', 'object', 'ol', 'optgroup', 'option',
|
||||
'output', 'p', 'param', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select',
|
||||
'small', 'source', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot',
|
||||
'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr'],
|
||||
'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul', 'const', 'video', 'wbr'],
|
||||
|
||||
stripTags: ['abbr', 'acronym', 'address', 'applet', 'area', 'article', 'aside', 'audio', 'base', 'basefont',
|
||||
'bdi', 'bdo', 'big', 'blink', 'body', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup',
|
||||
@@ -534,7 +534,7 @@
|
||||
'map', 'mark', 'marquee', 'menu', 'meta', 'meter', 'nav', 'noframes', 'noscript', 'object', 'ol', 'optgroup', 'option',
|
||||
'output', 'param', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select',
|
||||
'source', 'span', 'strike', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot',
|
||||
'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr'],
|
||||
'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul', 'const', 'video', 'wbr'],
|
||||
|
||||
escapeRegexChars: function (text) {
|
||||
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
|
||||
@@ -553,7 +553,7 @@
|
||||
|
||||
isAndroidBrowser: function () {
|
||||
// http://stackoverflow.com/questions/9286355/how-to-detect-only-the-native-android-browser
|
||||
var nua = navigator.userAgent;
|
||||
const nua = navigator.userAgent;
|
||||
return ((nua.indexOf('Mozilla/5.0') > -1 && nua.indexOf('Android ') > -1 && nua.indexOf('AppleWebKit') > -1) && !(nua.indexOf('Chrome') > -1));
|
||||
},
|
||||
|
||||
@@ -563,13 +563,13 @@
|
||||
|
||||
findBootstrapEnvironment: function () {
|
||||
// http://stackoverflow.com/questions/14441456/how-to-detect-which-device-view-youre-on-using-twitter-bootstrap-api
|
||||
var envs = ['xs', 'sm', 'md', 'lg'];
|
||||
var $el = $('<div>');
|
||||
const envs = ['xs', 'sm', 'md', 'lg'];
|
||||
const $el = $('<div>');
|
||||
|
||||
$el.appendTo($('body'));
|
||||
|
||||
for (var i = envs.length - 1; i >= 0; i -= 1) {
|
||||
var env = envs[i];
|
||||
for (let i = envs.length - 1; i >= 0; i -= 1) {
|
||||
const env = envs[i];
|
||||
|
||||
$el.addClass('hidden-' + env);
|
||||
if ($el.is(':hidden')) {
|
||||
@@ -580,18 +580,18 @@
|
||||
},
|
||||
|
||||
isMobile: function () {
|
||||
var env = utils.findBootstrapEnvironment();
|
||||
const env = utils.findBootstrapEnvironment();
|
||||
return ['xs', 'sm'].some(function (targetEnv) {
|
||||
return targetEnv === env;
|
||||
});
|
||||
},
|
||||
|
||||
getHoursArray: function () {
|
||||
var currentHour = new Date().getHours();
|
||||
var labels = [];
|
||||
const currentHour = new Date().getHours();
|
||||
const labels = [];
|
||||
|
||||
for (var i = currentHour, ii = currentHour - 24; i > ii; i -= 1) {
|
||||
var hour = i < 0 ? 24 + i : i;
|
||||
for (let i = currentHour, ii = currentHour - 24; i > ii; i -= 1) {
|
||||
const hour = i < 0 ? 24 + i : i;
|
||||
labels.push(hour + ':00');
|
||||
}
|
||||
|
||||
@@ -599,12 +599,12 @@
|
||||
},
|
||||
|
||||
getDaysArray: function (from, amount) {
|
||||
var currentDay = new Date(parseInt(from, 10) || Date.now()).getTime();
|
||||
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
var labels = [];
|
||||
var tmpDate;
|
||||
const currentDay = new Date(parseInt(from, 10) || Date.now()).getTime();
|
||||
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
const labels = [];
|
||||
let tmpDate;
|
||||
|
||||
for (var x = (amount || 30) - 1; x >= 0; x -= 1) {
|
||||
for (let x = (amount || 30) - 1; x >= 0; x -= 1) {
|
||||
tmpDate = new Date(currentDay - (1000 * 60 * 60 * 24 * x));
|
||||
labels.push(months[tmpDate.getMonth()] + ' ' + tmpDate.getDate());
|
||||
}
|
||||
@@ -619,7 +619,7 @@
|
||||
el = el[0];
|
||||
}
|
||||
|
||||
var rect = el.getBoundingClientRect();
|
||||
const rect = el.getBoundingClientRect();
|
||||
|
||||
return (
|
||||
rect.top >= 0 &&
|
||||
@@ -631,26 +631,26 @@
|
||||
|
||||
// get all the url params in a single key/value hash
|
||||
params: function (options) {
|
||||
var hash = {};
|
||||
const hash = {};
|
||||
|
||||
options = options || {};
|
||||
options.skipToType = options.skipToType || {};
|
||||
|
||||
var searchStr = window.location.search;
|
||||
let searchStr = window.location.search;
|
||||
if (options.hasOwnProperty('url')) {
|
||||
if (options.url) {
|
||||
var a = utils.urlToLocation(options.url);
|
||||
const a = utils.urlToLocation(options.url);
|
||||
searchStr = a ? a.search : '';
|
||||
} else {
|
||||
searchStr = '';
|
||||
}
|
||||
}
|
||||
var params = searchStr.substring(1).split('&');
|
||||
const params = searchStr.substring(1).split('&');
|
||||
|
||||
params.forEach(function (param) {
|
||||
var val = param.split('=');
|
||||
var key = decodeURI(val[0]);
|
||||
var value = (
|
||||
const val = param.split('=');
|
||||
let key = decodeURI(val[0]);
|
||||
const value = (
|
||||
options.disableToType ||
|
||||
options.skipToType[key] ? decodeURI(val[1]) : utils.toType(decodeURI(val[1]))
|
||||
);
|
||||
@@ -677,7 +677,7 @@
|
||||
},
|
||||
|
||||
urlToLocation: function (url) {
|
||||
var a = document.createElement('a');
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
return a;
|
||||
},
|
||||
@@ -685,11 +685,11 @@
|
||||
// return boolean if string 'true' or string 'false', or if a parsable string which is a number
|
||||
// also supports JSON object and/or arrays parsing
|
||||
toType: function (str) {
|
||||
var type = typeof str;
|
||||
const type = typeof str;
|
||||
if (type !== 'string') {
|
||||
return str;
|
||||
}
|
||||
var nb = parseFloat(str);
|
||||
const nb = parseFloat(str);
|
||||
if (!isNaN(nb) && isFinite(str)) {
|
||||
return nb;
|
||||
}
|
||||
@@ -719,15 +719,15 @@
|
||||
if (props == null) {
|
||||
return undefined;
|
||||
}
|
||||
var i = props.indexOf('.');
|
||||
const i = props.indexOf('.');
|
||||
if (i === -1) {
|
||||
if (value !== undefined) {
|
||||
obj[props] = value;
|
||||
}
|
||||
return obj[props];
|
||||
}
|
||||
var prop = props.slice(0, i);
|
||||
var newProps = props.slice(i + 1);
|
||||
const prop = props.slice(0, i);
|
||||
const newProps = props.slice(i + 1);
|
||||
|
||||
if (props !== undefined && !(obj[prop] instanceof Object)) {
|
||||
obj[prop] = {};
|
||||
@@ -753,17 +753,17 @@
|
||||
|
||||
debounce: function (func, wait, immediate) {
|
||||
// modified from https://davidwalsh.name/javascript-debounce-function
|
||||
var timeout;
|
||||
let timeout;
|
||||
return function () {
|
||||
var context = this;
|
||||
var args = arguments;
|
||||
var later = function () {
|
||||
const context = this;
|
||||
const args = arguments;
|
||||
const later = function () {
|
||||
timeout = null;
|
||||
if (!immediate) {
|
||||
func.apply(context, args);
|
||||
}
|
||||
};
|
||||
var callNow = immediate && !timeout;
|
||||
const callNow = immediate && !timeout;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
if (callNow) {
|
||||
@@ -772,17 +772,17 @@
|
||||
};
|
||||
},
|
||||
throttle: function (func, wait, immediate) {
|
||||
var timeout;
|
||||
let timeout;
|
||||
return function () {
|
||||
var context = this;
|
||||
var args = arguments;
|
||||
var later = function () {
|
||||
const context = this;
|
||||
const args = arguments;
|
||||
const later = function () {
|
||||
timeout = null;
|
||||
if (!immediate) {
|
||||
func.apply(context, args);
|
||||
}
|
||||
};
|
||||
var callNow = immediate && !timeout;
|
||||
const callNow = immediate && !timeout;
|
||||
if (!timeout) {
|
||||
timeout = setTimeout(later, wait);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user