Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from vapor/string-array
Browse files Browse the repository at this point in the history
removing unpredictable string array function
  • Loading branch information
loganwright authored Mar 23, 2017
2 parents f71ea20 + c5d63a7 commit d594121
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions Sources/Core/String+Polymorphic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,26 @@ extension String {

/// Attempts to convert the `String` to a `String`.
/// This always works.
public var string: String? {
public var string: String {
return self
}

/// Converts the string to a UTF8 array of bytes.
public var bytes: [UInt8] {
return [UInt8](self.utf8)
}
}

extension String {
/// Attempts to convert the `String` to an `Array`.
/// Comma separated items will be split into
/// multiple entries.
public var array: [String]? {
public func commaSeparatedArray() -> [String] {
return characters
.split(separator: ",")
.map { String($0) }
.map { $0.trimmedWhitespace() }
}

/// Converts the string to a UTF8 array of bytes.
public var bytes: [UInt8]? {
return [UInt8](self.utf8)
}
}

extension String {
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreTests/PolymorphicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PolymorphicTests: XCTestCase {

func testArray() {
let list = "oranges, apples , bananas, grapes"
let fruits = list.array?.flatMap { $0.string } ?? []
let fruits = list.commaSeparatedArray()
XCTAssert(fruits == ["oranges", "apples", "bananas", "grapes"])
}

Expand Down

0 comments on commit d594121

Please sign in to comment.