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

composed removed [] #426

Merged
merged 5 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions Sources/Composable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ extension NSAttributedString {
string.endEditing()
return string
}

/// Compose an `NSAttributedString` by concatenating every item in
/// `composables` with `baseStyle` applied. The `separator` is inserted
/// between every item. `baseStyle` acts as the default style, and apply to
/// the `Composable` item only if the `Composable` does not have a style
/// value configured.
///
/// - parameter composables: A list of `Composable` to join into an
/// `NSAttributedString`.
/// - parameter baseStyle: The base style to apply to every `Composable`.
/// If no `baseStyle` is supplied, no additional
/// styling will be added.
/// - parameter separator: The separator to insert between every pair of
/// elements in `composables`.
/// - returns: A new `NSAttributedString`.
@nonobjc public static func composed(of composables: Composable..., baseStyle: StringStyle = StringStyle(), separator: Composable? = nil) -> NSAttributedString {
return .composed(of: composables, baseStyle: baseStyle, separator: separator)
}

public func styled(with style: StringStyle, _ overrideParts: StringStyle.Part...) -> NSAttributedString {
let newStyle = style.byAdding(overrideParts)
Expand Down
7 changes: 6 additions & 1 deletion Tests/ComposableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import AppKit
#if canImport(UIKit) || canImport(AppKit)

class ComposableTests: XCTestCase {

DevVenusK marked this conversation as resolved.
Show resolved Hide resolved
func robotImage() throws -> BONImage {
#if os(OSX)
let imageForTest = testBundle.image(forResource: "robot")
Expand All @@ -40,6 +40,11 @@ class ComposableTests: XCTestCase {
XCTAssertEqual("A-B-C", string.string)
}

func testBasicJoinVariadic() {
let string = NSAttributedString.composed(of: "A", "B", "C", separator: NSAttributedString(string: "-"))
XCTAssertEqual("A-B-C", string.string)
}

func testAttributesArePassedAlongExtend() throws {
let imageForTest = try robotImage()

Expand Down