Skip to content

Commit 383944f

Browse files
committed
add some feature, add IdentifiedArray
1 parent 1657fb3 commit 383944f

18 files changed

+362
-80
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<array/>
5+
</plist>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>Builder.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>1</integer>
11+
</dict>
12+
<key>DataStructures.xcscheme_^#shared#^_</key>
13+
<dict>
14+
<key>orderHint</key>
15+
<integer>2</integer>
16+
</dict>
17+
<key>Transform.xcscheme_^#shared#^_</key>
18+
<dict>
19+
<key>orderHint</key>
20+
<integer>3</integer>
21+
</dict>
22+
<key>swift-extension-Package.xcscheme_^#shared#^_</key>
23+
<dict>
24+
<key>orderHint</key>
25+
<integer>0</integer>
26+
</dict>
27+
</dict>
28+
<key>SuppressBuildableAutocreation</key>
29+
<dict>
30+
<key>Builder</key>
31+
<dict>
32+
<key>primary</key>
33+
<true/>
34+
</dict>
35+
<key>DataStructures</key>
36+
<dict>
37+
<key>primary</key>
38+
<true/>
39+
</dict>
40+
<key>Transform</key>
41+
<dict>
42+
<key>primary</key>
43+
<true/>
44+
</dict>
45+
<key>UnitTests</key>
46+
<dict>
47+
<key>primary</key>
48+
<true/>
49+
</dict>
50+
<key>swift-extension</key>
51+
<dict>
52+
<key>primary</key>
53+
<true/>
54+
</dict>
55+
</dict>
56+
</dict>
57+
</plist>

Package.resolved

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,77 @@
11
{
2-
"object": {
3-
"pins": [
4-
{
5-
"package": "CwlCatchException",
6-
"repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git",
7-
"state": {
8-
"branch": null,
9-
"revision": "35f9e770f54ce62dd8526470f14c6e137cef3eea",
10-
"version": "2.1.1"
11-
}
12-
},
13-
{
14-
"package": "CwlPreconditionTesting",
15-
"repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git",
16-
"state": {
17-
"branch": null,
18-
"revision": "fb7a26374e8570ff5c68142e5c83406d6abae0d8",
19-
"version": "2.0.2"
20-
}
21-
},
22-
{
23-
"package": "Nimble",
24-
"repositoryURL": "https://github.com/Quick/Nimble.git",
25-
"state": {
26-
"branch": null,
27-
"revision": "c93f16c25af5770f0d3e6af27c9634640946b068",
28-
"version": "9.2.1"
29-
}
30-
},
31-
{
32-
"package": "Quick",
33-
"repositoryURL": "https://github.com/Quick/Quick.git",
34-
"state": {
35-
"branch": null,
36-
"revision": "bd86ca0141e3cfb333546de5a11ede63f0c4a0e6",
37-
"version": "4.0.0"
38-
}
39-
},
40-
{
41-
"package": "SwiftDocCPlugin",
42-
"repositoryURL": "https://github.com/apple/swift-docc-plugin",
43-
"state": {
44-
"branch": null,
45-
"revision": "9b1258905c21fc1b97bf03d1b4ca12c4ec4e5fda",
46-
"version": "1.2.0"
47-
}
48-
},
49-
{
50-
"package": "SymbolKit",
51-
"repositoryURL": "https://github.com/apple/swift-docc-symbolkit",
52-
"state": {
53-
"branch": null,
54-
"revision": "b45d1f2ed151d057b54504d653e0da5552844e34",
55-
"version": "1.0.0"
56-
}
57-
}
58-
]
59-
},
60-
"version": 1
2+
"pins" : [
3+
{
4+
"identity" : "cwlcatchexception",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/mattgallagher/CwlCatchException.git",
7+
"state" : {
8+
"revision" : "3ef6999c73b6938cc0da422f2c912d0158abb0a0",
9+
"version" : "2.2.0"
10+
}
11+
},
12+
{
13+
"identity" : "cwlpreconditiontesting",
14+
"kind" : "remoteSourceControl",
15+
"location" : "https://github.com/mattgallagher/CwlPreconditionTesting.git",
16+
"state" : {
17+
"revision" : "2ef56b2caf25f55fa7eef8784c30d5a767550f54",
18+
"version" : "2.2.1"
19+
}
20+
},
21+
{
22+
"identity" : "nimble",
23+
"kind" : "remoteSourceControl",
24+
"location" : "https://github.com/Quick/Nimble.git",
25+
"state" : {
26+
"revision" : "c93f16c25af5770f0d3e6af27c9634640946b068",
27+
"version" : "9.2.1"
28+
}
29+
},
30+
{
31+
"identity" : "quick",
32+
"kind" : "remoteSourceControl",
33+
"location" : "https://github.com/Quick/Quick.git",
34+
"state" : {
35+
"revision" : "bd86ca0141e3cfb333546de5a11ede63f0c4a0e6",
36+
"version" : "4.0.0"
37+
}
38+
},
39+
{
40+
"identity" : "swift-collections",
41+
"kind" : "remoteSourceControl",
42+
"location" : "https://github.com/apple/swift-collections",
43+
"state" : {
44+
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
45+
"version" : "1.1.0"
46+
}
47+
},
48+
{
49+
"identity" : "swift-docc-plugin",
50+
"kind" : "remoteSourceControl",
51+
"location" : "https://github.com/apple/swift-docc-plugin",
52+
"state" : {
53+
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
54+
"version" : "1.3.0"
55+
}
56+
},
57+
{
58+
"identity" : "swift-docc-symbolkit",
59+
"kind" : "remoteSourceControl",
60+
"location" : "https://github.com/apple/swift-docc-symbolkit",
61+
"state" : {
62+
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
63+
"version" : "1.0.0"
64+
}
65+
},
66+
{
67+
"identity" : "swift-identified-collections",
68+
"kind" : "remoteSourceControl",
69+
"location" : "https://github.com/pointfreeco/swift-identified-collections",
70+
"state" : {
71+
"revision" : "d1e45f3e1eee2c9193f5369fa9d70a6ddad635e8",
72+
"version" : "1.0.0"
73+
}
74+
}
75+
],
76+
"version" : 2
6177
}

