-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathExtensions.swift
81 lines (60 loc) · 2.61 KB
/
Extensions.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Extensions.swift
// NoMAD
//
// Created by Boushy, Phillip on 10/4/16.
// Copyright © 2016 Orchard & Grove Inc. All rights reserved.
//
import Foundation
// bitwise convenience
prefix operator ~~
prefix func ~~(value: Int) -> Bool {
return (value > 0) ? true : false
}
extension UserDefaults {
func sint(forKey defaultName: String) -> Int? {
let defaults = UserDefaults.standard
let item = defaults.object(forKey: defaultName)
if item == nil {
return nil
}
// test to see if it's an Int
if let result = item as? Int {
return result
} else {
// it's a String!
return Int(item as! String)
}
}
}
extension String {
func safeURLPath() -> String? {
let allowedCharacters = CharacterSet(bitmapRepresentation: CharacterSet.urlPathAllowed.bitmapRepresentation)
return addingPercentEncoding(withAllowedCharacters: allowedCharacters)
}
func trim() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
func containsIgnoringCase(_ find: String) -> Bool {
return self.range(of: find, options: NSString.CompareOptions.caseInsensitive) != nil
}
/*
// TODO: move this to UserInfo
func variableSwap() -> String {
var cleanString = self
let domain = defaults.string(forKey: Preferences.aDDomain) ?? ""
let fullName = defaults.string(forKey: Preferences.displayName)?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
let serial = getSerial().addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
let shortName = defaults.string(forKey: Preferences.userShortName) ?? ""
let upn = defaults.string(forKey: Preferences.userUPN) ?? ""
let email = defaults.string(forKey: Preferences.userEmail) ?? ""
cleanString = cleanString.replacingOccurrences(of: "<<domain>>", with: domain)
cleanString = cleanString.replacingOccurrences(of: "<<fullname>>", with: fullName)
cleanString = cleanString.replacingOccurrences(of: "<<serial>>", with: serial)
cleanString = cleanString.replacingOccurrences(of: "<<shortname>>", with: shortName)
cleanString = cleanString.replacingOccurrences(of: "<<upn>>", with: upn)
cleanString = cleanString.replacingOccurrences(of: "<<email>>", with: email)
return cleanString //.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
}
*/
}