Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Packard <[email protected]>
  • Loading branch information
NguyenPhongVN committed Jan 22, 2022
1 parent ae028e9 commit cdbd56e
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 7 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<dict>
<key>SchemeUserState</key>
<dict>
<key>Builder.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>ConvertSwift-Package.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
Expand All @@ -12,12 +17,12 @@
<key>ConvertSwift.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<key>DataStructures.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>3</integer>
</dict>
<key>SwiftExtension-Package.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -27,6 +32,11 @@
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>Builder</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>ConvertSwift</key>
<dict>
<key>primary</key>
Expand Down
6 changes: 6 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ let package = Package(
.library(
name: "DataStructures",
targets: ["DataStructures"]),
.library(
name: "Builder",
targets: ["Builder"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -28,6 +31,9 @@ let package = Package(
.target(
name: "DataStructures",
dependencies: []),
.target(
name: "Builder",
dependencies: []),
.testTarget(
name: "ConvertSwiftTests",
dependencies: ["DataStructures", "ConvertSwift", "Quick", "Nimble"]),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ConvertSwift
# Swift Extension

A description of this package.

Expand Down
14 changes: 14 additions & 0 deletions Sources/Builder/ArrayBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

@resultBuilder
public enum ArrayBuilder<Element> {
public static func buildBlock(_ components: Element...) -> [Element] {
components
}
}

public extension Array {
init(@ArrayBuilder<Element> builder: () -> [Element]) {
self = builder()
}
}
16 changes: 16 additions & 0 deletions Sources/Builder/DictionaryBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation

@resultBuilder
public enum DictionaryBuilder<Key: Hashable, Value> {
static func buildBlock(_ components: Dictionary<Key, Value>...) -> Dictionary<Key, Value> {
components.reduce(into: [:]) {
$0.merge($1) { _, new in new }
}
}
}

public extension Dictionary {
init(@DictionaryBuilder<Key, Value> builder: () -> Dictionary) {
self = builder()
}
}
48 changes: 48 additions & 0 deletions Sources/Builder/StringBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Foundation

public protocol StringBuilerProtocol {
var value: String {get}
}

@resultBuilder
public enum StringBuilder {
public static func buildArray(_ strings: [[String]]) -> [String] {
strings.flatMap { $0 }
}

public static func buildBlock(_ strings: [String]...) -> [String] {
strings.flatMap { $0 }
}

public static func buildEither(first strings: [String]) -> [String] {
strings
}

public static func buildEither(second strings: [String]) -> [String] {
strings
}

public static func buildExpression(_ string: String) -> [String] {
[string]
}

public static func buildLimitedAvailability(_ string: [String]) -> [String] {
string
}

public static func buildOptional(_ strings: [String]?) -> [String] {
strings ?? []
}

public static func buildFinalResult(_ strings: [String]) -> [String] {
strings
}
}

extension String {

public init(separator: String = "", @StringBuilder builder: () -> [String]) {
self = builder().joined(separator: separator)
}

}
69 changes: 69 additions & 0 deletions Sources/Builder/TextBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation
import SwiftUI

#if canImport(SwiftUI)
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension String {
func toText() -> Text {
Text(self)
}
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Sequence where Element == Text {
func joined(separator: Text = Text("")) -> Text {
return reduce(Text("")) { (result, text) in
return result + separator + text
}
}
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@resultBuilder
public struct TextBuilder {
public static func buildArray(_ texts: [[Text]]) -> [Text] {
texts.flatMap { $0 }
}

public static func buildBlock(_ texts: [Text]...) -> [Text] {
texts.flatMap { $0 }
}

public static func buildEither(first texts: [Text]) -> [Text] {
texts
}

public static func buildEither(second texts: [Text]) -> [Text] {
texts
}

public static func buildExpression(_ string: String) -> [Text] {
[string.toText()]
}

public static func buildExpression(_ text: Text) -> [Text] {
[text]
}

public static func buildLimitedAvailability(_ texts: [Text]) -> [Text] {
texts
}

public static func buildOptional(_ texts: [Text]?) -> [Text] {
texts ?? []
}

public static func buildFinalResult(_ texts: [Text]) -> [Text] {
texts
}
}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Text {
init(separator: Text = Text(""), @TextBuilder builder: () -> [Text]) {
self = builder().joined(separator: separator)
}

init(separator: String = "", @TextBuilder builder: () -> [Text]) {
self.init(separator: Text(separator), builder: builder)
}
}
#endif
39 changes: 35 additions & 4 deletions Sources/DataStructures/Tree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,35 @@ public extension TreeNode {

//MARK: Remove
public extension TreeNode {
func remove(id: UUID) {
if self.id == id {
parent?.children.removeAll(where: {$0.id == id})
}
for child in children {
child.remove(id: id)
}
}

}

// MARK: Extension
//MARK: Search
public extension TreeNode where T: Equatable {
func remove(value: T) {
if self.value == value {
parent?.children.removeAll(where: {$0.value == value})
}
for child in children {
child.remove(value: value)
}
}
}


// MARK: Extension Get
public extension TreeNode {

/// Get array TreeNode
/// - Returns: Convert a TreeNode to an array TreeNode without children
func arrayTreeNode() -> [TreeNode<T>] {
var arrayTreeNode = [TreeNode<T>]()
arrayTreeNode.append(self)
Expand All @@ -63,6 +87,8 @@ public extension TreeNode {
return arrayTreeNode
}

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

/// Get level of TreeNode
var level: Int {
var index: Int = 0
if let parent = parent {
Expand All @@ -84,12 +111,16 @@ public extension TreeNode {
}
}

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

}

//MARK: Search
public extension TreeNode where T: Equatable {
func search(value: T) -> TreeNode? {
if value == self.value {
Expand All @@ -103,7 +134,7 @@ public extension TreeNode where T: Equatable {
return nil
}
}

//MARK: Search
public extension TreeNode {
func search(id: UUID) -> TreeNode? {
if self.id == id {
Expand All @@ -117,7 +148,7 @@ public extension TreeNode {
return nil
}
}

//MARK: description
extension TreeNode: CustomStringConvertible {
public var description: String {
var s = "\(value)"
Expand Down

0 comments on commit cdbd56e

Please sign in to comment.