Skip to content

Commit

Permalink
Replace var with const/let, respectively
Browse files Browse the repository at this point in the history
This applies to the main code in
* `bin`
* `dust`
* `filters`
* `lib`

I'm leaving the tests for another day as those are less important.
  • Loading branch information
josebolos committed Jan 5, 2021
1 parent 4a2c409 commit 0ebb00c
Show file tree
Hide file tree
Showing 25 changed files with 303 additions and 301 deletions.
68 changes: 34 additions & 34 deletions bin/compile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env node
'use strict';

var fs = require('fs');
var path = require('path');
var async = require('async');
var glob = require('glob');
var mkdirp = require('mkdirp');
var uglify = require('uglify-js');
var yargs = require('yargs');

var argv = yargs
const fs = require('fs');
const path = require('path');
const async = require('async');
const glob = require('glob');
const mkdirp = require('mkdirp');
const uglify = require('uglify-js');
const yargs = require('yargs');

const argv = yargs
.options('x', {
alias: 'extra-js',
type: 'string'
Expand All @@ -26,15 +26,15 @@ var argv = yargs
})
.argv;

var config = require('../lib/config')('production', null, {});
const config = require('../lib/config')('production', null, {});

// All files named main.js will be minified, extra files to minify can be specified here
var EXTRA_MINIFY_PATHS = [];
const EXTRA_MINIFY_PATHS = [];
if (argv['extra-js']) {
EXTRA_MINIFY_PATHS = Array.isArray(argv['extra-js']) ? argv['extra-js'] : [argv['extra-js']];
}

