(name: string)
| 14 | * This aims to mirror npm's typosquatting detection algorithm. |
| 15 | */ |
| 16 | export function normalizePackageName(name: string): string { |
| 17 | // Remove scope if present |
| 18 | const unscoped = name.startsWith('@') ? name.split('/')[1] || name : name |
| 19 | |
| 20 | // Normalize: lowercase, remove punctuation (.-_), remove 'js' and 'node' suffixes/prefixes |
| 21 | return ( |
| 22 | unscoped |
| 23 | .toLowerCase() |
| 24 | // Remove all punctuation |
| 25 | .replace(/[.\-_]/g, '') |
| 26 | // Remove common suffixes/prefixes |
| 27 | .replace(/^(node|js)|(-?js|-?node)$/g, '') |
| 28 | ) |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Calculate similarity between two strings using Levenshtein distance. |
no outgoing calls
no test coverage detected