Skip to content

Commit c92fe0a

Browse files
author
Reed Es
committed
Deprecated blankIfZero(bool) in favor of ifZero(String?)
1 parent 652a21e commit c92fe0a

File tree

7 files changed

+62
-37
lines changed

7 files changed

+62
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ By default, values will show up to one fractional decimal point of the value, ro
2323

2424
### Options
2525

26-
* `blankIfZero` (default: `false`) - if true, a value of zero will return “”
26+
* `ifZero` (default: `(String?)nil`) - if nil and value is zero, a zero value will be shown. If !nil and value is zero, the specified string value will be shown
2727
* `roundSmallToWhole` (default: `false`) - if true, fractional parts excluded from values within `-100...100`
2828

2929
### Suffixes
@@ -52,7 +52,7 @@ By default, values within `-100...100` will have no fractional part, and are rou
5252

5353
### Options
5454

55-
* `blankIfZero` (default: `false`) - if true, a value of zero will return “”
55+
* `ifZero` (default: `(String?)nil`) - if nil and value is zero, a zero value will be shown. If !nil and value is zero, the specified string value will be shown
5656
* `roundSmallToWhole` (default: `true`) - if true, fractional parts excluded from values within `-100...100`
5757

5858
Note that `roundSmallToWhole` is true by default, because `$1.1` looks awkward when we’re accustomed to fractions in pennies.
@@ -81,7 +81,7 @@ By default, values will show up to one fractional decimal point of the value, ro
8181

8282
### Options
8383

84-
* `blankIfZero` (default: `false`) - if true, a value of zero will return “”
84+
* `ifZero` (default: `(String?)nil`) - if nil and value is zero, a zero value will be shown. If !nil and value is zero, the specified string value will be shown
8585
* `style` (default: `.short`) - the style of suffix used
8686
* `roundSmallToWhole` (default: `false`) - if true, fractional parts excluded from values within `-100...100`
8787

Sources/CurrencyCompactor.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ import Foundation
2020

