Skip to content

A lightweight Swift path pattern matcher for URL-style routing — supports named parameters, optional segments, and splat wildcards.

License

Notifications You must be signed in to change notification settings

jaywcjlove/swift-path-to

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using my app is also a way to support me:
Keyzer Vidwall Hub VidCrop Vidwall Mousio Hint Mousio Musicer Audioer FileSentinel FocusCursor Videoer KeyClicker DayBar Iconed Mousio Quick RSS Quick RSS Web Serve Copybook Generator DevTutor for SwiftUI RegexMate Time Passage Iconize Folder Textsound Saver Create Custom Symbols DevHub Resume Revise Palette Genius Symbol Scribe

PathTo

A lightweight Swift path pattern matcher for URL-style routing — supports named parameters, optional segments, and splat wildcards.

Installation

Swift Package Manager

Add CodeMirror to your project using Xcode:

  1. In Xcode, go to FileAdd Package Dependencies...
  2. Enter the repository URL: https://github.com/jaywcjlove/swift-path-to.git
  3. Click Add Package

Or add it to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/jaywcjlove/swift-path-to.git", from: "1.0.0")
]

Features

  • Named parameters: :name captures a single path segment.
  • Optional segments: {/:id} marks a single segment as optional.
  • Splat/wildcard: *rest captures remaining segments into an array.

Usage

import PathTo

Parameters

Parameters match arbitrary strings in a path by matching up to the end of a segment or up to any subsequent tokens. They are defined by prefixing the parameter name with a colon (:foo).

let fn = PathTo.match("/:foo/:bar")

if let r = fn("/test/route") {
    // r -> params: ["foo": "test", "bar": "route"]
    print(r.path) // "/test/route"
    print(r.params["foo"] as? String) // "test"
    print(r.params["bar"] as? String) // "route"
}

Wildcard

Wildcard parameters match one or more characters across multiple segments. They are defined the same way as regular parameters, but are prefixed with an asterisk (*foo).

let fn = PathTo.match("/*splat")
if let r2 = fn("/bar/baz") {
    // r -> params: ["splat": ["bar", "baz"]]
    print(r2.params["splat"] as? [String]) // ["bar", "baz"]
}

Optional

Braces can be used to define parts of the path that are optional.

let fn = PathTo.match("/users{/:id}/delete")
print(fn("/users/delete")?.params) // [:]
print(fn("/users/123/delete")?.params) // ["id": "123"]

License

Licensed under the MIT License.

About

A lightweight Swift path pattern matcher for URL-style routing — supports named parameters, optional segments, and splat wildcards.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages