Skip to content

Commit 0a631e3

Browse files
committed
Add initializer for component score enum that takes component string
1 parent 998b0d2 commit 0a631e3

File tree

1 file changed

+54
-41
lines changed

1 file changed

+54
-41
lines changed

WooCommerce/Classes/Extensions/String+Helpers.swift

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -120,51 +120,12 @@ extension String {
120120
return outString
121121
}
122122

123-
/// Ranked per https://www.php.net/manual/en/function.version-compare.php
124-
///
125-
enum ComponentScore: Comparable {
126-
case other
127-
case dev
128-
case alpha
129-
case beta
130-
case RC
131-
case number
132-
case patch
133-
}
134-
135-
/// Score each component
136-
///
137-
func scoreComponent(stringComponent: String) -> ComponentScore {
138-
if stringComponent == "dev" {
139-
return .dev
140-
}
141-
if stringComponent == "alpha" || stringComponent == "a" {
142-
return .alpha
143-
}
144-
if stringComponent == "beta" || stringComponent == "b" {
145-
return .beta
146-
}
147-
if stringComponent == "RC" || stringComponent == "rc" {
148-
return .RC
149-
}
150-
let componentCharacterSet = CharacterSet(charactersIn: stringComponent)
151-
if componentCharacterSet.isSubset(of: .decimalDigits) {
152-
return .number
153-
}
154-
155-
if stringComponent == "pl" || stringComponent == "p" {
156-
return .patch
157-
}
158-
159-
return .other
160-
}
161-
162123
/// Score and compare two string components
163124
///
164125
func compareStringComponents(_ lhs: String, _ rhs: String) -> ComparisonResult {
165126
/// Score each component
166-
let lhsScore = scoreComponent(stringComponent: lhs)
167-
let rhsScore = scoreComponent(stringComponent: rhs)
127+
let lhsScore = VersionComponentScore(from: lhs)
128+
let rhsScore = VersionComponentScore(from: rhs)
168129

169130
if lhsScore < rhsScore {
170131
return .orderedAscending
@@ -214,3 +175,55 @@ extension String {
214175
return .orderedSame
215176
}
216177
}
178+
179+
/// Defines the score (rank) of a component string within a version string.
180+
/// e.g. the "3" in 3.0.0beta3 should be treated as `.number`
181+
/// and the "beta" should be scored (ranked) lower as `.beta`.
182+
///
183+
/// The scores of components of version strings are used when comparing complete version strings
184+
/// to decide if one version is older, equal or newer than another.
185+
///
186+
/// Ranked per https://www.php.net/manual/en/function.version-compare.php
187+
///
188+
fileprivate enum VersionComponentScore: Comparable {
189+
case other
190+
case dev
191+
case alpha
192+
case beta
193+
case RC
194+
case number
195+
case patch
196+
}
197+
198+
extension VersionComponentScore {
199+
init(from: String) {
200+
if from == "dev" {
201+
self = .dev
202+
return
203+
}
204+
if from == "alpha" || from == "a" {
205+
self = .alpha
206+
return
207+
}
208+
if from == "beta" || from == "b" {
209+
self = .beta
210+
return
211+
}
212+
if from == "RC" || from == "rc" {
213+
self = .RC
214+
return
215+
}
216+
let componentCharacterSet = CharacterSet(charactersIn: from)
217+
if componentCharacterSet.isSubset(of: .decimalDigits) {
218+
self = .number
219+
return
220+
}
221+
222+
if from == "pl" || from == "p" {
223+
self = .patch
224+
return
225+
}
226+
227+
self = .other
228+
}
229+
}

0 commit comments

Comments
 (0)