Latest 1.0.1
Homepage https://github.com/gjeck/PathTemplate.swift
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0, requires ARC
Authors

Build Status
codecov

Swift implementation of templated paths. Inspired by path-to-regexp and URITemplate.swift.

Installation

Swift Package Manager:

Append the following to your Package.swift dependencies: []

.package(url: "https://github.com/gjeck/PathTemplate.swift.git")

Cocoapods

Add the following to your Podfile

pod 'PathTemplate'

Basic Usage

A PathTemplate is useful for working on structured String data like URLs.

Expanding a path template
let template: PathTemplate = "/user/:id"
template.expand(["id": 123])
=> "/user/123"
Determine the parameters in a template
let template: PathTemplate = "https://api.github.com/repos/:owner/:repo/"
template.parameterNames
=> ["owner", "repo"]
Extract the parameters used in a given path
let template: PathTemplate = "/artist/:artistId/album/:albumId"
template.extract("/artist/123/album/456")
=> ["artistId": "123", "albumId": "456"]

Parameter Modifiers

Named parameters are defined by prefixing a colon to a segment of word characters [A-Za-z0-9_] like :user.
Additional modifiers can be suffixed for different meaning:

  1. ? the parameter is optional
  2. * zero or more segments
  3. + one or more segments
? Optional
let template: PathTemplate = "https://:hostname/:path?"
template.expand(["hostname": "github.com"])
=> "https://github.com"

template.expand(["hostname": "github.com", "path": "user"])
=> "https://github.com/user"
* Zero or More
let template: PathTemplate = "https://:hostname/:path*"
template.expand(["hostname": "github.com", "path": ["user", "gjeck"]])
=> "https://github.com/user/gjeck"

template.expand(["hostname": "github.com"])
=> "https://github.com"
+ One or More
let template: PathTemplate = "https://:hostname/:path+"
template.expand(["hostname": "github.com", "path": ["user", "gjeck"]])
=> "https://github.com/user/gjeck"

template.expand(["hostname": "github.com"])
=> nil

Regular Expression Parameters

Parameters can be provided a custom regular expression that overrides the default match. For example, you could enforce matching digits in a path.

let template: PathTemplate = "/image-:imageId(\d+).png"
template.expand(["imageId": 123])
=> "/image-123.png"

template.expand(["imageId": "abc"])
=> nil

Unnamed Parameters

It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed.

let template: PathTemplate = "/cool/(\d+)/(.*)"
template.parameterNames
=> ["0", "1"]

template.expand(["0": 123, "1": "wow"])
=> "/cool/123/wow"

Advanced Usage

Enforcing case sensitive matching

let template = PathTemplate("/User/:id", options: Options(isCaseSensitive: true))
template.extract("/user/123") // Note that "user" has a lowercase "u"
=> [] 

Accessing the underlying path regular expression

let template: PathTemplate = "/user/:id"
template.regex
=> <NSRegularExpression: 0x102745860> ^/user/([^/]+?)(?:/)?$

Latest podspec

{
    "name": "PathTemplate",
    "version": "1.0.1",
    "summary": "Swift library for turning path strings like `/user/:id` into regular expressions",
    "homepage": "https://github.com/gjeck/PathTemplate.swift",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Greg Jeckell": ""
    },
    "social_media_url": "https://twitter.com/GJeckell",
    "source": {
        "git": "https://github.com/gjeck/PathTemplate.swift.git",
        "tag": "1.0.1"
    },
    "source_files": "Sources/**/*.{h,swift}",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "swift_version": "4.0"
}

Pin It on Pinterest

Share This