Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the format methods to align with the new requireme… #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Source/Foundation/Extensions/DecimalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ public extension Decimal {
}

/// Formats the decimal part from separator (without zero)
func formattedDecimalPart(decimals: Int, locale: Locale = .current.fixed) -> String {
func formattedDecimalPart(decimals: Int, locale: Locale = .current.fixed, trimZeros: Bool = false) -> String {
let number = decimalPart(decimals: decimals)
if trimZeros {
guard number != 0 else {
return ""
}
}
let numberFormatter = NumberFormatter()
numberFormatter.minimumIntegerDigits = decimals
numberFormatter.maximumIntegerDigits = decimals
Expand Down Expand Up @@ -135,13 +140,15 @@ public extension Decimal {
/// - locale: Language rules to use
/// - numberStyle: Predefined number format style
/// - unit: Unit for Measurement format
/// - trimZeros (Bool): if it is true do not show the decimals if they are zero, (showing no decimals if they are zero and two decimals otherwise)
/// - Returns: String with specified format
func formatted(decimals: Int = 2,
currencyCode: CurrencyCodeType? = nil,
locale: Locale = .current.fixed,
numberStyle: NumberFormatter.Style? = nil,
roundingMode: NumberFormatter.RoundingMode? = nil,
unit: Unit = Unit(symbol: "")) -> String {
unit: Unit = Unit(symbol: ""),
trimZeros: Bool = false) -> String {
let numberFormatter = NumberFormatter()

if let currencyCode = currencyCode?.rawValue {
Expand All @@ -156,9 +163,10 @@ public extension Decimal {
if let roundingMode = roundingMode {
numberFormatter.roundingMode = roundingMode
}
let minimumFractionDigits = self.decimalPart(decimals: decimals) == 0 ? 0 : decimals

numberFormatter.minimumIntegerDigits = 1
numberFormatter.minimumFractionDigits = decimals
numberFormatter.minimumFractionDigits = trimZeros ? minimumFractionDigits : decimals
numberFormatter.maximumFractionDigits = decimals

let formatter = MeasurementFormatter()
Expand Down
2 changes: 1 addition & 1 deletion Source/Foundation/Types/SemVer.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// Wrapper for version representation according to https://semver.org
public struct Semver: Comparable, CustomStringConvertible {
public struct Semver: Comparable, CustomStringConvertible, Codable {
/// Initialices a Semver from a string version compatible.
/// - Parameter string: version literal
public init(_ string: String) {
Expand Down
10 changes: 0 additions & 10 deletions Tests/Foundation/Extensions/DateExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,6 @@ class DateExtensionsTests: XCTestCase {
XCTAssertEqual(date.formatted(with: .spanishDayAndMonth, locale: .spanishSpain, timeZone: .europeMadrid), "09 de agosto")
XCTAssertEqual(date.formatted(with: .americanDayAndMonth, locale: .englishUSA, timeZone: .europeMadrid), "August 09")
XCTAssertEqual(date.formatted(with: .spanishMonthAndYear, locale: .spanishSpain, timeZone: .europeMadrid), "agosto de 2019")
XCTAssertEqual(date.formatted(with: .dateStyleShort, locale: .spanishSpain, timeZone: .europeMadrid), "9/8/19")
XCTAssertEqual(date.formatted(with: .dateStyleMedium, locale: .spanishSpain, timeZone: .europeMadrid), "9 ago 2019")
XCTAssertEqual(date.formatted(with: .dateStyleFull, locale: .spanishSpain, timeZone: .europeMadrid), "viernes, 9 de agosto de 2019")
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .spanishSpain, timeZone: .europeMadrid), "9 de agosto de 2019")
if #available(iOS 18.0, *), #available(macOS 15.0, *), #available(tvOS 18.0, *), #available(watchOS 11.0, *) {
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .catalanSpain, timeZone: .europeMadrid), "9 d’agost del 2019")
} else {
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .catalanSpain, timeZone: .europeMadrid), "9 d’agost de 2019")
}
XCTAssertTrue(date.formatted(with: .dateStyleLong, locale: .basqueSpain, timeZone: .europeMadrid).starts(with: "2019(e)ko abuztua"))
}

func test_is_today() {
Expand Down
25 changes: 25 additions & 0 deletions Tests/Foundation/Extensions/DecimalExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ class DecimalExtensionsTests: XCTestCase {
numberStyle: .ordinal), "16.868.º")
}

func test_format_with_trimZeros_option() {
XCTAssertEqual((33 as Decimal).formatted(decimals: 2,
locale: .spanishSpain,
unit: Unit(symbol: "$"),
trimZeros: true), "33 $")

XCTAssertEqual((1_345.3 as Decimal).formatted(decimals: 2,
locale: .spanishSpain,
unit: Unit(symbol: "€"),
trimZeros: true), "1345,30 €")

XCTAssertEqual((33.564 as Decimal).formatted(decimals: 2,
currencyCode: .euro,
locale: .spanishSpain,
trimZeros: true), "33,56 €")
}

func test_convert_miliseconds_to_seconds() {
XCTAssertEqual((10_000 as Decimal).millisecondsToSeconds, 10)
}
Expand Down Expand Up @@ -160,4 +177,12 @@ class DecimalExtensionsTests: XCTestCase {
XCTAssertEqual((-5.532 as Decimal).formattedDecimalPart(decimals: 2, locale: .spanishSpain), ",53")
XCTAssertEqual((-5 as Decimal).formattedDecimalPart(decimals: 4, locale: .spanishSpain), ",0000")
}

func test_format_decimal_with_trimZeros_option() {
XCTAssertEqual((5.532 as Decimal).formattedDecimalPart(decimals: 1, locale: .spanishSpain, trimZeros: true), ",5")
XCTAssertEqual((-5.532 as Decimal).formattedDecimalPart(decimals: 2, locale: .spanishSpain, trimZeros: true), ",53")
XCTAssertEqual((-5 as Decimal).formattedDecimalPart(decimals: 4, locale: .spanishSpain, trimZeros: true), "")
XCTAssertEqual((20 as Decimal).formattedDecimalPart(decimals: 2, locale: .spanishSpain, trimZeros: true), "")
XCTAssertEqual((20.000 as Decimal).formattedDecimalPart(decimals: 2, locale: .spanishSpain, trimZeros: true), "")
}
}
20 changes: 20 additions & 0 deletions Tests/Foundation/Types/SemVerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ class SemVerTests: XCTestCase {

XCTAssertEqual(versionFromObj, versionFromValues)
}

