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.

123 lines
3.0 KiB

6 years ago
6 years ago
5 years ago
6 years ago
5 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. enum CurrencyType {
  10. case usd
  11. case krw
  12. case `nil`
  13. var front: String {
  14. switch self {
  15. case .usd: return "USD "
  16. case .krw: return "KRW "
  17. case .nil: return ""
  18. }
  19. }
  20. var end: String {
  21. switch self {
  22. case .usd: return " USD"
  23. case .krw: return " KRW"
  24. case .nil: return ""
  25. }
  26. }
  27. }
  28. extension String {
  29. var imageURLInResource: String {
  30. switch self {
  31. case "001": return "ic_cap"
  32. case "002": return "ic_tshirt"
  33. case "003": return "ic_glass"
  34. case "004": return "ic_watch"
  35. default:
  36. return ""
  37. }
  38. }
  39. var isBlank: Bool {
  40. return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty
  41. }
  42. func toBase64() -> String {
  43. let data = self.data(using: String.Encoding.utf8)
  44. return data!.base64EncodedString()
  45. }
  46. func removeWhitespacesInBetween() -> String {
  47. return components(separatedBy: .whitespaces).joined()
  48. }
  49. func stringRemovingComma() -> String {
  50. return extract(regex: "[0-9.]")
  51. }
  52. func getDateFromDateTime() -> String {
  53. return components(separatedBy: .whitespaces)[0]
  54. }
  55. func removeSpacesTrailingPreceding() -> String {
  56. return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  57. }
  58. }
  59. extension String {
  60. func likeCommaMoney() -> String? {
  61. if Double(self) == nil {
  62. return nil
  63. }
  64. return Utility.getCommaSeperatedString(numberString: self)
  65. }
  66. func isEmail() -> Bool {
  67. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  68. let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
  69. return emailPred.evaluate(with: self)
  70. }
  71. func extract(regex: String) -> String {
  72. do {
  73. let regex = try NSRegularExpression(pattern: regex)
  74. let nsString = self as NSString
  75. let results = regex.matches(in: self, range: NSRange(location: 0, length: nsString.length))
  76. return results.map { nsString.substring(with: $0.range)}.joined()
  77. } catch let error {
  78. print("invalid regex: \(error.localizedDescription)")
  79. return self
  80. }
  81. }
  82. func decimalToCurrency(as type: CurrencyType = .nil, isFront: Bool = false) -> String {
  83. let text = self.extract(regex: "[0-9.]")
  84. guard let number = Double(text) else {
  85. return isFront ? "\(type.front)\(self)" : "\(self)\(type.end)"
  86. }
  87. let nsNumber = NSNumber(value: number)
  88. let currencyFormatter = NumberFormatter()
  89. currencyFormatter.locale = Locale(identifier: "en_US")
  90. currencyFormatter.numberStyle = .decimal
  91. let commaSeperatedNumberString = currencyFormatter.string(for: nsNumber)
  92. if isFront {
  93. return "\(type.front)\(commaSeperatedNumberString ?? self)"
  94. } else {
  95. return "\(commaSeperatedNumberString ?? self)\(type.end)"
  96. }
  97. }
  98. func currencyToDecimal() -> String {
  99. return self.extract(regex: "[0-9.]")
  100. }
  101. }