Skip to content

Commit

Permalink
Improve Bool initializer init?(_: String?)
Browse files Browse the repository at this point in the history
  • Loading branch information
F1248 committed Dec 30, 2024
1 parent 1be55ac commit 819f1de
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Genius/Extensions/Swift/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ extension Bool: DataInitializable {
}

init?(_ string: String?) {
guard let string = string?.lowercased() else { return nil }
guard
let string = string.map({ $0[($0.range(of: ":", options: .backwards)?.upperBound ?? $0.startIndex)...] })?.lowercased()
else { return nil }
if string.contains(any: [
"no",
"off",
"false",
"disabled",
]) {
self = false
} else if string.contains(any: [
"yes",
"on",
"true",
"enabled",
]) {
self = true
} else if string.contains(any: [
"no",
"off",
"false",
"disabled",
]) {
self = false
} else { return nil }
}
}
Expand Down

0 comments on commit 819f1de

Please sign in to comment.