-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new error handling system and optimizations
- Loading branch information
Showing
67 changed files
with
785 additions
and
653 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
import Foundation | ||
|
||
public protocol TextFormatable { | ||
var uniqueID: String { get } | ||
func formatText(_ string: String) -> String | ||
func format(_ value: String) -> String | ||
} | ||
|
||
public extension TextFormatable { | ||
var uniqueID: String { | ||
makeUniqueID() | ||
} | ||
|
||
func makeUniqueID() -> String { | ||
String(describing: type(of: self)) | ||
} | ||
|
||
func toFormatter() -> TextFormatter { | ||
return .init(self) | ||
} | ||
|
||
static func ==(lhs: Self, rhs: Self) -> Bool { | ||
return lhs.uniqueID == rhs.uniqueID | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
|
||
public extension Array { | ||
func combineRanges<T: Comparable>() -> [Range<T>] where Element == Range<T> { | ||
var result: [Range<T>] = [] | ||
var lastRange: Range<T>? | ||
for range in self { | ||
if let _lastRange = lastRange { | ||
if range.lowerBound <= _lastRange.upperBound { | ||
lastRange = _lastRange.lowerBound..<range.upperBound | ||
} else { | ||
result.append(_lastRange) | ||
lastRange = range | ||
} | ||
} else { | ||
lastRange = range | ||
} | ||
} | ||
if let lastRange { | ||
result.append(lastRange) | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
import Foundation | ||
|
||
public protocol TextValidatable { | ||
var uniqueID: String { get } | ||
var errorText: String? { get } | ||
func isValid(string: String) -> Bool | ||
func validate(_ value: String) -> TextValidationResult | ||
} | ||
|
||
public extension TextValidatable { | ||
var uniqueID: String { | ||
return makeUniqueID() | ||
} | ||
|
||
func makeUniqueID() -> String { | ||
return String(describing: type(of: self)) + (errorText.map { " errorText: \($0)" } ?? "") | ||
} | ||
|
||
func toValidator() -> TextValidator { | ||
return .init(self) | ||
} | ||
|
||
static func ==(lhs: Self, rhs: Self) -> Bool { | ||
return lhs.uniqueID == rhs.uniqueID | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
import Foundation | ||
|
||
public enum TextValidationResult: Equatable { | ||
case valid | ||
case invalid | ||
case invalidWithErrorText(String) | ||
public struct TextValidationResult: Equatable { | ||
public let invalidRanges: [Range<String.Index>] | ||
public let errorText: String? | ||
public let isValid: Bool | ||
|
||
public var isValid: Bool { | ||
switch self { | ||
case .invalid, | ||
.invalidWithErrorText: | ||
return false | ||
case .valid: | ||
return true | ||
} | ||
public static let valid: Self = .init(invalidRanges: [], errorText: nil, isValid: true) | ||
public static func invalid(withErrorText: String? = nil) -> Self { | ||
return .init(invalidRanges: [], errorText: withErrorText, isValid: false) | ||
} | ||
|
||
public var errorText: String? { | ||
switch self { | ||
case .invalidWithErrorText(let text): | ||
return text | ||
case .invalid, | ||
.valid: | ||
return nil | ||
} | ||
public init(invalidRanges: [Range<String.Index>] = [], | ||
errorText: String? = nil, | ||
isValid: Bool) { | ||
self.invalidRanges = invalidRanges | ||
self.errorText = errorText | ||
self.isValid = isValid | ||
} | ||
} | ||
|
||
public extension TextValidationResult { | ||
static let invalid: Self = .init(isValid: false) | ||
} | ||
|
||
public extension [TextValidationResult] { | ||
static let invalid: Self = [.invalid] | ||
static let valid: Self = [] | ||
} |
Oops, something went wrong.