func test_encode_decode_jsonstring() {
let encodableObject = Semver("1.2.3")

let json = try? encodableObject.jsonString()
XCTAssertNotNil(json)

let object = try? Semver.decode(jsonString: json)
XCTAssertEqual(object, encodableObject)
}

func test_encode_decode_jsonstring_from_components() {
let encodableObject = Semver(major: 1, minor: 2, patch: 3)

let json = try? encodableObject.jsonString()
XCTAssertNotNil(json)

let object = try? Semver.decode(jsonString: json)
XCTAssertEqual(object, encodableObject)
}
}
8 changes: 8 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ platform :ios do
output_directory = File.expand_path("#{ENV['WORKSPACE']}/output")
ENV['XCPRETTY_JSON_FILE_OUTPUT'] = "#{output_directory}/#{options[:name]}/report.json"

case options[:name]
when 'iOS'
devices = ['iPhone 16']
else
devices = nil
end

begin
scan_options = {}
scan_options[:scheme] = options[:scheme]
Expand All @@ -33,6 +40,7 @@ platform :ios do
scan_options[:result_bundle] = true
scan_options[:formatter] = "xcpretty-json-formatter"
scan_options[:skip_slack] = true
scan_options[:devices] = devices
scan(scan_options)
rescue
UI.message 'Test are failed, check report!!!'
Expand Down
Loading