From 476cdc146106ddc546b55a8419ac251229e6f8be Mon Sep 17 00:00:00 2001 From: Max Howell Date: Fri, 15 Feb 2019 13:38:36 -0500 Subject: [PATCH] Add Path.components --- Sources/Path.swift | 9 +++++++++ Tests/PathTests/PathTests.swift | 5 +++++ Tests/PathTests/XCTestManifests.swift | 1 + 3 files changed, 15 insertions(+) diff --git a/Sources/Path.swift b/Sources/Path.swift index f60c9f8..84ff596 100644 --- a/Sources/Path.swift +++ b/Sources/Path.swift @@ -200,6 +200,15 @@ public struct Path: Equatable, Hashable, Comparable { } } + /** + Splits the string representation on the directory separator. + - Important: The first element is always "/" to be consistent with `NSString.pathComponents`. + */ + @inlinable + public var components: [String] { + return ["/"] + string.split(separator: "/").map(String.init) + } + //MARK: Pathing /** diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 9bafc54..9560286 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -588,4 +588,9 @@ class PathTests: XCTestCase { XCTAssertNil(Path("../foo")) XCTAssertNil(Path("./foo")) } + + func testPathComponents() throws { + XCTAssertEqual(Path.root.foo.bar.components, ["/", "foo", "bar"]) + XCTAssertEqual(Path.root.components, ["/"]) + } } diff --git a/Tests/PathTests/XCTestManifests.swift b/Tests/PathTests/XCTestManifests.swift index e43831b..80cfb8c 100644 --- a/Tests/PathTests/XCTestManifests.swift +++ b/Tests/PathTests/XCTestManifests.swift @@ -28,6 +28,7 @@ extension PathTests { ("testMoveInto", testMoveInto), ("testMoveTo", testMoveTo), ("testNoUndesiredSymlinkResolution", testNoUndesiredSymlinkResolution), + ("testPathComponents", testPathComponents), ("testReadlinkOnFileReturnsSelf", testReadlinkOnFileReturnsSelf), ("testReadlinkOnNonExistantFileThrows", testReadlinkOnNonExistantFileThrows), ("testReadlinkOnRelativeSymlink", testReadlinkOnRelativeSymlink),