Skip to content

Commit

Permalink
Fixed sum formatting with 2 symbol suffix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Orlov committed Jun 20, 2019
1 parent 26caf82 commit e4e30df
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 9 deletions.
62 changes: 59 additions & 3 deletions AnyFormatKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
30EB6C9D22BB3B3800447D5A /* SumTextInputFormatterCaretPositionCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6C9C22BB3B3800447D5A /* SumTextInputFormatterCaretPositionCalculator.swift */; };
30EB6CA222BB484D00447D5A /* SumTextInputFormatterWith2SymbolsPrefixBy1SymbolErasingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6CA122BB484D00447D5A /* SumTextInputFormatterWith2SymbolsPrefixBy1SymbolErasingTests.swift */; };
30EB6CA422BB49D400447D5A /* SumTextInputFormatterWith2SymbolsPrefixDelete3SymbolsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6CA322BB49D400447D5A /* SumTextInputFormatterWith2SymbolsPrefixDelete3SymbolsTests.swift */; };
30EB6CA922BB608F00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6CA822BB608F00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolInputTests.swift */; };
30EB6CAD22BB61F600447D5A /* SumTextInputFormatterWith2SymbolsSuffixInsert2SymbolsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6CAC22BB61F600447D5A /* SumTextInputFormatterWith2SymbolsSuffixInsert2SymbolsTests.swift */; };
30EB6CAF22BB651E00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6CAE22BB651E00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift */; };
30EB6CB122BB69A000447D5A /* SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30EB6CB022BB69A000447D5A /* SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift */; };
30FE6FE21FAA13D30024D1B1 /* AnyFormatKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 30FE6FD81FAA13D30024D1B1 /* AnyFormatKit.framework */; };
30FE6FE71FAA13D30024D1B1 /* AnyFormatKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30FE6FE61FAA13D30024D1B1 /* AnyFormatKitTests.swift */; };
30FE70541FAA16890024D1B1 /* AnyFormatKit.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 30FE70531FAA16890024D1B1 /* AnyFormatKit.podspec */; };
Expand Down Expand Up @@ -109,6 +113,10 @@
30EB6C9C22BB3B3800447D5A /* SumTextInputFormatterCaretPositionCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterCaretPositionCalculator.swift; sourceTree = "<group>"; };
30EB6CA122BB484D00447D5A /* SumTextInputFormatterWith2SymbolsPrefixBy1SymbolErasingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterWith2SymbolsPrefixBy1SymbolErasingTests.swift; sourceTree = "<group>"; };
30EB6CA322BB49D400447D5A /* SumTextInputFormatterWith2SymbolsPrefixDelete3SymbolsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterWith2SymbolsPrefixDelete3SymbolsTests.swift; sourceTree = "<group>"; };
30EB6CA822BB608F00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolInputTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterWith2SymbolsSuffixBy1SymbolInputTests.swift; sourceTree = "<group>"; };
30EB6CAC22BB61F600447D5A /* SumTextInputFormatterWith2SymbolsSuffixInsert2SymbolsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterWith2SymbolsSuffixInsert2SymbolsTests.swift; sourceTree = "<group>"; };
30EB6CAE22BB651E00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift; sourceTree = "<group>"; };
30EB6CB022BB69A000447D5A /* SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift; sourceTree = "<group>"; };
30FE6FD81FAA13D30024D1B1 /* AnyFormatKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AnyFormatKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
30FE6FE11FAA13D30024D1B1 /* AnyFormatKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AnyFormatKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
30FE6FE61FAA13D30024D1B1 /* AnyFormatKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyFormatKitTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,9 +217,8 @@
307F0F3F22B0355E00C52AD2 /* WithSuffix */ = {
isa = PBXGroup;
children = (
30A0496622B52AC0001F6282 /* Insert */,
30A0496522B52ABA001F6282 /* Delete */,
30A0496422B52AAD001F6282 /* Replace */,
30EB6CA522BB602A00447D5A /* 1SymbolSuffix */,
30EB6CA622BB603A00447D5A /* 2SymbolSuffix */,
);
path = WithSuffix;
sourceTree = "<group>";
Expand Down Expand Up @@ -381,6 +388,51 @@
path = 1SymbolPrefix;
sourceTree = "<group>";
};
30EB6CA522BB602A00447D5A /* 1SymbolSuffix */ = {
isa = PBXGroup;
children = (
30A0496622B52AC0001F6282 /* Insert */,
30A0496522B52ABA001F6282 /* Delete */,
30A0496422B52AAD001F6282 /* Replace */,
);
path = 1SymbolSuffix;
sourceTree = "<group>";
};
30EB6CA622BB603A00447D5A /* 2SymbolSuffix */ = {
isa = PBXGroup;
children = (
30EB6CA722BB605500447D5A /* Insert */,
30EB6CAB22BB613C00447D5A /* Delete */,
30EB6CAA22BB613600447D5A /* Replace */,
);
path = 2SymbolSuffix;
sourceTree = "<group>";
};
30EB6CA722BB605500447D5A /* Insert */ = {
isa = PBXGroup;
children = (
30EB6CA822BB608F00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolInputTests.swift */,
30EB6CAC22BB61F600447D5A /* SumTextInputFormatterWith2SymbolsSuffixInsert2SymbolsTests.swift */,
);
path = Insert;
sourceTree = "<group>";
};
30EB6CAA22BB613600447D5A /* Replace */ = {
isa = PBXGroup;
children = (
);
path = Replace;
sourceTree = "<group>";
};
30EB6CAB22BB613C00447D5A /* Delete */ = {
isa = PBXGroup;
children = (
30EB6CAE22BB651E00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift */,
30EB6CB022BB69A000447D5A /* SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift */,
);
path = Delete;
sourceTree = "<group>";
};
30FE6FCE1FAA13D30024D1B1 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -619,10 +671,12 @@
30D1A4CA22AEF9770075764E /* SumTextInputFormatterWithSuffixInputTests.swift in Sources */,
309BD5B322B2BBF400BA69EF /* SumTextInputFormatterWithSuffixInsert2SymbolsTests.swift in Sources */,
30A0495922B51963001F6282 /* SumTextInputFormatterWithPrefixInputTests.swift in Sources */,
30EB6CAF22BB651E00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift in Sources */,
307F0F4222B0373E00C52AD2 /* SumTextInputFormatterWithSuffixDelete2SymbolsTests.swift in Sources */,
30FF731522ADA7A4005FBC38 /* DefaultTextInputFormatter2SymbolsReplaceTests.swift in Sources */,
307F0F4622B03CBC00C52AD2 /* SumTextInputFormatterWithSuffixReplace1SymbolTests.swift in Sources */,
307F0F4822B0405A00C52AD2 /* SumTextInputFormatterWithSuffixReplace2SymbolsTests.swift in Sources */,
30EB6CB122BB69A000447D5A /* SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift in Sources */,
30FF731322ADA53D005FBC38 /* DefaultTextInputFormatter1SymbolReplaceTests.swift in Sources */,
30A0495D22B5240E001F6282 /* SumTextInputFormatterWithPrefixBy1SymbolErasingTests.swift in Sources */,
306EBE9F1FC4669100E49BB8 /* FormatParsingTests.swift in Sources */,
Expand All @@ -637,6 +691,8 @@
30EB6C9B22BB39BA00447D5A /* SumTextInputFormatterWith2SymbolsPrefixInputTests.swift in Sources */,
30824C1122B176F2008E1E4E /* SumTextInputFormatterWithSuffixReplaceWith0Tests.swift in Sources */,
30A0497322B532FC001F6282 /* SumTextInputFormatterInsert3Symbols.swift in Sources */,
30EB6CA922BB608F00447D5A /* SumTextInputFormatterWith2SymbolsSuffixBy1SymbolInputTests.swift in Sources */,
30EB6CAD22BB61F600447D5A /* SumTextInputFormatterWith2SymbolsSuffixInsert2SymbolsTests.swift in Sources */,
30EB6CA222BB484D00447D5A /* SumTextInputFormatterWith2SymbolsPrefixBy1SymbolErasingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
11 changes: 9 additions & 2 deletions Example/iOS Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ class ViewController: UIViewController {
let cardNumberView = UITextView(frame: LayoutConstants.textInputViewFrame)
let sumInputField = UITextField(frame: LayoutConstants.sumTextInputFieldFrame)

let phoneNumberFormatter = DefaultTextInputFormatter(textPattern: "### (###) ###-##-##")
let phoneNumberFormatter = DefaultTextInputFormatter(textPattern: "+38 (###) ###-##-##")
let cardNumberFormatter = DefaultTextInputFormatter(textPattern: "XX XX XXXX XXXX", patternSymbol: "X")
let sumFormatter = SumTextInputFormatter(textPattern: "#.###,#$")
let sumFormatter = SumTextInputFormatter(textPattern: "#.###,## $")

// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
initConfigure()

let numberFormatter = NumberFormatter()
numberFormatter.groupingSize = 4
let formatter = SumTextInputFormatter(numberFormatter: numberFormatter)
let result = formatter.formatInput(currentText: "222", range: NSRange(location: 0, length: 0), replacementString: "1")
print(result)
print("")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ class SumTextInputFormatterCaretPositionCalculator {
private func findIndexOfNumberSymbol(numberOfSymbolsBefore: Int, newFormattedText: String) -> Int {
var numberSymbolsCount = 0
for (index, character) in newFormattedText.enumerated() {
if isDigit(character: character) || character == decimalSeparator.first! {
if isDigit(character: character) ||
character == decimalSeparator.first! ||
(!(suffix ?? "").isEmpty && (suffix ?? "").contains(character)) {
numberSymbolsCount += 1
}
if numberSymbolsCount >= numberOfSymbolsBefore {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class SumTextInputFormatterWith2SymbolsPrefixDelete3SymbolsTests: XCTestCase {
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// $| 12|,345.67 -> $ |345.67
// $| 12|,345.67 -> $| 345.67
func test2() {
let actualResult = formatter.formatInput(
currentText: "$ 12,345.67",
range: NSRange(location: 1, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "$ 345.67", caretBeginOffset: 2)
let expectedResult = FormattedTextValue(formattedText: "$ 345.67", caretBeginOffset: 1)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SumTextInputFormatterWithSuffixInputTests: XCTestCase {
private let formatter = SumTextInputFormatter(textPattern: "#,###.##$")

// "|" -> 1|$
func testIto1I$() { // #to_fix
func testIto1I$() {
let actualResult = formatter.formatInput(
currentText: "",
range: NSRange(location: 0, length: 0),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests.swift
// AnyFormatKitTests
//
// Created by branderstudio on 20.06.2019.
// Copyright © 2019 BRANDERSTUDIO. All rights reserved.
//

import XCTest
@testable import AnyFormatKit

class SumTextInputFormatterWith2SymbolsSuffixBy1SymbolErasingTests: XCTestCase {

private let formatter = SumTextInputFormatter(textPattern: "#,###.## $")

// 12,345.67 |$| -> 12,345.67 |$
func test1() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 10, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345.67 $", caretBeginOffset: 10)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345.67| |$ -> 12,345.67| $
func test2() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 9, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345.67 $", caretBeginOffset: 9)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345.6|7| $ -> 12,345.6| $
func test3() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 8, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345.6 $", caretBeginOffset: 8)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345.|6| $ -> 12,345.| $
func test4() {
let actualResult = formatter.formatInput(
currentText: "12,345.6 $",
range: NSRange(location: 7, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345. $", caretBeginOffset: 7)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345|.| $ -> 12,345| $
func test5() {
let actualResult = formatter.formatInput(
currentText: "12,345. $",
range: NSRange(location: 6, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345 $", caretBeginOffset: 6)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,34|5| $ -> 1,234| $
func test6() {
let actualResult = formatter.formatInput(
currentText: "12,345 $",
range: NSRange(location: 5, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "1,234 $", caretBeginOffset: 5)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 1,23|4| $ -> 123| $
func test7() {
let actualResult = formatter.formatInput(
currentText: "1,234 $",
range: NSRange(location: 4, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "123 $", caretBeginOffset: 3)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12|3| $ -> 12| $
func test8() {
let actualResult = formatter.formatInput(
currentText: "123 $",
range: NSRange(location: 2, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12 $", caretBeginOffset: 2)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 1|2| $ -> 1| $
func test9() {
let actualResult = formatter.formatInput(
currentText: "12 $",
range: NSRange(location: 1, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "1 $", caretBeginOffset: 1)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// |1| $ -> "| $"
func test10() {
let actualResult = formatter.formatInput(
currentText: "1 $",
range: NSRange(location: 0, length: 1),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: " $", caretBeginOffset: 0)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests.swift
// AnyFormatKitTests
//
// Created by branderstudio on 20.06.2019.
// Copyright © 2019 BRANDERSTUDIO. All rights reserved.
//

import XCTest
@testable import AnyFormatKit

class SumTextInputFormatterWith2SymbolsSuffixDelete3SymbolsTests: XCTestCase {

private let formatter = SumTextInputFormatter(textPattern: "#,###.## $")

// |12,|345.67 $ -> |345.67 $
func test1() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 0, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "345.67 $", caretBeginOffset: 0)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 1|2,3|45.67 $ -> 1|45.67 $
func test2() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 1, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "145.67 $", caretBeginOffset: 1)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12|,34|5.67 $ -> 12|5.67 $
func test3() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 2, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "125.67 $", caretBeginOffset: 2)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,|345|.67 $ -> 12|.67 $
func test4() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 3, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12.67 $", caretBeginOffset: 2)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,3|45.|67 $ -> 12,3|67 $
func test5() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 4, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,367 $", caretBeginOffset: 4)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,34|5.6|7 $ -> 12,34|7 $
func test6() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 5, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,347 $", caretBeginOffset: 5)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345|.67| $ -> 12,345| $
func test7() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 6, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345 $", caretBeginOffset: 6)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345.|67 |$ -> 12,345.| $
func test8() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 7, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345. $", caretBeginOffset: 7)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

// 12,345.6|7 $| -> 12,345.6| $
func test9() {
let actualResult = formatter.formatInput(
currentText: "12,345.67 $",
range: NSRange(location: 8, length: 3),
replacementString: "")
let expectedResult = FormattedTextValue(formattedText: "12,345.6 $", caretBeginOffset: 8)
XCTAssert(actualResult == expectedResult, "\n\(actualResult) must be equal to\n\(expectedResult)")
}

}
Loading

0 comments on commit e4e30df

Please sign in to comment.