-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftList.swift
42 lines (35 loc) · 1.02 KB
/
SwiftList.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// SwiftList.swift
//
// Created by Nick Fedoroff on 6/25/15.
// Copyright (c) 2015 Nick Fedoroff. All rights reserved.
//
import Foundation
public class SwiftList {
enum ListType {
case CommasOnly
case OxfordComma
case AllAnds
}
class func listify(#array: [String], listType: ListType, andString: String?) -> String {
switch listType {
case .CommasOnly:
return join(", ", array)
case .OxfordComma:
var newArray = array
var allButLast = newArray.removeLast()
var commas = join(", ", newArray)
if array.count < 2 {
return array.first!
} else if array.count < 3 {
return join(" \(andType!) ", array)
} else {
return join(", \(andType!) ", [commas, allButLast])
}
case .AllAnds:
return join(" \(andType!) ", array)
default:
break
}
}
}