-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjavascriptify.go
231 lines (214 loc) · 5.33 KB
/
javascriptify.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package reduxa
import (
"strings"
"unicode"
)
// JavaScriptify, based on Goify makes a valid JavaScript identifier out of any string.
// It does that by removing any non letter and non digit character and by making sure the first
// character is a letter or "_".
// JavaScriptify produces a "CamelCase" version of the string, if firstUpper is true the first character
// of the identifier is uppercase otherwise it's lowercase.
func JavaScriptify(str string, firstUpper bool, allowReserved bool) string {
runes := []rune(str)
// remove trailing invalid identifiers (makes code below simpler)
runes = removeTrailingInvalid(runes)
w, i := 0, 0 // index of start of word, scan
for i+1 <= len(runes) {
eow := false // whether we hit the end of a word
// remove leading invalid identifiers
runes = removeInvalidAtIndex(i, runes)
if i+1 == len(runes) {
eow = true
} else if !validIdentifier(runes[i]) {
// get rid of it
runes = append(runes[:i], runes[i+1:]...)
} else if runes[i+1] == '_' {
// underscore; shift the remainder forward over any run of underscores
eow = true
n := 1
for i+n+1 < len(runes) && runes[i+n+1] == '_' {
n++
}
copy(runes[i+1:], runes[i+n+1:])
runes = runes[:len(runes)-n]
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
// lower->non-lower
eow = true
}
i++
if !eow {
continue
}
// [w,i] is a word.
word := string(runes[w:i])
// is it one of our initialisms?
if u := strings.ToUpper(word); commonInitialisms[u] {
if firstUpper {
u = strings.ToUpper(u)
} else if w == 0 {
u = strings.ToLower(u)
}
// All the common initialisms are ASCII,
// so we can replace the bytes exactly.
copy(runes[w:], []rune(u))
} else if w > 0 && strings.ToLower(word) == word {
// already all lowercase, and not the first word, so uppercase the first character.
runes[w] = unicode.ToUpper(runes[w])
} else if w == 0 && strings.ToLower(word) == word && firstUpper {
runes[w] = unicode.ToUpper(runes[w])
}
if w == 0 && !firstUpper {
runes[w] = unicode.ToLower(runes[w])
}
//advance to next word
w = i
}
if allowReserved {
return string(runes)
}
return fixReserved(string(runes))
}
// Reserved JavaScript keywords
var JavaScriptReserved = map[string]bool{
// JavaScript Reserved Words
"break": true,
"case": true,
"comment": true,
"continue": true,
"default": true,
"delete": true,
"do": true,
"else": true,
"export": true,
"for": true,
"function": true,
"if": true,
"import": true,
"in": true,
"label": true,
"new": true,
"return": true,
"switch": true,
"this": true,
"var": true,
"void": true,
"while": true,
"with": true,
// ECMAScript Reserved Words
"catch": true,
"class": true,
"const": true,
"debugger": true,
"enum": true,
"extends": true,
"finally": true,
"super": true,
"throw": true,
"try": true,
// Others, not yet exhaustive
"alert": true,
"confirm": true,
"open": true,
"print": true,
"NaN": true,
"Date": true,
"constructor": true,
"assign": true,
"location": true,
"Location": true,
"window": true,
"Window": true,
// Java Keywords reserved by JavasScript
"abstract": true,
"boolean": true,
"byte": true,
"char": true,
"double": true,
"false": true,
"final": true,
"float": true,
"goto": true,
"implements": true,
"instanceof": true,
"int": true,
"interface": true,
"long": true,
"native": true,
"null": true,
"package": true,
"private": true,
"protected": true,
"public": true,
"short": true,
"static": true,
"synchronized": true,
"throws": true,
"transient": true,
"true": true,
}
// fixReserved appends an underscore on to JavaScript reserved keywords.
func fixReserved(w string) string {
if JavaScriptReserved[w] {
w += "_"
}
return w
}
// validIdentifier returns true if the rune is a letter or number
func validIdentifier(r rune) bool {
return unicode.IsLetter(r) || unicode.IsDigit(r)
}
// removeTrailingInvalid removes trailing invalid identifiers from runes.
func removeTrailingInvalid(runes []rune) []rune {
valid := len(runes) - 1
for ; valid >= 0 && !validIdentifier(runes[valid]); valid-- {
}
return runes[0 : valid+1]
}
// removeInvalidAtIndex removes consecutive invalid identifiers from runes starting at index i.
func removeInvalidAtIndex(i int, runes []rune) []rune {
valid := i
for ; valid < len(runes) && !validIdentifier(runes[valid]); valid++ {
}
return append(runes[:i], runes[valid:]...)
}
var commonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JMES": true,
"JSON": true,
"JWT": true,
"LHS": true,
"OK": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XSRF": true,
"XSS": true,
}