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

Fix number formatting #408

Merged
merged 7 commits into from
Jan 29, 2025
Merged
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
2 changes: 1 addition & 1 deletion Sources/Ignite/Elements/Slide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public struct Slide: BlockHTML {
.style(
.init(.height, value: "100%"),
.init(.objectFit, value: "cover"),
.init(.opacity, value: backgroundOpacity.formatted())
.init(.opacity, value: backgroundOpacity.formatted(.nonLocalizedDecimal))
)
}

Expand Down
42 changes: 42 additions & 0 deletions Sources/Ignite/Extensions/FormatStyle-NonLocalizedDecimal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// FormatStyle-NonLocalizedDecimal.swift
// Ignite
// https://www.github.com/twostraws/Ignite
// See LICENSE for license information.
//

import Foundation

extension FormatStyle where Self == FloatingPointFormatStyle<Double>, FormatInput == Double {
/// A format style that displays a floating point number with one decimal place,
/// enforcing the use of a `.` as the decimal separator.
static var nonLocalizedDecimal: Self {
nonLocalizedDecimal(places: 1)
}

/// A format style that displays a floating point number enforcing the use of a `.` as the decimal separator.
/// - Parameter places: The number of decimal places to display. Defaults to 1.
static func nonLocalizedDecimal(places: Int = 1) -> Self {
let precision = max(0, places)
return FloatingPointFormatStyle()
.precision(.fractionLength(0...precision))
.locale(Locale(identifier: "en_US"))
}
}

extension FormatStyle where Self == FloatingPointFormatStyle<Float>, FormatInput == Float {
/// A format style that displays a floating point number with one decimal place,
/// enforcing the use of a `.` as the decimal separator.
static var nonLocalizedDecimal: Self {
nonLocalizedDecimal(places: 1)
}

/// A format style that displays a floating point number enforcing the use of a `.` as the decimal separator.
/// - Parameter places: The number of decimal places to display. Defaults to 1.
static func nonLocalizedDecimal(places: Int = 1) -> Self {
let precision = max(0, places)
return FloatingPointFormatStyle()
.precision(.fractionLength(0...precision))
.locale(Locale(identifier: "en_US"))
}
}
4 changes: 2 additions & 2 deletions Sources/Ignite/Modifiers/LineSpacing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ struct LineSpacingModifier: HTMLModifier {
func body(content: some HTML) -> any HTML {
if content.body.isComposite {
if let customHeight {
content.containerStyle(.init(.lineHeight, value: customHeight.formatted()))
content.containerStyle(.init(.lineHeight, value: customHeight.formatted(.nonLocalizedDecimal)))
} else if let presetHeight {
content.containerClass("lh-\(presetHeight.rawValue)")
}
} else {
if let customHeight {
content.style(.init(.lineHeight, value: customHeight.formatted()))
content.style(.init(.lineHeight, value: customHeight.formatted(.nonLocalizedDecimal)))
} else if let presetHeight {
content.class("lh-\(presetHeight.rawValue)")
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/Ignite/Modifiers/Opacity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// See LICENSE for license information.
//

import Foundation

/// A modifier that applies opacity styling to HTML elements
struct OpacityModifier: HTMLModifier {
/// The opacity value between 0% (transparent) and 100% (opaque)
Expand Down Expand Up @@ -32,9 +34,9 @@ struct OpacityModifier: HTMLModifier {
/// - Returns: The modified HTML with opacity applied
func body(content: some HTML) -> any HTML {
if let percentage, percentage != 100% {
content.style(.opacity, percentage.value.formatted())
content.style(.opacity, percentage.value.formatted(.nonLocalizedDecimal(places: 3)))
} else if let doubleValue, doubleValue != 1 {
content.style(.opacity, doubleValue.formatted())
content.style(.opacity, doubleValue.formatted(.nonLocalizedDecimal(places: 3)))
}
content
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/IgniteTesting/Modifiers/Opacity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,26 @@ struct OpacityTests {

#expect(output == "<img alt=\"\(image.description)\" src=\"\(image.path)\" style=\"opacity: 0.2\" />")
}

@Test("Checks that the opacity value is correctly formatted", arguments: [
(value: 0.123456, expected: "0.123"),
(value: 0.15, expected: "0.15"),
(value: 0.1, expected: "0.1"),
(value: 0.45678, expected: "0.457"),
(value: 0, expected: "0")
])
func opacityFormatting(testCase: (value: Double, expected: String)) async throws {
let element = Text("Test").opacity(testCase.value)
let output = element.render()

#expect(output == "<p style=\"opacity: \(testCase.expected)\">Test</p>")
}

@Test("Checks that full opacity is not rendered")
func fullOpacity() async throws {
let element = Text("Test").opacity(1)
let output = element.render()

#expect(output == "<p>Test</p>")
}
}
Loading