-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.swift
57 lines (42 loc) · 1.43 KB
/
StringExtensions.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
54
55
56
57
//
// StringExtensions.swift
// Absinthe-iOS
//
// Created by Mitchell Kahn on 9/20/16.
// Copyright © 2016 Ourglass. All rights reserved.
//
import Foundation
extension String {
func blank() -> Bool {
let trimmed = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return trimmed.isEmpty
}
func isValidEmail() -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: self)
}
func isValidPwd() -> Bool {
let pwdRegEx = ".{8,}"
//let pwdRegEx = "^(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"
let passwordTest = NSPredicate(format:"SELF MATCHES %@",pwdRegEx)
let result = passwordTest.evaluate(with: self)
return result
}
// This is really shitty!
func munge() -> String {
var bytes = Array<UInt8>()
for c in self.utf8 {
bytes.append(c+1)
}
let munged = String(describing: bytes)
return munged
}
func trunc(_ length: Int, trailing: String? = "") -> String {
if self.characters.count > length {
return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) + (trailing ?? "")
} else {
return self
}
}
}