Skip to content

Commit f64f9b9

Browse files
committed
Add helpers for calculating the delta percentage for stats
1 parent 466c36f commit f64f9b9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Factories/StatsV4DataHelper.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ final class StatsV4DataHelper {
8383
}
8484
}
8585

86+
// MARK: Delta Calculations
87+
88+
/// Creates the text showing the percent change from the previous `Decimal` value to the current `Decimal` value
89+
///
90+
static func createDeltaText(from previousValue: Decimal, to currentValue: Decimal, locale: Locale = Locale.current) -> String {
91+
let numberFormatter = NumberFormatter()
92+
numberFormatter.numberStyle = .percent
93+
numberFormatter.positivePrefix = numberFormatter.plusSign
94+
numberFormatter.locale = locale
95+
96+
guard previousValue > 0 else {
97+
return numberFormatter.string(from: 1) ?? "+100%"
98+
}
99+
100+
let deltaValue = ((currentValue - previousValue) / previousValue)
101+
return numberFormatter.string(from: deltaValue as NSNumber) ?? Constants.placeholderText
102+
}
103+
104+
/// Creates the text showing the percent change from the previous `Double` value to the current `Double` value
105+
///
106+
static func createDeltaText(from previousValue: Double, to currentValue: Double, locale: Locale = Locale.current) -> String {
107+
createDeltaText(from: Decimal(previousValue), to: Decimal(currentValue))
108+
}
109+
86110
// MARK: Stats Intervals
87111

88112
/// Returns the order stats intervals, ordered by date.

WooCommerce/WooCommerceTests/ViewRelated/Dashboard/StatsV4DataHelperTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ final class StatsV4DataHelperTests: XCTestCase {
99

1010
private let currencyFormatter = CurrencyFormatter(currencySettings: CurrencySettings()) // Default is US
1111
private let currencyCode = CurrencySettings().currencyCode
12+
private let locale = Locale(identifier: "en_US")
1213

1314
// MARK: Revenue Stats
1415

@@ -200,4 +201,54 @@ final class StatsV4DataHelperTests: XCTestCase {
200201
// Then
201202
XCTAssertEqual(conversionRate, "10%")
202203
}
204+
205+
// MARK: Delta Calculations
206+
207+
func test_createDeltaText_returns_expected_positive_text() {
208+
// Given
209+
let previousValue: Double = 100
210+
let currentValue: Double = 150
211+
212+
// When
213+
let deltaText = StatsV4DataHelper.createDeltaText(from: previousValue, to: currentValue, locale: locale)
214+
215+
// Then
216+
XCTAssertEqual(deltaText, "+50%")
217+
}
218+
219+
func test_createDeltaText_returns_expected_negative_text() {
220+
// Given
221+
let previousValue: Double = 150
222+
let currentValue: Double = 100
223+
224+
// When
225+
let deltaText = StatsV4DataHelper.createDeltaText(from: previousValue, to: currentValue, locale: locale)
226+
227+
// Then
228+
XCTAssertEqual(deltaText, "-33%")
229+
}
230+
231+
func test_createDeltaText_returns_100_percent_change_when_previous_value_is_zero() {
232+
// Given
233+
let previousValue: Double = 0
234+
let currentValue: Double = 10
235+
236+
// When
237+
let deltaText = StatsV4DataHelper.createDeltaText(from: previousValue, to: currentValue, locale: locale)
238+
239+
// Then
240+
XCTAssertEqual(deltaText, "+100%")
241+
}
242+
243+
func test_createDeltaText_returns_negative_100_percent_change_when_current_value_is_zero() {
244+
// Given
245+
let previousValue: Double = 10
246+
let currentValue: Double = 0
247+
248+
// When
249+
let deltaText = StatsV4DataHelper.createDeltaText(from: previousValue, to: currentValue, locale: locale)
250+
251+
// Then
252+
XCTAssertEqual(deltaText, "-100%")
253+
}
203254
}

0 commit comments

Comments
 (0)