-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutilities.js
38 lines (29 loc) · 1.06 KB
/
utilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import squatter from 'squatter';
import {npmNameMany} from 'npm-name';
import thesaurus from 'thesaurus';
import slugify from '@sindresorhus/slugify';
const npmOrganizationRegex = /^@[a-z\d][\w-.]+\/?$/i;
export async function checkNames(name) {
const result = await npmNameMany(name);
const names = await Promise.all([...result].map(async ([name, isAvailable]) => {
const returnValue = {name, isAvailable, isOrganization: npmOrganizationRegex.test(name)};
if (!isAvailable && !returnValue.isOrganization) {
try {
returnValue.isSquatter = await squatter(name);
} catch {
returnValue.isSquatter = false;
}
}
return returnValue;
}));
return names;
}
export async function getSimilarPackages({name, isOrganization}) {
const similarNames = thesaurus.find(name.replace(/@/, ''));
if (!similarNames) {
return [];
}
const slugNames = similarNames.map(name => `${isOrganization ? '@' : ''}` + slugify(name.toLowerCase()));
const similarPackages = await checkNames(slugNames);
return similarPackages.filter(package_ => package_.isAvailable);
}