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.

158 lines
4.1 KiB

6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
4 years ago
  1. //
  2. // StringExtension.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 12/13/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. enum CurrencyType {
  11. case usd
  12. case krw
  13. case `nil`
  14. var front: String {
  15. switch self {
  16. case .usd: return "$ "
  17. case .krw: return "£ "
  18. case .nil: return ""
  19. }
  20. }
  21. var end: String {
  22. switch self {
  23. case .usd: return " USD"
  24. case .krw: return " GBP"
  25. case .nil: return ""
  26. }
  27. }
  28. }
  29. extension String {
  30. var imageURLInResource: String {
  31. switch self {
  32. case "001": return "ic_cap"
  33. case "002": return "ic_tshirt"
  34. case "003": return "ic_glass"
  35. case "004": return "ic_watch"
  36. default:
  37. return ""
  38. }
  39. }
  40. var isBlank: Bool {
  41. return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty
  42. }
  43. func toBase64() -> String {
  44. let data = self.data(using: String.Encoding.utf8)
  45. return data!.base64EncodedString()
  46. }
  47. func removeWhitespacesInBetween() -> String {
  48. return components(separatedBy: .whitespaces).joined()
  49. }
  50. func stringRemovingComma() -> String {
  51. return extract(regex: "[0-9.]")
  52. }
  53. func getDateFromDateTime() -> String {
  54. return components(separatedBy: .whitespaces)[0]
  55. }
  56. func removeSpacesTrailingPreceding() -> String {
  57. return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  58. }
  59. }
  60. extension String {
  61. func likeCommaMoney() -> String? {
  62. if Double(self) == nil {
  63. return nil
  64. }
  65. return Utility.getCommaSeperatedString(numberString: self)
  66. }
  67. func likeCommaMoneyWithDecimal() -> String? {
  68. if Double(self) == nil {
  69. return nil
  70. }
  71. return Utility.getCommaSeperatedStringWithDecimal(numberString: self)
  72. }
  73. func isEmail() -> Bool {
  74. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  75. let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
  76. return emailPred.evaluate(with: self)
  77. }
  78. func extract(regex: String) -> String {
  79. do {
  80. let regex = try NSRegularExpression(pattern: regex)
  81. let nsString = self as NSString
  82. let results = regex.matches(in: self, range: NSRange(location: 0, length: nsString.length))
  83. return results.map { nsString.substring(with: $0.range)}.joined()
  84. } catch let error {
  85. print("invalid regex: \(error.localizedDescription)")
  86. return self
  87. }
  88. }
  89. func getIntegersString() -> String {
  90. let text = self.extract(regex: "[0-9.]")
  91. if let endIndex = text.range(of: ".")?.lowerBound {
  92. return "\(text[..<endIndex])"
  93. } else {
  94. return self
  95. }
  96. }
  97. func decimalToCurrency(as type: CurrencyType = .nil, isFront: Bool = false) -> String {
  98. let text = self.extract(regex: "[0-9.]")
  99. guard let number = Double(text) else {
  100. return isFront ? "\(type.front)\(self)" : "\(self)\(type.end)"
  101. }
  102. let nsNumber = NSNumber(value: number)
  103. let currencyFormatter = NumberFormatter()
  104. currencyFormatter.locale = Locale(identifier: "en_US")
  105. currencyFormatter.numberStyle = .decimal
  106. let commaSeperatedNumberString = currencyFormatter.string(for: nsNumber)
  107. if isFront {
  108. return "\(type.front)\(commaSeperatedNumberString ?? self)"
  109. } else {
  110. return "\(commaSeperatedNumberString ?? self)\(type.end)"
  111. }
  112. }
  113. func currencyToDecimal() -> String {
  114. return self.extract(regex: "[0-9.]")
  115. }
  116. }
  117. extension String{
  118. func validateRegex(regex: String) -> Bool{
  119. let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
  120. return predicate.evaluate(with: self)
  121. }
  122. }
  123. extension String {
  124. func attributedText(color: UIColor, font: UIFont) -> NSMutableAttributedString {
  125. let range = (self as NSString).range(of: self)
  126. let attributedString = NSMutableAttributedString(string:self)
  127. attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
  128. attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: range)
  129. return attributedString
  130. }
  131. }