Skip to content

Commit

Permalink
Readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSativa committed Apr 11, 2024
1 parent 0493c75 commit 6ef3a49
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 51 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,34 @@
Swift library that provides a set of useful text formatting and validation tools that can be useful in your project.
'SmartTextField' is wrapping UITextField to use that powerfull tools and make it easy to use.
It handles text formatting, validation, error messages and correct position of cursor when formatting text.
Each UITextField events can be handled by 'Smart' closures in 'SmartTextField.Eventier'.
UITextField events can be handled by 'Smart' closures in 'SmartTextField.Eventier'.

```swift
let email = SmartTextField()
let emailConfig: SmartTextField.Configuration
emailConfig = .init(placeholder: "Email",
textFormatter: [
.email,
.stripLeadingAndTrailingSpaces
],
textValidator: [
.notEmpty(errorText: "Email is required"),
.email(errorText: "Invalid email format")
],
textContentType: .emailAddress,
keyboardType: .emailAddress)
email.configure(with: emailConfig)

let password = SmartTextField()
let passwordConfig: SmartTextField.Configuration
passwordConfig = .init(placeholder: "Password",
textFormatter: .stripLeadingAndTrailingSpaces,
textValidator: [
.notEmpty(errorText: "Password is required"),
.includesLowerAndUppercase(errorText: "Password must contain lower and uppercase characters"),
.minLengthLimited(8, errorText: "Password must be at least 8 characters long")
],
textContentType: .emailAddress,
keyboardType: .emailAddress)
password.configure(with: passwordConfig)
```
14 changes: 7 additions & 7 deletions Source/Formatter/TextFormatter.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import Foundation

