From cfac28bc9f75fc85edbdb8880ecaabd8c480b51b Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 23 Jan 2024 23:58:28 +0800 Subject: [PATCH] continue linting fixes [#112] (#125) * continue linting fixes [#112] --- src/attribute.ts | 67 ++++++++++++++++------------------- src/compat/json_style.ts | 72 +++++++++++++++++++++++--------------- src/default_style/style.ts | 37 +++++++++----------- src/extra_styles/toner.ts | 14 ++++---- src/frontends/leaflet.ts | 42 +++++++++------------- src/frontends/static.ts | 6 ++-- src/labeler.ts | 41 ++++++++++------------ src/line.ts | 34 ++++++++---------- src/painter.ts | 10 +++--- src/symbolizer.ts | 8 +++-- src/task.ts | 6 ++-- src/text.ts | 2 +- src/tilecache.ts | 45 ++++++++++++++---------- src/view.ts | 3 +- 14 files changed, 192 insertions(+), 195 deletions(-) diff --git a/src/attribute.ts b/src/attribute.ts index e846e4da..2214855c 100644 --- a/src/attribute.ts +++ b/src/attribute.ts @@ -14,9 +14,8 @@ export class StringAttr { public get(z: number, f?: Feature): T { if (typeof this.str === "function") { return this.str(z, f); - } else { - return this.str; } + return this.str; } } @@ -33,9 +32,8 @@ export class NumberAttr { public get(z: number, f?: Feature): number { if (typeof this.value === "function") { return this.value(z, f); - } else { - return this.value; } + return this.value; } } @@ -120,44 +118,42 @@ export class FontAttr { if (this.font) { if (typeof this.font === "function") { return this.font(z, f); - } else { - return this.font; - } - } else { - let style = ""; - if (this.style) { - if (typeof this.style === "function") { - style = this.style(z, f) + " "; - } else { - style = this.style + " "; - } } - - let weight = ""; - if (this.weight) { - if (typeof this.weight === "function") { - weight = this.weight(z, f) + " "; - } else { - weight = this.weight + " "; - } - } - - let size; - if (typeof this.size === "function") { - size = this.size(z, f); + return this.font; + } + let style = ""; + if (this.style) { + if (typeof this.style === "function") { + style = `${this.style(z, f)} `; } else { - size = this.size; + style = `${this.style} `; } + } - let family; - if (typeof this.family === "function") { - family = this.family(z, f); + let weight = ""; + if (this.weight) { + if (typeof this.weight === "function") { + weight = `${this.weight(z, f)} `; } else { - family = this.family; + weight = `${this.weight} `; } + } + + let size; + if (typeof this.size === "function") { + size = this.size(z, f); + } else { + size = this.size; + } - return `${style}${weight}${size}px ${family}`; + let family; + if (typeof this.family === "function") { + family = this.family(z, f); + } else { + family = this.family; } + + return `${style}${weight}${size}px ${family}`; } } @@ -174,8 +170,7 @@ export class ArrayAttr { public get(z: number, f?: Feature): T[] { if (typeof this.value === "function") { return this.value(z, f); - } else { - return this.value; } + return this.value; } } diff --git a/src/compat/json_style.ts b/src/compat/json_style.ts index ffb62ad9..4c8a3513 100644 --- a/src/compat/json_style.ts +++ b/src/compat/json_style.ts @@ -17,45 +17,57 @@ export function filterFn(arr: any[]): Filter { // hack around "$type" if (arr.includes("$type")) { return (z) => true; - } else if (arr[0] === "==") { + } + if (arr[0] === "==") { return (z, f) => f.props[arr[1]] === arr[2]; - } else if (arr[0] === "!=") { + } + if (arr[0] === "!=") { return (z, f) => f.props[arr[1]] !== arr[2]; - } else if (arr[0] === "!") { + } + if (arr[0] === "!") { const sub = filterFn(arr[1]); return (z, f) => !sub(z, f); - } else if (arr[0] === "<") { + } + if (arr[0] === "<") { return (z, f) => number(f.props[arr[1]], Infinity) < arr[2]; - } else if (arr[0] === "<=") { + } + if (arr[0] === "<=") { return (z, f) => number(f.props[arr[1]], Infinity) <= arr[2]; - } else if (arr[0] === ">") { + } + if (arr[0] === ">") { return (z, f) => number(f.props[arr[1]], -Infinity) > arr[2]; - } else if (arr[0] === ">=") { + } + if (arr[0] === ">=") { return (z, f) => number(f.props[arr[1]], -Infinity) >= arr[2]; - } else if (arr[0] === "in") { + } + if (arr[0] === "in") { return (z, f) => arr.slice(2, arr.length).includes(f.props[arr[1]]); - } else if (arr[0] === "!in") { + } + if (arr[0] === "!in") { return (z, f) => !arr.slice(2, arr.length).includes(f.props[arr[1]]); - } else if (arr[0] === "has") { + } + if (arr[0] === "has") { return (z, f) => f.props.hasOwnProperty(arr[1]); - } else if (arr[0] === "!has") { + } + if (arr[0] === "!has") { return (z, f) => !f.props.hasOwnProperty(arr[1]); - } else if (arr[0] === "all") { + } + if (arr[0] === "all") { const parts = arr.slice(1, arr.length).map((e) => filterFn(e)); return (z, f) => parts.every((p) => { return p(z, f); }); - } else if (arr[0] === "any") { + } + if (arr[0] === "any") { const parts = arr.slice(1, arr.length).map((e) => filterFn(e)); return (z, f) => parts.some((p) => { return p(z, f); }); - } else { - console.log("Unimplemented filter: ", arr[0]); - return (f) => false; } + console.log("Unimplemented filter: ", arr[0]); + return (f) => false; } export function numberFn(obj: any): (z: number, f?: Feature) => number { @@ -63,7 +75,8 @@ export function numberFn(obj: any): (z: number, f?: Feature) => number { return (z: number) => { return exp(obj.base, obj.stops)(z - 1); }; - } else if ( + } + if ( obj[0] === "interpolate" && obj[1][0] === "exponential" && obj[2][0] === "zoom" @@ -76,7 +89,8 @@ export function numberFn(obj: any): (z: number, f?: Feature) => number { return (z: number) => { return exp(obj[1][1], stops)(z - 1); }; - } else if (obj[0] === "step" && obj[1][0] === "get") { + } + if (obj[0] === "step" && obj[1][0] === "get") { const slice = obj.slice(2); const prop = obj[1][1]; return (z: number, f?: Feature) => { @@ -89,10 +103,9 @@ export function numberFn(obj: any): (z: number, f?: Feature) => number { } return slice[slice.length - 1]; }; - } else { - console.log("Unimplemented numeric fn: ", obj); - return (z) => 1; } + console.log("Unimplemented numeric fn: ", obj); + return (z) => 1; } export function numberOrFn( @@ -138,16 +151,17 @@ export function getFont(obj: any, fontsubmap: any) { // for fallbacks, use the weight and style of the first font let weight = ""; if (fontfaces.length && fontfaces[0].weight) - weight = fontfaces[0].weight + " "; + weight = `${fontfaces[0].weight} `; let style = ""; - if (fontfaces.length && fontfaces[0].style) style = fontfaces[0].style + " "; + if (fontfaces.length && fontfaces[0].style) style = `${fontfaces[0].style} `; if (typeof text_size === "number") { return (z: number) => `${style}${weight}${text_size}px ${fontfaces .map((f) => f.face) .join(", ")}`; - } else if (text_size.stops) { + } + if (text_size.stops) { let base = 1.4; if (text_size.base) base = text_size.base; else text_size.base = base; @@ -157,17 +171,17 @@ export function getFont(obj: any, fontsubmap: any) { .map((f) => f.face) .join(", ")}`; }; - } else if (text_size[0] === "step") { + } + if (text_size[0] === "step") { const t = numberFn(text_size); return (z: number, f?: Feature) => { return `${style}${weight}${t(z, f)}px ${fontfaces .map((f) => f.face) .join(", ")}`; }; - } else { - console.log("Can't parse font: ", obj); - return (z: number) => "12px sans-serif"; } + console.log("Can't parse font: ", obj); + return (z: number) => "12px sans-serif"; } export function json_style(obj: any, fontsubmap: Map) { @@ -186,7 +200,7 @@ export function json_style(obj: any, fontsubmap: Map) { const referenced = refs.get(layer.ref); layer.type = referenced.type; layer.filter = referenced.filter; - layer.source = referenced["source"]; + layer.source = referenced.source; layer["source-layer"] = referenced["source-layer"]; } diff --git a/src/default_style/style.ts b/src/default_style/style.ts index fba2df8f..0481faa2 100644 --- a/src/default_style/style.ts +++ b/src/default_style/style.ts @@ -336,15 +336,14 @@ export const labelRules = ( label_props: language2, }), ]); - } else { - return new FlexSymbolizer([ - symbolizer, - new CenteredTextSymbolizer({ - fill: fill, - label_props: language2, - }), - ]); } + return new FlexSymbolizer([ + symbolizer, + new CenteredTextSymbolizer({ + fill: fill, + label_props: language2, + }), + ]); }; return [ @@ -364,7 +363,7 @@ export const labelRules = ( params.countryLabel, ), filter: (z, f) => { - return f.props["pmap:kind"] == "country"; + return f.props["pmap:kind"] === "country"; }, }, { @@ -378,14 +377,14 @@ export const labelRules = ( params.stateLabel, ), filter: (z, f) => { - return f.props["pmap:kind"] == "state"; + return f.props["pmap:kind"] === "state"; }, }, { id: "cities_high", dataLayer: "places", filter: (z, f) => { - return f.props["pmap:kind"] == "city"; + return f.props["pmap:kind"] === "city"; }, minzoom: 7, symbolizer: languageStack( @@ -396,10 +395,9 @@ export const labelRules = ( if (f?.props["pmap:rank"] === 1) { if (z > 8) return "600 20px sans-serif"; return "600 12px sans-serif"; - } else { - if (z > 8) return "600 16px sans-serif"; - return "600 10px sans-serif"; } + if (z > 8) return "600 16px sans-serif"; + return "600 10px sans-serif"; }, }), params.cityLabel, @@ -412,7 +410,7 @@ export const labelRules = ( id: "cities_low", dataLayer: "places", filter: (z, f) => { - return f.props["pmap:kind"] == "city"; + return f.props["pmap:kind"] === "city"; }, maxzoom: 6, symbolizer: new GroupSymbolizer([ @@ -430,10 +428,9 @@ export const labelRules = ( if (f?.props["pmap:rank"] === 1) { if (z > 8) return "600 20px sans-serif"; return "600 12px sans-serif"; - } else { - if (z > 8) return "600 16px sans-serif"; - return "600 10px sans-serif"; } + if (z > 8) return "600 16px sans-serif"; + return "600 10px sans-serif"; }, }), params.cityLabel, @@ -456,7 +453,7 @@ export const labelRules = ( params.neighbourhoodLabel, ), filter: (z, f) => { - return f.props["pmap:kind"] == "neighbourhood"; + return f.props["pmap:kind"] === "neighbourhood"; }, }, { @@ -514,7 +511,7 @@ export const labelRules = ( fill: params.neighbourhoodLabel, }), filter: (z, f) => { - return f.props["pmap:kind"] == "highway"; + return f.props["pmap:kind"] === "highway"; }, }, { diff --git a/src/extra_styles/toner.ts b/src/extra_styles/toner.ts index a62d6141..b4594cd1 100644 --- a/src/extra_styles/toner.ts +++ b/src/extra_styles/toner.ts @@ -63,7 +63,7 @@ const Toner = (variant: string) => { pattern: halftone, }), filter: (z, f) => { - return f.props.leisure == "park"; + return f.props.leisure === "park"; }, }, { @@ -206,7 +206,7 @@ const Toner = (variant: string) => { justify: Justify.Center, }), filter: (z, f) => { - return f.props["pmap:kind"] == "country"; + return f.props["pmap:kind"] === "country"; }, }, { @@ -222,7 +222,7 @@ const Toner = (variant: string) => { justify: Justify.Center, }), filter: (z, f) => { - return f.props["pmap:kind"] == "state"; + return f.props["pmap:kind"] === "state"; }, }, { @@ -243,7 +243,7 @@ const Toner = (variant: string) => { fontFamily: "Inter", fontWeight: 600, fontSize: (z, f) => { - if (f!.props["pmap:rank"] == 1) return 15; + if (f!.props["pmap:rank"] === 1) return 15; return 13; }, }), @@ -252,7 +252,7 @@ const Toner = (variant: string) => { return a["pmap:rank"] - b["pmap:rank"]; }, filter: (z, f) => { - return f.props["pmap:kind"] == "city"; + return f.props["pmap:kind"] === "city"; }, maxzoom: 8, }, @@ -267,7 +267,7 @@ const Toner = (variant: string) => { fontFamily: "Inter", fontWeight: 600, fontSize: (z, f) => { - if (f!.props["pmap:rank"] == 1) return 15; + if (f!.props["pmap:rank"] === 1) return 15; return 13; }, }), @@ -275,7 +275,7 @@ const Toner = (variant: string) => { return a["pmap:rank"] - b["pmap:rank"]; }, filter: (z, f) => { - return f.props["pmap:kind"] == "city"; + return f.props["pmap:kind"] === "city"; }, minzoom: 9, }, diff --git a/src/frontends/leaflet.ts b/src/frontends/leaflet.ts index cd35d317..b8d36348 100644 --- a/src/frontends/leaflet.ts +++ b/src/frontends/leaflet.ts @@ -92,9 +92,9 @@ const leafletLayer = (options: LeafletLayerOptions = {}): any => { const scratch = document.createElement("canvas").getContext("2d"); this.scratch = scratch; this.onTilesInvalidated = (tiles: Set) => { - tiles.forEach((t) => { + for (const t of tiles) { this.rerenderTile(t); - }); + } }; this.labelers = new Labelers( this.scratch, @@ -178,9 +178,9 @@ const leafletLayer = (options: LeafletLayerOptions = {}): any => { if (!this._map) return; // the layer has been removed from the map const center = this._map.getCenter().wrap(); - const pixelBounds = this._getTiledPixelBounds(center), - tileRange = this._pxBoundsToTileRange(pixelBounds), - tileCenter = tileRange.getCenter(); + const pixelBounds = this._getTiledPixelBounds(center); + const tileRange = this._pxBoundsToTileRange(pixelBounds); + const tileCenter = tileRange.getCenter(); const priority = coords.distanceTo(tileCenter) * this.tileDelay; await timer(priority); @@ -233,28 +233,24 @@ const leafletLayer = (options: LeafletLayerOptions = {}): any => { ctx.save(); ctx.fillStyle = this.debug; ctx.font = "600 12px sans-serif"; - ctx.fillText(coords.z + " " + coords.x + " " + coords.y, 4, 14); + ctx.fillText(`${coords.z} ${coords.x} ${coords.y}`, 4, 14); ctx.font = "12px sans-serif"; let ypos = 28; for (const [k, v] of prepared_tilemap) { const dt = v[0].data_tile; - ctx.fillText( - k + (k ? " " : "") + dt.z + " " + dt.x + " " + dt.y, - 4, - ypos, - ); + ctx.fillText(`${k + (k ? " " : "") + dt.z} ${dt.x} ${dt.y}`, 4, ypos); ypos += 14; } ctx.font = "600 10px sans-serif"; if (painting_time > 8) { - ctx.fillText(painting_time.toFixed() + " ms paint", 4, ypos); + ctx.fillText(`${painting_time.toFixed()} ms paint`, 4, ypos); ypos += 14; } if (layout_time > 8) { - ctx.fillText(layout_time.toFixed() + " ms layout", 4, ypos); + ctx.fillText(`${layout_time.toFixed()} ms layout`, 4, ypos); } ctx.strokeStyle = this.debug; @@ -370,17 +366,13 @@ const leafletLayer = (options: LeafletLayerOptions = {}): any => { continue; } } - content = - content + - `
${ - typeGlyphs[result.feature.geomType - 1] - } ${sourceName} ${sourceName ? "/" : ""} ${ - result.layerName - } ${result.feature.id || ""}
`; + content = `${content}
${typeGlyphs[result.feature.geomType - 1]} ${sourceName} ${ + sourceName ? "/" : "" + } ${result.layerName} ${result.feature.id || ""}
`; for (const prop in result.feature.props) { - content = - content + - `
${prop} = ${result.feature.props[prop]}
`; + content = `${content}
${prop} = ${result.feature.props[prop]}
`; } firstRow = false; } @@ -391,9 +383,7 @@ const leafletLayer = (options: LeafletLayerOptions = {}): any => { L.popup() .setLatLng(ev.latlng) .setContent( - '
' + - content + - "
", + `
${content}
`, ) .openOn(layer._map); }; diff --git a/src/frontends/static.ts b/src/frontends/static.ts index 55867de5..7b43aaed 100644 --- a/src/frontends/static.ts +++ b/src/frontends/static.ts @@ -25,7 +25,7 @@ const project = (latlng: Point): Point => { }; const unproject = (point: Point) => { - var d = 180 / Math.PI; + const d = 180 / Math.PI; return { lat: (2 * Math.atan(Math.exp(point.y / R)) - Math.PI / 2) * d, lng: (point.x * d) / R, @@ -115,7 +115,7 @@ export class Static { // the origin of the painter call in global Z coordinates const origin = normalized_center .clone() - .mult(Math.pow(2, display_zoom) * 256) + .mult(2 ** display_zoom * 256) .sub(new Point(width / 2, height / 2)); // the bounds of the painter call in global Z coordinates @@ -203,7 +203,7 @@ export class Static { ); const dt = prepared_tile.data_tile; ctx.fillText( - k + (k ? " " : "") + dt.z + " " + dt.x + " " + dt.y, + `${k + (k ? " " : "") + dt.z} ${dt.x} ${dt.y}`, prepared_tile.origin.x + 4, prepared_tile.origin.y + 14 * (1 + idx), ); diff --git a/src/labeler.ts b/src/labeler.ts index 020bec4e..7c8d0d43 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -248,8 +248,7 @@ export class Index { if (existing[3] === added[3]) { keys_for_ds++; const dist = Math.sqrt( - Math.pow(+existing[0] - +added[0], 2) + - Math.pow(+existing[1] - +added[1], 2), + (+existing[0] - +added[0]) ** 2 + (+existing[1] - +added[1]) ** 2, ); if (dist > max_dist) { max_dist = dist; @@ -272,9 +271,9 @@ export class Index { entries_to_delete.push(entry); } } - entries_to_delete.forEach((entry) => { + for (const entry of entries_to_delete) { this.tree.remove(entry); - }); + } this.current.delete(keyToRemove); } @@ -289,9 +288,9 @@ export class Index { entries_to_delete.push(entry); } } - entries_to_delete.forEach((entry) => { + for (const entry of entries_to_delete) { this.tree.remove(entry); - }); + } const c = this.current.get(labelToRemove.tileKey); if (c) c.delete(labelToRemove); } @@ -325,7 +324,7 @@ export class Labeler { // if it already exists... short circuit for (const [k, prepared_tiles] of prepared_tilemap) { for (const prepared_tile of prepared_tiles) { - const key = toIndex(prepared_tile.data_tile) + ":" + k; + const key = `${toIndex(prepared_tile.data_tile)}:${k}`; if (!this.index.has(key)) { this.index.makeEntry(key); keys_adding.add(key); @@ -344,7 +343,7 @@ export class Labeler { if (!prepared_tiles) continue; for (const prepared_tile of prepared_tiles) { - const key = toIndex(prepared_tile.data_tile) + ":" + dsName; + const key = `${toIndex(prepared_tile.data_tile)}:${dsName}`; if (!keys_adding.has(key)) continue; const layer = prepared_tile.data.get(rule.dataLayer); @@ -460,17 +459,16 @@ export class Labeler { let all_added = true; for (const [k, prepared_tiles] of prepared_tilemap) { for (const prepared_tile of prepared_tiles) { - if (!this.index.has(toIndex(prepared_tile.data_tile) + ":" + k)) + if (!this.index.has(`${toIndex(prepared_tile.data_tile)}:${k}`)) all_added = false; } } if (all_added) { return 0; - } else { - const timing = this.layout(prepared_tilemap); - return timing; } + const timing = this.layout(prepared_tilemap); + return timing; } } @@ -498,17 +496,16 @@ export class Labelers { let labeler = this.labelers.get(z); if (labeler) { return labeler.add(prepared_tilemap); - } else { - labeler = new Labeler( - z, - this.scratch, - this.labelRules, - this.maxLabeledTiles, - this.callback, - ); - this.labelers.set(z, labeler); - return labeler.add(prepared_tilemap); } + labeler = new Labeler( + z, + this.scratch, + this.labelRules, + this.maxLabeledTiles, + this.callback, + ); + this.labelers.set(z, labeler); + return labeler.add(prepared_tilemap); } public getIndex(z: number) { diff --git a/src/line.ts b/src/line.ts index f7a56429..b1565dde 100644 --- a/src/line.ts +++ b/src/line.ts @@ -15,27 +15,25 @@ const linelabel = ( targetLen: number, ): LabelableSegment[] => { const chunks = []; - let a, - b, - c, - i = 0, - n = 0, - d = 0; - let abmag = 0, - bcmag = 0; - let abx = 0, - aby = 0; - let bcx = 0, - bcy = 0; + let a; + let b; + let c; + let i = 0; + let n = 0; + let d = 0; + let abmag = 0; + let bcmag = 0; + let abx = 0; + let aby = 0; + let bcx = 0; + let bcy = 0; let dt = 0; let i_start = 0; let d_start = 0; if (pts.length < 2) return []; if (pts.length === 2) { - d = Math.sqrt( - Math.pow(pts[1].x - pts[0].x, 2) + Math.pow(pts[1].y - pts[0].y, 2), - ); + d = Math.sqrt((pts[1].x - pts[0].x) ** 2 + (pts[1].y - pts[0].y) ** 2); return [ { @@ -48,9 +46,7 @@ const linelabel = ( ]; } - abmag = Math.sqrt( - Math.pow(pts[1].x - pts[0].x, 2) + Math.pow(pts[1].y - pts[0].y, 2), - ); + abmag = Math.sqrt((pts[1].x - pts[0].x) ** 2 + (pts[1].y - pts[0].y) ** 2); for (i = 1, n = pts.length - 1; i < n; i++) { a = pts[i - 1]; b = pts[i]; @@ -145,7 +141,7 @@ export function lineCells(a: Point, b: Point, length: number, spacing: number) { // determine function of line const dx = b.x - a.x; const dy = b.y - a.y; - const dist = Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2)); + const dist = Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2); const retval = []; // starting from the anchor, generate square cells, diff --git a/src/painter.ts b/src/painter.ts index 4046ef27..a1dd7708 100644 --- a/src/painter.ts +++ b/src/painter.ts @@ -31,13 +31,13 @@ export function painter( ctx.save(); ctx.miterLimit = 2; - for (var rule of rules) { + for (const rule of rules) { if (rule.minzoom && z < rule.minzoom) continue; if (rule.maxzoom && z > rule.maxzoom) continue; const prepared_tiles = prepared_tilemap.get(rule.dataSource || ""); if (!prepared_tiles) continue; for (const prepared_tile of prepared_tiles) { - var layer = prepared_tile.data.get(rule.dataLayer); + const layer = prepared_tile.data.get(rule.dataLayer); if (layer === undefined) continue; if (rule.symbolizer.before) rule.symbolizer.before(ctx, prepared_tile.z); @@ -68,7 +68,7 @@ export function painter( // ctx.translate(-dim / 2, -dim / 2); // } - for (var feature of layer) { + for (const feature of layer) { let geom = feature.geom; const fbox = feature.bbox; if ( @@ -80,7 +80,7 @@ export function painter( continue; } if (rule.filter && !rule.filter(prepared_tile.z, feature)) continue; - if (ps != 1) { + if (ps !== 1) { geom = transformGeom(geom, ps, new Point(0, 0)); } rule.symbolizer.draw(ctx, geom, prepared_tile.z, feature); @@ -102,7 +102,7 @@ export function painter( if (label_data) { const matches = label_data.searchBbox(bbox, Infinity); - for (var label of matches) { + for (const label of matches) { ctx.save(); ctx.translate(label.anchor.x - origin.x, label.anchor.y - origin.y); label.draw(ctx); diff --git a/src/symbolizer.ts b/src/symbolizer.ts index a62d58a7..fa671bf4 100644 --- a/src/symbolizer.ts +++ b/src/symbolizer.ts @@ -193,8 +193,8 @@ function computeInterpolationFactor( const difference = stops[idx + 1][0] - stops[idx][0]; const progress = z - stops[idx][0]; if (difference === 0) return 0; - else if (base === 1) return progress / difference; - else return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1); + if (base === 1) return progress / difference; + return (base ** progress - 1) / (base ** difference - 1); } export function exp(base: number, stops: number[][]): (z: number) => number { @@ -605,7 +605,9 @@ export class GroupSymbolizer implements LabelSymbolizer { draws.push(label.draw); } const draw = (ctx: CanvasRenderingContext2D) => { - draws.forEach((d) => d(ctx)); + for (const d of draws) { + d(ctx); + } }; return [{ anchor: anchor, bboxes: [bbox], draw: draw }]; diff --git a/src/task.ts b/src/task.ts index 8287e4c4..76ea1206 100644 --- a/src/task.ts +++ b/src/task.ts @@ -2,7 +2,7 @@ import potpack from "potpack"; // https://github.com/tangrams/tangram/blob/master/src/styles/text/font_manager.js export const Font = (name: string, url: string, weight: string) => { - const ff = new FontFace(name, "url(" + url + ")", { weight: weight }); + const ff = new FontFace(name, `url(${url})`, { weight: weight }); document.fonts.add(ff); return ff.load(); }; @@ -66,7 +66,7 @@ export class Sheet { const icons = Array.from(tree.body.children); const missingImg = await mkimg( - "data:image/svg+xml;base64," + btoa(MISSING), + `data:image/svg+xml;base64,${btoa(MISSING)}`, ); const boxes: PotPackInput[] = [ @@ -81,7 +81,7 @@ export class Sheet { const serializer = new XMLSerializer(); for (const ps of icons) { const svg64 = btoa(serializer.serializeToString(ps)); - const image64 = "data:image/svg+xml;base64," + svg64; + const image64 = `data:image/svg+xml;base64,${svg64}`; const img = await mkimg(image64); boxes.push({ w: img.width * scale, diff --git a/src/text.ts b/src/text.ts index 0d200c34..f5b9feda 100644 --- a/src/text.ts +++ b/src/text.ts @@ -24,7 +24,7 @@ export function linebreak(str: string, maxUnits: number): string[] { const CJK_CHARS = "\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3220-\u3229\u3248-\u324F\u3251-\u325F\u3280-\u3289\u32B1-\u32BF\u3400-\u4DB5\u4E00-\u9FEA\uF900-\uFA6D\uFA70-\uFAD9\u2000"; -const cjk_test = new RegExp("^[" + CJK_CHARS + "]+$"); +const cjk_test = new RegExp(`^[${CJK_CHARS}]+$`); export function isCjk(s: string) { return cjk_test.test(s); diff --git a/src/tilecache.ts b/src/tilecache.ts index fbd756c1..d4229291 100644 --- a/src/tilecache.ts +++ b/src/tilecache.ts @@ -43,7 +43,7 @@ export interface Zxy { } export function toIndex(c: Zxy): string { - return c.x + ":" + c.y + ":" + c.z; + return `${c.x}:${c.y}:${c.z}`; } export interface TileSource { @@ -54,15 +54,15 @@ export interface TileSource { // so the general tile rendering case does not need rescaling. const loadGeomAndBbox = (pbf: any, geometry: number, scale: number) => { pbf.pos = geometry; - let end = pbf.readVarint() + pbf.pos, - cmd = 1, - length = 0, - x = 0, - y = 0, - x1 = Infinity, - x2 = -Infinity, - y1 = Infinity, - y2 = -Infinity; + const end = pbf.readVarint() + pbf.pos; + let cmd = 1; + let length = 0; + let x = 0; + let y = 0; + let x1 = Infinity; + let x2 = -Infinity; + let y1 = Infinity; + let y2 = -Infinity; const lines: Point[][] = []; let line: Point[] = []; @@ -87,7 +87,7 @@ const loadGeomAndBbox = (pbf: any, geometry: number, scale: number) => { line.push(new Point(x, y)); } else if (cmd === 7) { if (line) line.push(line[0].clone()); - } else throw new Error("unknown command " + cmd); + } else throw new Error(`unknown command ${cmd}`); } if (line) lines.push(line); return { geom: lines, bbox: { minX: x1, minY: y1, maxX: x2, maxY: y2 } }; @@ -157,9 +157,8 @@ export class PmtilesSource implements TileSource { if (result) { return parseTile(result.data, tileSize); - } else { - return new Map(); } + return new Map(); } } @@ -248,10 +247,10 @@ function distToSegmentSquared(p: Point, v: Point, w: Point) { export function isInRing(point: Point, ring: Point[]): boolean { let inside = false; for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) { - const xi = ring[i].x, - yi = ring[i].y; - const xj = ring[j].x, - yj = ring[j].y; + const xi = ring[i].x; + const yi = ring[i].y; + const xj = ring[j].x; + const yj = ring[j].y; const intersect = yi > point.y !== yj > point.y && point.x < ((xj - xi) * (point.y - yi)) / (yj - yi) + xi; @@ -392,7 +391,11 @@ export class TileCache { this.cache.set(idx, { used: performance.now(), data: tile }); const ifentry2 = this.inflight.get(idx); - if (ifentry2) ifentry2.forEach((f) => f[0](tile)); + if (ifentry2) { + for (const f of ifentry2) { + f[0](tile); + } + } this.inflight.delete(idx); resolve(tile); @@ -410,7 +413,11 @@ export class TileCache { }) .catch((e) => { const ifentry2 = this.inflight.get(idx); - if (ifentry2) ifentry2.forEach((f) => f[1](e)); + if (ifentry2) { + for (const f of ifentry2) { + f[1](e); + } + } this.inflight.delete(idx); reject(e); }); diff --git a/src/view.ts b/src/view.ts index 972b716e..7953b5e9 100644 --- a/src/view.ts +++ b/src/view.ts @@ -76,8 +76,7 @@ export class View { display_zoom: number, bounds: any, ): Array { - const fractional = - Math.pow(2, display_zoom) / Math.pow(2, Math.ceil(display_zoom)); + const fractional = 2 ** display_zoom / 2 ** Math.ceil(display_zoom); const needed = []; let scale = 1; const dim = this.tileCache.tileSize;