Skip to content

Commit cdbd56e

Browse files
committed
Update README.md
Signed-off-by: Mike Packard <[email protected]>
1 parent ae028e9 commit cdbd56e

File tree

9 files changed

+201
-7
lines changed

9 files changed

+201
-7
lines changed

.swiftpm/xcode/xcuserdata/nguyenphong.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
<dict>
55
<key>SchemeUserState</key>
66
<dict>
7+
<key>Builder.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>1</integer>
11+
</dict>
712
<key>ConvertSwift-Package.xcscheme_^#shared#^_</key>
813
<dict>
914
<key>orderHint</key>
@@ -12,12 +17,12 @@
1217
<key>ConvertSwift.xcscheme_^#shared#^_</key>
1318
<dict>
1419
<key>orderHint</key>
15-
<integer>1</integer>
20+
<integer>2</integer>
1621
</dict>
1722
<key>DataStructures.xcscheme_^#shared#^_</key>
1823
<dict>
1924
<key>orderHint</key>
20-
<integer>2</integer>
25+
<integer>3</integer>
2126
</dict>
2227
<key>SwiftExtension-Package.xcscheme_^#shared#^_</key>
2328
<dict>
@@ -27,6 +32,11 @@
2732
</dict>
2833
<key>SuppressBuildableAutocreation</key>
2934
<dict>
35+
<key>Builder</key>
36+
<dict>
37+
<key>primary</key>
38+
<true/>
39+
</dict>
3040
<key>ConvertSwift</key>
3141
<dict>
3242
<key>primary</key>

Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ let package = Package(
1313
.library(
1414
name: "DataStructures",
1515
targets: ["DataStructures"]),
16+
.library(
17+
name: "Builder",
18+
targets: ["Builder"]),
1619
],
1720
dependencies: [
1821
// Dependencies declare other packages that this package depends on.
@@ -28,6 +31,9 @@ let package = Package(
2831
.target(
2932
name: "DataStructures",
3033
dependencies: []),
34+
.target(
35+
name: "Builder",
36+
dependencies: []),
3137
.testTarget(
3238
name: "ConvertSwiftTests",
3339
dependencies: ["DataStructures", "ConvertSwift", "Quick", "Nimble"]),

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ConvertSwift
1+
# Swift Extension
22

33
A description of this package.
44

Sources/Builder/ArrayBuilder.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
@resultBuilder
4+
public enum ArrayBuilder<Element> {
5+
public static func buildBlock(_ components: Element...) -> [Element] {
6+
components
7+
}
8+
}
9+
10+
public extension Array {
11+
init(@ArrayBuilder<Element> builder: () -> [Element]) {
12+
self = builder()
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
3+
@resultBuilder
4+
public enum DictionaryBuilder<Key: Hashable, Value> {
5+
static func buildBlock(_ components: Dictionary<Key, Value>...) -> Dictionary<Key, Value> {
6+
components.reduce(into: [:]) {
7+
$0.merge($1) { _, new in new }
8+
}
9+
}
10+
}
11+
12+
public extension Dictionary {
13+
init(@DictionaryBuilder<Key, Value> builder: () -> Dictionary) {
14+
self = builder()
15+
}
16+
}

Sources/Builder/StringBuilder.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Foundation
2+
3+
public protocol StringBuilerProtocol {
4+
var value: String {get}
5+
}
6+
7+
@resultBuilder
8+
public enum StringBuilder {
9+
public static func buildArray(_ strings: [[String]]) -> [String] {
10+
strings.flatMap { $0 }
11+
}
12+
13+
public static func buildBlock(_ strings: [String]...) -> [String] {
14+
strings.flatMap { $0 }
15+
}
16+
17+
public static func buildEither(first strings: [String]) -> [String] {
18+
strings
19+
}
20+
21+
public static func buildEither(second strings: [String]) -> [String] {
22+
strings
23+
}
24+
25+
public static func buildExpression(_ string: String) -> [String] {
26+
[string]
27+
}
28+
29+
public static func buildLimitedAvailability(_ string: [String]) -> [String] {
30+
string
31+
}
32+
33+
public static func buildOptional(_ strings: [String]?) -> [String] {
34+
strings ?? []
35+
}
36+
37+
public static func buildFinalResult(_ strings: [String]) -> [String] {
38+
strings
39+
}
40+
}
41+
42+
extension String {
43+
44+
public init(separator: String = "", @StringBuilder builder: () -> [String]) {
45+
self = builder().joined(separator: separator)
46+
}
47+
48+
}

Sources/Builder/TextBuilder.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Foundation
2+
import SwiftUI
3+
4+
#if canImport(SwiftUI)
5+
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
6+
public extension String {
7+
func toText() -> Text {
8+
Text(self)
9+
}
10+
}
11+
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
12+
public extension Sequence where Element == Text {
13+
func joined(separator: Text = Text("")) -> Text {
14+
return reduce(Text("")) { (result, text) in
15+
return result + separator + text
16+
}
17+
}
18+
}
19+
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
20+
@resultBuilder
21+
public struct TextBuilder {
22+
public static func buildArray(_ texts: [[Text]]) -> [Text] {
23+
texts.flatMap { $0 }
24+
}
25+
26+
public static func buildBlock(_ texts: [Text]...) -> [Text] {
27+
texts.flatMap { $0 }
28+
}
29+
30+
public static func buildEither(first texts: [Text]) -> [Text] {
31+
texts
32+
}
33+
34+
public static func buildEither(second texts: [Text]) -> [Text] {
35+
texts
36+
}
37+
38+
public static func buildExpression(_ string: String) -> [Text] {
39+
[string.toText()]
40+
}
41+
42+
public static func buildExpression(_ text: Text) -> [Text] {
43+
[text]
44+
}
45+
46+
public static func buildLimitedAvailability(_ texts: [Text]) -> [Text] {
47+
texts
48+
}
49+
50+
public static func buildOptional(_ texts: [Text]?) -> [Text] {
51+
texts ?? []
52+
}
53+
54+
public static func buildFinalResult(_ texts: [Text]) -> [Text] {
55+
texts
56+
}
57+
}
58+
59+
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
60+
public extension Text {
61+
init(separator: Text = Text(""), @TextBuilder builder: () -> [Text]) {
62+
self = builder().joined(separator: separator)
63+
}
64+
65+
init(separator: String = "", @TextBuilder builder: () -> [Text]) {
66+
self.init(separator: Text(separator), builder: builder)
67+
}
68+
}
69+
#endif

Sources/DataStructures/Tree.swift

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,35 @@ public extension TreeNode {
4949

5050
//MARK: Remove
5151
public extension TreeNode {
52+
func remove(id: UUID) {
53+
if self.id == id {
54+
parent?.children.removeAll(where: {$0.id == id})
55+
}
56+
for child in children {
57+
child.remove(id: id)
58+
}
59+
}
5260

5361
}
5462

55-
// MARK: Extension
63+
//MARK: Search
64+
public extension TreeNode where T: Equatable {
65+
func remove(value: T) {
66+
if self.value == value {
67+
parent?.children.removeAll(where: {$0.value == value})
68+
}
69+
for child in children {
70+
child.remove(value: value)
71+
}
72+
}
73+
}
74+
75+
76+
// MARK: Extension Get
5677
public extension TreeNode {
78+
79+
/// Get array TreeNode
80+
/// - Returns: Convert a TreeNode to an array TreeNode without children
5781
func arrayTreeNode() -> [TreeNode<T>] {
5882
var arrayTreeNode = [TreeNode<T>]()
5983
arrayTreeNode.append(self)
@@ -63,6 +87,8 @@ public extension TreeNode {
6387
return arrayTreeNode
6488
}
6589

90+
/// Get array TreeNode
91+
/// - Returns: Convert a TreeNode to an array TreeNode without children and hiddenChildren
6692
func arrayTreeNodeWithRemoveHiddenChildren() -> [TreeNode<T>] {
6793
var allTreeNode = [TreeNode<T>]()
6894
allTreeNode.append(self)
@@ -74,6 +100,7 @@ public extension TreeNode {
74100
return allTreeNode
75101
}
76102

103+
/// Get level of TreeNode
77104
var level: Int {
78105
var index: Int = 0
79106
if let parent = parent {
@@ -84,12 +111,16 @@ public extension TreeNode {
84111
}
85112
}
86113

87-
func children(with level: Int) -> [TreeNode<T>] {
114+
/// Get all TreeNode in one Level
115+
/// - Parameter level: level
116+
/// - Returns: return an array TreeNode without children
117+
func allchildrenInLevel(_ level: Int) -> [TreeNode<T>] {
88118
arrayTreeNode().filter{$0.level == level}
89119
}
90120

91121
}
92122

123+
//MARK: Search
93124
public extension TreeNode where T: Equatable {
94125
func search(value: T) -> TreeNode? {
95126
if value == self.value {
@@ -103,7 +134,7 @@ public extension TreeNode where T: Equatable {
103134
return nil
104135
}
105136
}
106-
137+
//MARK: Search
107138
public extension TreeNode {
108139
func search(id: UUID) -> TreeNode? {
109140
if self.id == id {
@@ -117,7 +148,7 @@ public extension TreeNode {
117148
return nil
118149
}
119150
}
120-
151+
//MARK: description
121152
extension TreeNode: CustomStringConvertible {
122153
public var description: String {
123154
var s = "\(value)"

0 commit comments

Comments
 (0)