feat: add money validators and converters

This commit is contained in:
greyder 2022-06-05 12:41:28 +08:00
parent 6017cd29d9
commit baed7cf0aa
3 changed files with 75 additions and 9 deletions

View File

@ -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)"
}
}

View File

@ -3,10 +3,17 @@ import Foundation
public extension String { public extension String {
/// A Boolean value indicating whether the String is a valid email /// A Boolean value indicating whether the String is a valid email
var isValidEmail: Bool { var isEmail: Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx) let pred = NSPredicate(format:"SELF MATCHES %@", regex)
return emailPred.evaluate(with: self) 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 /// A Boolean value indicating whether the String has content other than whitespaces

View File

@ -15,12 +15,45 @@ final class StringValidatorTests: XCTestCase {
XCTAssertTrue("a\n".isPresent) // with new line XCTAssertTrue("a\n".isPresent) // with new line
} }
func testStringIsValidEmail() { func testStringIsEmail() {
XCTAssertTrue("test@test.com".isValidEmail) XCTAssertTrue("test@test.com".isEmail)
XCTAssertFalse("test@test".isValidEmail) XCTAssertFalse("test@test".isEmail)
XCTAssertFalse("t@t".isValidEmail) XCTAssertFalse("t@t".isEmail)
XCTAssertFalse("test@.com".isValidEmail) 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")
} }
} }