-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
148 lines (134 loc) · 3.59 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import "strings"
/**
* Get the type from a documentation command line
* @param {string} [line] The comment line to parse
* @returns {string,string}
*/
func GetType(line string) (string, string) {
array := Split(line, " ")
Type := ""
for _, word := range array {
if StartsWith(word, "{") && EndsWith(word, "}") {
Type = Remove(Remove(word, "{"), "}")
}
}
line = Replace(line, "{"+Type+"}", "")
return Type, line
}
/**
* Get the name from a documentation command line
* @param {string} [line] The comment line to parse
* @returns {string,string}
*/
func GetName(line string) (string, string) {
array := Split(line, " ")
Name := ""
for _, word := range array {
if StartsWith(word, "[") && EndsWith(word, "]") {
Name = Remove(Remove(word, "["), "]")
}
}
line = Replace(line, "["+Name+"]", "")
return Name, line
}
/**
* Split a line by a seperator
* @param {string} [line] The comment line to split
* @param {string} [seperator] The seperator to split line with
* @returns {[]string}
*/
func Split(line string, seperator string) []string {
return strings.Split(line, seperator)
}
/**
* Check if a line by starts with a prefix
* @param {string} [line] The comment line to check
* @param {string} [prefix] The prefix to check
* @returns {bool}
*/
func StartsWith(line string, prefix string) bool {
return strings.HasPrefix(line, prefix)
}
/**
* Check if a line by ends with a suffix
* @param {string} [line] The comment line to check
* @param {string} [suffix] The suffix to check
* @returns {bool}
*/
func EndsWith(line string, suffix string) bool {
return strings.HasSuffix(line, suffix)
}
/**
* Replace a word by another one
* @param {string} [line] The comment line to replace the word in
* @param {string} [replace] The word to replace
* @param {string} [with] The word to replace with
* @returns {string}
*/
func Replace(line string, replace string, with string) string {
return strings.ReplaceAll(line, replace, with)
}
/**
* Replace a word by another one
* @param {string} [line] The comment line to replace the word in
* @param {string} [replace] The word to remove
* @returns {string}
*/
func Remove(line string, remove string) string {
return strings.ReplaceAll(line, remove, "")
}
/**
* Trim spaces from a line
* @param {string} [line] The comment line to trim
* @returns {string}
*/
func Trim(line string) string {
return strings.TrimSpace(line)
}
/**
* Check if given line starts with func
* @param {string} [data] The comment line to check
* @returns {bool}
*/
func IsFunctionLine(data string) bool {
return strings.HasPrefix(data, "func")
}
/**
* Check if given line starts with type
* @param {string} [data] The comment line to check
* @returns {bool}
*/
func IsStructureLine(data string) bool {
return strings.HasPrefix(data, "type")
}
/**
* Check if given comment documents a function of structure
* @param {string} [data] The comment to check
* @returns {string}
*/
func IsFunctionOfStructureLine(data string) bool {
array := Split(Trim(Remove(data, "func")), " ")
if len(array) == 0 {
return false
}
return StartsWith(array[0], "(") && EndsWith(array[1], ")")
}
/**
* Check if given comment documents a function
* @param {string} [data] The comment to check
* @returns {bool}
*/
func IsFunction(data string) bool {
array := strings.Split(data, "\n")
return IsFunctionLine(array[len(array)-1])
}
/**
* Check if given comment documents a structure
* @param {string} [data] The comment to check
* @returns {bool}
*/
func IsStructure(data string) bool {
array := strings.Split(data, "\n")
return IsStructureLine(array[len(array)-1])
}