-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Handle semver comparisons (#49)
- Loading branch information
1 parent
f45aa61
commit 0ffe8c4
Showing
5 changed files
with
482 additions
and
25 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
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,47 @@ | ||
import FeaturevisorTypes | ||
import Foundation | ||
|
||
struct Semver: Equatable { | ||
let version: String | ||
let major: Int | ||
let minor: Int | ||
let patch: Int | ||
|
||
init(_ version: String) { | ||
self.version = version | ||
let components = version.components(separatedBy: ".") | ||
major = Int(components[0]) ?? 0 | ||
minor = components.count > 1 ? Int(components[1]) ?? 0 : 0 | ||
patch = components.count > 2 ? Int(components[2]) ?? 0 : 0 | ||
} | ||
|
||
static func == (lhs: Semver, rhs: Semver) -> Bool { | ||
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch | ||
} | ||
|
||
static func != (lhs: Semver, rhs: Semver) -> Bool { | ||
return !(lhs == rhs) | ||
} | ||
|
||
static func > (lhs: Semver, rhs: Semver) -> Bool { | ||
if lhs.major != rhs.major { | ||
return lhs.major > rhs.major | ||
} | ||
if lhs.minor != rhs.minor { | ||
return lhs.minor > rhs.minor | ||
} | ||
return lhs.patch > rhs.patch | ||
} | ||
|
||
static func >= (lhs: Semver, rhs: Semver) -> Bool { | ||
return lhs == rhs || lhs > rhs | ||
} | ||
|
||
static func < (lhs: Semver, rhs: Semver) -> Bool { | ||
return !(lhs >= rhs) | ||
} | ||
|
||
static func <= (lhs: Semver, rhs: Semver) -> Bool { | ||
return !(lhs > rhs) | ||
} | ||
} |
Oops, something went wrong.