Skip to content

Commit

Permalink
refactor: prefer const to let
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Jan 18, 2019
1 parent 0aa0dbd commit 3d188a3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 84 deletions.
5 changes: 1 addition & 4 deletions lib/.eslintrc.js
Expand Up @@ -10,9 +10,6 @@ module.exports = {
sourceType: "module"
},
rules: {
indent: ["off"],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"]
"prefer-const": ["error"]
}
};
52 changes: 26 additions & 26 deletions lib/gpx.js
Expand Up @@ -8,11 +8,11 @@ function initializeArray(arr, size) {
}
//
function getLineStyle(extensions) {
let style = {};
const style = {};
if (extensions) {
let lineStyle = get1(extensions, "line");
const lineStyle = get1(extensions, "line");
if (lineStyle) {
let color = nodeVal(get1(lineStyle, "color")),
const color = nodeVal(get1(lineStyle, "color")),
opacity = parseFloat(nodeVal(get1(lineStyle, "opacity"))),
width = parseFloat(nodeVal(get1(lineStyle, "width")));
if (color) style.stroke = color;
Expand All @@ -25,7 +25,7 @@ function getLineStyle(extensions) {
}
// get the contents of multiple text nodes, if present
function getMulti(x, ys) {
let o = {};
const o = {};
let n;
let k;
for (k = 0; k < ys.length; k++) {
Expand All @@ -35,15 +35,15 @@ function getMulti(x, ys) {
return o;
}
function getProperties(node) {
let prop = getMulti(node, [
const prop = getMulti(node, [
"name",
"cmt",
"desc",
"type",
"time",
"keywords"
]);
let links = node.getElementsByTagName("link");
const links = node.getElementsByTagName("link");
if (links.length) prop.links = [];
for (let i = 0; i < links.length; i++) {
prop.links.push(
Expand All @@ -63,14 +63,14 @@ function get1(x, y) {
}

function coordPair(x) {
let ll = [
const ll = [
parseFloat(x.getAttribute("lon")),
parseFloat(x.getAttribute("lat"))
];
let ele = get1(x, "ele");
const ele = get1(x, "ele");
// handle namespaced attribute in browser
let heartRate = get1(x, "gpxtpx:hr") || get1(x, "hr");
let time = get1(x, "time");
const heartRate = get1(x, "gpxtpx:hr") || get1(x, "hr");
const time = get1(x, "time");
let e;
if (ele) {
e = parseFloat(nodeVal(ele));
Expand All @@ -85,7 +85,7 @@ function coordPair(x) {
};
}
function getRoute(node) {
let line = getPoints(node, "rtept");
const line = getPoints(node, "rtept");
if (!line.line) return;
return {
type: "Feature",
Expand All @@ -100,14 +100,14 @@ function getRoute(node) {
};
}
function getPoints(node, pointname) {
let pts = node.getElementsByTagName(pointname);
let line = [];
let times = [];
let heartRates = [];
let l = pts.length;
const pts = node.getElementsByTagName(pointname);
const line = [];
const times = [];
const heartRates = [];
const l = pts.length;
if (l < 2) return {}; // Invalid line in GeoJSON
for (let i = 0; i < l; i++) {
let c = coordPair(pts[i]);
const c = coordPair(pts[i]);
line.push(c.coordinates);
if (c.time) times.push(c.time);
if (c.heartRate || heartRates.length) {
Expand All @@ -122,10 +122,10 @@ function getPoints(node, pointname) {
};
}
function getTrack(node) {
let segments = node.getElementsByTagName("trkseg");
let track = [];
let times = [];
let heartRates = [];
const segments = node.getElementsByTagName("trkseg");
const track = [];
const times = [];
const heartRates = [];
let line;
for (let i = 0; i < segments.length; i++) {
line = getPoints(segments[i], "trkpt");
Expand All @@ -147,7 +147,7 @@ function getTrack(node) {
}
}
if (track.length === 0) return;
let properties = Object.assign(
const properties = Object.assign(
getProperties(node),
getLineStyle(get1(node, "extensions"))
);
Expand Down Expand Up @@ -178,11 +178,11 @@ function getPoint(node) {

export function gpx(doc) {
let i;
let tracks = doc.getElementsByTagName("trk");
let routes = doc.getElementsByTagName("rte");
let waypoints = doc.getElementsByTagName("wpt");
const tracks = doc.getElementsByTagName("trk");
const routes = doc.getElementsByTagName("rte");
const waypoints = doc.getElementsByTagName("wpt");
// a feature collection
let gj = {
const gj = {
type: "FeatureCollection",
features: []
};
Expand Down
108 changes: 54 additions & 54 deletions lib/kml.js
Expand Up @@ -80,9 +80,9 @@ function kmlColor(v) {
}

function gxCoords(root) {
let elems = root.getElementsByTagName("coord"),
coords = [],
times = [];
let elems = root.getElementsByTagName("coord");
const coords = [];
const times = [];
if (elems.length === 0) elems = root.getElementsByTagName("gx:coord");
for (let i = 0; i < elems.length; i++) {
coords.push(
Expand All @@ -91,7 +91,7 @@ function gxCoords(root) {
.map(parseFloat)
);
}
let timeElems = root.getElementsByTagName("when");
const timeElems = root.getElementsByTagName("when");
for (let j = 0; j < timeElems.length; j++) times.push(nodeVal(timeElems[j]));
return {
coords: coords,
Expand All @@ -100,13 +100,13 @@ function gxCoords(root) {
}

function getGeometry(root) {
let geomNode,
geomNodes,
i,
j,
k,
geoms = [],
coordTimes = [];
let geomNode;
let geomNodes;
let i;
let j;
let k;
const geoms = [];
const coordTimes = [];
if (get1(root, "MultiGeometry")) {
return getGeometry(get1(root, "MultiGeometry"));
}
Expand All @@ -132,7 +132,7 @@ function getGeometry(root) {
coordinates: coord(nodeVal(get1(geomNode, "coordinates")))
});
} else if (geotypes[i] === "Polygon") {
let rings = geomNode.getElementsByTagName("LinearRing"),
const rings = geomNode.getElementsByTagName("LinearRing"),
coords = [];
for (k = 0; k < rings.length; k++) {
coords.push(coord(nodeVal(get1(rings[k], "coordinates"))));
Expand All @@ -142,7 +142,7 @@ function getGeometry(root) {
coordinates: coords
});
} else if (geotypes[i] === "Track" || geotypes[i] === "gx:Track") {
let track = gxCoords(geomNode);
const track = gxCoords(geomNode);
geoms.push({
type: "LineString",
coordinates: track.coords
Expand All @@ -159,19 +159,19 @@ function getGeometry(root) {
}

function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
let geomsAndTimes = getGeometry(root),
i,
properties = {},
name = nodeVal(get1(root, "name")),
address = nodeVal(get1(root, "address")),
styleUrl = nodeVal(get1(root, "styleUrl")),
description = nodeVal(get1(root, "description")),
timeSpan = get1(root, "TimeSpan"),
timeStamp = get1(root, "TimeStamp"),
extendedData = get1(root, "ExtendedData"),
lineStyle = get1(root, "LineStyle"),
polyStyle = get1(root, "PolyStyle"),
visibility = get1(root, "visibility");
const geomsAndTimes = getGeometry(root);
let i;
const properties = {};
const name = nodeVal(get1(root, "name"));
const address = nodeVal(get1(root, "address"));
let styleUrl = nodeVal(get1(root, "styleUrl"));
const description = nodeVal(get1(root, "description"));
const timeSpan = get1(root, "TimeSpan");
const timeStamp = get1(root, "TimeStamp");
const extendedData = get1(root, "ExtendedData");
let lineStyle = get1(root, "LineStyle");
let polyStyle = get1(root, "PolyStyle");
const visibility = get1(root, "visibility");

if (!geomsAndTimes.geoms.length) return [];
if (name) properties.name = name;
Expand All @@ -190,31 +190,31 @@ function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
properties.styleHash = styleIndex[styleMapIndex[styleUrl].normal];
}
// Try to populate the lineStyle or polyStyle since we got the style hash
let style = styleByHash[properties.styleHash];
const style = styleByHash[properties.styleHash];
if (style) {
if (!lineStyle) lineStyle = get1(style, "LineStyle");
if (!polyStyle) polyStyle = get1(style, "PolyStyle");
let iconStyle = get1(style, "IconStyle");
const iconStyle = get1(style, "IconStyle");
if (iconStyle) {
let icon = get1(iconStyle, "Icon");
const icon = get1(iconStyle, "Icon");
if (icon) {
let href = nodeVal(get1(icon, "href"));
const href = nodeVal(get1(icon, "href"));
if (href) properties.icon = href;
}
}
}
}
if (description) properties.description = description;
if (timeSpan) {
let begin = nodeVal(get1(timeSpan, "begin"));
let end = nodeVal(get1(timeSpan, "end"));
const begin = nodeVal(get1(timeSpan, "begin"));
const end = nodeVal(get1(timeSpan, "end"));
properties.timespan = { begin: begin, end: end };
}
if (timeStamp) {
properties.timestamp = nodeVal(get1(timeStamp, "when"));
}
if (lineStyle) {
let linestyles = kmlColor(nodeVal(get1(lineStyle, "color"))),
const linestyles = kmlColor(nodeVal(get1(lineStyle, "color"))),
color = linestyles[0],
opacity = linestyles[1],
width = parseFloat(nodeVal(get1(lineStyle, "width")));
Expand All @@ -223,7 +223,7 @@ function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
if (!isNaN(width)) properties["stroke-width"] = width;
}
if (polyStyle) {
let polystyles = kmlColor(nodeVal(get1(polyStyle, "color"))),
const polystyles = kmlColor(nodeVal(get1(polyStyle, "color"))),
pcolor = polystyles[0],
popacity = polystyles[1],
fill = nodeVal(get1(polyStyle, "fill")),
Expand All @@ -238,7 +238,7 @@ function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
outline === "1" ? properties["stroke-opacity"] || 1 : 0;
}
if (extendedData) {
let datas = extendedData.getElementsByTagName("Data"),
const datas = extendedData.getElementsByTagName("Data"),
simpleDatas = extendedData.getElementsByTagName("SimpleData");

for (i = 0; i < datas.length; i++) {
Expand All @@ -259,7 +259,7 @@ function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
? geomsAndTimes.coordTimes[0]
: geomsAndTimes.coordTimes;
}
let feature = {
const feature = {
type: "Feature",
geometry:
geomsAndTimes.geoms.length === 1
Expand All @@ -275,33 +275,33 @@ function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
}

export function kml(doc) {
let gj = {
type: "FeatureCollection",
features: []
},
// styleindex keeps track of hashed styles in order to match features
styleIndex = {},
styleByHash = {},
// stylemapindex keeps track of style maps to expose in properties
styleMapIndex = {},
// atomic geospatial types supported by KML - MultiGeometry is
// handled separately
// all root placemarks in the file
placemarks = doc.getElementsByTagName("Placemark"),
styles = doc.getElementsByTagName("Style"),
styleMaps = doc.getElementsByTagName("StyleMap");
const gj = {
type: "FeatureCollection",
features: []
};
// styleindex keeps track of hashed styles in order to match featurej
const styleIndex = {};
const styleByHash = {};
// stylemapindex keeps track of style maps to expose in properties
const styleMapIndex = {};
// atomic geospatial types supported by KML - MultiGeometry is
// handled separately
// all root placemarks in the file
const placemarks = doc.getElementsByTagName("Placemark");
const styles = doc.getElementsByTagName("Style");
const styleMaps = doc.getElementsByTagName("StyleMap");

for (let k = 0; k < styles.length; k++) {
let hash = okhash(xml2str(styles[k])).toString(16);
const hash = okhash(xml2str(styles[k])).toString(16);
styleIndex["#" + styles[k].getAttribute("id")] = hash;
styleByHash[hash] = styles[k];
}
for (let l = 0; l < styleMaps.length; l++) {
styleIndex["#" + styleMaps[l].getAttribute("id")] = okhash(
xml2str(styleMaps[l])
).toString(16);
let pairs = styleMaps[l].getElementsByTagName("Pair");
let pairsMap = {};
const pairs = styleMaps[l].getElementsByTagName("Pair");
const pairsMap = {};
for (let m = 0; m < pairs.length; m++) {
pairsMap[nodeVal(get1(pairs[m], "key"))] = nodeVal(
get1(pairs[m], "styleUrl")
Expand Down

0 comments on commit 3d188a3

Please sign in to comment.