Skip to content

Commit

Permalink
Deprecated blankIfZero(bool) in favor of ifZero(String?)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Es committed Jan 10, 2022
1 parent 652a21e commit c92fe0a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ By default, values will show up to one fractional decimal point of the value, ro

### Options

* `blankIfZero` (default: `false`) - if true, a value of zero will return “”
* `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
* `roundSmallToWhole` (default: `false`) - if true, fractional parts excluded from values within `-100...100`

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

### Options

* `blankIfZero` (default: `false`) - if true, a value of zero will return “”
* `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
* `roundSmallToWhole` (default: `true`) - if true, fractional parts excluded from values within `-100...100`

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

### Options

* `blankIfZero` (default: `false`) - if true, a value of zero will return “”
* `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
* `style` (default: `.short`) - the style of suffix used
* `roundSmallToWhole` (default: `false`) - if true, fractional parts excluded from values within `-100...100`

Expand Down
13 changes: 10 additions & 3 deletions Sources/CurrencyCompactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ import Foundation

public class CurrencyCompactor: NumberCompactor {

public override init(blankIfZero: Bool = false,
roundSmallToWhole: Bool = true) {
@available(*, deprecated, message: "use init(ifZero: String?, roundSmallToWhole: Bool) instead")
public convenience init(blankIfZero: Bool = false,
roundSmallToWhole: Bool = true) {
self.init(ifZero: blankIfZero ? "" : nil,
roundSmallToWhole: roundSmallToWhole)
}

public override init(ifZero: String? = nil,
roundSmallToWhole: Bool = true) {

super.init(blankIfZero: blankIfZero,
super.init(ifZero: ifZero,
roundSmallToWhole: roundSmallToWhole)

self.numberStyle = .currency
Expand Down
16 changes: 12 additions & 4 deletions Sources/NumberCompactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ import Foundation

public class NumberCompactor: NumberFormatter {

public var blankIfZero: Bool
public var ifZero: String?
public var roundSmallToWhole: Bool

public init(blankIfZero: Bool = false,
@available(*, deprecated, message: "use init(ifZero: String?, roundSmallToWhole: Bool) instead")
public convenience init(blankIfZero: Bool = false,
roundSmallToWhole: Bool = false) {
self.init(ifZero: blankIfZero ? "" : nil,
roundSmallToWhole: roundSmallToWhole)
}

public init(ifZero: String? = nil,
roundSmallToWhole: Bool = false) {
self.blankIfZero = blankIfZero

self.ifZero = ifZero
self.roundSmallToWhole = roundSmallToWhole
super.init()
}
Expand All @@ -39,7 +47,7 @@ public class NumberCompactor: NumberFormatter {
let absValue = abs(rawValue)
let threshold = NumberCompactor.getThreshold(roundSmallToWhole)

if blankIfZero, absValue <= threshold { return "" }
if ifZero != nil, absValue <= threshold { return ifZero! }

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

Expand Down
18 changes: 14 additions & 4 deletions Sources/TimeCompactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ import Foundation

public class TimeCompactor: NumberFormatter {

public var blankIfZero: Bool
public var ifZero: String?
public var style: Style
public var roundSmallToWhole: Bool

public init(blankIfZero: Bool = false,
@available(*, deprecated, message: "use init(ifZero: String?, style: Style, roundSmallToWhole: Bool) instead")
public convenience init(blankIfZero: Bool = false,
style: Style = .short,
roundSmallToWhole: Bool = false) {
self.init(ifZero: blankIfZero ? "" : nil,
style: style,
roundSmallToWhole: roundSmallToWhole)
}

public init(ifZero: String? = nil,
style: Style = .short,
roundSmallToWhole: Bool = false) {
self.blankIfZero = blankIfZero

self.ifZero = ifZero
self.style = style
self.roundSmallToWhole = roundSmallToWhole
super.init()
Expand All @@ -42,7 +52,7 @@ public class TimeCompactor: NumberFormatter {
let absValue = abs(rawValue)
let threshold = TimeCompactor.getThreshold(roundSmallToWhole)

if blankIfZero, absValue <= threshold { return "" }
if ifZero != nil, absValue <= threshold { return ifZero! }

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

Expand Down
10 changes: 5 additions & 5 deletions Tests/CurrencyCompactorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
class CurrencyCompactorTests: XCTestCase {

func testBlankIfZero() {
let c = CurrencyCompactor(blankIfZero: true)
let c = CurrencyCompactor(ifZero: "")
c.currencyCode = "USD"
c.decimalSeparator = "."

Expand All @@ -48,7 +48,7 @@ class CurrencyCompactorTests: XCTestCase {
}

func testCompactDropTrailingDotZero() {
let c = CurrencyCompactor(blankIfZero: false)
let c = CurrencyCompactor(ifZero: nil)
c.currencyCode = "USD"
c.decimalSeparator = "."

Expand All @@ -58,7 +58,7 @@ class CurrencyCompactorTests: XCTestCase {
}

func testCompactUS() {
let c = CurrencyCompactor(blankIfZero: false)
let c = CurrencyCompactor(ifZero: nil)
c.currencyCode = "USD"

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

func testNormalEU() {

let c = CurrencyCompactor(blankIfZero: false)
let c = CurrencyCompactor(ifZero: nil)
c.locale = Locale(identifier: "FR")
c.currencyCode = "EUR"

Expand All @@ -139,7 +139,7 @@ class CurrencyCompactorTests: XCTestCase {
}

func testCompactEU() {
let c = CurrencyCompactor(blankIfZero: false)
let c = CurrencyCompactor(ifZero: nil)
c.locale = Locale(identifier: "US")
c.currencyCode = "EUR"
c.currencyDecimalSeparator = ","
Expand Down
20 changes: 10 additions & 10 deletions Tests/NumberCompactorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import XCTest
class NumberCompactorTests: XCTestCase {

func testExample() {
let c = TimeCompactor(style: .full)
let c = TimeCompactor(ifZero: nil, style: .full)
XCTAssertEqual("14.3 days", c.string(from: 1_234_567))
}

func testBlankIfZero() {
let c = NumberCompactor(blankIfZero: true)
let c = NumberCompactor(ifZero: "")

XCTAssertEqual("", c.string(from: 0))
XCTAssertEqual("", c.string(from: 0.05))
Expand All @@ -39,7 +39,7 @@ class NumberCompactorTests: XCTestCase {
}

func testWholeNumber() {
let c = NumberCompactor()
let c = NumberCompactor(ifZero: nil)
c.roundSmallToWhole = true

XCTAssertEqual("8", c.string(from: 8))
Expand Down Expand Up @@ -88,7 +88,7 @@ class NumberCompactorTests: XCTestCase {
}

func testSmallValues() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)

XCTAssertEqual("0.0", c.string(from: 0))
XCTAssertEqual("0.0", c.string(from: 0.05))
Expand All @@ -114,7 +114,7 @@ class NumberCompactorTests: XCTestCase {
}

func testK() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)

XCTAssertEqual("1.0k", c.string(from: 1_000))
XCTAssertEqual("1.0k", c.string(from: 1_050.00000))
Expand Down Expand Up @@ -150,7 +150,7 @@ class NumberCompactorTests: XCTestCase {
}

func testM() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)

XCTAssertEqual("1.0M", c.string(from: 1_000_000))
XCTAssertEqual("1.0M", c.string(from: 1_050_000.00000))
Expand Down Expand Up @@ -186,31 +186,31 @@ class NumberCompactorTests: XCTestCase {
}

func testG() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)
XCTAssertEqual("1.0G", c.string(from: 1_000_000_000))
XCTAssertEqual("1.0G", c.string(from: 1_000_000_000.000001))
XCTAssertEqual("1.0G", c.string(from: 1_050_000_000.000000))
XCTAssertEqual("1.1G", c.string(from: 1_050_000_000.000001))
}

func testT() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)
XCTAssertEqual("1.0T", c.string(from: 1_000_000_000_000))
XCTAssertEqual("1.0T", c.string(from: 1_000_000_000_000.001))
XCTAssertEqual("1.0T", c.string(from: 1_050_000_000_000.000))
XCTAssertEqual("1.1T", c.string(from: 1_050_000_000_000.001))
}

func testP() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)
XCTAssertEqual("1.0P", c.string(from: 1_000_000_000_000_000))
XCTAssertEqual("1.0P", c.string(from: 1_000_000_000_000_000))
XCTAssertEqual("1.0P", c.string(from: 1_050_000_000_000_000))
XCTAssertEqual("1.1P", c.string(from: 1_050_000_000_000_001))
}

func testE() {
let c = NumberCompactor(blankIfZero: false)
let c = NumberCompactor(ifZero: nil)
XCTAssertEqual("1.0E", c.string(from: 1_000_000_000_000_000_000))
XCTAssertEqual("1.0E", c.string(from: 1_000_000_000_000_000_000))
XCTAssertEqual("1.0E", c.string(from: 1_050_000_000_000_000_000))
Expand Down
16 changes: 8 additions & 8 deletions Tests/TimeCompactorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
class TimeCompactorTests: XCTestCase {

func testBlankIfZero() {
let c = TimeCompactor(blankIfZero: true, style: .short)
let c = TimeCompactor(ifZero: "", style: .short)

XCTAssertEqual("", c.string(from: 0.049))
XCTAssertEqual("", c.string(from: -0.049))
Expand All @@ -48,7 +48,7 @@ class TimeCompactorTests: XCTestCase {
}

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

XCTAssertEqual("59 seconds", c.string(from: 59))
XCTAssertEqual("59 seconds", c.string(from: 59.499))
Expand All @@ -71,7 +71,7 @@ class TimeCompactorTests: XCTestCase {
}

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

XCTAssertEqual("59s", c.string(from: 59))
XCTAssertEqual("59s", c.string(from: 59.499))
Expand All @@ -94,7 +94,7 @@ class TimeCompactorTests: XCTestCase {
}

func testPluralFull() {
let c = TimeCompactor(blankIfZero: false, style: .full)
let c = TimeCompactor(ifZero: nil, style: .full)

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

func testCompactDropTrailingDotZero() {
let c = TimeCompactor(blankIfZero: false, style: .short)
let c = TimeCompactor(ifZero: nil, style: .short)

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

func testShort() {
let c = TimeCompactor(blankIfZero: false, style: .short)
let c = TimeCompactor(ifZero: nil, style: .short)

XCTAssertEqual("-3818ky", c.string(from: -120_500_000_000_000))
XCTAssertEqual("-3.8ky", c.string(from: -120_500_000_000))
Expand Down Expand Up @@ -168,7 +168,7 @@ class TimeCompactorTests: XCTestCase {
}

func testFull() {
let c = TimeCompactor(blankIfZero: false, style: .full)
let c = TimeCompactor(ifZero: nil, style: .full)

XCTAssertEqual("-3818 millenia", c.string(from: -120_500_000_000_000))
XCTAssertEqual("-3.8 millenia", c.string(from: -120_500_000_000))
Expand Down Expand Up @@ -217,7 +217,7 @@ class TimeCompactorTests: XCTestCase {
}

func testCompactEU() {
let c = TimeCompactor(blankIfZero: false, style: .short)
let c = TimeCompactor(ifZero: nil, style: .short)
c.decimalSeparator = ","

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

0 comments on commit c92fe0a

Please sign in to comment.