Skip to content

Commit 209e62b

Browse files
committed
Default bounds and precision are now provided by each Scheme.
1 parent 8f91b62 commit 209e62b

File tree

8 files changed

+110
-45
lines changed

8 files changed

+110
-45
lines changed

Sources/DiffableTextStylesXNumeric/Models/Scheme+Currency.swift

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,28 @@ import Foundation
2121
// MARK: State
2222
//=------------------------------------------------------------------------=
2323

24-
@usableFromInline let label: Label
25-
@usableFromInline let lexicon: Lexicon
24+
@usableFromInline let label: Label
25+
@usableFromInline let lexicon: Lexicon
2626
@usableFromInline let defaults: Defaults
27-
27+
@usableFromInline let identifier: ID
28+
2829
//=------------------------------------------------------------------------=
2930
// MARK: Initializers
3031
//=------------------------------------------------------------------------=
3132

32-
@inlinable init(_ key: ID) {
33+
@inlinable init(_ identifier: ID) {
3334
let formatter = NumberFormatter()
34-
formatter.numberStyle = .currency
35-
formatter.locale = key.locale
36-
formatter.currencyCode = key.code
35+
formatter.numberStyle = .decimal
36+
formatter.locale = identifier.locale
37+
formatter.currencyCode = identifier.code
3738
//=--------------------------------------=
3839
// MARK: Instantiate
3940
//=--------------------------------------=
40-
self.lexicon = .currency(formatter)
41-
self.label = .currency(key.locale, code: key.code, lexicon: lexicon)
41+
self.identifier = identifier
4242
self.defaults = .init(formatter)
43+
self.lexicon = .currency(formatter)
44+
self.label = .currency(identifier.locale,
45+
code: identifier.code, lexicon: lexicon)
4346
}
4447

4548
//=------------------------------------------------------------------------=
@@ -51,13 +54,15 @@ import Foundation
5154
}
5255

5356
//=------------------------------------------------------------------------=
54-
// MARK: Autocorrect
57+
// MARK: Accessors
5558
//=------------------------------------------------------------------------=
5659

57-
@inlinable func autocorrect(_ snapshot: inout Snapshot) {
58-
guard !label.characters.isEmpty else { return }
59-
guard let indices = label.indices(in: snapshot) else { return }
60-
snapshot.update(attributes: indices) { attribute in attribute = .phantom }
60+
@inlinable var code: String {
61+
identifier.code
62+
}
63+
64+
@inlinable var locale: Locale {
65+
identifier.locale
6166
}
6267

6368
//*========================================================================*
@@ -103,6 +108,30 @@ import Foundation
103108
formatter.minimumFractionDigits ...
104109
formatter.maximumFractionDigits
105110
}
106-
111+
}
112+
}
113+
114+
//=----------------------------------------------------------------------------=
115+
// MARK: + Specialization
116+
//=----------------------------------------------------------------------------=
117+
118+
extension NumericTextSchemeXCurrency {
119+
120+
//=------------------------------------------------------------------------=
121+
// MARK: Preferences
122+
//=------------------------------------------------------------------------=
123+
124+
@inlinable func precision<T>(_ value: T.Type) -> Precision<T> where T: Value {
125+
Precision(fraction: defaults.fractionLimits)
126+
}
127+
128+
//=------------------------------------------------------------------------=
129+
// MARK: Autocorrect
130+
//=------------------------------------------------------------------------=
131+
132+
@inlinable func autocorrect(_ snapshot: inout Snapshot) {
133+
guard !label.characters.isEmpty else { return }
134+
guard let indices = label.indices(in: snapshot) else { return }
135+
snapshot.update(attributes: indices) { attribute in attribute = .phantom }
107136
}
108137
}

Sources/DiffableTextStylesXNumeric/Models/Scheme+Standard.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Foundation
2121
// MARK: State
2222
//=------------------------------------------------------------------------=
2323

24+
@usableFromInline let identifier: ID
2425
@usableFromInline let lexicon: Lexicon
2526

2627
//=------------------------------------------------------------------------=
@@ -34,6 +35,7 @@ import Foundation
3435
//=--------------------------------------=
3536
// MARK: Instantiate
3637
//=--------------------------------------=
38+
self.identifier = identifier
3739
self.lexicon = .standard(formatter)
3840
}
3941

@@ -49,6 +51,14 @@ import Foundation
4951
reuseable(ID(format.locale))
5052
}
5153

54+
//=------------------------------------------------------------------------=
55+
// MARK: Accessors
56+
//=------------------------------------------------------------------------=
57+
58+
@inlinable var locale: Locale {
59+
identifier.locale
60+
}
61+
5262
//=------------------------------------------------------------------------=
5363
// MARK: Autocorrect
5464
//=------------------------------------------------------------------------=

