Skip to content

Instantly share code, notes, and snippets.

@sayler8182
Created November 13, 2020 13:43
Show Gist options
  • Save sayler8182/f0435cac02f43e9ae8558aeac8859331 to your computer and use it in GitHub Desktop.
Save sayler8182/f0435cac02f43e9ae8558aeac8859331 to your computer and use it in GitHub Desktop.
// MARK: Version
extension Version: Equatable, Comparable {
public func isEqual(_ version: Version?) -> Bool {
guard let version: String = version?.version else { return false }
return version.compare(self.version, options: String.CompareOptions.numeric) == .orderedSame
}
public func isGreater(_ version: Version?) -> Bool {
guard let version: String = version?.version else { return false }
return version.compare(self.version, options: String.CompareOptions.numeric) == .orderedAscending
}
public func isLower(_ version: Version?) -> Bool {
guard let version: String = version?.version else { return false }
return version.compare(self.version, options: String.CompareOptions.numeric) == .orderedDescending
}
public static func == (lhs: Version, rhs: Version) -> Bool {
return lhs.isEqual(rhs)
}
public static func < (lhs: Version, rhs: Version) -> Bool {
return lhs.isLower(rhs)
}
public static func > (lhs: Version, rhs: Version) -> Bool {
return lhs.isGreater(rhs)
}
public static func <= (lhs: Version, rhs: Version) -> Bool {
return lhs.isLower(rhs) || lhs.isEqual(rhs)
}
public static func >= (lhs: Version, rhs: Version) -> Bool {
return lhs.isGreater(rhs) || lhs.isEqual(rhs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment