j
Size: a a a
j
К
C
j
К
SB
К
IS
К
К
SB
К
SB
A
IS
(()=>{
const findMatches = (str, query) => {
const matches = [];
for (let sliceSize = 1; sliceSize < query.length; sliceSize++) {
for (let i = 0; i < query.length - sliceSize; i++) {
const slice = query.slice(i, i + sliceSize);
if (str.includes(slice)) {
matches.push({ matchLength: sliceSize, match: slice });
}
}
}
return matches;
};
const sort = (data, input) => {
const bestMatch = matches => matches.reduce((prev, current) => (prev.matchLength > current.matchLength) ? prev : current);
const matches = Object.fromEntries(
data.map(item => {
const currentMatches = findMatches(item, input);
return [item, currentMatches.length ? bestMatch(currentMatches) : { matchLength: 0 }]
})
);
return [...data].sort((a, b) => matches[b].matchLength - matches[a].matchLength);
};
const data = [
'repo',
'orange',
'russia',
'x'
];
const input = 'repository';
console.log(sort(data, input));
})()
IS
IS
IS
К