Skip to content

Commit ece38c8

Browse files
committed
Number destination upon arrival
Added optional arguments to OSRMInstructionFormatter.string(for:modifyValueByKey:) to track which leg the instruction is on. When there are multiple waypoints in the route, indicate the ordinal number of the waypoint in the arrival instruction. Fixed the raw value of TokenType.wayPoint. Updated tests. Ensure that formatted ordinals match the fixture language (English) rather than the test machine’s language.
1 parent fb72202 commit ece38c8

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

OSRMTextInstructions/OSRMTextInstructions.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ public class OSRMInstructionFormatter: Formatter {
121121
typealias InstructionsByModifier = [String: InstructionsByToken]
122122

123123
override public func string(for obj: Any?) -> String? {
124-
return string(for: obj, modifyValueByKey: nil)
124+
return string(for: obj, legIndex: nil, numberOfLegs: nil, modifyValueByKey: nil)
125125
}
126126

127-
public func string(for obj: Any?, modifyValueByKey: ((TokenType, String) -> String)?) -> String? {
127+
public func string(for obj: Any?, legIndex: Int?, numberOfLegs: Int?, modifyValueByKey: ((TokenType, String) -> String)?) -> String? {
128128
guard let step = obj as? RouteStep else {
129129
return nil
130130
}
@@ -221,7 +221,10 @@ public class OSRMInstructionFormatter: Formatter {
221221
}
222222

223223
// Prepare token replacements
224-
let nthWaypoint = "" // TODO: add correct waypoint counting
224+
var nthWaypoint: String? = nil
225+
if let legIndex = legIndex, let numberOfLegs = numberOfLegs, legIndex != numberOfLegs - 1 {
226+
nthWaypoint = ordinalFormatter.string(from: (legIndex + 1) as NSNumber)
227+
}
225228
let destination = step.destinations?.first ?? ""
226229
var exit: String = ""
227230
if let exitIndex = step.exitIndex, exitIndex <= 10 {
@@ -262,7 +265,7 @@ public class OSRMInstructionFormatter: Formatter {
262265
case .laneInstruction: replacement = laneInstruction ?? ""
263266
case .modifier: replacement = modifierConstant
264267
case .direction: replacement = directionFromDegree(degree: bearing)
265-
case .wayPoint: replacement = nthWaypoint
268+
case .wayPoint: replacement = nthWaypoint ?? ""
266269
}
267270
if tokenType == .wayName {
268271
result += replacement // already modified above

OSRMTextInstructions/TokenType.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public enum TokenType: Int, CustomStringConvertible {
2929
type = .modifier
3030
case "direction":
3131
type = .direction
32-
case "way_point":
32+
case "nth":
3333
type = .wayPoint
3434
default:
3535
return nil
@@ -54,7 +54,7 @@ public enum TokenType: Int, CustomStringConvertible {
5454
case .direction:
5555
return "direction"
5656
case .wayPoint:
57-
return "way_point"
57+
return "nth"
5858
}
5959
}
6060
}

OSRMTextInstructionsTests/OSRMTextInstructionsTests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import XCTest
22
import MapboxDirections
3-
import OSRMTextInstructions
3+
@testable import OSRMTextInstructions
44

55
class OSRMTextInstructionsTests: XCTestCase {
66
let instructions = OSRMInstructionFormatter(version: "v5")
7+
8+
override func setUp() {
9+
super.setUp()
10+
11+
// Force an English locale to match the fixture language rather than the test machine’s language.
12+
instructions.ordinalFormatter.locale = Locale(identifier: "en-US")
13+
}
714

815
func testSentenceCasing() {
916
XCTAssertEqual("Capitalized String", "capitalized String".sentenceCased)
@@ -27,11 +34,12 @@ class OSRMTextInstructionsTests: XCTestCase {
2734
XCTAssert(false, "invalid json")
2835
return
2936
}
37+
let options = json["options"] as? [String: Int]
3038

3139
let step = RouteStep(json: json["step"] as! [String: Any])
3240

3341
// compile instruction
34-
let instruction = instructions.string(for: step)
42+
let instruction = instructions.string(for: step, legIndex: options?["legIndex"], numberOfLegs: options?["legCount"], modifyValueByKey: nil)
3543

3644
// check generated instruction against fixture
3745
XCTAssertEqual(
@@ -107,6 +115,7 @@ class OSRMTextInstructionsTests: XCTestCase {
107115
fixture["step"] = step
108116
fixture["instruction"] = (json["instructions"] as! [ String: Any ])["en"] as! String
109117

118+
fixture["options"] = json["options"]
110119
return fixture
111120
}
112121
}

0 commit comments

Comments
 (0)