-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspoonify.go
More file actions
146 lines (135 loc) · 2.48 KB
/
spoonify.go
File metadata and controls
146 lines (135 loc) · 2.48 KB
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
package main
import (
"errors"
"strings"
"github.com/gopherjs/gopherjs/js"
)
const (
stateStart = iota
stateV1
stateV1L
stateV1E
stateV2
stateV3
stateDiph
)
func main() {
js.Global.Set("spoon", map[string]interface{}{
"to": toSpoon,
"from": fromSpoon,
})
}
func isVowel(input rune) bool {
return strings.ContainsRune("AEIOUÄÖÜaeiouäöü", input)
}
func isValidDiphtong(input string) bool {
for _, diph := range []string{"ei", "au", "eu", "äu", "ie"} {
if strings.ToLower(input) == diph {
return true
}
}
return false
}
func spoonifyVowel(input string) string {
return string([]rune(input)[0]) + "lew" + strings.ToLower(input)
}
func toSpoon(input string) (output string) {
output = ""
stor := ""
for _, c := range input {
if !isVowel(c) {
if len(stor) > 0 {
output += spoonifyVowel(stor)
stor = ""
}
output += string(c)
} else {
if len(stor) == 0 {
stor = string(c)
} else {
if isValidDiphtong(stor + string(c)) {
output += spoonifyVowel(stor + string(c))
} else {
output += spoonifyVowel(stor) + spoonifyVowel(string(c))
}
stor = ""
}
}
}
if len(stor) > 0 {
output += spoonifyVowel(stor)
}
return
}
func fromSpoon(input string) (output string, err error) {
output = ""
err = nil
stor := ""
state := stateStart
upper := false
for _, c := range input {
switch state {
case stateStart:
if isVowel(c) {
state = stateV1
if strings.ToLower(string(c)) != string(c) {
upper = true
}
} else {
output += string(c)
}
case stateV1:
if c == rune('l') {
state = stateV1L
} else {
err = errors.New("Vowel not followed by 'l'")
return
}
case stateV1L:
if c == rune('e') {
state = stateV1E
} else {
err = errors.New("Vowel not followed by 'e'")
return
}
case stateV1E:
if c == rune('w') {
state = stateV2
} else {
err = errors.New("Vowel not followed by 'w'")
return
}
case stateV2:
if isVowel(c) {
stor = string(c)
state = stateV3
} else {
err = errors.New("'lew' not followed by vowel")
return
}
case stateV3:
st := stor
if upper {
st = strings.ToUpper(st)
}
if isValidDiphtong(st + string(c)) {
output += st + string(c)
state = stateStart
} else {
output += st
if isVowel(c) {
state = stateV1
} else {
output += string(c)
state = stateStart
}
}
stor = ""
upper = false
}
}
if len(stor) > 0 {
output += stor
}
return
}