Skip to content

Commit

Permalink
Merge pull request #2 from Inquisitor0/develop
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
luximetr authored Nov 29, 2017
2 parents a4085ac + aee1c05 commit 6438084
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 25 additions & 2 deletions Source/TextFormatter/SumFormatters/SumTextFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ open class SumTextFormatter: TextFormatterProtocol {
for i in 0..<reversLenght {
guard let currentChar = reversString.characterAt(i) else { continue }
result.append(currentChar)
if i % numberOfCharactersInGroup == (numberOfCharactersInGroup - 1), i != 0, i != reversLenght - 1 {
if i % numberOfCharactersInGroup == (numberOfCharactersInGroup - 1), i != reversLenght - 1 {
result.append(groupingSeparator)
}
}
Expand All @@ -167,9 +167,11 @@ open class SumTextFormatter: TextFormatterProtocol {

open func formattedText(from unformatted: String?) -> String? {

guard let unformattedString = unformatted else { return nil }
guard var unformattedString = unformatted else { return nil }
guard !(unformattedString.isEmpty) else { return "\(prefixStr ?? "")\(suffixStr ?? "")" }

unformattedString = correctedIntegers(from: unformattedString)

var resultString = ""
var hasIncompletedDecimal = false
var isSpecialFormat = false // Decimal separator other than '.'
Expand Down Expand Up @@ -239,4 +241,25 @@ open class SumTextFormatter: TextFormatterProtocol {
return string.replacingOccurrences(of: ".", with: decimalSeparator)
}
}

private func correctedIntegers(from string: String) -> String {
var decimalPart = ""
var integerPart = ""

if string.contains(decimalSeparator) {
guard let position = string.index(of: decimalSeparator.first ?? ".") else { return string }
decimalPart = String(string[position..<string.endIndex])
integerPart = String(string[string.startIndex..<position])
} else {
integerPart = string
}

if integerPart.count > maximumIntegerCharacters {
let diff = integerPart.count - maximumIntegerCharacters
integerPart.removeLast(diff)
}

let result = integerPart + decimalPart
return result
}
}
8 changes: 4 additions & 4 deletions Tests/SumFormatBoundaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SumFormatBoundaryTests: XCTestCase {
func testGreaterThanMaxIntegerCharacters() {
sumFormatter.maximumIntegerCharacters = 5
let initialString = "123456"
let expectedString = "12345"
let expectedString = "12.345"
let formattedString = sumFormatter.formattedText(from: initialString)
XCTAssert(expectedString == formattedString,
"\(String(describing: formattedString)) must be equal to \(expectedString)")
Expand All @@ -36,7 +36,7 @@ class SumFormatBoundaryTests: XCTestCase {
func testEqualThanMaxIntegerCharacters() {
sumFormatter.maximumIntegerCharacters = 5
let initialString = "12345"
let expectedString = "12345"
let expectedString = "12.345"
let formattedString = sumFormatter.formattedText(from: initialString)
XCTAssert(expectedString == formattedString,
"\(String(describing: formattedString)) must be equal to \(expectedString)")
Expand All @@ -45,7 +45,7 @@ class SumFormatBoundaryTests: XCTestCase {
func testLessThanMaxIntegerCharacters() {
sumFormatter.maximumIntegerCharacters = 5
let initialString = "1234"
let expectedString = "1234"
let expectedString = "1.234"
let formattedString = sumFormatter.formattedText(from: initialString)
XCTAssert(expectedString == formattedString,
"\(String(describing: formattedString)) must be equal to \(expectedString)")
Expand Down Expand Up @@ -93,7 +93,7 @@ class SumFormatBoundaryTests: XCTestCase {
func testMoreThat14MaxIntegerCharacters() {
sumFormatter.maximumIntegerCharacters = 14
let initialString = String(repeating: "9", count: 15) // 999999999999999
let expectedString = "999.999.999.999.999"
let expectedString = "99.999.999.999.999"
let formattedString = sumFormatter.formattedText(from: initialString)
XCTAssert(expectedString == formattedString,
"\(String(describing: formattedString)) must be equal to \(expectedString)")
Expand Down

0 comments on commit 6438084

Please sign in to comment.