Package.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,27 @@ let package = Package(
2222
.package(url: "https://github.com/Quick/Quick.git", from: "4.0.0"),
2323
.package(url: "https://github.com/Quick/Nimble.git", from: "9.2.0"),
2424
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
25+
.package(url: "https://github.com/apple/swift-collections", from: "1.0.2"),
26+
.package(url: "https://github.com/pointfreeco/swift-identified-collections", from: "1.0.0"),
2527
],
2628
targets: [
2729
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2830
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2931
.target(
3032
name: "Transform",
31-
dependencies: []),
33+
dependencies: [
34+
.product(name: "IdentifiedCollections", package: "swift-identified-collections"),
35+
.product(name: "OrderedCollections", package: "swift-collections"),
36+
]),
3237
.target(
3338
name: "DataStructures",
3439
dependencies: []),
3540
.target(
3641
name: "Builder",
37-
dependencies: []),
42+
dependencies: [
43+
.product(name: "IdentifiedCollections", package: "swift-identified-collections"),
44+
.product(name: "OrderedCollections", package: "swift-collections"),
45+
]),
3846
.testTarget(
3947
name: "UnitTests",
4048
dependencies: ["DataStructures", "Transform", "Quick", "Nimble"]),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import IdentifiedCollections
2+
import Foundation
3+
4+
@available(iOS 13, macOS 12, tvOS 13, watchOS 6, *)
5+
public extension IdentifiedArray where Element: Identifiable {
6+
init(@ArrayBuilder<Element> builder: () -> [Element]) where ID == Element.ID {
7+
var identifiedArray: IdentifiedArrayOf<Element> = []
8+
let items = builder()
9+
for item in items {
10+
identifiedArray.updateOrAppend(item)
11+
}
12+
self = identifiedArray
13+
}
14+
}

