feat: add money validators and converters
This commit is contained in:
parent
6017cd29d9
commit
baed7cf0aa
|
@ -0,0 +1,26 @@
|
|||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Yhanco Grey Esteban on 6/5/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension String {
|
||||
|
||||
func prettyDecimal() -> String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .decimal
|
||||
return formatter.string(from: NSNumber(value: Double(self) ?? 0)) ?? "0"
|
||||
}
|
||||
|
||||
func plainDecimal() -> String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .decimal
|
||||
guard let number = formatter.number(from: self) else {
|
||||
return "0"
|
||||
}
|
||||
return "\(number.decimalValue)"
|
||||
}
|
||||
}
|
|
@ -3,10 +3,17 @@ import Foundation
|
|||
public extension String {
|
||||
|
||||
/// A Boolean value indicating whether the String is a valid email
|
||||
var isValidEmail: Bool {
|
||||
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
|
||||
let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
|
||||
return emailPred.evaluate(with: self)
|
||||
var isEmail: Bool {
|
||||
let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
|
||||
let pred = NSPredicate(format:"SELF MATCHES %@", regex)
|
||||
return pred.evaluate(with: self)
|
||||
}
|
||||
|
||||
/// A Boolean value indicating whether the String is a positive numeric value
|
||||
var isMoney: Bool {
|
||||
let regex = "[0-9,]*[.]{0,1}[0-9]*"
|
||||
let pred = NSPredicate(format:"SELF MATCHES %@", regex)
|
||||
return pred.evaluate(with: self)
|
||||
}
|
||||
|
||||
/// A Boolean value indicating whether the String has content other than whitespaces
|
||||
|
|
|
@ -15,12 +15,45 @@ final class StringValidatorTests: XCTestCase {
|
|||
XCTAssertTrue("a\n".isPresent) // with new line
|
||||
}
|
||||
|
||||
func testStringIsValidEmail() {
|
||||
XCTAssertTrue("test@test.com".isValidEmail)
|
||||
func testStringIsEmail() {
|
||||
XCTAssertTrue("test@test.com".isEmail)
|
||||
|
||||
XCTAssertFalse("test@test".isValidEmail)
|
||||
XCTAssertFalse("t@t".isValidEmail)
|
||||
XCTAssertFalse("test@.com".isValidEmail)
|
||||
XCTAssertFalse("test@test".isEmail)
|
||||
XCTAssertFalse("t@t".isEmail)
|
||||
XCTAssertFalse("test@.com".isEmail)
|
||||
}
|
||||
|
||||
func testStringIsMoney() {
|
||||
XCTAssertTrue("1000".isMoney)
|
||||
XCTAssertTrue("1000.0".isMoney)
|
||||
XCTAssertTrue("1000.00".isMoney)
|
||||
XCTAssertTrue("1,000".isMoney)
|
||||
XCTAssertTrue("1,000.0".isMoney)
|
||||
XCTAssertTrue("1,000.00".isMoney)
|
||||
XCTAssertTrue("1,000,000.00".isMoney)
|
||||
|
||||
XCTAssertFalse("1000.00,0".isMoney)
|
||||
XCTAssertFalse("1000.00.0".isMoney)
|
||||
}
|
||||
|
||||
func testPrettyDecimal() {
|
||||
XCTAssertEqual("1000".prettyDecimal(), "1,000")
|
||||
XCTAssertEqual("1000.0".prettyDecimal(), "1,000")
|
||||
XCTAssertEqual("1000.00".prettyDecimal(), "1,000")
|
||||
XCTAssertEqual("1000.01".prettyDecimal(), "1,000.01")
|
||||
XCTAssertEqual("10000.01".prettyDecimal(), "10,000.01")
|
||||
XCTAssertEqual("100000.01".prettyDecimal(), "100,000.01")
|
||||
XCTAssertEqual("1000000.01".prettyDecimal(), "1,000,000.01")
|
||||
}
|
||||
|
||||
func testPlainDecimal() {
|
||||
XCTAssertEqual("1,000".plainDecimal(), "1000")
|
||||
XCTAssertEqual("1,000.0".plainDecimal(), "1000")
|
||||
XCTAssertEqual("1,000.00".plainDecimal(), "1000")
|
||||
XCTAssertEqual("1,000.01".plainDecimal(), "1000.01")
|
||||
XCTAssertEqual("10,000.01".plainDecimal(), "10000.01")
|
||||
XCTAssertEqual("100,000.01".plainDecimal(), "100000.01")
|
||||
XCTAssertEqual("1,000,000.01".plainDecimal(), "1000000.01")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue