Проверка шаблона шины для iOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
626B

  1. import Foundation
  2. public extension MeetupId {
  3. static func onlyAllowJoin(_ s: String) -> Bool? {
  4. s.hasPrefix("123")
  5. }
  6. static func onlyFormat(_ s: String) -> String? {
  7. let digits = s.components(separatedBy: NSCharacterSet.decimalDigits.inverted).reduce("") { $0 + $1 }
  8. var r = ""
  9. var i = 0
  10. // Делим каждые три цифры дефисом.
  11. for v in digits {
  12. r += String(v)
  13. i = i + 1
  14. if i % 3 == 0 {
  15. r += "-"
  16. }
  17. }
  18. // Исключаем дефис в конце.
  19. if r.hasSuffix("-") {
  20. r = String(r.dropLast(1))
  21. }
  22. return r
  23. }
  24. }