Skip to content

Commit

Permalink
Replace deprecated use of flatMap with compactMap
Browse files Browse the repository at this point in the history
  • Loading branch information
cezheng committed Apr 30, 2018
1 parent f30ae28 commit aad43c0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/Document.swift
Expand Up @@ -33,7 +33,7 @@ open class XMLDocument {
/// The string encoding for the document. This is NSUTF8StringEncoding if no encoding is set, or it cannot be calculated.
open fileprivate(set) lazy var encoding: String.Encoding = {
if let encodingName = ^-^self.cDocument.pointee.encoding {
let encoding = CFStringConvertIANACharSetNameToEncoding(encodingName as CFString!)
let encoding = CFStringConvertIANACharSetNameToEncoding(encodingName as CFString?)
if encoding != kCFStringEncodingInvalidId {
return String.Encoding(rawValue: UInt(CFStringConvertEncodingToNSStringEncoding(encoding)))
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Element.swift
Expand Up @@ -78,7 +78,7 @@ open class XMLElement: XMLNode {

/// The element's children elements.
open var children: [XMLElement] {
return LinkedCNodes(head: cNode.pointee.children).flatMap {
return LinkedCNodes(head: cNode.pointee.children).compactMap {
XMLElement(cNode: $0, document: self.document)
}
}
Expand All @@ -91,7 +91,7 @@ open class XMLElement: XMLNode {
- returns: all children of specified types
*/
open func childNodes(ofTypes types: [XMLNodeType]) -> [XMLNode] {
return LinkedCNodes(head: cNode.pointee.children, types: types).flatMap { node in
return LinkedCNodes(head: cNode.pointee.children, types: types).compactMap { node in
switch node.pointee.type {
case XMLNodeType.Element:
return XMLElement(cNode: node, document: self.document)
Expand Down Expand Up @@ -134,7 +134,7 @@ open class XMLElement: XMLNode {
- returns: The children elements.
*/
open func children(tag: XMLCharsComparable, inNamespace ns: XMLCharsComparable? = nil) -> [XMLElement] {
return LinkedCNodes(head: cNode.pointee.children).flatMap {
return LinkedCNodes(head: cNode.pointee.children).compactMap {
cXMLNode($0, matchesTag: tag, inNamespace: ns)
? XMLElement(cNode: $0, document: self.document) : nil
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/HTMLTests.swift
Expand Up @@ -114,9 +114,9 @@ class HTMLTests: XCTestCase {
let mixedNode = document.firstChild(css: "#ajax-error-message")
let childNodes = mixedNode?.childNodes(ofTypes: [.Element, .Text])
XCTAssertEqual(childNodes?.count, 5, "should have 5 child nodes")
XCTAssertEqual(childNodes?.flatMap { $0.toElement() }.count, 2, "should have 2 element nodes")
XCTAssertEqual(childNodes?.flatMap { $0.type == .Element ? $0 : nil }.count, 2, "should have 2 element nodes")
XCTAssertEqual(childNodes?.flatMap { $0.type == .Text ? $0 : nil }.count, 3, "should have 3 text nodes")
XCTAssertEqual(childNodes?.compactMap { $0.toElement() }.count, 2, "should have 2 element nodes")
XCTAssertEqual(childNodes?.compactMap { $0.type == .Element ? $0 : nil }.count, 2, "should have 2 element nodes")
XCTAssertEqual(childNodes?.compactMap { $0.type == .Text ? $0 : nil }.count, 3, "should have 3 text nodes")
}

func testNextSiblingDoesNotCrash() {
Expand Down

0 comments on commit aad43c0

Please sign in to comment.