2121
public class CurrencyCompactor: NumberCompactor {
2222

23-
public override init(blankIfZero: Bool = false,
24-
roundSmallToWhole: Bool = true) {
23+
@available(*, deprecated, message: "use init(ifZero: String?, roundSmallToWhole: Bool) instead")
24+
public convenience init(blankIfZero: Bool = false,
25+
roundSmallToWhole: Bool = true) {
26+
self.init(ifZero: blankIfZero ? "" : nil,
27+
roundSmallToWhole: roundSmallToWhole)
28+
}
29+
30+
public override init(ifZero: String? = nil,
31+
roundSmallToWhole: Bool = true) {
2532

26-
super.init(blankIfZero: blankIfZero,
33+
super.init(ifZero: ifZero,
2734
roundSmallToWhole: roundSmallToWhole)
2835

2936
self.numberStyle = .currency

Sources/NumberCompactor.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@ import Foundation
2020

2121
public class NumberCompactor: NumberFormatter {
2222

23-
public var blankIfZero: Bool
23+
public var ifZero: String?
2424
public var roundSmallToWhole: Bool
2525

26-
public init(blankIfZero: Bool = false,
26+
@available(*, deprecated, message: "use init(ifZero: String?, roundSmallToWhole: Bool) instead")
27+
public convenience init(blankIfZero: Bool = false,
28+
roundSmallToWhole: Bool = false) {
29+
self.init(ifZero: blankIfZero ? "" : nil,
30+
roundSmallToWhole: roundSmallToWhole)
31+
}
32+
33+
public init(ifZero: String? = nil,
2734
roundSmallToWhole: Bool = false) {
28-
self.blankIfZero = blankIfZero
35+
36+
self.ifZero = ifZero
2937
self.roundSmallToWhole = roundSmallToWhole
3038
super.init()
3139
}
@@ -39,7 +47,7 @@ public class NumberCompactor: NumberFormatter {
3947
let absValue = abs(rawValue)
4048
let threshold = NumberCompactor.getThreshold(roundSmallToWhole)
4149

42-
if blankIfZero, absValue <= threshold { return "" }
50+
if ifZero != nil, absValue <= threshold { return ifZero! }
4351

4452
let (scaledValue, scaleSymbol) = NumberCompactor.getScaledValue(rawValue, roundSmallToWhole)
4553

Sources/TimeCompactor.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,24 @@ import Foundation
2020

2121
public class TimeCompactor: NumberFormatter {
2222

23-
public var blankIfZero: Bool
23+
public var ifZero: String?
2424
public var style: Style
2525
public var roundSmallToWhole: Bool
2626

27-
public init(blankIfZero: Bool = false,
27+
@available(*, deprecated, message: "use init(ifZero: String?, style: Style, roundSmallToWhole: Bool) instead")
28+
public convenience init(blankIfZero: Bool = false,
29+
style: Style = .short,
30+
roundSmallToWhole: Bool = false) {
31+
self.init(ifZero: blankIfZero ? "" : nil,
32+
style: style,
33+
roundSmallToWhole: roundSmallToWhole)
34+
}
35+
36+
public init(ifZero: String? = nil,
2837
style: Style = .short,
2938
roundSmallToWhole: Bool = false) {
30-
self.blankIfZero = blankIfZero
39+
40+
self.ifZero = ifZero
3141
self.style = style
3242
self.roundSmallToWhole = roundSmallToWhole
3343
super.init()
@@ -42,7 +52,7 @@ public class TimeCompactor: NumberFormatter {
4252
let absValue = abs(rawValue)
4353
let threshold = TimeCompactor.getThreshold(roundSmallToWhole)
4454

45-
if blankIfZero, absValue <= threshold { return "" }
55+
if ifZero != nil, absValue <= threshold { return ifZero! }
4656

4757
let (scaledValue, scaleSymbol) = TimeCompactor.getScaledValue(rawValue, roundSmallToWhole)
4858

Tests/CurrencyCompactorTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import XCTest
2323
class CurrencyCompactorTests: XCTestCase {
2424

2525
func testBlankIfZero() {
26-
let c = CurrencyCompactor(blankIfZero: true)
26+
let c = CurrencyCompactor(ifZero: "")
2727
c.currencyCode = "USD"
2828
c.decimalSeparator = "."
2929

@@ -48,7 +48,7 @@ class CurrencyCompactorTests: XCTestCase {
4848
}
4949

5050
func testCompactDropTrailingDotZero() {
51-
let c = CurrencyCompactor(blankIfZero: false)
51+
let c = CurrencyCompactor(ifZero: nil)
5252
c.currencyCode = "USD"
5353
c.decimalSeparator = "."
5454

@@ -58,7 +58,7 @@ class CurrencyCompactorTests: XCTestCase {
5858
}
5959

6060
func testCompactUS() {
61-
let c = CurrencyCompactor(blankIfZero: false)
61+
let c = CurrencyCompactor(ifZero: nil)
6262
c.currencyCode = "USD"
6363

6464
XCTAssertEqual("-$121T", c.string(from: -120_500_000_000_000.01))
@@ -126,7 +126,7 @@ class CurrencyCompactorTests: XCTestCase {
126126

127127
func testNormalEU() {
128128

129-
let c = CurrencyCompactor(blankIfZero: false)
129+
let c = CurrencyCompactor(ifZero: nil)
130130
c.locale = Locale(identifier: "FR")
131131
c.currencyCode = "EUR"
132132

@@ -139,7 +139,7 @@ class CurrencyCompactorTests: XCTestCase {
139139
}
140140

141141
func testCompactEU() {
142-
let c = CurrencyCompactor(blankIfZero: false)
142+
let c = CurrencyCompactor(ifZero: nil)
143143
c.locale = Locale(identifier: "US")
144144
c.currencyCode = "EUR"
145145
c.currencyDecimalSeparator = ","

Tests/NumberCompactorTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import XCTest
2323
class NumberCompactorTests: XCTestCase {
2424

2525
func testExample() {
26-
let c = TimeCompactor(style: .full)
26+
let c = TimeCompactor(ifZero: nil, style: .full)
2727
XCTAssertEqual("14.3 days", c.string(from: 1_234_567))
2828
}
2929

3030
func testBlankIfZero() {
31-
let c = NumberCompactor(blankIfZero: true)
31+
let c = NumberCompactor(ifZero: "")
3232

3333
XCTAssertEqual("", c.string(from: 0))
3434
XCTAssertEqual("", c.string(from: 0.05))
@@ -39,7 +39,7 @@ class NumberCompactorTests: XCTestCase {
3939
}
4040

4141
func testWholeNumber() {
42-
let c = NumberCompactor()
42+
let c = NumberCompactor(ifZero: nil)
4343
c.roundSmallToWhole = true
4444

4545
XCTAssertEqual("8", c.string(from: 8))
@@ -88,7 +88,7 @@ class NumberCompactorTests: XCTestCase {
8888
}
8989

9090
func testSmallValues() {
91-
let c = NumberCompactor(blankIfZero: false)
91+
let c = NumberCompactor(ifZero: nil)
9292

9393
XCTAssertEqual("0.0", c.string(from: 0))
9494
XCTAssertEqual("0.0", c.string(from: 0.05))
@@ -114,7 +114,7 @@ class NumberCompactorTests: XCTestCase {
114114
}
115115

116116
func testK() {
117-
let c = NumberCompactor(blankIfZero: false)
117+
let c = NumberCompactor(ifZero: nil)
118118

119119
XCTAssertEqual("1.0k", c.string(from: 1_000))
120120
XCTAssertEqual("1.0k", c.string(from: 1_050.00000))
@@ -150,7 +150,7 @@ class NumberCompactorTests: XCTestCase {
150150
}
151151

152152
func testM() {
153-
let c = NumberCompactor(blankIfZero: false)
153+
let c = NumberCompactor(ifZero: nil)
154154

155155
XCTAssertEqual("1.0M", c.string(from: 1_000_000))
156156
XCTAssertEqual("1.0M", c.string(from: 1_050_000.00000))
@@ -186,31 +186,31 @@ class NumberCompactorTests: XCTestCase {
186186
}
187187

188188
func testG() {
189-
let c = NumberCompactor(blankIfZero: false)
189+
let c = NumberCompactor(ifZero: nil)
190190
XCTAssertEqual("1.0G", c.string(from: 1_000_000_000))
191191
XCTAssertEqual("1.0G", c.string(from: 1_000_000_000.000001))
192192
XCTAssertEqual("1.0G", c.string(from: 1_050_000_000.000000))
193193
XCTAssertEqual("1.1G", c.string(from: 1_050_000_000.000001))
194194
}
195195

196196
func testT() {
197-
let c = NumberCompactor(blankIfZero: false)
197+
let c = NumberCompactor(ifZero: nil)
198198
XCTAssertEqual("1.0T", c.string(from: 1_000_000_000_000))
199199
XCTAssertEqual("1.0T", c.string(from: 1_000_000_000_000.001))
200200
XCTAssertEqual("1.0T", c.string(from: 1_050_000_000_000.000))
201201
XCTAssertEqual("1.1T", c.string(from: 1_050_000_000_000.001))
202202
}
203203

204204
func testP() {
205-
let c = NumberCompactor(blankIfZero: false)
205+
let c = NumberCompactor(ifZero: nil)
206206
XCTAssertEqual("1.0P", c.string(from: 1_000_000_000_000_000))
207207
XCTAssertEqual("1.0P", c.string(from: 1_000_000_000_000_000))
208208
XCTAssertEqual("1.0P", c.string(from: 1_050_000_000_000_000))
209209
XCTAssertEqual("1.1P", c.string(from: 1_050_000_000_000_001))
210210
}
211211

212212
func testE() {
213-
let c = NumberCompactor(blankIfZero: false)
213+
let c = NumberCompactor(ifZero: nil)
214214
XCTAssertEqual("1.0E", c.string(from: 1_000_000_000_000_000_000))
215215
XCTAssertEqual("1.0E", c.string(from: 1_000_000_000_000_000_000))
216216
XCTAssertEqual("1.0E", c.string(from: 1_050_000_000_000_000_000))

Tests/TimeCompactorTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import XCTest
2323
class TimeCompactorTests: XCTestCase {
2424

2525
func testBlankIfZero() {
26-
let c = TimeCompactor(blankIfZero: true, style: .short)
26+
let c = TimeCompactor(ifZero: "", style: .short)
2727

2828
XCTAssertEqual("", c.string(from: 0.049))
2929
XCTAssertEqual("", c.string(from: -0.049))
@@ -48,7 +48,7 @@ class TimeCompactorTests: XCTestCase {
4848
}
4949

5050
func testWholeNumberFull() {
51-
let c = TimeCompactor(blankIfZero: false, style: .full, roundSmallToWhole: true)
51+
let c = TimeCompactor(ifZero: nil, style: .full, roundSmallToWhole: true)
5252

5353
XCTAssertEqual("59 seconds", c.string(from: 59))
5454
XCTAssertEqual("59 seconds", c.string(from: 59.499))
@@ -71,7 +71,7 @@ class TimeCompactorTests: XCTestCase {
7171
}
7272

7373
func testWholeNumberShort() {
74-
let c = TimeCompactor(blankIfZero: false, style: .short, roundSmallToWhole: true)
74+
let c = TimeCompactor(ifZero: nil, style: .short, roundSmallToWhole: true)
7575

7676
XCTAssertEqual("59s", c.string(from: 59))
7777
XCTAssertEqual("59s", c.string(from: 59.499))
@@ -94,7 +94,7 @@ class TimeCompactorTests: XCTestCase {
9494
}
9595

9696
func testPluralFull() {
97-
let c = TimeCompactor(blankIfZero: false, style: .full)
97+
let c = TimeCompactor(ifZero: nil, style: .full)
9898

9999
XCTAssertEqual("1.0 seconds", c.string(from: 1))
100100
XCTAssertEqual("1.1 seconds", c.string(from: 1.09))
@@ -112,14 +112,14 @@ class TimeCompactorTests: XCTestCase {
112112
}
113113

114114
func testCompactDropTrailingDotZero() {
115-
let c = TimeCompactor(blankIfZero: false, style: .short)
115+
let c = TimeCompactor(ifZero: nil, style: .short)
116116

117117
XCTAssertEqual("12.0s", c.string(from: 12.049))
118118
XCTAssertEqual("-12.0s", c.string(from: -12.050))
119119
}
120120

121121
func testShort() {
122-
let c = TimeCompactor(blankIfZero: false, style: .short)
122+
let c = TimeCompactor(ifZero: nil, style: .short)
123123

124124
XCTAssertEqual("-3818ky", c.string(from: -120_500_000_000_000))
125125
XCTAssertEqual("-3.8ky", c.string(from: -120_500_000_000))
@@ -168,7 +168,7 @@ class TimeCompactorTests: XCTestCase {
168168
}
169169

170170
func testFull() {
171-
let c = TimeCompactor(blankIfZero: false, style: .full)
171+
let c = TimeCompactor(ifZero: nil, style: .full)
172172

173173
XCTAssertEqual("-3818 millenia", c.string(from: -120_500_000_000_000))
174174
XCTAssertEqual("-3.8 millenia", c.string(from: -120_500_000_000))
@@ -217,7 +217,7 @@ class TimeCompactorTests: XCTestCase {
217217
}
218218

219219
func testCompactEU() {
220-
let c = TimeCompactor(blankIfZero: false, style: .short)
220+
let c = TimeCompactor(ifZero: nil, style: .short)
221221
c.decimalSeparator = ","
222222

223223
XCTAssertEqual("3,3h", c.string(from: 12050))

0 commit comments

Comments
 (0)