Sources/DiffableTextStylesXNumeric/Models/Scheme.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ import Foundation
2525
//*============================================================================*
2626

2727
public protocol NumericTextScheme {
28-
28+
associatedtype ID: Hashable
29+
2930
//=------------------------------------------------------------------------=
3031
// MARK: State
3132
//=------------------------------------------------------------------------=
3233

3334
@inlinable var lexicon: Lexicon { get }
3435

3536
//=------------------------------------------------------------------------=
36-
// MARK: Defaults
37+
// MARK: Preferences
3738
//=------------------------------------------------------------------------=
3839

39-
@inlinable func bounds<Value>(_ value: Value.Type) -> Bounds<Value>
40+
@inlinable func bounds<T>(_ value: T.Type) -> Bounds<T>
4041

41-
@inlinable func precision<Value>(_ value: Value.Type) -> Precision<Value>
42+
@inlinable func precision<T>(_ value: T.Type) -> Precision<T>
4243

4344
//=------------------------------------------------------------------------=
4445
// MARK: Autocorrect
@@ -47,18 +48,36 @@ public protocol NumericTextScheme {
4748
@inlinable func autocorrect(_ snapshot: inout Snapshot)
4849
}
4950

51+
//=----------------------------------------------------------------------------=
52+
// MARK: + Details
53+
//=----------------------------------------------------------------------------=
54+
55+
extension NumericTextScheme {
56+
57+
//=------------------------------------------------------------------------=
58+
// MARK: Preferences
59+
//=------------------------------------------------------------------------=
60+
61+
@inlinable func bounds<T>(_ value: T.Type) -> Bounds<T> where T: Value {
62+
Bounds()
63+
}
64+
65+
@inlinable func precision<T>(_ value: T.Type) -> Precision<T> where T: Value {
66+
Precision()
67+
}
68+
}
69+
5070
//*============================================================================*
5171
// MARK: * Scheme x Reusable
5272
//*============================================================================*
5373

5474
@usableFromInline protocol NumericTextSchemeXReuseable: AnyObject, Scheme {
55-
associatedtype ID: Hashable
5675

5776
//=------------------------------------------------------------------------=
5877
// MARK: Initializers
5978
//=------------------------------------------------------------------------=
6079

61-
@inlinable init(_ key: ID)
80+
@inlinable init(_ identifier: ID)
6281

6382
//=------------------------------------------------------------------------=
6483
// MARK: Cache

Sources/DiffableTextStylesXNumeric/Style.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ import Foundation
1515
//*============================================================================*
1616

1717
public struct _NumericTextStyle<Format: NumericTextFormat>: DiffableTextStyle {
18-
@usableFromInline typealias Adapter = DiffableTextStylesXNumeric.Adapter<Format>
18+
public typealias Value = Format.FormatInput
1919
public typealias Bounds = DiffableTextStylesXNumeric.Bounds<Value>
2020
public typealias Precision = DiffableTextStylesXNumeric.Precision<Value>
21-
public typealias Value = Format.FormatInput
21+
22+
//=------------------------------------------------------------------------=
23+
// MARK: Aliases
24+
//=------------------------------------------------------------------------=
25+
26+
@usableFromInline typealias Scheme = Format.NumericTextScheme
27+
@usableFromInline typealias Adapter = DiffableTextStylesXNumeric.Adapter<Format>
2228

2329
//=------------------------------------------------------------------------=
2430
// MARK: State

Tests/DiffableTextStylesXNumericTests/Constants.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let currencyCodes: [String] = locales
3333
.reduce(into: Set()) { $0.insert($1) }
3434
.lazy.map({ $0 }).sorted(by: <)
3535

36-
let standard: [Lexicon] = locales.map(Lexicon.standard)
36+
let standards: [Schemes.Standard] = locales.lazy
37+
.map(Schemes.Standard.ID.init).map(Schemes.Standard.init)
3738

3839
#endif

Tests/DiffableTextStylesXNumericTests/Helpers/HelpersTests+Lexicon.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ final class HelpersTestsXLexicon: Tests {
2222
// MARK: Styles
2323
//=------------------------------------------------------------------------=
2424

25-
@inlinable func int(_ lexicon: Lexicon) -> IntegerFormatStyle<Int> {
26-
.number.locale(lexicon.locale)
25+
@inlinable func int(_ scheme: Schemes.Standard) -> IntegerFormatStyle<Int> {
26+
.number.locale(scheme.locale)
2727
}
2828

29-
@inlinable func double(_ lexicon: Lexicon) -> FloatingPointFormatStyle<Double> {
30-
.number.locale(lexicon.locale)
29+
@inlinable func double(_ scheme: Schemes.Standard) -> FloatingPointFormatStyle<Double> {
30+
.number.locale(scheme.locale)
3131
}
3232

3333
//=------------------------------------------------------------------------=
@@ -40,12 +40,12 @@ final class HelpersTestsXLexicon: Tests {
4040
//=--------------------------------------=
4141
// MARK: Lexicons
4242
//=--------------------------------------=
43-
for lexicon in standard {
44-
let style = int(lexicon).sign(strategy: .always())
43+
for scheme in standards {
44+
let style = int(scheme).sign(strategy: .always())
4545
let positives = positive.formatted(style)
4646
let negatives = negative.formatted(style)
47-
XCTAssertNotNil(positives.first(where: { lexicon.signs[$0] != nil }))
48-
XCTAssertNotNil(negatives.first(where: { lexicon.signs[$0] != nil }))
47+
XCTAssertNotNil(positives.first(where: { scheme.lexicon.signs[$0] != nil }))
48+
XCTAssertNotNil(negatives.first(where: { scheme.lexicon.signs[$0] != nil }))
4949
}
5050
}
5151

@@ -54,10 +54,10 @@ final class HelpersTestsXLexicon: Tests {
5454
//=--------------------------------------=
5555
// MARK: Lexicons
5656
//=--------------------------------------=
57-
for lexicon in standard {
58-
let style = int(lexicon).grouping(.never)
57+
for scheme in standards {
58+
let style = int(scheme).grouping(.never)
5959
let numbers = number.formatted(style)
60-
XCTAssert(numbers.allSatisfy({ lexicon.digits[$0] != nil }))
60+
XCTAssert(numbers.allSatisfy({ scheme.lexicon.digits[$0] != nil }))
6161
}
6262
}
6363

@@ -66,10 +66,10 @@ final class HelpersTestsXLexicon: Tests {
6666
//=--------------------------------------=
6767
// MARK: Lexicons
6868
//=--------------------------------------=
69-
for lexicon in standard {
70-
let style = int(lexicon).grouping(.automatic)
69+
for scheme in standards {
70+
let style = int(scheme).grouping(.automatic)
7171
let nonnumbers = number.formatted(style).filter({ !$0.isNumber })
72-
XCTAssert(nonnumbers.allSatisfy({ lexicon.separators[$0] == .grouping }))
72+
XCTAssert(nonnumbers.allSatisfy({ scheme.lexicon.separators[$0] == .grouping }))
7373
}
7474
}
7575

@@ -78,10 +78,10 @@ final class HelpersTestsXLexicon: Tests {
7878
//=--------------------------------------=
7979
// MARK: Lexicons
8080
//=--------------------------------------=
81-
for lexicon in standard {
82-
let style = double(lexicon).decimalSeparator(strategy: .always)
81+
for scheme in standards {
82+
let style = double(scheme).decimalSeparator(strategy: .always)
8383
let nonnumbers = number.formatted(style).filter({ !$0.isNumber })
84-
XCTAssert(nonnumbers.allSatisfy({ lexicon.separators[$0] == .fraction }))
84+
XCTAssert(nonnumbers.allSatisfy({ scheme.lexicon.separators[$0] == .fraction }))
8585
}
8686
}
8787
}

Tests/DiffableTextStylesXNumericTests/Helpers/HelpersTests+Links.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class HelpersTestsXLinks: Tests {
3636
//=--------------------------------------=
3737
// MARK: Lexicons
3838
//=--------------------------------------=
39-
for lexicon in standard {
39+
for lexicon in standards.lazy.map(\.lexicon) {
4040
XCTAssertEachCaseIsBidirectionallyLinked(lexicon.signs)
4141
XCTAssertEachCaseIsBidirectionallyLinked(lexicon.digits)
4242
XCTAssertEachCaseIsBidirectionallyLinked(lexicon.separators)

Tests/DiffableTextStylesXNumericTests/Miscellaneous/MiscellaneousTests+Percent.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ final class MiscellaneousTestsXPercent: Tests {
2828
//=--------------------------------------=
2929
// MARK: Locales
3030
//=--------------------------------------=
31-
for lexicon in standard {
32-
let zero = IntegerFormatStyle<Int>.Percent(locale: lexicon.locale).format(0)
33-
XCTAssert(zero.count(where: lexicon.nonvirtual) == 1, "\(zero), \(lexicon.locale)")
31+
for scheme in standards {
32+
let zero = IntegerFormatStyle<Int>.Percent(locale: scheme.locale).format(0)
33+
XCTAssert(zero.count(where: scheme.lexicon.nonvirtual) == 1, "\(zero), \(scheme.locale)")
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)