From c50bc51534d387d1a1eab46cabbb4465f62d1828 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Fri, 23 Feb 2018 10:09:27 +0100 Subject: [PATCH] Better variables scoping --- src/ol/format/GML2.js | 15 +++++------- src/ol/format/GMLBase.js | 5 ++-- src/ol/format/GPX.js | 5 ++-- src/ol/format/KML.js | 49 ++++++++++++++----------------------- src/ol/format/MVT.js | 3 +-- src/ol/format/Polyline.js | 24 ++++++------------ src/ol/format/TopoJSON.js | 31 +++++++++-------------- src/ol/format/WKT.js | 12 +++------ src/ol/format/XMLFeature.js | 9 +++---- 9 files changed, 56 insertions(+), 97 deletions(-) diff --git a/src/ol/format/GML2.js b/src/ol/format/GML2.js index 5f6355cc68..64ac17bde4 100644 --- a/src/ol/format/GML2.js +++ b/src/ol/format/GML2.js @@ -13,7 +13,7 @@ import {createElementNS, getAllTextContent, makeArrayPusher, makeChildAppender, makeReplacer, makeSimpleNodeFactory, OBJECT_PROPERTY_NODE_FACTORY, pushParseAndPop, pushSerializeAndPop} from '../xml.js'; - /** +/** * @const * @type {string} */ @@ -69,13 +69,12 @@ GML2.prototype.readFlatCoordinates_ = function(node, objectStack) { } } const coordsGroups = s.trim().split(/\s+/); - let x, y, z; const flatCoordinates = []; for (let i = 0, ii = coordsGroups.length; i < ii; i++) { const coords = coordsGroups[i].split(/,+/); - x = parseFloat(coords[0]); - y = parseFloat(coords[1]); - z = (coords.length === 3) ? parseFloat(coords[2]) : 0; + const x = parseFloat(coords[0]); + const y = parseFloat(coords[1]); + const z = (coords.length === 3) ? parseFloat(coords[2]) : 0; if (axisOrientation.substr(0, 2) === 'en') { flatCoordinates.push(x, y, z); } else { @@ -333,9 +332,8 @@ GML2.prototype.writeCoordinates_ = function(node, value, objectStack) { const points = value.getCoordinates(); const len = points.length; const parts = new Array(len); - let point; for (let i = 0; i < len; ++i) { - point = points[i]; + const point = points[i]; parts[i] = this.getCoords_(point, srsName, hasZ); } writeStringTextNode(node, parts.join(' ')); @@ -349,8 +347,7 @@ GML2.prototype.writeCoordinates_ = function(node, value, objectStack) { * @private */ GML2.prototype.writeCurveSegments_ = function(node, line, objectStack) { - const child = createElementNS(node.namespaceURI, - 'LineStringSegment'); + const child = createElementNS(node.namespaceURI, 'LineStringSegment'); node.appendChild(child); this.writeCurveOrLineString_(child, line, objectStack); }; diff --git a/src/ol/format/GMLBase.js b/src/ol/format/GMLBase.js index 852e1e8e43..1ebd849982 100644 --- a/src/ol/format/GMLBase.js +++ b/src/ol/format/GMLBase.js @@ -122,12 +122,11 @@ GMLBase.prototype.readFeaturesInternal = function(node, objectStack) { const context = objectStack[0]; let featureType = context['featureType']; let featureNS = context['featureNS']; - let i, ii; const prefix = 'p'; const defaultPrefix = 'p0'; if (!featureType && node.childNodes) { featureType = [], featureNS = {}; - for (i = 0, ii = node.childNodes.length; i < ii; ++i) { + for (let i = 0, ii = node.childNodes.length; i < ii; ++i) { const child = node.childNodes[i]; if (child.nodeType === 1) { const ft = child.nodeName.split(':').pop(); @@ -165,7 +164,7 @@ GMLBase.prototype.readFeaturesInternal = function(node, objectStack) { const featureTypes = Array.isArray(featureType) ? featureType : [featureType]; for (const p in featureNS) { const parsers = {}; - for (i = 0, ii = featureTypes.length; i < ii; ++i) { + for (let i = 0, ii = featureTypes.length; i < ii; ++i) { const featurePrefix = featureTypes[i].indexOf(':') === -1 ? defaultPrefix : featureTypes[i].split(':')[0]; if (featurePrefix === p) { diff --git a/src/ol/format/GPX.js b/src/ol/format/GPX.js index c5284c6f24..23e670a1a5 100644 --- a/src/ol/format/GPX.js +++ b/src/ol/format/GPX.js @@ -427,8 +427,7 @@ function applyLayoutOptions(layoutOptions, flatCoordinates, ends) { stride = 3; } if (stride !== 4) { - let i, ii; - for (i = 0, ii = flatCoordinates.length / 4; i < ii; i++) { + for (let i = 0, ii = flatCoordinates.length / 4; i < ii; i++) { flatCoordinates[i * stride] = flatCoordinates[i * 4]; flatCoordinates[i * stride + 1] = flatCoordinates[i * 4 + 1]; if (layoutOptions.hasZ) { @@ -440,7 +439,7 @@ function applyLayoutOptions(layoutOptions, flatCoordinates, ends) { } flatCoordinates.length = flatCoordinates.length / 4 * stride; if (ends) { - for (i = 0, ii = ends.length; i < ii; i++) { + for (let i = 0, ii = ends.length; i < ii; i++) { ends[i] = ends[i] / 4 * stride; } } diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index 213415bb74..052ae49ca6 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -928,9 +928,7 @@ function readGxTrack(node, objectStack) { } const flatCoordinates = gxTrackObject.flatCoordinates; const whens = gxTrackObject.whens; - let i, ii; - for (i = 0, ii = Math.min(flatCoordinates.length, whens.length); i < ii; - ++i) { + for (let i = 0, ii = Math.min(flatCoordinates.length, whens.length); i < ii; ++i) { flatCoordinates[4 * i + 3] = whens[i]; } const lineString = new LineString(null); @@ -1081,8 +1079,8 @@ function readMultiGeometry(node, objectStack) { let multiGeometry; let homogeneous = true; const type = geometries[0].getType(); - let geometry, i, ii; - for (i = 1, ii = geometries.length; i < ii; ++i) { + let geometry; + for (let i = 1, ii = geometries.length; i < ii; ++i) { geometry = geometries[i]; if (geometry.getType() != type) { homogeneous = false; @@ -1096,7 +1094,7 @@ function readMultiGeometry(node, objectStack) { const point = geometries[0]; layout = point.getLayout(); flatCoordinates = point.getFlatCoordinates(); - for (i = 1, ii = geometries.length; i < ii; ++i) { + for (let i = 1, ii = geometries.length; i < ii; ++i) { geometry = geometries[i]; extend(flatCoordinates, geometry.getFlatCoordinates()); } @@ -1171,13 +1169,11 @@ function readPolygon(node, objectStack) { const polygon = new Polygon(null); const flatCoordinates = flatLinearRings[0]; const ends = [flatCoordinates.length]; - let i, ii; - for (i = 1, ii = flatLinearRings.length; i < ii; ++i) { + for (let i = 1, ii = flatLinearRings.length; i < ii; ++i) { extend(flatCoordinates, flatLinearRings[i]); ends.push(flatCoordinates.length); } - polygon.setFlatCoordinates( - GeometryLayout.XYZ, flatCoordinates, ends); + polygon.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates, ends); polygon.setProperties(properties); return polygon; } else { @@ -1251,16 +1247,15 @@ function readStyle(node, objectStack) { * multiGeometry A multi-geometry. * @param {Array.} geometries List of geometries. */ -function setCommonGeometryProperties(multiGeometry, - geometries) { +function setCommonGeometryProperties(multiGeometry, geometries) { const ii = geometries.length; const extrudes = new Array(geometries.length); const tessellates = new Array(geometries.length); const altitudeModes = new Array(geometries.length); - let geometry, i, hasExtrude, hasTessellate, hasAltitudeMode; + let hasExtrude, hasTessellate, hasAltitudeMode; hasExtrude = hasTessellate = hasAltitudeMode = false; - for (i = 0; i < ii; ++i) { - geometry = geometries[i]; + for (let i = 0; i < ii; ++i) { + const geometry = geometries[i]; extrudes[i] = geometry.get('extrude'); tessellates[i] = geometry.get('tessellate'); altitudeModes[i] = geometry.get('altitudeMode'); @@ -1846,8 +1841,7 @@ KML.prototype.readFeaturesFromNode = function(node, opt_options) { } } else if (localName == 'kml') { features = []; - let n; - for (n = node.firstElementChild; n; n = n.nextElementSibling) { + for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const fs = this.readFeaturesFromNode(n, opt_options); if (fs) { extend(features, fs); @@ -1886,8 +1880,7 @@ KML.prototype.readName = function(source) { * @return {string|undefined} Name. */ KML.prototype.readNameFromDocument = function(doc) { - let n; - for (n = doc.firstChild; n; n = n.nextSibling) { + for (let n = doc.firstChild; n; n = n.nextSibling) { if (n.nodeType == Node.ELEMENT_NODE) { const name = this.readNameFromNode(n); if (name) { @@ -1904,14 +1897,13 @@ KML.prototype.readNameFromDocument = function(doc) { * @return {string|undefined} Name. */ KML.prototype.readNameFromNode = function(node) { - let n; - for (n = node.firstElementChild; n; n = n.nextElementSibling) { + for (let n = node.firstElementChild; n; n = n.nextElementSibling) { if (includes(NAMESPACE_URIS, n.namespaceURI) && n.localName == 'name') { return readString(n); } } - for (n = node.firstElementChild; n; n = n.nextElementSibling) { + for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const localName = n.localName; if (includes(NAMESPACE_URIS, n.namespaceURI) && (localName == 'Document' || @@ -2078,8 +2070,7 @@ function writeColorTextNode(node, color) { const rgba = asArray(color); const opacity = (rgba.length == 4) ? rgba[3] : 1; const abgr = [opacity * 255, rgba[2], rgba[1], rgba[0]]; - let i; - for (i = 0; i < 4; ++i) { + for (let i = 0; i < 4; ++i) { const hex = parseInt(abgr[i], 10).toString(16); abgr[i] = (hex.length == 1) ? '0' + hex : hex; } @@ -2109,17 +2100,16 @@ function writeCoordinatesTextNode(node, coordinates, objectStack) { assert(false, 34); // Invalid geometry layout } - let d, i; const ii = coordinates.length; let text = ''; if (ii > 0) { text += coordinates[0]; - for (d = 1; d < dimension; ++d) { + for (let d = 1; d < dimension; ++d) { text += ',' + coordinates[d]; } - for (i = stride; i < ii; i += stride) { + for (let i = stride; i < ii; i += stride) { text += ' ' + coordinates[i]; - for (d = 1; d < dimension; ++d) { + for (let d = 1; d < dimension; ++d) { text += ',' + coordinates[i + d]; } } @@ -2501,8 +2491,7 @@ const GEOMETRY_TYPE_TO_NODENAME = { * @param {string=} opt_nodeName Node name. * @return {Node|undefined} Node. */ -const GEOMETRY_NODE_FACTORY = function(value, objectStack, - opt_nodeName) { +const GEOMETRY_NODE_FACTORY = function(value, objectStack, opt_nodeName) { if (value) { const parentNode = objectStack[objectStack.length - 1].node; return createElementNS(parentNode.namespaceURI, diff --git a/src/ol/format/MVT.js b/src/ol/format/MVT.js index fa54a2dcff..9c9c82f9ae 100644 --- a/src/ol/format/MVT.js +++ b/src/ol/format/MVT.js @@ -372,12 +372,11 @@ MVT.prototype.readFeatures = function(source, opt_options) { const pbfLayers = pbf.readFields(layersPBFReader, {}); /** @type {Array.} */ const features = []; - let pbfLayer; for (const name in pbfLayers) { if (layers && layers.indexOf(name) == -1) { continue; } - pbfLayer = pbfLayers[name]; + const pbfLayer = pbfLayers[name]; for (let i = 0, ii = pbfLayer.length; i < ii; ++i) { const rawFeature = readRawFeature(pbf, pbfLayer, i); diff --git a/src/ol/format/Polyline.js b/src/ol/format/Polyline.js index 43d07a73fc..70f59650f0 100644 --- a/src/ol/format/Polyline.js +++ b/src/ol/format/Polyline.js @@ -74,8 +74,7 @@ export function encodeDeltas(numbers, stride, opt_factor) { lastNumbers[d] = 0; } - let i, ii; - for (i = 0, ii = numbers.length; i < ii;) { + for (let i = 0, ii = numbers.length; i < ii;) { for (d = 0; d < stride; ++d, ++i) { const num = numbers[i]; const delta = num - lastNumbers[d]; @@ -112,8 +111,7 @@ export function decodeDeltas(encoded, stride, opt_factor) { const numbers = decodeFloats(encoded, factor); - let i, ii; - for (i = 0, ii = numbers.length; i < ii;) { + for (let i = 0, ii = numbers.length; i < ii;) { for (d = 0; d < stride; ++d, ++i) { lastNumbers[d] += numbers[i]; @@ -139,8 +137,7 @@ export function decodeDeltas(encoded, stride, opt_factor) { */ export function encodeFloats(numbers, opt_factor) { const factor = opt_factor ? opt_factor : 1e5; - let i, ii; - for (i = 0, ii = numbers.length; i < ii; ++i) { + for (let i = 0, ii = numbers.length; i < ii; ++i) { numbers[i] = Math.round(numbers[i] * factor); } @@ -160,8 +157,7 @@ export function encodeFloats(numbers, opt_factor) { export function decodeFloats(encoded, opt_factor) { const factor = opt_factor ? opt_factor : 1e5; const numbers = decodeSignedIntegers(encoded); - let i, ii; - for (i = 0, ii = numbers.length; i < ii; ++i) { + for (let i = 0, ii = numbers.length; i < ii; ++i) { numbers[i] /= factor; } return numbers; @@ -177,8 +173,7 @@ export function decodeFloats(encoded, opt_factor) { * @return {string} The encoded string. */ export function encodeSignedIntegers(numbers) { - let i, ii; - for (i = 0, ii = numbers.length; i < ii; ++i) { + for (let i = 0, ii = numbers.length; i < ii; ++i) { const num = numbers[i]; numbers[i] = (num < 0) ? ~(num << 1) : (num << 1); } @@ -194,8 +189,7 @@ export function encodeSignedIntegers(numbers) { */ export function decodeSignedIntegers(encoded) { const numbers = decodeUnsignedIntegers(encoded); - let i, ii; - for (i = 0, ii = numbers.length; i < ii; ++i) { + for (let i = 0, ii = numbers.length; i < ii; ++i) { const num = numbers[i]; numbers[i] = (num & 1) ? ~(num >> 1) : (num >> 1); } @@ -211,8 +205,7 @@ export function decodeSignedIntegers(encoded) { */ export function encodeUnsignedIntegers(numbers) { let encoded = ''; - let i, ii; - for (i = 0, ii = numbers.length; i < ii; ++i) { + for (let i = 0, ii = numbers.length; i < ii; ++i) { encoded += encodeUnsignedInteger(numbers[i]); } return encoded; @@ -229,8 +222,7 @@ export function decodeUnsignedIntegers(encoded) { const numbers = []; let current = 0; let shift = 0; - let i, ii; - for (i = 0, ii = encoded.length; i < ii; ++i) { + for (let i = 0, ii = encoded.length; i < ii; ++i) { const b = encoded.charCodeAt(i) - 63; current |= (b & 0x1f) << shift; if (b < 0x20) { diff --git a/src/ol/format/TopoJSON.js b/src/ol/format/TopoJSON.js index 35a9cf83a7..53df71e8eb 100644 --- a/src/ol/format/TopoJSON.js +++ b/src/ol/format/TopoJSON.js @@ -78,9 +78,7 @@ function concatenateArcs(indices, arcs) { /** @type {Array.} */ const coordinates = []; let index, arc; - let i, ii; - let j, jj; - for (i = 0, ii = indices.length; i < ii; ++i) { + for (let i = 0, ii = indices.length; i < ii; ++i) { index = indices[i]; if (i > 0) { // splicing together arcs, discard last point @@ -96,7 +94,7 @@ function concatenateArcs(indices, arcs) { coordinates.push.apply(coordinates, arc); } // provide fresh copies of coordinate arrays - for (j = 0, jj = coordinates.length; j < jj; ++j) { + for (let j = 0, jj = coordinates.length; j < jj; ++j) { coordinates[j] = coordinates[j].slice(); } return coordinates; @@ -130,9 +128,8 @@ function readPointGeometry(object, scale, translate) { */ function readMultiPointGeometry(object, scale, translate) { const coordinates = object.coordinates; - let i, ii; if (scale && translate) { - for (i = 0, ii = coordinates.length; i < ii; ++i) { + for (let i = 0, ii = coordinates.length; i < ii; ++i) { transformVertex(coordinates[i], scale, translate); } } @@ -162,8 +159,7 @@ function readLineStringGeometry(object, arcs) { */ function readMultiLineStringGeometry(object, arcs) { const coordinates = []; - let i, ii; - for (i = 0, ii = object.arcs.length; i < ii; ++i) { + for (let i = 0, ii = object.arcs.length; i < ii; ++i) { coordinates[i] = concatenateArcs(object.arcs[i], arcs); } return new MultiLineString(coordinates); @@ -179,8 +175,7 @@ function readMultiLineStringGeometry(object, arcs) { */ function readPolygonGeometry(object, arcs) { const coordinates = []; - let i, ii; - for (i = 0, ii = object.arcs.length; i < ii; ++i) { + for (let i = 0, ii = object.arcs.length; i < ii; ++i) { coordinates[i] = concatenateArcs(object.arcs[i], arcs); } return new Polygon(coordinates); @@ -196,13 +191,11 @@ function readPolygonGeometry(object, arcs) { */ function readMultiPolygonGeometry(object, arcs) { const coordinates = []; - let polyArray, ringCoords, j, jj; - let i, ii; - for (i = 0, ii = object.arcs.length; i < ii; ++i) { + for (let i = 0, ii = object.arcs.length; i < ii; ++i) { // for each polygon - polyArray = object.arcs[i]; - ringCoords = []; - for (j = 0, jj = polyArray.length; j < jj; ++j) { + const polyArray = object.arcs[i]; + const ringCoords = []; + for (let j = 0, jj = polyArray.length; j < jj; ++j) { // for each ring ringCoords[j] = concatenateArcs(polyArray[j], arcs); } @@ -229,8 +222,7 @@ function readMultiPolygonGeometry(object, arcs) { function readFeaturesFromGeometryCollection(collection, arcs, scale, translate, property, name, opt_options) { const geometries = collection.geometries; const features = []; - let i, ii; - for (i = 0, ii = geometries.length; i < ii; ++i) { + for (let i = 0, ii = geometries.length; i < ii; ++i) { features[i] = readFeatureFromGeometry( geometries[i], arcs, scale, translate, property, name, opt_options); } @@ -294,8 +286,7 @@ TopoJSON.prototype.readFeatures; /** * @inheritDoc */ -TopoJSON.prototype.readFeaturesFromObject = function( - object, opt_options) { +TopoJSON.prototype.readFeaturesFromObject = function(object, opt_options) { if (object.type == 'Topology') { const topoJSONTopology = /** @type {TopoJSONTopology} */ (object); let transform, scale = null, translate = null; diff --git a/src/ol/format/WKT.js b/src/ol/format/WKT.js index 5da81da3e8..aff1f9948a 100644 --- a/src/ol/format/WKT.js +++ b/src/ol/format/WKT.js @@ -611,8 +611,7 @@ function encodeMultiLineStringGeometry(geom) { const array = []; const components = geom.getLineStrings(); for (let i = 0, ii = components.length; i < ii; ++i) { - array.push('(' + encodeLineStringGeometry( - components[i]) + ')'); + array.push('(' + encodeLineStringGeometry(components[i]) + ')'); } return array.join(','); } @@ -626,8 +625,7 @@ function encodePolygonGeometry(geom) { const array = []; const rings = geom.getLinearRings(); for (let i = 0, ii = rings.length; i < ii; ++i) { - array.push('(' + encodeLineStringGeometry( - rings[i]) + ')'); + array.push('(' + encodeLineStringGeometry(rings[i]) + ')'); } return array.join(','); } @@ -641,8 +639,7 @@ function encodeMultiPolygonGeometry(geom) { const array = []; const components = geom.getPolygons(); for (let i = 0, ii = components.length; i < ii; ++i) { - array.push('(' + encodePolygonGeometry( - components[i]) + ')'); + array.push('(' + encodePolygonGeometry(components[i]) + ')'); } return array.join(','); } @@ -768,9 +765,8 @@ WKT.prototype.readFeaturesFromText = function(text, opt_options) { geometries = [geometry]; } const features = []; - let feature; for (let i = 0, ii = geometries.length; i < ii; ++i) { - feature = new Feature(); + const feature = new Feature(); feature.setGeometry(geometries[i]); features.push(feature); } diff --git a/src/ol/format/XMLFeature.js b/src/ol/format/XMLFeature.js index 72accb915b..a35afd7504 100644 --- a/src/ol/format/XMLFeature.js +++ b/src/ol/format/XMLFeature.js @@ -44,8 +44,7 @@ XMLFeature.prototype.getType = function() { */ XMLFeature.prototype.readFeature = function(source, opt_options) { if (isDocument(source)) { - return this.readFeatureFromDocument( - /** @type {Document} */ (source), opt_options); + return this.readFeatureFromDocument(/** @type {Document} */ (source), opt_options); } else if (isNode(source)) { return this.readFeatureFromNode(/** @type {Node} */ (source), opt_options); } else if (typeof source === 'string') { @@ -107,12 +106,10 @@ XMLFeature.prototype.readFeatures = function(source, opt_options) { * @protected * @return {Array.} Features. */ -XMLFeature.prototype.readFeaturesFromDocument = function( - doc, opt_options) { +XMLFeature.prototype.readFeaturesFromDocument = function(doc, opt_options) { /** @type {Array.} */ const features = []; - let n; - for (n = doc.firstChild; n; n = n.nextSibling) { + for (let n = doc.firstChild; n; n = n.nextSibling) { if (n.nodeType == Node.ELEMENT_NODE) { extend(features, this.readFeaturesFromNode(n, opt_options)); }