Skip to content

Commit

Permalink
add some feature, add IdentifiedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenPhongVN committed Mar 16, 2024
1 parent 1657fb3 commit 383944f
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Builder.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>DataStructures.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>Transform.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
</dict>
<key>swift-extension-Package.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>Builder</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>DataStructures</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>Transform</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>UnitTests</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>swift-extension</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
134 changes: 75 additions & 59 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,61 +1,77 @@
{
"object": {
"pins": [
{
"package": "CwlCatchException",
"repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git",
"state": {
"branch": null,
"revision": "35f9e770f54ce62dd8526470f14c6e137cef3eea",
"version": "2.1.1"
}
},
{
"package": "CwlPreconditionTesting",
"repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state": {
"branch": null,
"revision": "fb7a26374e8570ff5c68142e5c83406d6abae0d8",
"version": "2.0.2"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "c93f16c25af5770f0d3e6af27c9634640946b068",
"version": "9.2.1"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "bd86ca0141e3cfb333546de5a11ede63f0c4a0e6",
"version": "4.0.0"
}
},
{
"package": "SwiftDocCPlugin",
"repositoryURL": "https://github.com/apple/swift-docc-plugin",
"state": {
"branch": null,
"revision": "9b1258905c21fc1b97bf03d1b4ca12c4ec4e5fda",
"version": "1.2.0"
}
},
{
"package": "SymbolKit",
"repositoryURL": "https://github.com/apple/swift-docc-symbolkit",
"state": {
"branch": null,
"revision": "b45d1f2ed151d057b54504d653e0da5552844e34",
"version": "1.0.0"
}
}
]
},
"version": 1
"pins" : [
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlCatchException.git",
"state" : {
"revision" : "3ef6999c73b6938cc0da422f2c912d0158abb0a0",
"version" : "2.2.0"
}
},
{
"identity" : "cwlpreconditiontesting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state" : {
"revision" : "2ef56b2caf25f55fa7eef8784c30d5a767550f54",
"version" : "2.2.1"
}
},
{
"identity" : "nimble",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Nimble.git",
"state" : {
"revision" : "c93f16c25af5770f0d3e6af27c9634640946b068",
"version" : "9.2.1"
}
},
{
"identity" : "quick",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Quick.git",
"state" : {
"revision" : "bd86ca0141e3cfb333546de5a11ede63f0c4a0e6",
"version" : "4.0.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-identified-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-identified-collections",
"state" : {
"revision" : "d1e45f3e1eee2c9193f5369fa9d70a6ddad635e8",
"version" : "1.0.0"
}
}
],
"version" : 2
}
12 changes: 10 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@ let package = Package(
.package(url: "https://github.com/Quick/Quick.git", from: "4.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "9.2.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-collections", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-identified-collections", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Transform",
dependencies: []),
dependencies: [
.product(name: "IdentifiedCollections", package: "swift-identified-collections"),
.product(name: "OrderedCollections", package: "swift-collections"),
]),
.target(
name: "DataStructures",
dependencies: []),
.target(
name: "Builder",
dependencies: []),
dependencies: [
.product(name: "IdentifiedCollections", package: "swift-identified-collections"),
.product(name: "OrderedCollections", package: "swift-collections"),
]),
.testTarget(
name: "UnitTests",
dependencies: ["DataStructures", "Transform", "Quick", "Nimble"]),
Expand Down
14 changes: 14 additions & 0 deletions Sources/Builder/IdentifiedArrayBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import IdentifiedCollections
import Foundation

@available(iOS 13, macOS 12, tvOS 13, watchOS 6, *)
public extension IdentifiedArray where Element: Identifiable {
init(@ArrayBuilder<Element> builder: () -> [Element]) where ID == Element.ID {
var identifiedArray: IdentifiedArrayOf<Element> = []
let items = builder()
for item in items {
identifiedArray.updateOrAppend(item)
}
self = identifiedArray
}
}
10 changes: 10 additions & 0 deletions Sources/Transform/Collection/Dictionary+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ public extension Dictionary where Value: Hashable {
func has(key: Self.Key) -> Bool {
self[key] != nil
}

func pick(keys: [Key]) -> [Key: Value] {
keys.reduce(into: [Key: Value]()) { partialResult, item in
partialResult[item] = self[item]
}
}

func keys(for value: Value) -> [Key] {
return keys.filter({ self[$0] == value})
}
}
17 changes: 17 additions & 0 deletions Sources/Transform/Collection/IdentifiedArray+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import IdentifiedCollections

public extension IdentifiedArray {
func toArray() -> [Element] {
var array: [Element] = []
for value in self {
array.append(value)
}
return array
}
}

public extension IdentifiedArray where Element: Hashable {
func toSet() -> Set<Element> {
toArray().toSet()
}
}
10 changes: 10 additions & 0 deletions Sources/Transform/Fundamental/Bool+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public extension Bool {

func toInt() -> Int {
self ? 1 : 0
}

func toString() -> String {
self ? "true" : "false"
}
}
16 changes: 16 additions & 0 deletions Sources/Transform/Fundamental/Double+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ public extension Double {
func toFloat() -> Float {
toString().toFloat()!
}

func toNano() -> Double {
self * 1_000_000_000
}

func toGiga() -> Double {
self / 1_000_000_000
}

var nano: Double {
self * 1_000_000_000
}

var giga: Double {
self / 1_000_000_000
}
}
9 changes: 9 additions & 0 deletions Sources/Transform/Fundamental/Int+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ public extension Int {
return UInt(self)
}

func toNano() -> Double {
self.toDouble()/1_000_000_000
}
}

extension UInt64 {
public init(seconds: Double) {
self.init(seconds.nano)
}
}
11 changes: 11 additions & 0 deletions Sources/Transform/Fundamental/URL+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ public extension URL {
func toString() -> String {
absoluteString
}

func toQueryParameters() -> [String: String]? {
guard let queryItems = URLComponents(url: self, resolvingAgainstBaseURL: false)?.queryItems else {
return nil
}
return Dictionary(queryItems.lazy.compactMap {
guard let value = $0.value else { return nil }
return ($0.name, value)
}) { first, _ in first }
}

}
34 changes: 33 additions & 1 deletion Sources/Transform/Important/ArrayExt.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import Foundation
import IdentifiedCollections

public extension Array {
@discardableResult
func appending(value element: Element) -> Self {
var copy = self
copy.append(element)
return copy
}
}

public extension Array where Element: Equatable {
var unique: [Element] {
var removedDuplicates: [Element] {
var uniqueValues: [Element] = []
forEach { item in
guard !uniqueValues.contains(item) else { return }
Expand All @@ -10,3 +20,25 @@ public extension Array where Element: Equatable {
return uniqueValues
}
}

public extension Array where Element == [String: Any] {

func toData(options opt: JSONSerialization.WritingOptions = []) -> Data? {
try? JSONSerialization.data(withJSONObject: self, options: opt)
}
}


public extension Array where Element: Codable {

func toData(options opt: JSONSerialization.WritingOptions = []) -> Data? {
compactMap({$0.toDictionary()}).toData(options: opt)
}
}

public extension IdentifiedArray where Element: Codable {

func toData(options opt: JSONSerialization.WritingOptions = []) -> Data? {
toArray().toData(options: opt)
}
}
Loading

0 comments on commit 383944f

Please sign in to comment.