.../Source/Extensions/String+dashed.swift
Line | Count | Source |
1 | | // |
2 | | // String+dashed.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by jason on 13/9/23. |
6 | | // Copyright © 2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Foundation |
10 | | |
11 | | extension String { |
12 | 102 | var dashed: String { |
13 | 102 | var copy = self |
14 | 102 | var result = "" |
15 | 102 | |
16 | 2.10k | if self.contains(where: { $0.isWhitespace }) { |
17 | 2 | copy = self.lowercased() |
18 | 102 | } |
19 | 102 | |
20 | 2.12k | for (index, character) in copy.enumerated() { |
21 | 2.12k | if character.isWhitespace { |
22 | 7 | if index > 0 { |
23 | 7 | result += "-" |
24 | 7 | } |
25 | 2.12k | } else if character.isUppercase { |
26 | 301 | if index > 0 { |
27 | 300 | result += "-" |
28 | 301 | } |
29 | 301 | result += String(character).lowercased() |
30 | 2.12k | } else { |
31 | 1.82k | result += String(character) |
32 | 2.12k | } |
33 | 2.12k | } |
34 | 102 | return result |
35 | 102 | } |
36 | | } |