Skip to content

Commit bdb25f4

Browse files
committed
Nicer single-char name filters
1 parent 443c289 commit bdb25f4

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/index.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ function initExampleMap(data: SearchEntry[]) {
8181
exampleMap["\""] = """;
8282
}
8383

84+
const nameFilterMap: { [key: string]: string } = {};
85+
86+
function initNameFilterMap(data: SearchEntry[]) {
87+
for (var letter = 'A'.charCodeAt(0); letter <= 'Z'.charCodeAt(0); letter++) {
88+
const char = String.fromCharCode(letter);
89+
nameFilterMap[char] = `LETTER ${char}`;
90+
}
91+
nameFilterMap['0'] = `DIGIT ZERO`;
92+
nameFilterMap['1'] = `DIGIT ONE`;
93+
nameFilterMap['2'] = `DIGIT TWO`;
94+
nameFilterMap['3'] = `DIGIT THREE`;
95+
nameFilterMap['4'] = `DIGIT FOUR`;
96+
nameFilterMap['5'] = `DIGIT FIVE`;
97+
nameFilterMap['6'] = `DIGIT SIX`;
98+
nameFilterMap['7'] = `DIGIT SEVEN`;
99+
nameFilterMap['8'] = `DIGIT EIGHT`;
100+
nameFilterMap['9'] = `DIGIT NINE`;
101+
nameFilterMap[' '] = `SPACE`;
102+
nameFilterMap['='] = `EQUALS`;
103+
nameFilterMap['~'] = `TILDE`;
104+
nameFilterMap['-'] = 'MINUS';
105+
nameFilterMap[':'] = 'COLON';
106+
nameFilterMap['_'] = 'UNDERSCORE';
107+
nameFilterMap['?'] = 'QUESTION MARK';
108+
nameFilterMap['.'] = 'FULL STOP';
109+
}
110+
84111
// translates from a hex codepoint string to the actual character
85112
function codeToString(code: string):string {
86113
return String.fromCodePoint(parseInt(code, 16));
@@ -114,10 +141,11 @@ function filterName(
114141

115142
const rowValue = rowData.name;
116143

117-
if (headerValue.length == 1 && headerValue != "^" && headerValue != "/") {
118-
// single character, do starts with
119-
const search = headerValue.toLowerCase();
120-
return rowValue.toLowerCase().startsWith(search);
144+
if (headerValue.length == 1) {
145+
if (headerValue == "^") {
146+
return true;
147+
}
148+
headerValue = nameFilterMap[headerValue] || headerValue;
121149
}
122150

123151
if (headerValue.startsWith("^")) {
@@ -377,6 +405,7 @@ async function main() {
377405
}
378406

379407
initExampleMap(data);
408+
initNameFilterMap(data);
380409

381410
console.log(data[0]);
382411

0 commit comments

Comments
 (0)