Sources/Transform/Collection/Dictionary+.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public extension Dictionary where Value: Hashable {
2222
func has(key: Self.Key) -> Bool {
2323
self[key] != nil
2424
}
25+
26+
func pick(keys: [Key]) -> [Key: Value] {
27+
keys.reduce(into: [Key: Value]()) { partialResult, item in
28+
partialResult[item] = self[item]
29+
}
30+
}
31+
32+
func keys(for value: Value) -> [Key] {
33+
return keys.filter({ self[$0] == value})
34+
}
2535
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import IdentifiedCollections
2+
3+
public extension IdentifiedArray {
4+
func toArray() -> [Element] {
5+
var array: [Element] = []
6+
for value in self {
7+
array.append(value)
8+
}
9+
return array
10+
}
11+
}
12+
13+
public extension IdentifiedArray where Element: Hashable {
14+
func toSet() -> Set<Element> {
15+
toArray().toSet()
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public extension Bool {
2+
3+
func toInt() -> Int {
4+
self ? 1 : 0
5+
}
6+
7+
func toString() -> String {
8+
self ? "true" : "false"
9+
}
10+
}

Sources/Transform/Fundamental/Double+.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,20 @@ public extension Double {
2020
func toFloat() -> Float {
2121
toString().toFloat()!
2222
}
23+
24+
func toNano() -> Double {
25+
self * 1_000_000_000
26+
}
27+
28+
func toGiga() -> Double {
29+
self / 1_000_000_000
30+
}
31+
32+
var nano: Double {
33+
self * 1_000_000_000
34+
}
35+
36+
var giga: Double {
37+
self / 1_000_000_000
38+
}
2339
}

Sources/Transform/Fundamental/Int+.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ public extension Int {
2525
return UInt(self)
2626
}
2727

28+
func toNano() -> Double {
29+
self.toDouble()/1_000_000_000
30+
}
31+
}
32+
33+
extension UInt64 {
34+
public init(seconds: Double) {
35+
self.init(seconds.nano)
36+
}
2837
}

Sources/Transform/Fundamental/URL+.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,15 @@ public extension URL {
44
func toString() -> String {
55
absoluteString
66
}
7+
8+
func toQueryParameters() -> [String: String]? {
9+
guard let queryItems = URLComponents(url: self, resolvingAgainstBaseURL: false)?.queryItems else {
10+
return nil
11+
}
12+
return Dictionary(queryItems.lazy.compactMap {
13+
guard let value = $0.value else { return nil }
14+
return ($0.name, value)
15+
}) { first, _ in first }
16+
}
17+
718
}
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import Foundation
2+
import IdentifiedCollections
3+
4+
public extension Array {
5+
@discardableResult
6+
func appending(value element: Element) -> Self {
7+
var copy = self
8+
copy.append(element)
9+
return copy
10+
}
11+
}
212

313
public extension Array where Element: Equatable {
4-
var unique: [Element] {
14+
var removedDuplicates: [Element] {
515
var uniqueValues: [Element] = []
616
forEach { item in
717
guard !uniqueValues.contains(item) else { return }
@@ -10,3 +20,25 @@ public extension Array where Element: Equatable {
1020
return uniqueValues
1121
}
1222
}
23+
24+
public extension Array where Element == [String: Any] {
25+
26+
func toData(options opt: JSONSerialization.WritingOptions = []) -> Data? {
27+
try? JSONSerialization.data(withJSONObject: self, options: opt)
28+
}
29+
}
30+
31+
32+
public extension Array where Element: Codable {
33+
34+
func toData(options opt: JSONSerialization.WritingOptions = []) -> Data? {
35+
compactMap({$0.toDictionary()}).toData(options: opt)
36+
}
37+
}
38+
39+
public extension IdentifiedArray where Element: Codable {
40+
41+
func toData(options opt: JSONSerialization.WritingOptions = []) -> Data? {
42+
toArray().toData(options: opt)
43+
}
44+
}

0 commit comments

Comments
 (0)