Update code to ES6

* var  ->  let / const
* [].forEach  ->  for (... of ...)
* '...' + var  ->  template strings  `...${var}`
* function  ->  arrow functions `=>`
* use === and !==  instead of == and !=
This commit is contained in:
Yuri Astrakhan
2019-12-21 14:09:20 -05:00
parent 736e8d393a
commit 7f8be27844
7 changed files with 701 additions and 733 deletions
+46 -46
View File
@@ -1,42 +1,42 @@
'use strict';
var path = require('path'),
fs = require('fs');
const path = require('path');
const fs = require('fs');
var clone = require('clone'),
express = require('express');
const clone = require('clone');
const express = require('express');
var utils = require('./utils');
const utils = require('./utils');
module.exports = function(options, repo, params, id, publicUrl, reportTiles, reportFont) {
var app = express().disable('x-powered-by');
module.exports = (options, repo, params, id, publicUrl, reportTiles, reportFont) => {
const app = express().disable('x-powered-by');
var styleFile = path.resolve(options.paths.styles, params.style);
const styleFile = path.resolve(options.paths.styles, params.style);
var styleJSON = clone(require(styleFile));
Object.keys(styleJSON.sources).forEach(function(name) {
var source = styleJSON.sources[name];
var url = source.url;
const styleJSON = clone(require(styleFile));
for (const name of Object.keys(styleJSON.sources)) {
const source = styleJSON.sources[name];
const url = source.url;
if (url && url.lastIndexOf('mbtiles:', 0) === 0) {
var mbtilesFile = url.substring('mbtiles://'.length);
var fromData = mbtilesFile[0] == '{' &&
mbtilesFile[mbtilesFile.length - 1] == '}';
let mbtilesFile = url.substring('mbtiles://'.length);
const fromData = mbtilesFile[0] === '{' &&
mbtilesFile[mbtilesFile.length - 1] === '}';
if (fromData) {
mbtilesFile = mbtilesFile.substr(1, mbtilesFile.length - 2);
var mapsTo = (params.mapping || {})[mbtilesFile];
const mapsTo = (params.mapping || {})[mbtilesFile];
if (mapsTo) {
mbtilesFile = mapsTo;
}
}
var identifier = reportTiles(mbtilesFile, fromData);
source.url = 'local://data/' + identifier + '.json';
const identifier = reportTiles(mbtilesFile, fromData);
source.url = `local://data/${identifier}.json`;
}
});
}
styleJSON.layers.forEach(function(obj) {
if (obj['type'] == 'symbol') {
var fonts = (obj['layout'] || {})['text-font'];
for(let obj of styleJSON.layers) {
if (obj['type'] === 'symbol') {
const fonts = (obj['layout'] || {})['text-font'];
if (fonts && fonts.length) {
fonts.forEach(reportFont);
} else {
@@ -44,18 +44,18 @@ module.exports = function(options, repo, params, id, publicUrl, reportTiles, rep
reportFont('Arial Unicode MS Regular');
}
}
});
}
var spritePath;
let spritePath;
var httpTester = /^(http(s)?:)?\/\//;
const httpTester = /^(http(s)?:)?\/\//;
if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) {
spritePath = path.join(options.paths.sprites,
styleJSON.sprite
.replace('{style}', path.basename(styleFile, '.json'))
.replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleFile)))
);
styleJSON.sprite = 'local://styles/' + id + '/sprite';
styleJSON.sprite = `local://styles/${id}/sprite`;
}
if (styleJSON.glyphs && !httpTester.test(styleJSON.glyphs)) {
styleJSON.glyphs = 'local://fonts/{fontstack}/{range}.pbf';
@@ -63,31 +63,31 @@ module.exports = function(options, repo, params, id, publicUrl, reportTiles, rep
repo[id] = styleJSON;
app.get('/' + id + '/style.json', function(req, res, next) {
var fixUrl = function(url, opt_nokey, opt_nostyle) {
app.get(`/${id}/style.json`, (req, res, next) => {
const fixUrl = (url, opt_nokey, opt_nostyle) => {
if (!url || (typeof url !== 'string') || url.indexOf('local://') !== 0) {
return url;
}
var queryParams = [];
const queryParams = [];
if (!opt_nostyle && global.addStyleParam) {
queryParams.push('style=' + id);
queryParams.push(`style=${id}`);
}
if (!opt_nokey && req.query.key) {
queryParams.unshift('key=' + req.query.key);
queryParams.unshift(`key=${req.query.key}`);
}
var query = '';
let query = '';
if (queryParams.length) {
query = '?' + queryParams.join('&');
query = `?${queryParams.join('&')}`;
}
return url.replace(
'local://', utils.getPublicUrl(publicUrl, req)) + query;
'local://', utils.getPublicUrl(publicUrl, req)) + query;
};
var styleJSON_ = clone(styleJSON);
Object.keys(styleJSON_.sources).forEach(function(name) {
var source = styleJSON_.sources[name];
const styleJSON_ = clone(styleJSON);
for (const name of Object.keys(styleJSON_.sources)) {
const source = styleJSON_.sources[name];
source.url = fixUrl(source.url);
});
}
// mapbox-gl-js viewer cannot handle sprite urls with query
if (styleJSON_.sprite) {
styleJSON_.sprite = fixUrl(styleJSON_.sprite, true, true);
@@ -98,21 +98,21 @@ module.exports = function(options, repo, params, id, publicUrl, reportTiles, rep
return res.send(styleJSON_);
});
app.get('/' + id + '/sprite:scale(@[23]x)?\.:format([\\w]+)',
function(req, res, next) {
app.get(`/${id}/sprite:scale(@[23]x)?.:format([\\w]+)`,
(req, res, next) => {
if (!spritePath) {
return res.status(404).send('File not found');
}
var scale = req.params.scale,
format = req.params.format;
var filename = spritePath + (scale || '') + '.' + format;
return fs.readFile(filename, function(err, data) {
const scale = req.params.scale,
format = req.params.format;
const filename = `${spritePath + (scale || '')}.${format}`;
return fs.readFile(filename, (err, data) => {
if (err) {
console.log('Sprite load error:', filename);
return res.status(404).send('File not found');
} else {
if (format == 'json') res.header('Content-type', 'application/json');
if (format == 'png') res.header('Content-type', 'image/png');
if (format === 'json') res.header('Content-type', 'application/json');
if (format === 'png') res.header('Content-type', 'image/png');
return res.send(data);
}
});