Created
June 29, 2018 15:19
-
-
Save policante/9fcf3af921673c7266136d6ae0f8119d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// String+extensions.swift | |
// | |
// Created by Rodrigo Martins on 01/09/17. | |
// Copyright © 2017 Tinker Tecnologia. All rights reserved. | |
// | |
import Foundation | |
extension String { | |
var length : Int { | |
return self.characters.count | |
} | |
func digitsOnly() -> String{ | |
return String(characters.filter { "0"..."9" ~= $0 }) | |
} | |
func trim() -> String { | |
return self.trimmingCharacters(in: .whitespaces) | |
} | |
func isValidEmail() -> Bool { | |
let regex = try! NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .caseInsensitive) | |
return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: characters.count)) != nil | |
} | |
func currencyInputFormatting() -> String { | |
var number: NSNumber! | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .currencyAccounting | |
formatter.currencySymbol = "R$" | |
formatter.maximumFractionDigits = 2 | |
formatter.minimumFractionDigits = 2 | |
let amountWithPrefix = self.digitsOnly() | |
let double = (amountWithPrefix as NSString).doubleValue | |
number = NSNumber(value: (double / 100)) | |
// if first number is 0 or all numbers were deleted | |
guard number != 0 as NSNumber else { | |
return "" | |
} | |
return formatter.string(from: number)! | |
} | |
static let numberFormatter = NumberFormatter() | |
var doubleValue: Double { | |
var negative: Bool = false | |
if self.contains("-") { | |
negative = true | |
} | |
var digits = self.digitsOnly() | |
if digits.length > 2 { | |
digits = "\(digits.substring(to: digits.length - 2)),\(digits.substring(from: digits.length - 2))" | |
}else if digits.length == 2 { | |
digits = "\(digits.substring(to: digits.length - 1)),\(digits.substring(from: digits.length - 1))" | |
} | |
String.numberFormatter.decimalSeparator = "." | |
String.numberFormatter.groupingSeparator = "," | |
if let result = String.numberFormatter.number(from: digits) { | |
if negative { | |
return -result.doubleValue | |
}else{ | |
return result.doubleValue | |
} | |
} else { | |
String.numberFormatter.decimalSeparator = "," | |
String.numberFormatter.groupingSeparator = "." | |
if let result = String.numberFormatter.number(from: digits) { | |
if negative { | |
return -result.doubleValue | |
}else{ | |
return result.doubleValue | |
} | |
} | |
} | |
return 0 | |
} | |
var floatValue: Float { | |
return (self as NSString).floatValue | |
} | |
func index(from: Int) -> Index { | |
return self.index(startIndex, offsetBy: from) | |
} | |
func substring(from: Int) -> String { | |
let fromIndex = index(from: from) | |
return substring(from: fromIndex) | |
} | |
func substring(to: Int) -> String { | |
let toIndex = index(from: to) | |
return substring(to: toIndex) | |
} | |
func substring(with r: Range<Int>) -> String { | |
let startIndex = index(from: r.lowerBound) | |
let endIndex = index(from: r.upperBound) | |
return substring(with: startIndex..<endIndex) | |
} | |
func mask(withFormat format: String) -> String { | |
var maskedText = "" | |
var position: Int = 0 | |
for chr in format.characters { | |
if chr != "#" { | |
maskedText.append(chr) | |
continue | |
} | |
if position < self.length { | |
let digitsChr: Character = self[position] | |
maskedText.append(digitsChr) | |
}else{ | |
break | |
} | |
position += 1 | |
} | |
return maskedText | |
} | |
subscript (i: Int) -> Character { | |
return self[index(startIndex, offsetBy: i)] | |
} | |
subscript (i: Int) -> String { | |
return String(self[i] as Character) | |
} | |
subscript (r: Range<Int>) -> String { | |
let start = index(startIndex, offsetBy: r.lowerBound) | |
let end = index(startIndex, offsetBy: r.upperBound) | |
return self[Range(start ..< end)] | |
} | |
} | |
protocol StringProtocol { | |
var asString: String { get } | |
} | |
extension StringProtocol { | |
var asString: String { return self as! String } | |
} | |
extension String: StringProtocol { } | |
extension Optional where Wrapped : StringProtocol { | |
var isBlank: Bool { | |
if case let .some(value) = self { | |
return value.asString.isEmpty | |
} | |
return true | |
} | |
var strEmptyToNull: String? { | |
if case let .some(value) = self, !self.isBlank { | |
return value.asString | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment