Skip to content

Commit

Permalink
Simplify caseKludge function (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcornelissen committed Jul 23, 2020
1 parent ea584b8 commit aa9277f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"overrides": [{
"files": ["lib/{main,ui}.js"],
"rules": {"space-before-function-paren": 0}
},{
"files": ["lib/utils.js"],
"rules": {"multiline-ternary": 0}
}]
}
4 changes: 2 additions & 2 deletions lib/icons/.icondb.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ module.exports = [
["arc-icon",["medium-blue","medium-blue"],/\.arc$/i],
["arduino-icon",["dark-cyan","dark-cyan"],/\.ino$/i,,false,,/\.arduino$/i,/^Arduin[0o]$/i],
["asciidoctor-icon",["medium-blue","medium-blue"],/\.(?:ad|adoc|asc|asciidoc)$/i,,false,,/\.asciidoc$/i,/^Ascii[\W_ \t]?D[0o]c$/i],
["asp-icon",["dark-blue","dark-blue"],/\.asp$/i,,false,,/\.asp$/i,/^[Aa][Ss][Pp][\W_ \t]?[Nn][Ee][Tt]$|^aspx(?:-vb)?$/],
["asp-icon",["dark-blue","dark-blue"],/\.asp$/i,,false,,/\.asp$/i,/^[Aa][Ss][Pp].[nN][eE][tT]$|^aspx(?:-vb)?$/],
["asp-icon",["medium-maroon","medium-maroon"],/\.asax$/i],
["asp-icon",["dark-green","dark-green"],/\.ascx$/i],
["asp-icon",["medium-green","medium-green"],/\.ashx$/i],
Expand Down Expand Up @@ -1462,7 +1462,7 @@ module.exports = [
["python-icon",["dark-green","dark-green"],/^(?:SConstruct|SConscript)$/],
["python-icon",["medium-green","medium-green"],/^(?:Snakefile|WATCHLISTS)$/],
["python-icon",["dark-maroon","dark-maroon"],/^wscript$/],
["kx-icon",["medium-blue","medium-blue"],/\.q$/i,,false,,/^source\.q$/,/^[Qq][\W_ \t]?[Kk][Dd][Bb][\W_ \t]?$|^Kdb\s*\+$/],
["kx-icon",["medium-blue","medium-blue"],/\.q$/i,,false,,/^source\.q$/,/^[Qq]\/[Kk][dD][bB]+$|^Kdb\s*\+$/],
["kx-icon",["dark-purple","dark-purple"],/\.k$/i,,false,,/^source\.k4$/,/^Q\/?Kdb\+?$/i],
["qiskit-icon",["dark-blue","dark-blue"],/\.qasm$/i,,false,,/\.qasm$/i,/^Qasm$|^[0o]pen[\W_ \t]?Qasm$/i],
["qlik-icon",["medium-green","medium-green"],/\.qvw$/i],
Expand Down
26 changes: 6 additions & 20 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,14 @@ module.exports = {
* so this function attempts to approximate the closest thing.
*
* @param {String} input - Case-insensitive text
* @param {Boolean} fuzz - Apply {@link fuzzyRegExp} to input
* @return {String}
*/
function caseKludge(input, fuzz = false){
let output = input.split("").map((s, index, array) => {
if(/[A-Z]/.test(s)){
const output = "[" + s + s.toLowerCase() + "]";
const prev = array[index - 1];
if(fuzz && prev && /[a-z]/.test(prev))
return "[\\W_\\S]*" + output;
return output;
}
if(/[a-z]/.test(s)) return "[" + s.toUpperCase() + s + "]";
if(!fuzz) return escapeRegExp(s);
if("0" === s) return "[0Oo]";
if(/[\W_ \t]?/.test(s)) return "[\\W_ \\t]?";
return s;
}).join("");

if(fuzz)
output = output.replace(/\[Oo\]/g, "[0Oo]");
return output.replace(/(\[\w{2,3}\])(\1+)/g, (match, first, rest) =>
function caseKludge(input){
return input.split("").map(match =>
/[A-Z]/.test(match) ? "[" + match + match.toLowerCase() + "]" :
/[a-z]/.test(match) ? "[" + match + match.toUpperCase() + "]" :
match
).join("").replace(/(\[\w{2,3}\])(\1+)/g, (match, first, rest) =>
first + "{" + ((rest.length / first.length) + 1) + "}");
}

Expand Down
24 changes: 0 additions & 24 deletions test/2-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,6 @@ describe("Utility functions", () => {
});
});

describe("caseKludge()", () => {
const {caseKludge} = utils;
it("generates case-insensitive regex source", () => {
const pattern = new RegExp(`^(ABC|${caseKludge("DEF")})`);
expect("dEf").to.match(pattern);
expect("aBc").not.to.match(pattern);
});

it("fuzzes word boundaries", () => {
const source = caseKludge("camelCase", true);
const pattern = new RegExp(`^abc: ${source}$`);
expect("abc: camelCASE").to.match(pattern);
expect("abc: camel-CASE").to.match(pattern);
expect("ABC: camel-CASE").not.to.match(pattern);
});

it("allows multiple separators between fuzzed boundaries", () => {
const source = caseKludge("camelCase", true);
const pattern = new RegExp(`^abc: ${source}$`);
expect("abc: camel----CASE").to.match(pattern);
expect("abc: camel--CA").not.to.match(pattern);
});
});

describe("escapeRegExp()", () => {
const {escapeRegExp} = utils;
it("escapes backslashes", () => void expect(escapeRegExp("\\")).to.equal("\\\\"));
Expand Down

0 comments on commit aa9277f

Please sign in to comment.