Skip to content

Commit

Permalink
Merge pull request #30 from kazuhiro4949/feature/optional_operator
Browse files Browse the repository at this point in the history
add operator for optional
  • Loading branch information
kazuhiro4949 committed Oct 14, 2019
2 parents 4b4f069 + 8cd79ce commit b62bfe1
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
76 changes: 76 additions & 0 deletions StringStylizer/Operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,62 @@ public func + (left: NSAttributedString, right: NSAttributedString) -> NSAttribu
return concat
}

/**
combine two NSAttributedString object
- paramter left:NSAttributedString?
- paramter right:NSAttributedString
- returns: NSAttributedString
*/
public func + (left: NSAttributedString?, right: NSAttributedString) -> NSAttributedString {
let concat = NSMutableAttributedString()
if let left = left {
concat.append(left)
}
concat.append(right)
return concat
}

/**
combine two NSAttributedString object
- paramter left:NSAttributedString
- paramter right:NSAttributedString?
- returns: NSAttributedString
*/
public func + (left: NSAttributedString, right: NSAttributedString?) -> NSAttributedString {
let concat = NSMutableAttributedString()
concat.append(left)
if let right = right {
concat.append(right)
}
return concat
}

/**
combine two NSAttributedString object
- paramter left:NSAttributedString?
- paramter right:NSAttributedString?
- returns: NSAttributedString
*/
public func + (left: NSAttributedString?, right: NSAttributedString?) -> NSAttributedString {
let concat = NSMutableAttributedString()
if let left = left {
concat.append(left)
}
if let right = right {
concat.append(right)
}
return concat
}

/**
append NSAttributedString object
Expand Down Expand Up @@ -70,3 +126,23 @@ public func + (left: NSAttributedString, right: NSTextAttachment) -> NSAttribute
public func + (left: NSTextAttachment, right: NSAttributedString) -> NSAttributedString {
return NSAttributedString(attachment: left) + right
}

/// combine two NSAttributedString and NSTextAttachment objects
///
/// - Parameters:
/// - left: NSAttributedString
/// - right: NSTextAttachment?
/// - Returns: combinded NSAttributedString
public func + (left: NSAttributedString, right: NSTextAttachment?) -> NSAttributedString {
return left + right.flatMap(NSAttributedString.init(attachment:))
}

/// combine two NSTextAttachment and NSAttributedString objects
///
/// - Parameters:
/// - left: NSTextAttachment?
/// - right: NSAttributedString
/// - Returns: combinded NSAttributedString
public func + (left: NSTextAttachment?, right: NSAttributedString) -> NSAttributedString {
return left.flatMap(NSAttributedString.init(attachment:)) + right
}
26 changes: 26 additions & 0 deletions StringStylizerTests/StringStylizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ class StringStylizerTests: XCTestCase {
XCTAssert(combined.isEqual(expected), "optional StringStylized strings do not add any content to the resulting NSAttributedString")
}

func testOptionalAttributedString() {
let optional: NSAttributedString? = "a".stylize().attr
let notOptional: NSAttributedString = "b".stylize().attr

do {
let actual = optional + notOptional

let expected = NSAttributedString(string: "ab")
XCTAssertEqual(actual, expected)
}

do {
let actual = notOptional + optional

let expected = NSAttributedString(string: "ba")
XCTAssertEqual(actual, expected)
}

do {
let actual = optional + optional

let expected = NSAttributedString(string: "aa")
XCTAssertEqual(actual, expected)
}
}

// MARK:- private
fileprivate func rgb(_ rgb: UInt, alpha: Double) -> UIColor {
return UIColor(
Expand Down

0 comments on commit b62bfe1

Please sign in to comment.