From c7ab8941d686cc11404265cf6ef99424d1aa0a41 Mon Sep 17 00:00:00 2001 From: James Beard Date: Tue, 5 Nov 2024 12:17:15 +1100 Subject: [PATCH] Geting rid of some vars in favour of const / let and defining variables closer to where they are used. --- src/common.ts | 8 +-- src/interpolation.ts | 6 +-- src/isobands.ts | 22 ++++---- src/isolines.ts | 16 +++--- src/polygons.ts | 122 ++++++++++++++++--------------------------- src/quadtree.ts | 8 ++- 6 files changed, 70 insertions(+), 112 deletions(-) diff --git a/src/common.ts b/src/common.ts index 4d233a9..750b123 100644 --- a/src/common.ts +++ b/src/common.ts @@ -77,12 +77,12 @@ export { SuccessCallback, Path, Ring, + LineEntry, + LineEdge, LineCell, - BandCell, - BandCellGrid, LineCellGrid, BandEntry, - LineEntry, - LineEdge, BandEdge, + BandCell, + BandCellGrid, }; diff --git a/src/interpolation.ts b/src/interpolation.ts index 83c2660..00eed2b 100644 --- a/src/interpolation.ts +++ b/src/interpolation.ts @@ -20,12 +20,8 @@ function linear(a: number, b: number, v: number) { * Note, that we assume that 'a' and 'b' have unit distance (i.e. 1) */ function linear_ab(a: number, b: number, v0: number, v1: number) { - var tmp; - if (v0 > v1) { - tmp = v0; - v0 = v1; - v1 = tmp; + [v0, v1] = [v1, v0]; // swap } if (a < b) { diff --git a/src/isobands.ts b/src/isobands.ts index 685a4ab..c6474ba 100644 --- a/src/isobands.ts +++ b/src/isobands.ts @@ -2071,7 +2071,7 @@ function computeCenterAverage( minV: number, maxV: number ) { - var average = (tl + tr + br + bl) / 4; + const average = (tl + tr + br + bl) / 4; if (average > maxV) return 2; /* above isoband limits */ @@ -2086,16 +2086,14 @@ function prepareCell( y: number, opt: IsoBandOptions ) { - var cell, center_avg; - /* compose the 4-trit corner representation */ - var cval = 0; - var x3 = grid[y + 1][x]; - var x2 = grid[y + 1][x + 1]; - var x1 = grid[y][x + 1]; - var x0 = grid[y][x]; - const minV = opt.minV!; // assume minV defined - const maxV = opt.maxV!; // assume maxV defined + let cval = 0; + const x3 = grid[y + 1][x], + x2 = grid[y + 1][x + 1], + x1 = grid[y][x + 1], + x0 = grid[y][x], + minV = opt.minV!, // assume minV defined + maxV = opt.maxV!; // assume maxV defined /* * Note that missing data within the grid will result @@ -2152,9 +2150,9 @@ function prepareCell( * 1 ... within iso band * 2 ... above isoband */ - center_avg = 0; + let center_avg = 0; - cell = { + let cell = { cval: cval, polygons: [], edges: {}, diff --git a/src/isolines.ts b/src/isolines.ts index 1df5734..14bdd5c 100644 --- a/src/isolines.ts +++ b/src/isolines.ts @@ -196,14 +196,12 @@ function prepareCell( y: number, settings: IsoLineOptions ): LineCell | undefined { - var left, right, top, bottom, average, cell: LineCell; - let cval = 0; - const x3 = grid[y + 1][x]; - const x2 = grid[y + 1][x + 1]; - const x1 = grid[y][x + 1]; - const x0 = grid[y][x]; - const threshold = settings.threshold!; // assume threshold defined + const x3 = grid[y + 1][x], + x2 = grid[y + 1][x + 1], + x1 = grid[y][x + 1], + x0 = grid[y][x], + threshold = settings.threshold!; // assume threshold defined /* * Note that missing data within the grid will result @@ -254,7 +252,7 @@ function prepareCell( cval = +cval; /* compose the cell object */ - cell = { + const cell: LineCell = { cval: cval, polygons: [], edges: {}, @@ -269,6 +267,8 @@ function prepareCell( * with the cell borders and (i) add edges for polygon * trace-back, or (ii) a list of small closed polygons */ + let left, right, top, bottom, average; + switch (cval) { case 0: if (settings.polygons) diff --git a/src/polygons.ts b/src/polygons.ts index 6ad6223..dc6a6f1 100644 --- a/src/polygons.ts +++ b/src/polygons.ts @@ -8,11 +8,9 @@ import { type Path, type Ring, type LineEntry, - type LineEdge, type LineCell, type LineCellGrid, type BandEntry, - type BandEdge, type BandCell, type BandCellGrid, } from "./common.js"; @@ -88,13 +86,11 @@ function requireFrame( lowerBound: number, upperBound: number ) { - var frameRequired, cols, rows, i, j; + let frameRequired = true; + const cols = data[0].length; + const rows = data.length; - frameRequired = true; - cols = data[0].length; - rows = data.length; - - for (j = 0; j < rows; j++) { + for (let j = 0; j < rows; j++) { if ( data[j][0] < lowerBound || data[j][0] > upperBound || @@ -117,7 +113,7 @@ function requireFrame( } if (frameRequired) - for (i = 0; i < cols - 1; i++) { + for (let i = 0; i < cols - 1; i++) { if ( data[0][i] < lowerBound || data[0][i] > upperBound || @@ -133,13 +129,11 @@ function requireFrame( } function requireLineFrame(data: number[][], threshold: number) { - var frameRequired, cols, rows, i, j; - - frameRequired = true; - cols = data[0].length; - rows = data.length; + let frameRequired = true; + const cols = data[0].length; + const rows = data.length; - for (j = 0; j < rows; j++) { + for (let j = 0; j < rows; j++) { if (data[j][0] >= threshold || data[j][cols - 1] >= threshold) { frameRequired = false; break; @@ -154,7 +148,7 @@ function requireLineFrame(data: number[][], threshold: number) { } if (frameRequired) - for (i = 0; i < cols - 1; i++) { + for (let i = 0; i < cols - 1; i++) { if (data[0][i] >= threshold || data[rows - 1][i] > threshold) { frameRequired = false; break; @@ -169,21 +163,6 @@ function traceBandPaths( cellGrid: BandCellGrid, settings: IsoBandOptions ) { - var nextedge, - e, - ee: BandEdge, - s, - ve: BandEntry, - enter: BandEntry, - x, - y, - finalized, - cc, - dir: 0 | 1 | 2 | 3, - count, - point, - found_entry; - const polygons: Ring[] = []; const rows = data.length - 1; const cols = data[0].length - 1; @@ -246,29 +225,28 @@ function traceBandPaths( /* finally, start tracing back first polygon(s) */ cellGrid.forEach(function (a, i) { a.forEach(function (cell, j) { - nextedge = null; - /* trace paths for all available edges that go through this cell */ - for (e = 0; e < 8; e++) { - nextedge = available_starts[e]; + for (let e = 0; e < 8; e++) { + const nextedge = available_starts[e]; if (typeof cell?.edges[nextedge] !== "object") continue; /* start a new, full path */ - const path: Path = []; - ee = cell.edges[nextedge]!; // assume edge is defined - enter = nextedge; - x = i; - y = j; - finalized = false; - const origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]]; + let ee = cell.edges[nextedge]!, // assume edge is defined + enter = nextedge, + x = i, + y = j, + finalized = false; + + const path: Path = [], + origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]]; /* add start coordinate */ path.push(origin); /* start traceback */ while (!finalized) { - cc = cellGrid[x][y]; + let cc = cellGrid[x][y]; if (typeof cc?.edges[enter] !== "object") break; @@ -278,7 +256,7 @@ function traceBandPaths( delete cc.edges[enter]; /* add last point of edge to path arra, since we extend a polygon */ - point = ee.path[1]; + const point = ee.path[1]; point[0] += x; point[1] += y; path.push(point); @@ -292,8 +270,8 @@ function traceBandPaths( typeof cellGrid[x] === "undefined" || typeof cellGrid[x][y] === "undefined" ) { - dir = 0; - count = 0; + let dir: 0 | 1 | 2 | 3 = 0, + count = 0; if (x === cols) { x--; @@ -317,8 +295,8 @@ function traceBandPaths( break; } - while (1) { - found_entry = false; + while (true) { + let found_entry = false; if (count > 4) throw new Error( @@ -334,8 +312,8 @@ function traceBandPaths( cc = cellGrid[x][y]; /* check for re-entry */ - for (s = 0; s < valid_entries[dir].length; s++) { - ve = valid_entries[dir][s]; + for (let s = 0; s < valid_entries[dir].length; s++) { + const ve = valid_entries[dir][s]; if (typeof cc?.edges[ve] === "object") { /* found re-entry */ ee = cc.edges[ve]!; // assume edge is defined @@ -405,19 +383,6 @@ function traceLinePaths( cellGrid: LineCellGrid, settings: IsoLineOptions ) { - var e, - ee: LineEdge, - cc, - enter, - x, - y, - finalized, - point, - dir: 0 | 1 | 2 | 3, - count, - found_entry, - ve; - const polygons: Ring[] = []; const rows = data.length - 1; const cols = data[0].length - 1; @@ -472,26 +437,27 @@ function traceLinePaths( cellGrid.forEach(function (a, i) { a.forEach(function (cell, j) { /* trace paths for all available edges that go through this cell */ - for (e = 0; e < 4; e++) { + for (let e = 0; e < 4; e++) { const nextedge = valid_entries[e]; if (typeof cell?.edges[nextedge] !== "object") continue; /* start a new, full path */ - const path: Path = []; - ee = cell.edges[nextedge]; - enter = nextedge; - x = i; - y = j; - finalized = false; - const origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]]; + let ee = cell.edges[nextedge], + enter = nextedge, + x = i, + y = j, + finalized = false; + + const path: Path = [], + origin: Coord = [i + ee.path[0][0], j + ee.path[0][1]]; /* add start coordinate */ path.push(origin); /* start traceback */ while (!finalized) { - cc = cellGrid[x][y]; + let cc = cellGrid[x][y]; if (typeof cc?.edges[enter] !== "object") break; @@ -501,7 +467,7 @@ function traceLinePaths( delete cc.edges[enter]; /* add last point of edge to path arra, since we extend a polygon */ - point = ee.path[1]; + const point = ee.path[1]; point[0] += x; point[1] += y; path.push(point); @@ -517,8 +483,8 @@ function traceLinePaths( ) { if (!settings.linearRing) break; - dir = 0; - count = 0; + let dir: 0 | 1 | 2 | 3 = 0, + count = 0; if (x === cols) { x--; @@ -540,8 +506,8 @@ function traceLinePaths( break; } - while (1) { - found_entry = false; + while (true) { + let found_entry = false; if (count > 4) throw new Error( @@ -557,7 +523,7 @@ function traceLinePaths( cc = cellGrid[x][y]; /* check for re-entry */ - ve = valid_entries[dir]; + const ve = valid_entries[dir]; if (typeof cc?.edges[ve] === "object") { /* found re-entry */ ee = cc.edges[ve]!; // assume edge is defined diff --git a/src/quadtree.ts b/src/quadtree.ts index 1171ae9..70dfe1c 100644 --- a/src/quadtree.ts +++ b/src/quadtree.ts @@ -130,7 +130,7 @@ class TreeNode { upperBound: number, subsumed: boolean | undefined ) { - var cells: Cell[] = []; + let cells: Cell[] = []; subsumed = typeof subsumed === "undefined" ? true : subsumed; @@ -224,8 +224,6 @@ class QuadTree { root: TreeNode; constructor(data: number[][]) { - var i, cols; - /* do some input checking */ if (!data) throw new Error("data is required"); @@ -235,11 +233,11 @@ class QuadTree { if (data.length < 2) throw new Error("data must contain at least two rows"); /* check if we've got a regular grid */ - cols = data[0].length; + const cols = data[0].length; if (cols < 2) throw new Error("data must contain at least two columns"); - for (i = 1; i < data.length; i++) { + for (let i = 1; i < data.length; i++) { if (!Array.isArray(data[i])) throw new Error("Row " + i + " is not an array");