Skip to content

Commit

Permalink
Minor changes and adding some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Panayot Panayotov committed May 30, 2018
1 parent 0070720 commit 449e1be
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 41 deletions.
28 changes: 15 additions & 13 deletions InputValidation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
5A043BEB1F753C460021149B /* InputValidation_macOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A043BE21F753C460021149B /* InputValidation_macOS.framework */; };
5A043BF01F753C460021149B /* InputValidation_macOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BEF1F753C460021149B /* InputValidation_macOSTests.swift */; };
5A043BF21F753C470021149B /* InputValidation_macOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A043BE41F753C460021149B /* InputValidation_macOS.h */; settings = {ATTRIBUTES = (Public, ); }; };
5A043BF91F753C700021149B /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
5A043BFA1F753C730021149B /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
5A043BFB1F753C740021149B /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
5A043C0B1F753DBA0021149B /* InputValidation_tvOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A043C021F753DBA0021149B /* InputValidation_tvOS.framework */; };
5A043C101F753DBA0021149B /* InputValidation_tvOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043C0F1F753DBA0021149B /* InputValidation_tvOSTests.swift */; };
5A043C121F753DBA0021149B /* InputValidation_tvOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A043C041F753DBA0021149B /* InputValidation_tvOS.h */; settings = {ATTRIBUTES = (Public, ); }; };
5A043C1A1F753DC50021149B /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
5A043C1B1F753DC50021149B /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
5A7A02AC20BEBB0C00CCB76C /* InputValidationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043B891F7510880021149B /* InputValidationTests.swift */; };
5A7A02AD20BEBB0D00CCB76C /* InputValidationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043B891F7510880021149B /* InputValidationTests.swift */; };
5A7A02AE20BEBB1E00CCB76C /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
5A7A02AF20BEBB1F00CCB76C /* InputValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A043BDB1F753B770021149B /* InputValue.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -345,7 +344,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0900;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0930;
ORGANIZATIONNAME = "Panayot Panayotov";
TargetAttributes = {
5A043B7A1F7510880021149B = {
Expand Down Expand Up @@ -455,7 +454,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5A043BFA1F753C730021149B /* InputValue.swift in Sources */,
5A043B8A1F7510880021149B /* InputValidationTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -464,33 +462,33 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5A043BF91F753C700021149B /* InputValue.swift in Sources */,
5A7A02AE20BEBB1E00CCB76C /* InputValue.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
5A043BE61F753C460021149B /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5A043BFB1F753C740021149B /* InputValue.swift in Sources */,
5A043BF01F753C460021149B /* InputValidation_macOSTests.swift in Sources */,
5A7A02AC20BEBB0C00CCB76C /* InputValidationTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
5A043BFD1F753DBA0021149B /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5A043C1A1F753DC50021149B /* InputValue.swift in Sources */,
5A7A02AF20BEBB1F00CCB76C /* InputValue.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
5A043C061F753DBA0021149B /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5A043C1B1F753DC50021149B /* InputValue.swift in Sources */,
5A043C101F753DBA0021149B /* InputValidation_tvOSTests.swift in Sources */,
5A7A02AD20BEBB0D00CCB76C /* InputValidationTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -529,13 +527,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -589,13 +589,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -708,7 +710,7 @@
5A043BF41F753C470021149B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
Expand All @@ -732,7 +734,7 @@
5A043BF51F753C470021149B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
86 changes: 67 additions & 19 deletions InputValidation/InputValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,95 @@ public enum ValueType: String {
}

public protocol InputValue: CustomStringConvertible {


/// Holds the ValueType of the object implementing InputValue protocol
var type: ValueType { get }


/// True if the object implementing InputValue protocol is Integer
var isInt: Bool { get }
/// True if the object implementing InputValue protocol is Float
var isFloat: Bool { get }
/// True if the object implementing InputValue protocol is Double
var isDouble: Bool { get }
/// True if the object implementing InputValue protocol is String
var isString: Bool { get }
/// True if the object implementing InputValue protocol is Bool
var isBool: Bool { get }
/// True if the object implementing InputValue protocol is Number
var isNumber: Bool { get }
/// True if the object implementing InputValue protocol is Empty (ignores spaces)
var isEmpty: Bool { get }


/// Integer value of the object implementing InputValue protocol
var toInt: Int? { get }
/// Float value of the object implementing InputValue protocol
var toFloat: Float? { get }
/// Double value of the object implementing InputValue protocol
var toDouble: Double? { get }
/// Boolean value of the object implementing InputValue protocol
var toBool: Bool? { get }
/// String value of the object implementing InputValue protocol
var toString: String { get }


/// Logical comparison between this and two other objects
/// also implementing InputValue protocol
///
/// - Parameters:
/// - min: Minimum input value
/// - max: Maximum input value
/// - Returns: True if this InputValue is between min and max values. Example 'result = this >= min && <= max'
func isBetween<T: InputValue>(min: T, max: T) -> Bool

/// Logical comparison to check if this InputValue is
/// less than the reference value passed as an argument
///
/// - Parameter test: Reference value used for comparison
/// - Returns: True if this InputValue is less than the reference. Example 'result = this < reference'
func isLessThan<T: InputValue>(test: T) -> Bool

/// Logical comparison to check if this InputValue is
/// less than or equal to the reference value passed as an argument
///
/// - Parameter test: Reference value used for comparison
/// - Returns: True if this InputValue is less than or equal to the reference. Example 'result = this <= reference'
func isLessThanOrEqualTo<T: InputValue>(test: T) -> Bool

/// Logical comparison to check if this InputValue is
/// greater than the reference value passed as an argument
///
/// - Parameter test: Reference value used for comparison
/// - Returns: True if this InputValue is greater than the reference. Example 'result = this > reference'
func isGreaterThan<T: InputValue>(test: T) -> Bool

/// Logical comparison to check if this InputValue is
/// greater than or equal to the reference value passed as an argument
///
/// - Parameter test: Reference value used for comparison
/// - Returns: True if this InputValue is greater than or equal to the reference. Example 'result = this >= reference'
func isGreaterThanOrEqualTo<T: InputValue>(test: T) -> Bool


/// Performs a RegEx evaluation on the current InputValue
///
/// - Parameter regex: RegEx value. Example '^[\d]+$' will match only numbers
/// - Returns: True if RexEx evaluation was successfull
func matches(regex: String) -> Bool
}

public extension InputValue {
extension InputValue {

public var isFloat: Bool { return self.type == .float }
public var isBool: Bool { return self.type == .bool }
public var isDouble: Bool { return self.type == .double }
public var isNumber: Bool { return self.type == .number }
public var isInt: Bool { return self.type == .int }
public var isString: Bool { return self.type == .string }
public var toInt: Int? { return NumberFormatter().number(from: self.description)?.intValue }
public var toFloat: Float? { return NumberFormatter().number(from: self.description)?.floatValue }
public var toDouble: Double? { return NumberFormatter().number(from: self.description)?.doubleValue }
public var toInt: Int? { return NumberFormatter().number(from: self.toString)?.intValue }
public var toFloat: Float? { return NumberFormatter().number(from: self.toString)?.floatValue }
public var toDouble: Double? { return NumberFormatter().number(from: self.toString)?.doubleValue }
public var toBool: Bool? { return nil }
public var toString: String { return self.description }
public var isEmpty: Bool { return self.description.replacingOccurrences(of: " ", with: "").characters.count == 0 }
public var toString: String { return String(describing: self) }
public var isEmpty: Bool { return self.toString.replacingOccurrences(of: " ", with: "").count == 0 }

public func isBetween<T: InputValue, X: InputValue>(min: T, max: X) -> Bool {
guard let thisFloat = self.toFloat,
let minFloat = min.toFloat,
Expand All @@ -65,7 +113,7 @@ public extension InputValue {
}
return minFloat ... maxFloat ~= thisFloat
}

public func isLessThan<T: InputValue>(test: T) -> Bool {
guard let thisFloat = self.toFloat,
let testFloat = test.toFloat,
Expand All @@ -76,7 +124,7 @@ public extension InputValue {
}
return thisFloat < testFloat
}

public func isLessThanOrEqualTo<T: InputValue>(test: T) -> Bool {
guard let thisFloat = self.toFloat,
let testFloat = test.toFloat,
Expand All @@ -87,7 +135,7 @@ public extension InputValue {
}
return thisFloat <= testFloat
}

public func isGreaterThan<T: InputValue>(test: T) -> Bool {
guard let thisFloat = self.toFloat,
let testFloat = test.toFloat,
Expand All @@ -98,7 +146,7 @@ public extension InputValue {
}
return thisFloat > testFloat
}

public func isGreaterThanOrEqualTo<T: InputValue>(test: T) -> Bool {
guard let thisFloat = self.toFloat,
let testFloat = test.toFloat,
Expand All @@ -109,9 +157,9 @@ public extension InputValue {
}
return thisFloat >= testFloat
}

public func matches(regex: String) -> Bool {
return self.description.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
return self.toString.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
}
}

Expand Down

0 comments on commit 449e1be

Please sign in to comment.