fix(registry): align OCI and RubyGems API behavior and improve npm search result ordering
This commit is contained in:
@@ -749,6 +749,22 @@ export class NpmRegistry extends BaseRegistry {
|
||||
this.logger.log('error', 'handleSearch failed', { error: (error as Error).message });
|
||||
}
|
||||
|
||||
// Sort results by relevance: exact match first, then prefix match, then substring match
|
||||
if (text) {
|
||||
const lowerText = text.toLowerCase();
|
||||
results.sort((a, b) => {
|
||||
const aName = a.package.name.toLowerCase();
|
||||
const bName = b.package.name.toLowerCase();
|
||||
const aExact = aName === lowerText ? 0 : 1;
|
||||
const bExact = bName === lowerText ? 0 : 1;
|
||||
if (aExact !== bExact) return aExact - bExact;
|
||||
const aPrefix = aName.startsWith(lowerText) ? 0 : 1;
|
||||
const bPrefix = bName.startsWith(lowerText) ? 0 : 1;
|
||||
if (aPrefix !== bPrefix) return aPrefix - bPrefix;
|
||||
return aName.localeCompare(bName);
|
||||
});
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
const paginatedResults = results.slice(from, from + size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user