Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,33 @@ function fit(word, { words, otherWords, codes }) {
return word.length > 3 && words.some((own) => nearly(word, own)) ? 1 : 0;
}

const total = (fits) => fits.reduce((sum, points) => sum + points, 0);

/** Where a code sits in the words, as "Galaxy SM-G991B" splits into galaxy sm g 991 b */
function codeRun(words, codes) {
for (let from = 0; from < words.length; from++) {
for (let to = words.length; to > from + 1; to--) {
const run = words.slice(from, to).join('');
const tail = /^[a-z]/.test(run) && run.length > 3;
if (/\d/.test(run) && codes.some((code) => code === run || (tail && code.endsWith(run)))) {
return [from, to];
}
}
}
}

/** Every word has to fit, unless the whole query is the device's name or a code run together */
function score(words, device) {
const whole = words.join('');
if (device.whole === whole || device.codes.includes(whole)) return 100;
const fits = words.map((word) => fit(word, device));
if (fits.every(Boolean)) return fits.reduce((sum, points) => sum + points, 0);
if (fits.every(Boolean)) return total(fits);
const run = codeRun(words, device.codes);
if (run) {
const rest = [...words.slice(0, run[0]), ...words.slice(run[1])];
const restFits = rest.map((word) => fit(word, device));
if (restFits.every(Boolean)) return 50 + total(restFits);
}
return whole.length > 3 && device.codes.some((code) => code.includes(whole)) ? 5 : 0;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ test('codenames and model numbers find their phone, however they are typed', ()
}
});

test('a model number still finds its phone with the brand or name in front', () => {
for (const query of ['samsung SM-G991B', 'galaxy s21 SM-G991B', 'samsung g991b']) {
assert.deepEqual(names(query), ['Samsung Galaxy S21']);
}
assert.deepEqual(names('pixel SM-G991B'), []);
});

test('a ROM or feature in the query keeps only the phones where it fits', () => {
assert.deepEqual(names('pixel lineage'), ['Google Pixel 7', 'Google Pixel 7 Pro']);
assert.deepEqual(names('pixel volte'), ['Google Pixel 7', 'Google Pixel 7a']);
Expand Down
Loading