Skip to content

Commit

Permalink
Merge pull request #275 from Symbiomatrix/wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikDoom committed Mar 3, 2024
1 parent 312cec5 commit d404109
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
6 changes: 5 additions & 1 deletion javascript/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ function toNgrams(inputArray, size) {
);
}

function escapeRegExp(string) {
function escapeRegExp(string, wildcardMatching = false) {
if (wildcardMatching) {
// Escape all characters except asterisks and ?, which should be treated separately as placeholders.
return string.replace(/[-[\]{}()+.,\\^$|#\s]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.');
}
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function escapeHTML(unsafeText) {
Expand Down
7 changes: 5 additions & 2 deletions javascript/ext_chants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class ChantParser extends BaseTagParser {
let tempResults = [];
if (tagword !== "<" && tagword !== "<c:") {
let searchTerm = tagword.replace("<chant:", "").replace("<c:", "").replace("<", "");
let filterCondition = x => x.terms.toLowerCase().includes(searchTerm) || x.name.toLowerCase().includes(searchTerm);
let filterCondition = x => {
let regex = new RegExp(escapeRegExp(searchTerm, true), 'i');
return regex.test(x.terms.toLowerCase()) || regex.test(x.name.toLowerCase());
};
tempResults = chants.filter(x => filterCondition(x)); // Filter by tagword
} else {
tempResults = chants;
Expand Down Expand Up @@ -51,4 +54,4 @@ PARSERS.push(new ChantParser(CHANT_TRIGGER));
// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);
QUEUE_AFTER_CONFIG_CHANGE.push(load);
QUEUE_AFTER_CONFIG_CHANGE.push(load);
7 changes: 5 additions & 2 deletions javascript/ext_embeddings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class EmbeddingParser extends BaseTagParser {
searchTerm = searchTerm.slice(3);
}

let filterCondition = x => x[0].toLowerCase().includes(searchTerm) || x[0].toLowerCase().replaceAll(" ", "_").includes(searchTerm);
let filterCondition = x => {
let regex = new RegExp(escapeRegExp(searchTerm, true), 'i');
return regex.test(x[0].toLowerCase()) || regex.test(x[0].toLowerCase().replaceAll(" ", "_"));
};

if (versionString)
tempResults = embeddings.filter(x => filterCondition(x) && x[2] && x[2].toLowerCase() === versionString.toLowerCase()); // Filter by tagword
Expand Down Expand Up @@ -62,4 +65,4 @@ PARSERS.push(new EmbeddingParser(EMB_TRIGGER));

// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);
QUEUE_SANITIZE.push(sanitize);
7 changes: 5 additions & 2 deletions javascript/ext_hypernets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class HypernetParser extends BaseTagParser {
let tempResults = [];
if (tagword !== "<" && tagword !== "<h:" && tagword !== "<hypernet:") {
let searchTerm = tagword.replace("<hypernet:", "").replace("<h:", "").replace("<", "");
let filterCondition = x => x.toLowerCase().includes(searchTerm) || x.toLowerCase().replaceAll(" ", "_").includes(searchTerm);
let filterCondition = x => {
let regex = new RegExp(escapeRegExp(searchTerm, true), 'i');
return regex.test(x.toLowerCase()) || regex.test(x.toLowerCase().replaceAll(" ", "_"));
};
tempResults = hypernetworks.filter(x => filterCondition(x[0])); // Filter by tagword
} else {
tempResults = hypernetworks;
Expand Down Expand Up @@ -49,4 +52,4 @@ PARSERS.push(new HypernetParser(HYP_TRIGGER));

// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);
QUEUE_SANITIZE.push(sanitize);
7 changes: 5 additions & 2 deletions javascript/ext_loras.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class LoraParser extends BaseTagParser {
let tempResults = [];
if (tagword !== "<" && tagword !== "<l:" && tagword !== "<lora:") {
let searchTerm = tagword.replace("<lora:", "").replace("<l:", "").replace("<", "");
let filterCondition = x => x.toLowerCase().includes(searchTerm) || x.toLowerCase().replaceAll(" ", "_").includes(searchTerm);
let filterCondition = x => {
let regex = new RegExp(escapeRegExp(searchTerm, true), 'i');
return regex.test(x.toLowerCase()) || regex.test(x.toLowerCase().replaceAll(" ", "_"));
};
tempResults = loras.filter(x => filterCondition(x[0])); // Filter by tagword
} else {
tempResults = loras;
Expand Down Expand Up @@ -61,4 +64,4 @@ PARSERS.push(new LoraParser(LORA_TRIGGER));

// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);
QUEUE_SANITIZE.push(sanitize);
7 changes: 5 additions & 2 deletions javascript/ext_lycos.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class LycoParser extends BaseTagParser {
let tempResults = [];
if (tagword !== "<" && tagword !== "<l:" && tagword !== "<lyco:" && tagword !== "<lora:") {
let searchTerm = tagword.replace("<lyco:", "").replace("<lora:", "").replace("<l:", "").replace("<", "");
let filterCondition = x => x.toLowerCase().includes(searchTerm) || x.toLowerCase().replaceAll(" ", "_").includes(searchTerm);
let filterCondition = x => {
let regex = new RegExp(escapeRegExp(searchTerm, true), 'i');
return regex.test(x.toLowerCase()) || regex.test(x.toLowerCase().replaceAll(" ", "_"));
};
tempResults = lycos.filter(x => filterCondition(x[0])); // Filter by tagword
} else {
tempResults = lycos;
Expand Down Expand Up @@ -62,4 +65,4 @@ PARSERS.push(new LycoParser(LYCO_TRIGGER));

// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);
QUEUE_SANITIZE.push(sanitize);
7 changes: 5 additions & 2 deletions javascript/ext_styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class StyleParser extends BaseTagParser {
if (tagword !== matchGroups[1]) {
let searchTerm = tagword.replace(matchGroups[1], "");

let filterCondition = x => x[0].toLowerCase().includes(searchTerm) || x[0].toLowerCase().replaceAll(" ", "_").includes(searchTerm);
let filterCondition = x => {
let regex = new RegExp(escapeRegExp(searchTerm, true), 'i');
return regex.test(x[0].toLowerCase()) || regex.test(x[0].toLowerCase().replaceAll(" ", "_"));
};
tempResults = styleNames.filter(x => filterCondition(x)); // Filter by tagword
} else {
tempResults = styleNames;
Expand Down Expand Up @@ -64,4 +67,4 @@ PARSERS.push(new StyleParser(STYLE_TRIGGER));

// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);
QUEUE_SANITIZE.push(sanitize);

0 comments on commit d404109

Please sign in to comment.