-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-matcher.js
More file actions
291 lines (250 loc) · 8.21 KB
/
pattern-matcher.js
File metadata and controls
291 lines (250 loc) · 8.21 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Pattern Matcher for AutoFill Plugin
* Håndterer matching av mønstre med støtte for wildcards og regex
*/
const PatternMatcher = {
/**
* Matcher en streng mot et mønster
* @param {string} text - Tekst å matche
* @param {string} pattern - Mønster
* @param {boolean} useRegex - Om mønsteret er regex
* @returns {boolean} Om det er match
*/
match(text, pattern, useRegex = false) {
if (!text || !pattern) return false;
if (useRegex) {
return this.matchRegex(text, pattern);
} else {
return this.matchWildcard(text, pattern);
}
},
/**
* Matcher med regex
* @param {string} text - Tekst å matche
* @param {string} pattern - Regex-mønster
* @returns {boolean} Om det er match
*/
matchRegex(text, pattern) {
try {
const regex = new RegExp(pattern, 'i');
return regex.test(text);
} catch (error) {
console.error('Invalid regex pattern:', pattern, error);
return false;
}
},
/**
* Matcher med wildcards (* og ?)
* @param {string} text - Tekst å matche
* @param {string} pattern - Wildcard-mønster
* @returns {boolean} Om det er match
*/
matchWildcard(text, pattern) {
// Eksakt match
if (text === pattern) return true;
// Konverter wildcard-mønster til regex
const regexPattern = this.wildcardToRegex(pattern);
try {
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(text);
} catch (error) {
console.error('Error converting wildcard to regex:', pattern, error);
return false;
}
},
/**
* Konverterer wildcard-mønster til regex
* @param {string} pattern - Wildcard-mønster
* @returns {string} Regex-mønster
*/
wildcardToRegex(pattern) {
// Escape spesialtegn unntatt * og ?
let regex = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&');
// Konverter wildcards
regex = regex.replace(/\*/g, '.*'); // * matcher null eller flere tegn
regex = regex.replace(/\?/g, '.'); // ? matcher nøyaktig ett tegn
return regex;
},
/**
* Validerer et regex-mønster
* @param {string} pattern - Regex-mønster
* @returns {Object} { valid: boolean, error: string|null }
*/
validateRegex(pattern) {
try {
new RegExp(pattern);
return { valid: true, error: null };
} catch (error) {
return { valid: false, error: error.message };
}
},
/**
* Tester om et mønster inneholder wildcards
* @param {string} pattern - Mønster
* @returns {boolean} Om mønsteret inneholder wildcards
*/
hasWildcards(pattern) {
return pattern.includes('*') || pattern.includes('?');
},
/**
* Normaliserer et mønster (fjerner unødvendige wildcards)
* @param {string} pattern - Mønster
* @returns {string} Normalisert mønster
*/
normalizePattern(pattern) {
// Fjern gjentatte wildcards
let normalized = pattern.replace(/\*+/g, '*');
// Fjern wildcard på starten hvis det ikke gir mening
// (dette er valgfritt og kan tilpasses)
return normalized;
},
/**
* Foreslår mønstre basert på en liste av strenger
* @param {Array<string>} strings - Liste av strenger
* @returns {Array<Object>} Foreslåtte mønstre med score
*/
suggestPatterns(strings) {
if (strings.length === 0) return [];
if (strings.length === 1) return [{ pattern: strings[0], matches: 1, coverage: 1 }];
const suggestions = [];
// Finn felles prefix
const commonPrefix = this.findCommonPrefix(strings);
if (commonPrefix.length > 0) {
const pattern = commonPrefix + '*';
const matches = strings.filter(s => s.startsWith(commonPrefix)).length;
suggestions.push({
pattern,
matches,
coverage: matches / strings.length,
type: 'prefix'
});
}
// Finn felles suffix
const commonSuffix = this.findCommonSuffix(strings);
if (commonSuffix.length > 0) {
const pattern = '*' + commonSuffix;
const matches = strings.filter(s => s.endsWith(commonSuffix)).length;
suggestions.push({
pattern,
matches,
coverage: matches / strings.length,
type: 'suffix'
});
}
// Finn felles deler i midten
const commonSubstrings = this.findCommonSubstrings(strings);
for (const substring of commonSubstrings) {
if (substring.length >= 3) { // Minimum lengde for å være relevant
const pattern = '*' + substring + '*';
const matches = strings.filter(s => s.includes(substring)).length;
suggestions.push({
pattern,
matches,
coverage: matches / strings.length,
type: 'substring'
});
}
}
// Sorter etter coverage (høyest først)
suggestions.sort((a, b) => b.coverage - a.coverage);
return suggestions;
},
/**
* Finner felles prefix i en liste av strenger
* @param {Array<string>} strings - Liste av strenger
* @returns {string} Felles prefix
*/
findCommonPrefix(strings) {
if (strings.length === 0) return '';
let prefix = strings[0];
for (let i = 1; i < strings.length; i++) {
while (strings[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === '') return '';
}
}
return prefix;
},
/**
* Finner felles suffix i en liste av strenger
* @param {Array<string>} strings - Liste av strenger
* @returns {string} Felles suffix
*/
findCommonSuffix(strings) {
if (strings.length === 0) return '';
const reversed = strings.map(s => s.split('').reverse().join(''));
const reversedPrefix = this.findCommonPrefix(reversed);
return reversedPrefix.split('').reverse().join('');
},
/**
* Finner felles substrings i en liste av strenger
* @param {Array<string>} strings - Liste av strenger
* @returns {Array<string>} Liste av felles substrings
*/
findCommonSubstrings(strings) {
if (strings.length === 0) return [];
const substrings = new Map();
const minLength = 3;
// Ekstraher alle substrings fra første string
for (let i = 0; i < strings[0].length - minLength + 1; i++) {
for (let j = i + minLength; j <= strings[0].length; j++) {
const substring = strings[0].substring(i, j);
substrings.set(substring, 0);
}
}
// Tell hvor mange strenger hver substring finnes i
for (const [substring] of substrings) {
let count = 0;
for (const str of strings) {
if (str.includes(substring)) count++;
}
substrings.set(substring, count);
}
// Filtrer og sorter
const common = Array.from(substrings.entries())
.filter(([_, count]) => count >= Math.ceil(strings.length * 0.5)) // Minst 50% coverage
.sort((a, b) => b[1] - a[1] || b[0].length - a[0].length)
.map(([substring]) => substring);
return common.slice(0, 5); // Returner topp 5
},
/**
* Matcher en URL mot et site-mønster
* @param {string} url - Full URL å matche
* @param {string} sitePattern - Site-mønster
* @param {string} siteMatchType - Matchtype: 'host', 'domain', 'url', 'regex'
* @returns {boolean} Om URL-en matcher mønsteret
*/
matchSite(url, sitePattern, siteMatchType) {
if (!url || !sitePattern) return false;
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname;
const fullUrl = url;
switch (siteMatchType) {
case 'host':
// Eksakt match på hostname
return this.match(hostname, sitePattern, false);
case 'domain':
// Match på domene (inkluderer subdomener)
const domain = sitePattern.replace(/^\*\./, '');
return hostname === domain || hostname.endsWith('.' + domain);
case 'url':
// Match på full URL
return this.match(fullUrl, sitePattern, false);
case 'regex':
// Regex match på full URL
return this.matchRegex(fullUrl, sitePattern);
default:
// Default til wildcard match på hostname
return this.match(hostname, sitePattern, false);
}
} catch (error) {
console.error('Error matching site:', error);
return false;
}
}
};
// Eksporter for bruk i andre moduler
if (typeof module !== 'undefined' && module.exports) {
module.exports = PatternMatcher;
}