var resourceModules;
let resourceModules;
if (argv['resource-module']) {
resourceModules = (
Array.isArray(argv['resource-module']) ?
Expand All @@ -44,10 +44,10 @@ if (argv['resource-module']) {
config.modules = resourceModules;
}

var renderer = require('../lib/renderer')(config);
const renderer = require('../lib/renderer')(config);

var environment = renderer.environment;
var manifest = renderer.manifest;
const environment = renderer.environment;
const manifest = renderer.manifest;

environment.cssCompressor = 'csswring';

Expand All @@ -57,22 +57,22 @@ process.on('uncaughtException', function (err) {
process.exit(128);
});

var compile = function (data, callback) {
var findAssets = function () {
var pattern = new RegExp('\.(' + [].slice.call(arguments, 0).join('|') + ')$');
const compile = function (data, callback) {
const findAssets = function () {
const pattern = new RegExp('\.(' + [].slice.call(arguments, 0).join('|') + ')$');
return Object.keys(data.assets).filter(function (key) {
return key.match(pattern);
});
};

var deleteAsset = function (name) {
const deleteAsset = function (name) {
delete manifest.assets[name];
return null;
};

var stylesheets = findAssets('css').map(function (name) {
var asset = environment.findAsset(name);
var content = asset ? asset.toString() : null;
const stylesheets = findAssets('css').map(function (name) {
const asset = environment.findAsset(name);
const content = asset ? asset.toString() : null;

if (!content) {
return deleteAsset(name);
Expand All @@ -86,20 +86,20 @@ var compile = function (data, callback) {
return stylesheet !== null;
});

var jsToMinify = ['main'].concat(EXTRA_MINIFY_PATHS);
const jsToMinify = ['main'].concat(EXTRA_MINIFY_PATHS);

var javascripts = findAssets('js').filter(function (name) {
for (var i = 0; jsToMinify[i]; ++i) {
const javascripts = findAssets('js').filter(function (name) {
for (let i = 0; jsToMinify[i]; ++i) {
if (name.indexOf(jsToMinify[i]) !== -1) {
return true;
}
}
return false;
}).map(function (name) {
var asset = environment.findAsset(name);
var content = asset ? asset.toString() : null;
var start;
var end;
const asset = environment.findAsset(name);
const content = asset ? asset.toString() : null;
let start;
let end;

if (!content) {
return deleteAsset(name);
Expand Down Expand Up @@ -128,15 +128,15 @@ var compile = function (data, callback) {
});
};

var generate = function (callback) {
const generate = function (callback) {
async.waterfall([
function (fn) {
mkdirp(config.path.publicResources, fn);
},
function (dir, fn) {
// Glob returns absolute path and we need to strip that out
var readGlobDir = function (p, cb) {
var pth = p.replace(/\\\?/g, '\/'); // Glob must use / as path separator even on windows
const readGlobDir = function (p, cb) {
const pth = p.replace(/\\\?/g, '\/'); // Glob must use / as path separator even on windows
glob(pth + '/**/*.*', function (er, files) {
if (er) {
return cb(er);
Expand All @@ -150,7 +150,7 @@ var generate = function (callback) {
async.concat(environment.paths, readGlobDir, fn);
},
function (files) {
var data = null;
let data = null;
try {
data = manifest.compile(files.filter(function (file) {
return /(?:\.([^.]+))?$/.exec(file) === 'scss';
Expand All @@ -163,7 +163,7 @@ var generate = function (callback) {
}
},
function (files) {
var data = null;
let data = null;
try {
data = manifest.compile(files.map(function (file) {
return file.replace(/\.ejs$/, '');
Expand Down
30 changes: 16 additions & 14 deletions bin/serve.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node
'use strict';

var path = require('path');
var jserve = require('jserve');
var query = require('qs-middleware');
var request = require('request');
var yargs = require('yargs');
const path = require('path');
const jserve = require('jserve');
const query = require('qs-middleware');
const request = require('request');
const yargs = require('yargs');

// Parse command-line arguments
var args = yargs
const args = yargs
.options('p', {
alias: 'port',
nargs: 1,
Expand Down Expand Up @@ -53,7 +53,7 @@ if (args.data && !/^[\/\~]/.test(args.data)) {

// Create a JServe application
// See https://github.com/rowanmanning/jserve
var app = jserve({
const app = jserve({
contentType: 'application/x-shunter+json',
log: {
debug: console.log.bind(console),
Expand Down Expand Up @@ -107,11 +107,11 @@ function serveRemoteJson(request, response, next) {
if (request.path !== '/remote') {
return next();
}
var options = {
const options = {
url: request.query.url,
headers: request.query.headers
};
var error;
let error;

if (!options.url || typeof options.url !== 'string') {
error = new Error('Invalid query parameter: url');
Expand Down Expand Up @@ -140,11 +140,11 @@ function serveRemoteJson(request, response, next) {

// Load remote JSON
function loadRemoteJson(options, done) {
var requestOptions = {
const requestOptions = {
url: options.url,
headers: options.headers
};
var error;
let error;

request(requestOptions, function (err, response, body) {
if (err) {
Expand All @@ -166,11 +166,13 @@ function loadRemoteJson(options, done) {

// Parse a HTTP header string
function parseHeaders(headerString) {
var headers = {};
var headersArray = headerString.split(/[\r\n]+/);
let headers = {};
const headersArray = headerString.split(/[\r\n]+/);

headersArray.forEach(function (headerString) {
var headerChunks = headerString.split(':');
const headerChunks = headerString.split(':');
headers[headerChunks.shift().trim()] = headerChunks.join(':').trim();
});

return headers;
}
16 changes: 8 additions & 8 deletions dust/and.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ function initHelper(dust) {
*/
dust.helpers.and = function (chunk, context, bodies, params) {
params = params || {};
var alternate = bodies.else;
var keys = context.resolve(params.keys);
var not = context.resolve(params.not);
const alternate = bodies.else;
const keys = context.resolve(params.keys);
const not = context.resolve(params.not);

var checkContext = function (arr) {
var count = 0;
var item;
var nestedKeys;
for (var i = 0; arr[i]; ++i) {
const checkContext = function (arr) {
let count = 0;
let item;
let nestedKeys;
for (let i = 0; arr[i]; ++i) {
nestedKeys = arr[i].split('.');
item = context.get(nestedKeys.shift());
// Handle finding nested properties like foo.bar
Expand Down
6 changes: 3 additions & 3 deletions dust/date-format.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var dateformat = require('dateformat');
const dateformat = require('dateformat');

module.exports = initHelper;

function initHelper(dust, renderer, config) {
dust.helpers.dateFormat = function (chunk, context, bodies, params) {
var date = null;
var value = null;
let date = null;
let value = null;

params = params || {};

Expand Down
2 changes: 1 addition & 1 deletion dust/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = initFilter;

function initFilter(dust) {
dust.filters.html = function (value) {
var escapes = {
const escapes = {
'<': '&#60;',
'>': '&#62;',
'"': '&#34;',
Expand Down
2 changes: 1 addition & 1 deletion dust/number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = initHelper;

function initHelper(dust) {
dust.helpers.numberFormat = function (chunk, context, bodies, params) {
var num = context.resolve(params.num);
let num = context.resolve(params.num);
if (num) {
return chunk.write(num.replace(/\B(?=(\d{3})+(?!\d))/g, ','));
}
Expand Down
16 changes: 8 additions & 8 deletions dust/or.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ function initHelper(dust) {
*/
dust.helpers.or = function (chunk, context, bodies, params) {
params = params || {};
var alternate = bodies.else;
var keys = context.resolve(params.keys);
var not = context.resolve(params.not);
const alternate = bodies.else;
const keys = context.resolve(params.keys);
const not = context.resolve(params.not);

var checkContext = function (arr) {
var count = 0;
var item;
var nestedKeys;
for (var i = 0; arr[i]; ++i) {
const checkContext = function (arr) {
let count = 0;
let item;
let nestedKeys;
for (let i = 0; arr[i]; ++i) {
nestedKeys = arr[i].split('.');
item = context.get(nestedKeys.shift());
// Handle finding nested properties like foo.bar
Expand Down
8 changes: 4 additions & 4 deletions filters/input/environment.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

var qs = require('qs');
const qs = require('qs');

module.exports = function (config, req, res, data, next) {
var cast = function (params) {
var output = {};
const cast = function (params) {
let output = {};

params = params || {};

var transform = function (value) {
const transform = function (value) {
var val = (typeof value === 'string') ? value.toLowerCase() : value;
if (val === 'true' || val === 'false') {
return val === 'true';
Expand Down
6 changes: 3 additions & 3 deletions lib/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

module.exports = function (config) {
var statsd = require('./statsd')(config);
const statsd = require('./statsd')(config);

return function (req, res, next) {
var timer = config.timer();
var end = res.end;
const timer = config.timer();
const end = res.end;

res.end = function () {
statsd.classifiedTiming(req.url, 'response_time', timer('Request completed ' + req.url));
Expand Down
Loading

0 comments on commit 0ebb00c

Please sign in to comment.