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.

151 lines
3.9 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 " JPY"
  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 isEmail() -> Bool {
  68. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  69. let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
  70. return emailPred.evaluate(with: self)
  71. }
  72. func extract(regex: String) -> String {
  73. do {
  74. let regex = try NSRegularExpression(pattern: regex)
  75. let nsString = self as NSString
  76. let results = regex.matches(in: self, range: NSRange(location: 0, length: nsString.length))
  77. return results.map { nsString.substring(with: $0.range)}.joined()
  78. } catch let error {
  79. print("invalid regex: \(error.localizedDescription)")
  80. return self
  81. }
  82. }
  83. func getIntegersString() -> String {
  84. let text = self.extract(regex: "[0-9.]")
  85. if let endIndex = text.range(of: ".")?.lowerBound {
  86. return "\(text[..<endIndex])"
  87. } else {
  88. return self
  89. }
  90. }
  91. func decimalToCurrency(as type: CurrencyType = .nil, isFront: Bool = false) -> String {
  92. let text = self.extract(regex: "[0-9.]")
  93. guard let number = Double(text) else {
  94. return isFront ? "\(type.front)\(self)" : "\(self)\(type.end)"
  95. }
  96. let nsNumber = NSNumber(value: number)
  97. let currencyFormatter = NumberFormatter()
  98. currencyFormatter.locale = Locale(identifier: "en_US")
  99. currencyFormatter.numberStyle = .decimal
  100. let commaSeperatedNumberString = currencyFormatter.string(for: nsNumber)
  101. if isFront {
  102. return "\(type.front)\(commaSeperatedNumberString ?? self)"
  103. } else {
  104. return "\(commaSeperatedNumberString ?? self)\(type.end)"
  105. }
  106. }
  107. func currencyToDecimal() -> String {
  108. return self.extract(regex: "[0-9.]")
  109. }
  110. }
  111. extension String{
  112. func validateRegex(regex: String) -> Bool{
  113. let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
  114. return predicate.evaluate(with: self)
  115. }
  116. }
  117. extension String {
  118. func attributedText(color: UIColor, font: UIFont) -> NSMutableAttributedString {
  119. let range = (self as NSString).range(of: self)
  120. let attributedString = NSMutableAttributedString(string:self)
  121. attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
  122. attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: range)
  123. return attributedString
  124. }
  125. }