-
Notifications
You must be signed in to change notification settings - Fork 301
/
String.swift
53 lines (42 loc) · 1.51 KB
/
String.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Foundation
extension String {
var ISO8601Date: Date? {
return DateFormatter.ISO8601.date(from: self)
}
/**
Check if the current string is empty. If the string is empty, `nil` is returned, otherwise, the string is returned.
*/
public var nonEmptyString: String? {
return isEmpty ? nil : self
}
var wholeRange: Range<String.Index> {
return startIndex..<endIndex
}
typealias Replacement = (of: String, with: String)
func byReplacing(_ replacements: [Replacement]) -> String {
return replacements.reduce(self) { $0.replacingOccurrences(of: $1.of, with: $1.with) }
}
var addingXMLEscapes: String {
return byReplacing([
("&", "&"),
("<", "<"),
("\"", """),
("'", "'")
])
}
var asSSMLAddress: String {
return "<say-as interpret-as=\"address\">\(self.addingXMLEscapes)</say-as>"
}
var asSSMLCharacters: String {
return "<say-as interpret-as=\"characters\">\(self.addingXMLEscapes)</say-as>"
}
func withSSMLPhoneme(ipaNotation: String) -> String {
return "<phoneme alphabet=\"ipa\" ph=\"\(ipaNotation.addingXMLEscapes)\">\(self.addingXMLEscapes)</phoneme>"
}
var isUppercased: Bool {
return self == uppercased() && self != lowercased()
}
var containsDecimalDigit: Bool {
return self.rangeOfCharacter(from: CharacterSet.decimalDigits) != nil
}
}