Skip to content

Commit 6af5f43

Browse files
committed
Migrate to SafeCollection
1 parent 7bbaf79 commit 6af5f43

8 files changed

Lines changed: 120 additions & 94 deletions

File tree

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ language: objective-c
33
sudo: required
44
env:
55
global:
6-
- PROJECT="SafeIndex.xcodeproj"
7-
- SCHEME="SafeIndex-Package"
6+
- PROJECT="SafeCollection.xcodeproj"
7+
- SCHEME="SafeCollection-Package"
88
- IOS_SDK="iphonesimulator11.0"
99
- MACOS_SDK="macosx10.13"
1010
- TVOS_SDK="appletvsimulator11.0"
1111
- WATCHOS_SDK="watchsimulator4.0"
12-
- FRAMEWORK="SafeIndex"
12+
- FRAMEWORK="SafeCollection"
1313
matrix:
1414
- SDK="$IOS_SDK" TEST=1 DESTINATION="platform=iOS Simulator,name=iPhone 8,OS=11.0"
1515
- SDK="$MACOS_SDK" TEST=1 DESTINATION="arch=x86_64"
@@ -46,5 +46,5 @@ script:
4646

4747
after_success:
4848
- if [ $TEST == 1 ]; then
49-
bash <(curl -s https://codecov.io/bash) -X xcodeplist -J 'SafeIndex';
49+
bash <(curl -s https://codecov.io/bash) -X xcodeplist -J 'SafeCollection';
5050
fi

Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import PackageDescription
44

55
let package = Package(
6-
name: "SafeIndex",
6+
name: "SafeCollection",
77
products: [
8-
.library(name: "SafeIndex", targets: ["SafeIndex"]),
8+
.library(name: "SafeCollection", targets: ["SafeCollection"]),
99
],
1010
targets: [
11-
.target(name: "SafeIndex"),
12-
.testTarget(name: "SafeIndexTests", dependencies: ["SafeIndex"]),
11+
.target(name: "SafeCollection"),
12+
.testTarget(name: "SafeCollectionTests", dependencies: ["SafeCollection"]),
1313
]
1414
)

README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
SafeIndex
2-
=========
1+
# SafeCollection
32

43
![Swift](https://img.shields.io/badge/Swift-4.0-orange.svg)
5-
[![Build Status](https://travis-ci.org/devxoul/SafeIndex.svg)](https://travis-ci.org/devxoul/SafeIndex)
6-
[![CocoaPods](http://img.shields.io/cocoapods/v/SafeIndex.svg)](https://cocoapods.org/pods/SafeIndex)
4+
[![Build Status](https://travis-ci.org/devxoul/SafeCollection.svg)](https://travis-ci.org/devxoul/SafeCollection)
5+
[![CocoaPods](http://img.shields.io/cocoapods/v/SafeCollection.svg)](https://cocoapods.org/pods/SafeCollection)
76
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
87

9-
Optional Index for Swift.
8+
SafeCollection provides a safer way to deal with subscripts.
109

1110

12-
At a Glance
13-
-----------
11+
## At a Glance
1412

1513
```swift
1614
let arr = ["A", "B", "C"]
1715

1816
arr[0] // "A"
1917
arr[100] // fatal error: Array index out of range 😟
2018

21-
arr[safe: 0] // Optional("A")
22-
arr[safe: 100] // nil 😄
19+
arr.safe[0] // Optional("A")
20+
arr.safe[100] // nil 😄
2321
```
2422

25-
26-
Installation
27-
------------
23+
## Installation
2824

2925
- **For iOS 8+ projects** with [CocoaPods](https://cocoapods.org):
3026

3127
```ruby
32-
pod 'SafeIndex'
28+
pod 'SafeCollection'
3329
```
3430

3531
- **For iOS 8+ projects** with [Carthage](https://github.com/Carthage/Carthage):
3632

3733
```
38-
github "devxoul/SafeIndex"
34+
github "devxoul/SafeCollection"
3935
```
4036

4137
- **Using [Swift Package Manager](https://swift.org/package-manager)**:
@@ -46,13 +42,11 @@ Installation
4642
let package = Package(
4743
name: "MyAwesomeApp",
4844
dependencies: [
49-
.package(url: "https://github.com/devxoul/SafeIndex", "1.0.0"),
45+
.package(url: "https://github.com/devxoul/SafeCollection", "1.0.0"),
5046
]
5147
)
5248
```
5349

50+
## License
5451

55-
License
56-
-------
57-
58-
SafeIndex is under MIT license. See the [LICENSE](LICENSE) file for more info.
52+
SafeCollection is under MIT license. See the [LICENSE](LICENSE) file for more info.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Pod::Spec.new do |s|
2-
s.name = "SafeIndex"
2+
s.name = "SafeCollection"
33
s.version = "2.0.0"
4-
s.summary = "Optional Index for Swift"
5-
s.homepage = "https://github.com/devxoul/SafeIndex"
4+
s.summary = "Safe Collection for Swift"
5+
s.homepage = "https://github.com/devxoul/SafeCollection"
66
s.license = { :type => "MIT", :file => "LICENSE" }
77
s.author = { "Suyeol Jeon" => "devxoul@gmail.com" }
8-
s.source = { :git => "https://github.com/devxoul/SafeIndex.git",
8+
s.source = { :git => "https://github.com/devxoul/SafeCollection.git",
99
:tag => s.version.to_s }
1010
s.source_files = "Sources/**/*.swift"
1111
s.requires_arc = true
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
public struct SafeCollection<C>: Collection where C: Collection {
2+
public typealias Index = C.Index
3+
public typealias Element = Optional<C.Element>
4+
5+
private var collection: C
6+
7+
init(_ collection: C) {
8+
self.collection = collection
9+
}
10+
11+
public subscript(position: Index) -> Element {
12+
guard self.collection.indices.contains(position) else { return nil }
13+
return self.collection[position]
14+
}
15+
16+
public var startIndex: Index {
17+
return self.collection.startIndex
18+
}
19+
20+
public var endIndex: Index {
21+
return self.collection.endIndex
22+
}
23+
24+
public func index(after i: Index) -> Index {
25+
return self.collection.index(after: i)
26+
}
27+
}
28+
29+
public struct SafeMutableCollection<C>: MutableCollection where C: MutableCollection {
30+
public typealias Index = C.Index
31+
public typealias Element = Optional<C.Element>
32+
33+
fileprivate var collection: C
34+
35+
init(_ collection: C) {
36+
self.collection = collection
37+
}
38+
39+
public subscript(position: Index) -> Element {
40+
get {
41+
guard self.collection.indices.contains(position) else { return nil }
42+
return self.collection[position]
43+
}
44+
set {
45+
guard let value = newValue else { return }
46+
guard self.collection.indices.contains(position) else { return }
47+
self.collection[position] = value
48+
}
49+
}
50+
51+
public var startIndex: Index {
52+
return self.collection.startIndex
53+
}
54+
55+
public var endIndex: Index {
56+
return self.collection.endIndex
57+
}
58+
59+
public func index(after i: Index) -> Index {
60+
return self.collection.index(after: i)
61+
}
62+
}
63+
64+
public extension Collection {
65+
public var safe: SafeCollection<Self> {
66+
return .init(self)
67+
}
68+
}
69+
70+
public extension MutableCollection {
71+
public var safe: SafeMutableCollection<Self> {
72+
get { return .init(self) }
73+
set { self = newValue.collection }
74+
}
75+
}

Sources/SafeIndex/SafeIndex.swift

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import XCTest
2+
import SafeCollection
3+
4+
class SafeCollectionTests: XCTestCase {
5+
func testSafeCollection() {
6+
let arr = ["A", "B", "C"]
7+
XCTAssertEqual(arr.safe[0], "A")
8+
XCTAssertEqual(arr.safe[1], "B")
9+
XCTAssertEqual(arr.safe[2], "C")
10+
XCTAssertNil(arr.safe[3])
11+
12+
var mutableArr = ["A", "B", "C"]
13+
XCTAssertEqual(mutableArr.safe[0], "A")
14+
XCTAssertEqual(mutableArr.safe[1], "B")
15+
XCTAssertEqual(mutableArr.safe[2], "C")
16+
XCTAssertNil(mutableArr.safe[3])
17+
mutableArr.safe[2] = "D"
18+
XCTAssertEqual(mutableArr.safe[2], "D")
19+
}
20+
}

Tests/SafeIndexTests/SafeIndexTests.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)