Skip to content

Commit

Permalink
Use <> instead of {}
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Feb 11, 2016
1 parent 366daf3 commit 39774f1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ At a Glance

#### Mapping URL Patterns

URL patterns can contain placeholders. Placeholders will be replaced with matching values from URLs. Use `{` and `}` to make placeholders.
URL patterns can contain placeholders. Placeholders will be replaced with matching values from URLs. Use `<` and `>` to make placeholders.

Here's an example of mapping URL patterns with view controllers and a closure. View controllers should conform a protocol `URLNavigable` to be mapped with URL patterns. See [Implementing URLNavigable](#implementing-urlnavigable) section for details.

```swift
Navigator.map("myapp://user/{id}", UserViewController.self)
Navigator.map("myapp://post/{id}", PostViewController.self)
Navigator.map("myapp://user/<id>", UserViewController.self)
Navigator.map("myapp://post/<id>", PostViewController.self)

Navigator.map("myapp://alert") { URL, values in
print(URL.parameters["title"])
Expand Down
2 changes: 1 addition & 1 deletion Sources/URLNavigable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public protocol URLNavigable {
///
/// - Parameter URL: The URL which is used to create an instance.
/// - Parameter values: The URL pattern placeholder values by placeholder names. For example, if the URL pattern is
/// `myapp://user/{id}` and the given URL is `myapp://user/123`, values will be `["id": "123"]`.
/// `myapp://user/<id>` and the given URL is `myapp://user/123`, values will be `["id": "123"]`.
init?(URL: URLStringConvertible, values: [String: AnyObject])

}
12 changes: 6 additions & 6 deletions Sources/URLNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import UIKit
///
/// Here's an example of mapping URLNaviable-conforming class `UserViewController` to URL:
///
/// Navigator.map("myapp://user/{id}", UserViewController.self)
/// Navigator.map("myapp://user/<id>", UserViewController.self)
///
/// This URL can be used to push or present the `UserViewController` by providing URLs:
///
Expand Down Expand Up @@ -108,9 +108,9 @@ public class URLNavigator {
///
/// For example:
///
/// let (URLPattern, values) = URLNavigator.matchURL("myapp://user/123", from: ["myapp://user/{id}"])
/// let (URLPattern, values) = URLNavigator.matchURL("myapp://user/123", from: ["myapp://user/<id>"])
///
/// The value of the `URLPattern` from an example above is `"myapp://user/{id}"` and the value of the `values` is
/// The value of the `URLPattern` from an example above is `"myapp://user/<id>"` and the value of the `values` is
/// `["id": "123"]`.
///
/// - Parameter URL: The placeholder-filled URL.
Expand All @@ -122,17 +122,17 @@ public class URLNavigator {
let URLPathComponents = URLNavigator.normalizedURL(URL).URLString.componentsSeparatedByString("/")

outer: for URLPattern in URLPatterns {
// e.g. ["myapp:", "user", "{id}"]
// e.g. ["myapp:", "user", "<id>"]
let URLPatternPathComponents = URLPattern.componentsSeparatedByString("/")
if URLPatternPathComponents.count != URLPathComponents.count {
continue
}

var values = [String: AnyObject]()

// e.g. ["user", "{id}"]
// e.g. ["user", "<id>"]
for (i, component) in URLPatternPathComponents.enumerate() {
if component.hasPrefix("{") && component.hasSuffix("}") { // e.g. "{id}"
if component.hasPrefix("<") && component.hasSuffix(">") { // e.g. "<id>"
let start = component.startIndex.advancedBy(1)
let end = component.endIndex.advancedBy(-1)
let placeholder = component[start..<end] // e.g. "id"
Expand Down
22 changes: 11 additions & 11 deletions Tests/URLNavigatorInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class URLNavigatorInternalTests: XCTestCase {
XCTAssertNil(URLNavigator.matchURL("myapp://user/1", from: []))
}();
{
XCTAssertNil(URLNavigator.matchURL("myapp://user/1", from: ["myapp://comment/{id}"]))
XCTAssertNil(URLNavigator.matchURL("myapp://user/1", from: ["myapp://comment/<id>"]))
}();
{
XCTAssertNil(URLNavigator.matchURL("myapp://user/1", from: ["myapp://user/{id}/hello"]))
XCTAssertNil(URLNavigator.matchURL("myapp://user/1", from: ["myapp://user/<id>/hello"]))
}();
{
let from = ["myapp://hello"]
Expand All @@ -42,29 +42,29 @@ class URLNavigatorInternalTests: XCTestCase {
XCTAssertEqual(values.count, 0)
}();
{
let from = ["myapp://user/{id}"]
let from = ["myapp://user/<id>"]
let (URLPattern, values) = URLNavigator.matchURL("myapp://user/1", from: from)!
XCTAssertEqual(URLPattern, "myapp://user/{id}")
XCTAssertEqual(URLPattern, "myapp://user/<id>")
XCTAssertEqual(values as! [String: String], ["id": "1"])
}();
{
let from = ["myapp://user/{id}", "myapp://user/{id}/hello"]
let from = ["myapp://user/<id>", "myapp://user/<id>/hello"]
let (URLPattern, values) = URLNavigator.matchURL("myapp://user/1", from: from)!
XCTAssertEqual(URLPattern, "myapp://user/{id}")
XCTAssertEqual(URLPattern, "myapp://user/<id>")
XCTAssertEqual(values as! [String: String], ["id": "1"])
}();
{
let from = ["myapp://user/{id}", "myapp://user/{id}/{object}"]
let from = ["myapp://user/<id>", "myapp://user/<id>/<object>"]
let (URLPattern, values) = URLNavigator.matchURL("myapp://user/1/posts", from: from)!
XCTAssertEqual(URLPattern, "myapp://user/{id}/{object}")
XCTAssertEqual(URLPattern, "myapp://user/<id>/<object>")
XCTAssertEqual(values as! [String: String], ["id": "1", "object": "posts"])
}();
}

func testNormalizedURL() {
XCTAssertEqual(URLNavigator.normalizedURL("myapp://user/{id}/hello").URLString, "myapp://user/{id}/hello")
XCTAssertEqual(URLNavigator.normalizedURL("myapp:///////user///{id}//hello/??/#abc=/def").URLString,
"myapp://user/{id}/hello")
XCTAssertEqual(URLNavigator.normalizedURL("myapp://user/<id>/hello").URLString, "myapp://user/<id>/hello")
XCTAssertEqual(URLNavigator.normalizedURL("myapp:///////user///<id>//hello/??/#abc=/def").URLString,
"myapp://user/<id>/hello")
}

func testReplaceRegex() {
Expand Down
10 changes: 5 additions & 5 deletions Tests/URLNavigatorPublicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class URLNavigatorPublicTests: XCTestCase {
}

func testViewControllerForURL() {
self.navigator.map("myapp://user/{id}", UserViewController.self)
self.navigator.map("myapp://post/{id}", PostViewController.self)
self.navigator.map("myapp://user/<id>", UserViewController.self)
self.navigator.map("myapp://post/<id>", PostViewController.self)

XCTAssertNil(self.navigator.viewControllerForURL("myapp://user/"))
XCTAssertNil(self.navigator.viewControllerForURL("myapp://user/awesome"))
Expand All @@ -49,7 +49,7 @@ class URLNavigatorPublicTests: XCTestCase {
}

func testPushURL_URLNavigable() {
self.navigator.map("myapp://user/{id}", UserViewController.self)
self.navigator.map("myapp://user/<id>", UserViewController.self)
let navigationController = UINavigationController(rootViewController: UIViewController())
let viewController = self.navigator.pushURL("myapp://user/1", from: navigationController, animated: false)
XCTAssertNotNil(viewController)
Expand All @@ -65,7 +65,7 @@ class URLNavigatorPublicTests: XCTestCase {
}

func testPresentURL_URLNavigable() {
self.navigator.map("myapp://user/{id}", UserViewController.self)
self.navigator.map("myapp://user/<id>", UserViewController.self)
;{
let fromViewController = UIViewController()
let viewController = self.navigator.presentURL("myapp://user/1", from: fromViewController)
Expand Down Expand Up @@ -98,7 +98,7 @@ class URLNavigatorPublicTests: XCTestCase {
}

func testOpenURL_URLNavigable() {
self.navigator.map("myapp://user/{id}", UserViewController.self)
self.navigator.map("myapp://user/<id>", UserViewController.self)
XCTAssertFalse(self.navigator.openURL("myapp://user/1"))
}

Expand Down

0 comments on commit 39774f1

Please sign in to comment.