Code Monkey home page Code Monkey logo

Comments (3)

devxoul avatar devxoul commented on August 21, 2024

@steve228uk, What is the URL you want to handle?

from urlnavigator.

steve228uk avatar steve228uk commented on August 21, 2024

The URL is http://example.com/jun16/user-1234

The pattern would look something like this.

http://example.com/<month><int:year>/user-<int:id>

from urlnavigator.

devxoul avatar devxoul commented on August 21, 2024

@steve228uk, you can add your own URL value matcher. This is not yet documented.

struct User {
  init(id: Int) {}
}

func match(pattern: String, in string: String) -> [String]? {
  guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
  let range = NSRange(location: 0, length: string.characters.count)
  guard let match = regex.firstMatch(in: string, options: [], range: range) else { return nil }
  return (0..<match.numberOfRanges)
    .map { match.rangeAt($0) }
    .map { (string as NSString).substring(with: $0) }
}

URLMatcher.default.addURLValueMatcherHandler(for: "monthyear") { string -> Any? in
  guard let groups = match(pattern: "([a-z]{3})(\\d{2})", in: string) else { return nil }
  let month = groups[1]
  guard let year = Int(groups[2]) else { return nil }
  return (month: month, year: year)
}

URLMatcher.default.addURLValueMatcherHandler(for: "user") { string -> Any? in
  guard let groups = match(pattern: "user-(\\d+)", in: string) else { return nil }
  guard let userID = Int(groups[1]) else { return nil }
  return User(id: userID)
}

Navigator.map("http://example.com/<monthyear:monthyear>/<user:user>") { url, values in
  print("values:", values) // ["monthyear": ("jun", 16), "user": User()]
  return true
}

Navigator.open("http://example.com/jun16/user-1234")

from urlnavigator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.