-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword-finder.html
250 lines (234 loc) · 7.98 KB
/
word-finder.html
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Word Finder</title>
<link rel="icon" href="/favicon-32x32.png" type="image/gif" sizes="32x32">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<!-- // App -->
<div id="app">
<!-- // Navbar -->
<nav class="navbar has-shadow is-dark" role="navigation" aria-label="main navigation">
<div class="container">
<div :class="['navbar-brand', {'ml-4': isMobile}]">
<img src="/logo.png" width="70">
</div>
</div>
</nav>
<!-- // Breadcrumb -->
<section class="section py-0">
<nav class="breadcrumb is-black mt-4" aria-label="breadcrumbs">
<div class="container">
<ul>
<li><a href="/">Home</a></li>
<li class="is-active"><a href="#" aria-current="page">Word Finder</a></li>
</ul>
</div>
</nav>
</section>
<section class="section" v-cloak>
<div class="container">
<!-- // Form -->
<!-- // Word Length -->
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Length</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<input class="input" type="text" placeholder="Length"
v-model.number="wordLength"
@focus="prevWordLength = wordLength"
@blur="lengthChanged">
</div>
<p class="help">Must be >= 2</p>
</div>
</div>
</div>
<!-- // Word -->
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Letters</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<input class="input" type="text" placeholder="*l*ck"
v-model.trim="wordChars" @blur="wordChanged">
</div>
<p class="help">Max letters: {{wordLength}}</p>
<p class="help">* - Any Letter</p>
</div>
</div>
</div>
<!-- // Must have letters -->
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Must have Letters</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<input class="input" type="text" placeholder="b"
v-model.trim="mustChars" @blur="mustCharsChanged">
</div>
<p class="help">Max letters: {{wordLength}}</p>
</div>
</div>
</div>
<!-- // Other Letters -->
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Can have Letters</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<input class="input" type="text" placeholder="afjov"
v-model.trim="canChars" @blur="canCharsChanged">
</div>
</div>
</div>
</div>
<!-- // Submit -->
<div class="field is-horizontal">
<div class="field-label"> </div>
<div class="field-body">
<div class="field">
<div class="control">
<button class="button is-primary" @click="filterWords">
Find Words
</button>
</div>
</div>
</div>
</div>
<!-- // Filtered Words -->
<div class="field is-horizontal">
<div class="field-label"> </div>
<div class="field-body">
<div class="content">
<ul>
<li v-for="word in filteredWords.slice(0, 50)" :key="word">{{word}}</li>
</ul>
</div>
</div>
</div>
</div>
</section>
</div>
<!-- // JS Code -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.prod.js"></script>
<script>
// Utils
function isInteger(str) {
return /^\d+$/.test(str)
}
// Alpha string
const alphaStr = 'abcdefghijklmnopqrstuvwxyz'
// Vue App
const { createApp } = Vue
createApp({
data() {
return {
prevWordLength: 5,
wordLength: 5,
wordChars: "",
mustChars: "",
canChars: "",
allWords: [],
filteredWords: [],
isMobile: false
}
},
mounted() {
// Check is mobile
this.isMobile = window.matchMedia("only screen and (max-width: 760px)").matches
// Reset fields
this.resetFields()
// Fetch words
fetch('/words_alpha.txt')
.then(res => res.text())
.then(data => (this.allWords = data.split('\n').map(x => x.trim())))
},
methods: {
// Reset fields
resetFields() {
// Fill word
this.wordChars = '*'.repeat(this.wordLength)
this.mustChars = ""
this.canChars = ""
},
// Length Changed
lengthChanged() {
// Validate and reset
if (!isInteger(this.wordLength) || this.wordLength < 2)
this.wordLength = this.prevWordLength
// Skip if length unchanged
if (this.wordLength == this.prevWordLength) return
this.resetFields()
},
// Word Changed
wordChanged() {
// Change case and Replace invalid chars
this.wordChars = this.wordChars.toLowerCase().replace(/[^a-z*]/g, '*')
// Trim string
if (this.wordChars.length > this.wordLength)
this.wordChars = this.wordChars.slice(0, this.wordLength)
// Add chars
if (this.wordChars.length < this.wordLength)
this.wordChars = this.wordChars + '*'.repeat(this.wordLength - this.wordChars.length)
},
// Must have Changed
mustCharsChanged() {
// Remove duplicates, change case and Replace invalid chars
this.mustChars = [...new Set(this.mustChars.toLowerCase().split(''))]
.join('')
.replace(/[^a-z]/g, '*')
// Trim string
if (this.mustChars.length > this.wordLength)
this.mustChars = this.mustChars.slice(0, this.wordLength)
// Replace invalid chars
this.mustChars = this.mustChars.toLowerCase().replace(/[^a-z]/g, '')
},
// Can Have Changed
canCharsChanged() {
// Remove duplicates, change case and Replace invalid chars
this.canChars = [...new Set(this.canChars.toLowerCase().split(''))]
.join('')
.replace(/[^a-z]/g, '*')
// Replace invalid chars
this.canChars = this.canChars.toLowerCase().replace(/[^a-z]/g, '')
},
// Filter words
filterWords() {
// Chars for regex
let otherChars = this.canChars === ''
? 'a-z'
: Array.from(
new Set((this.wordChars.replace(/[*]/g, '') + this.mustChars + this.canChars).split(''))
).join('')
// Word Regex
let wordRegex = new RegExp(this.wordChars.replace(/[*]/g, `[${otherChars}]`))
// Apply filter
this.filteredWords = this.allWords.filter(w => {
if (w.length != this.wordLength || !wordRegex.test(w)) return false
if (this.mustChars !== '' && this.mustChars.split('').some(mc => !w.includes(mc))) return false
return true
})
}
}
}).mount('#app')
</script>
</body>
</html>