-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
194 lines (165 loc) · 4.37 KB
/
Copy pathindex.js
File metadata and controls
194 lines (165 loc) · 4.37 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
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
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import axios from 'axios';
class SearchEngine {
wordsURL = 'https://raw.githubusercontent.com/tiborsimon/words-hun/master/words/words.json.min?nocache=' + new Date().getTime()
words_key = 'words'
collection_key = 'collection'
constructor() {
this.words = this.loadWords()
this.collection = this.loadCollection()
this.result = []
}
loadWords = () => {
if (this.words_key in localStorage) {
let words = JSON.parse(localStorage.getItem(this.words_key))
console.log('Words found in local storage')
return words
}
axios.get(this.wordsURL)
.then(res => {
console.log('Words downloaded')
localStorage.setItem(this.words_key, JSON.stringify(res.data))
this.words = res.data
return res.data
})
}
loadCollection = () => {
if (this.collection_key in localStorage) {
let collection = JSON.parse(localStorage.getItem(this.collection_key))
console.log('Collection found in local storage')
return collection
}
return []
}
saveCollection = () => {
localStorage.setItem(this.collection_key, JSON.stringify(this.collection))
}
getCollection = () => {
return this.collection.map((word) => ({
word,
selected: true
}))
}
select = (word) => {
console.log('select called')
if (this.collection.indexOf(word) === -1) {
this.collection.push(word)
this.collection.sort()
this.saveCollection()
console.log(`"${word}" was added`)
}
}
remove = (word) => {
const index = this.collection.indexOf(word)
if (index > -1) {
this.collection.splice(index, 1);
this.saveCollection()
console.log(`"${word}" was removed`)
}
}
filterMethod = (word, method, len) => {
switch (method) {
case "irrelevant":
return true
case "min":
return word.length >= len
case "max":
return word.length <= len
case "exactly":
return word.length === len
default:
break
}
}
permut = (xs) => {
let ret = [];
for (let i = 0; i < xs.length; i = i + 1) {
let rest = this.permut(xs.slice(0, i).concat(xs.slice(i + 1)));
if (!rest.length) {
ret.push([xs[i]])
} else {
for (let j = 0; j < rest.length; j = j + 1) {
ret.push([xs[i]].concat(rest[j]))
}
}
}
return ret
}
generateRegex = (spec) => {
const { first, inner, last } = spec
let patternString = ''
if (inner.length > 0) {
// assemble first
if (first.length > 0) {
patternString = `^${first}.*(`
} else {
patternString = `^.+(`
}
// assemble inner
let r = /(["'])(?:\\\1|.)*?\1/g
let m;
let letters = inner
let quotes = []
do {
m = r.exec(inner)
if (m) {
quotes.push(m)
}
} while (m)
let Q = []
if (quotes) {
for (let q of quotes) {
Q.push(q[0].slice(1,-1))
letters = letters.replace(q[0],'')
}
}
letters = new Set(letters)
letters = Array.from(letters).concat(Q)
for (let p of this.permut(letters)) {
patternString += '('
for (let c of p) {
patternString += `${c}.*`
}
patternString += ')|'
}
patternString = patternString.slice(0, -1)
console.log(patternString)
// assemble last
if (last.length > 0) {
patternString += `).*${last}$`
} else {
patternString += `).+$`
}
} else {
patternString = `^${first}.*${last}$`
}
return new RegExp(patternString)
}
search = (spec) => {
const { method, len } = spec
const r = this.generateRegex(spec)
let result = []
for (let word of this.words) {
if (r.test(word.toLowerCase())) {
if (!this.filterMethod(word, method, len)) continue
result.push(word)
}
}
this.result = result
}
getResult = () => {
return this.result.map((word) => ({
word,
selected: this.collection.includes(word)
}))
}
}
let searchEngine = new SearchEngine()
ReactDOM.render(
<App searchEngine={searchEngine} />
, document.getElementById('root'));
registerServiceWorker();