Skip to content

Commit 1ab8fb7

Browse files
committed
refactor: improve syntax and comments to address PR review
See #72
1 parent 2ad0098 commit 1ab8fb7

File tree

11 files changed

+44
-36
lines changed

11 files changed

+44
-36
lines changed

Diff for: lib/core/network.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ export default class Network {
148148
const turnLookup = {}
149149
const addTurn = function (turn1, turn2) {
150150
if (!(turn1.getId() in turnLookup)) turnLookup[turn1.getId()] = []
151-
if (turnLookup[turn1.getId()].indexOf(turn2) === -1)
151+
if (turnLookup[turn1.getId()].indexOf(turn2) === -1) {
152152
turnLookup[turn1.getId()].push(turn2)
153+
}
153154
}
154155
forEach(Object.values(this.streetEdges), (streetEdge) => {
155156
if (streetEdge.fromTurnPoint && streetEdge.toTurnPoint) {
@@ -341,8 +342,9 @@ export default class Network {
341342

342343
addStopAdjacency(stopIdA, stopIdB) {
343344
if (!this.adjacentStops[stopIdA]) this.adjacentStops[stopIdA] = []
344-
if (this.adjacentStops[stopIdA].indexOf(stopIdB) === -1)
345+
if (this.adjacentStops[stopIdA].indexOf(stopIdB) === -1) {
345346
this.adjacentStops[stopIdA].push(stopIdB)
347+
}
346348
}
347349

348350
/**
@@ -452,10 +454,12 @@ export default class Network {
452454
toVertex,
453455
segment.getType()
454456
)
455-
if (geomCoords && geomCoords.length > 0)
457+
if (geomCoords && geomCoords.length > 0) {
456458
edge.geomCoords = geomCoords
457-
if (pointGeom && pointGeom.length > 0)
459+
}
460+
if (pointGeom && pointGeom.length > 0) {
458461
edge.pointGeom = pointGeom
462+
}
459463
}
460464

461465
// associate the graph edge and path segment with each other
@@ -525,10 +529,11 @@ export default class Network {
525529
pathSegment.points[0].isBoardPoint = true
526530

527531
// if there are additional transit segments, mark the 'to' endpoint as a transfer point
528-
if (transitSegments.length > 1)
532+
if (transitSegments.length > 1) {
529533
pathSegment.points[
530534
pathSegment.points.length - 1
531535
].isTransferPoint = true
536+
}
532537

533538
// if last transit segment in path, mark 'to' endpoint as alight point
534539
} else if (
@@ -540,8 +545,9 @@ export default class Network {
540545
].isAlightPoint = true
541546

542547
// if there are additional transit segments, mark the 'from' endpoint as a transfer point
543-
if (transitSegments.length > 1)
548+
if (transitSegments.length > 1) {
544549
pathSegment.points[0].isTransferPoint = true
550+
}
545551

546552
// if this is an 'internal' transit segment, mark both endpoints as transfer points
547553
} else if (transitSegments.length > 2) {

Diff for: lib/core/pathsegment.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ export default class PathSegment {
120120
}
121121

122122
endVertex() {
123-
if (this.points[this.points.length - 1].multipoint)
123+
if (this.points[this.points.length - 1].multipoint) {
124124
return this.points[this.points.length - 1].multipoint.graphVertex
125+
}
125126
if (!this.edges || this.edges.length === 0) return null
126127

127128
const lastGraphEdge = this.edges[this.edges.length - 1].graphEdge

Diff for: lib/display/canvas-display.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ export default class CanvasDisplay extends Display {
132132

133133
if (attrs.stroke) {
134134
this.ctx.strokeStyle = attrs.stroke
135-
if (attrs['stroke-opacity'])
135+
if (attrs['stroke-opacity']) {
136136
this.ctx.globalAlpha = attrs['stroke-opacity']
137+
}
137138
this.ctx.lineWidth = attrs['stroke-width'] || 1
138139
this.ctx.strokeText(text, anchor.x, anchor.y)
139140
}

Diff for: lib/graph/edge.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -817,10 +817,12 @@ export default class Edge {
817817
if (
818818
this.fromVertex === edge.fromVertex ||
819819
this.fromVertex === edge.toVertex
820-
)
820+
) {
821821
return this.fromVertex
822-
if (this.toVertex === edge.fromVertex || this.toVertex === edge.toVertex)
822+
}
823+
if (this.toVertex === edge.fromVertex || this.toVertex === edge.toVertex) {
823824
return this.toVertex
825+
}
824826
return null
825827
}
826828

@@ -829,8 +831,9 @@ export default class Edge {
829831
*/
830832

831833
setPointLabelPosition(pos, skip) {
832-
if (this.fromVertex.point !== skip)
834+
if (this.fromVertex.point !== skip) {
833835
this.fromVertex.point.labelPosition = pos
836+
}
834837
if (this.toVertex.point !== skip) this.toVertex.point.labelPosition = pos
835838

836839
forEach(this.pointArray, (point) => {

Diff for: lib/graph/graph.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ export default class NetworkGraph {
2828
this.edgeGroups = {}
2929

3030
// Add all base vertices
31-
for (const i in vertices)
31+
for (const i in vertices) {
3232
this.addVertex(vertices[i], vertices[i].worldX, vertices[i].worldY)
33+
}
3334
}
3435

3536
/**
@@ -162,8 +163,9 @@ export default class NetworkGraph {
162163
(vertexGroups.STOP.length > 0 && vertexGroups.PLACE.length > 0) ||
163164
vertexGroups.PLACE.length > 1 ||
164165
vertexGroups.MULTI.length > 0
165-
)
166+
) {
166167
return
168+
}
167169

168170
let mergePoint
169171

@@ -369,8 +371,9 @@ export default class NetworkGraph {
369371

370372
forEach(vertex.edges, (edge) => {
371373
const pathSegmentIds = edge.getPathSegmentIds()
372-
if (!(pathSegmentIds in pathSegmentBundles))
374+
if (!(pathSegmentIds in pathSegmentBundles)) {
373375
pathSegmentBundles[pathSegmentIds] = []
376+
}
374377
pathSegmentBundles[pathSegmentIds].push(edge)
375378
const opp = edge.oppositeVertex(vertex)
376379
if (opposites.indexOf(opp) === -1) opposites.push(opp)

Diff for: lib/labeler/label.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ export default class Label {
3232
refresh(display) {}
3333

3434
setVisibility(visibility) {
35-
if (this.svgGroup)
35+
if (this.svgGroup) {
3636
this.svgGroup.attr('display', visibility ? 'initial' : 'none')
37+
}
3738
}
3839

3940
getBBox() {

Diff for: lib/labeler/labeler.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ export default class Labeler {
152152
bbox.x - bbox.width / 2 > this.width ||
153153
bbox.y + bbox.height / 2 < 0 ||
154154
bbox.y - bbox.height / 2 > this.height
155-
)
155+
) {
156156
return
157+
}
157158

158159
this.quadtree.add([bbox.x + bbox.width / 2, bbox.y + bbox.height / 2, bbox])
159160

@@ -264,8 +265,9 @@ export default class Labeler {
264265
const busRSegments = []
265266
forEach(this.transitive.network.paths, (path) => {
266267
forEach(path.getRenderedSegments(), (rSegment) => {
267-
if (rSegment.type === 'TRANSIT' && rSegment.mode === 3)
268+
if (rSegment.type === 'TRANSIT' && rSegment.mode === 3) {
268269
busRSegments.push(rSegment)
270+
}
269271
})
270272
})
271273

Diff for: lib/point/multipoint.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export default class MultiPoint extends Point {
4545
let shortest = null
4646
forEach(this.points, (point) => {
4747
if (point.getType() === 'TURN') return
48-
if (!shortest || point.getName().length < shortest.length)
48+
if (!shortest || point.getName().length < shortest.length) {
4949
shortest = point.getName()
50+
}
5051
})
5152

5253
return shortest
@@ -135,8 +136,9 @@ export default class MultiPoint extends Point {
135136
*/
136137

137138
addRenderData(pointInfo) {
138-
if (pointInfo.offsetX !== 0 || pointInfo.offsetY !== 0)
139+
if (pointInfo.offsetX !== 0 || pointInfo.offsetY !== 0) {
139140
this.hasOffsetPoints = true
141+
}
140142
this.renderData.push(pointInfo)
141143
}
142144

Diff for: lib/renderer/renderededge.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ export default class RenderedEdge {
205205
if (
206206
this.graphEdge.renderedEdges.length !==
207207
rEdge.graphEdge.renderedEdges.length
208-
)
208+
) {
209209
return
210+
}
210211

211212
const commonVertex = this.graphEdge.commonVertex(rEdge.graphEdge)
212213
if (!commonVertex || commonVertex.point.isSegmentEndPoint) return

Diff for: lib/transitive.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ type Journey = {
117117
*/
118118
journey_name: string
119119
/**
120-
* What is this?
121120
* The segments of a journey
122121
*/
123122
segments: Segment[]
@@ -382,7 +381,7 @@ type TransitiveStyleComputeFn = (
382381
) => number | string
383382

384383
/**
385-
* A map describing a styling override applied to a particular styleing value
384+
* A map describing a styling override applied to a particular styling value
386385
* noted with the key value. The value for this style can be either a number,
387386
* string or result of a custom function.
388387
*
@@ -604,7 +603,7 @@ export default class Transitive {
604603
display!: CanvasDisplay | SvgDisplay
605604
el?: HTMLElement
606605
// FIXME: somehow typing the emit method (which is injected at the end of this
607-
// file by component-emitter causes the emit method to not work.
606+
// file by component-emitter) causes the emit method to not work.
608607
// emit!: (message: string, transitiveInstance: this, el?: HTMLElement) => void
609608
labeler!: Labeler
610609
options?: TransitiveOptions
@@ -645,7 +644,6 @@ export default class Transitive {
645644
/**
646645
* Clear the Network data and redraw the (empty) map
647646
*/
648-
649647
clearData() {
650648
this.network = this.data = null
651649
this.labeler.clear()
@@ -656,7 +654,6 @@ export default class Transitive {
656654
/**
657655
* Update the Network data and redraw the map
658656
*/
659-
660657
updateData(data: TransitiveData, resetDisplay?: boolean) {
661658
this.network = null
662659
this.data = data
@@ -672,7 +669,6 @@ export default class Transitive {
672669
*
673670
* @param {String} an OTP mode string
674671
*/
675-
676672
getModeStyles(mode: string) {
677673
return this.styler.getModeStyles(mode, this.display || new Display(this))
678674
}
@@ -682,7 +678,6 @@ export default class Transitive {
682678
/**
683679
* Set the DOM element that serves as the main map canvas
684680
*/
685-
686681
setElement(el?: HTMLElement) {
687682
if (this.el) d3.select(this.el).selectAll('*').remove()
688683

@@ -696,7 +691,6 @@ export default class Transitive {
696691
/**
697692
* Set the DOM element that serves as the main map canvas
698693
*/
699-
700694
setRenderer(type: RendererType) {
701695
switch (type) {
702696
case 'wireframe':
@@ -711,7 +705,6 @@ export default class Transitive {
711705
/**
712706
* Render
713707
*/
714-
715708
render() {
716709
if (!this.network) {
717710
this.network = new Network(this, this.data)
@@ -732,7 +725,6 @@ export default class Transitive {
732725
*
733726
* @param {Element} el
734727
*/
735-
736728
renderTo(el: HTMLElement) {
737729
this.setElement(el)
738730
this.render()
@@ -745,7 +737,6 @@ export default class Transitive {
745737
/**
746738
* focusJourney
747739
*/
748-
749740
focusJourney(journeyId: string) {
750741
if (!this.network) {
751742
console.warn('Transitive network is not defined! Cannot focus journey!')
@@ -760,7 +751,6 @@ export default class Transitive {
760751
* Sets the Display bounds
761752
* @param {Array} lon/lat bounds expressed as [[west, south], [east, north]]
762753
*/
763-
764754
setDisplayBounds(llBounds: Bounds) {
765755
if (!this.display) return
766756
const smWestSouth = sm.forward(llBounds[0])
@@ -780,7 +770,6 @@ export default class Transitive {
780770
* Gets the Network bounds
781771
* @returns {Array} lon/lat bounds expressed as [[west, south], [east, north]]
782772
*/
783-
784773
getNetworkBounds() {
785774
if (!this.network || !this.network.graph) return null
786775
const graphBounds = this.network.graph.bounds()
@@ -795,7 +784,6 @@ export default class Transitive {
795784
/**
796785
* resize
797786
*/
798-
799787
resize(width: number, height: number) {
800788
if (!this.display) return
801789
// @ts-expect-error I have no idea what magic this display object uses to
@@ -811,7 +799,6 @@ export default class Transitive {
811799
/**
812800
* trigger a display resize action (for externally-managed SVG containers)
813801
*/
814-
815802
resized(width: number, height: number) {
816803
// @ts-expect-error I have no idea what magic this display object uses to
817804
// get a resized method. - Evan :(

Diff for: lib/util/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ function renderDataToSvgPath(renderData) {
189189
return renderData
190190
.map((d, k) => {
191191
if (k === 0) return `M${d.x} ${d.y}`
192-
if (d.arc)
192+
if (d.arc) {
193193
return `A${d.radius} ${d.radius} ${d.arc} 0 ${d.arc > 0 ? 0 : 1} ${
194194
d.x
195195
} ${d.y}`
196+
}
196197
return `L${d.x} ${d.y}`
197198
})
198199
.join(' ')

0 commit comments

Comments
 (0)