infix operator +: AdditionPrecedence
public class TextFormatter: Equatable, ExpressibleByArrayLiteral {
public final class TextFormatter: Equatable, ExpressibleByArrayLiteral {
private let formatters: [TextFormatable]

public required init(arrayLiteral: TextFormatable...) {
self.formatters = arrayLiteral
}

public required init(_ formatables: [TextFormatable]) {
self.formatters = formatables
}
Expand All @@ -17,7 +12,12 @@ public class TextFormatter: Equatable, ExpressibleByArrayLiteral {
}

public convenience init(formatters: [TextFormatter]) {
let formatables = formatters.reduce([]) { $0 + $1.formatters }
let formatables = formatters.flatMap(\.formatters)
self.init(formatables)
}

public convenience init(arrayLiteral formatters: TextFormatter...) {
let formatables = formatters.flatMap(\.formatters)
self.init(formatables)
}

Expand Down
68 changes: 34 additions & 34 deletions Source/SmartTextField.State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ public extension SmartTextField {
}

struct DatePicker: Equatable {
let calendar: Calendar
let dateFormatter: Foundation.DateFormatter
let minDate: Date
let selecatedDate: Date?
let maxDate: Date
let mode: UIDatePicker.Mode
let eventier: SmartTextField.Eventier?
public let calendar: Calendar
public let dateFormatter: Foundation.DateFormatter
public let minDate: Date
public let selecatedDate: Date?
public let maxDate: Date
public let mode: UIDatePicker.Mode
public let eventier: SmartTextField.Eventier?

init(calendar: Calendar = .current,
dateFormatter: Foundation.DateFormatter,
minDate: Date,
selecatedDate: Date?,
maxDate: Date,
mode: UIDatePicker.Mode = .date,
eventier: SmartTextField.Eventier? = nil) {
public init(calendar: Calendar = .current,
dateFormatter: Foundation.DateFormatter,
minDate: Date = .init(),
selecatedDate: Date?,
maxDate: Date,
mode: UIDatePicker.Mode = .date,
eventier: SmartTextField.Eventier? = nil) {
self.calendar = calendar
self.dateFormatter = dateFormatter
self.minDate = minDate
Expand All @@ -34,29 +34,29 @@ public extension SmartTextField {
}

struct Configuration: Equatable {
let placeholder: String
let textFormatter: TextFormatter
let textValidator: TextValidator
public let placeholder: String?
public let textFormatter: TextFormatter
public let textValidator: TextValidator

let textContentType: UITextContentType?
let isSecureTextEntry: Bool
public let textContentType: UITextContentType?
public let isSecureTextEntry: Bool

let keyboardType: UIKeyboardType
let returnKeyType: UIReturnKeyType
let autocapitalizationType: UITextAutocapitalizationType
let autocorrectionType: UITextAutocorrectionType
let clearButtonMode: UITextField.ViewMode
public let keyboardType: UIKeyboardType
public let returnKeyType: UIReturnKeyType
public let autocapitalizationType: UITextAutocapitalizationType
public let autocorrectionType: UITextAutocorrectionType
public let clearButtonMode: UITextField.ViewMode

init(placeholder: String,
textFormatter: TextFormatter = .identity,
textValidator: TextValidator = .identity,
textContentType: UITextContentType? = nil,
isSecureTextEntry: Bool = false,
keyboardType: UIKeyboardType = .default,
returnKeyType: UIReturnKeyType = .default,
autocapitalizationType: UITextAutocapitalizationType = .none,
autocorrectionType: UITextAutocorrectionType = .no,
clearButtonMode: UITextField.ViewMode = .never) {
public init(placeholder: String? = nil,
textFormatter: TextFormatter = .identity,
textValidator: TextValidator = .identity,
textContentType: UITextContentType? = nil,
isSecureTextEntry: Bool = false,
keyboardType: UIKeyboardType = .default,
returnKeyType: UIReturnKeyType = .default,
autocapitalizationType: UITextAutocapitalizationType = .none,
autocorrectionType: UITextAutocorrectionType = .no,
clearButtonMode: UITextField.ViewMode = .never) {
self.placeholder = placeholder
self.textFormatter = textFormatter
self.textValidator = textValidator
Expand Down
11 changes: 6 additions & 5 deletions Source/Validator/TextValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ public final class TextValidator: Equatable, ExpressibleByArrayLiteral {

private let validators: [TextValidatable]

public required init(arrayLiteral elements: TextValidatable...) {
self.validators = elements
}

public required init(_ validatables: [TextValidatable]) {
self.validators = validatables
}
Expand All @@ -18,7 +14,12 @@ public final class TextValidator: Equatable, ExpressibleByArrayLiteral {
}

public convenience init(validators: [TextValidator]) {
let validatables = validators.reduce([]) { $0 + $1.validators }
let validatables = validators.flatMap(\.validators)
self.init(validatables)
}

public convenience init(arrayLiteral validators: TextValidator...) {
let validatables = validators.flatMap(\.validators)
self.init(validatables)
}

Expand Down
11 changes: 7 additions & 4 deletions Tests/TextValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ final class TextValidatorTests: XCTestCase {
let mock1 = TestValidator(uniqueID: "1")
let mock2 = TestValidator(uniqueID: "2")

applyTest(subject: .init(arrayLiteral: mock1, mock2), [mock1, mock2])
applyTest(subject: [mock1, mock2], [mock1, mock2])
let mockV1 = TextValidator(mock1)
let mockV2 = TextValidator(mock2)

applyTest(subject: .init(arrayLiteral: mockV1, mockV2), [mock1, mock2])
applyTest(subject: [mockV1, mockV2], [mock1, mock2])
applyTest(subject: .init([mock1, mock2]), [mock1, mock2])
applyTest(subject: .init(mock1), [mock1])
applyTest(subject: .init(validators: [TextValidator(mock1), TextValidator(mock2)]), [mock1, mock2])
Expand All @@ -59,7 +62,7 @@ final class TextValidatorTests: XCTestCase {
applyTest(subject: mock1 + TextValidator(mock2), [mock1, mock2])
applyTest(subject: mock1 + TextValidator(mock2) + mock1, [mock1, mock2])
applyTest(subject: TextValidator(mock2) + mock1 + mock2, [mock1, mock2])
applyTest(subject: mock1 + [mock2, mock1], [mock1, mock2])
applyTest(subject: TextValidator(mock2) + [mock1, mock2], [mock1, mock2])
applyTest(subject: mock1 + [mockV2, mockV1], [mock1, mock2])
applyTest(subject: TextValidator(mock2) + [mockV1, mockV2], [mock1, mock2])
}
}

0 comments on commit 6ef3a49

Please sign in to comment.