Skip to content

Commit 70e40fa

Browse files
committed
Cleanup: NumericTextStyles/Schemes.Currency.
1 parent 2857e57 commit 70e40fa

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Sources/DiffableTextStylesXNumeric/Models/Scheme+Currency.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import Foundation
4545
//=--------------------------------------=
4646
formatter.numberStyle = .currency
4747
self.preferences = Preferences(formatter)
48+
formatter.maximumFractionDigits = .zero
4849
self.instruction = Instruction(formatter, lexicon)
4950
}
5051

@@ -94,6 +95,9 @@ import Foundation
9495
// MARK: Initializers
9596
//=--------------------------------------------------------------------=
9697

98+
/// - Requires that formatter.numberStyle == .currency.
99+
/// - Requires that formatter.maximumFractionDigits == default.
100+
///
97101
@inlinable init(_ formatter: NumberFormatter) {
98102
self.fraction = formatter.minimumFractionDigits ...
99103
formatter.maximumFractionDigits
@@ -123,30 +127,30 @@ import Foundation
123127
// MARK: Initializers
124128
//=--------------------------------------------------------------------=
125129

126-
/// Requires that formatter.numberStyle == .currency
130+
/// Returns an instance if it is needed, returns nil otherwise.
127131
///
128132
/// Correctness is assert by tests parsing currency formats for all locale-currency pairs.
129133
///
134+
/// - Requires that formatter.numberStyle == .currency.
135+
/// - Requires that formatter.maximumFractionDigits == .zero.
136+
///
130137
@inlinable init?(_ formatter: NumberFormatter, _ lexicon: Lexicon) {
131138
self.label = formatter.currencySymbol
132139
//=----------------------------------=
133-
// MARK: Check Instruction Is Needed
140+
// MARK: Necessity
134141
//=----------------------------------=
135142
guard label.contains(lexicon.separators[.fraction]) else { return nil }
136143
//=----------------------------------=
137144
// MARK: Formatted
138145
//=----------------------------------=
139-
let sides = IntegerFormatStyle<Int>
140-
.Currency(code: formatter.currencyCode, locale: formatter.locale)
141-
.precision(.fractionLength(0)).format(0)
142-
.split(separator: lexicon.digits[.zero], omittingEmptySubsequences: false)
146+
let sides = formatter.string(from: 0)!.split(
147+
separator: lexicon.digits[.zero], omittingEmptySubsequences: false)
143148
//=----------------------------------=
144149
// MARK: Direction
145150
//=----------------------------------=
146-
if sides[0].contains(label) {
147-
self.direction = .forwards
148-
} else {
149-
self.direction = .backwards; assert(sides[1].contains(label))
151+
switch sides[0].contains(label) {
152+
case true: self.direction = .forwards
153+
case false: self.direction = .backwards; assert(sides[1].contains(label))
150154
}
151155
}
152156

0 commit comments

Comments
 (0)