Skip to content

Commit

Permalink
feat(formatPkg): add .js to alternative names (#383)
Browse files Browse the repository at this point in the history
fixes #217

1. split out getAlternativeNames
2. avoid duplicates in getAlternativeNames
3. no longer put concatenatedName in _searchInternal (not relied upon)
4. add .js or non-.js name
5. small prettier fix
6. update readme
  • Loading branch information
Haroenv committed Aug 6, 2019
1 parent 29afbd5 commit 8463308
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -40,7 +40,6 @@ For every single NPM package, we create a record in the Algolia index. The resul
```json5
{
name: 'babel-core',
concatenatedName: 'babelcore',
downloadsLast30Days: 10978749,
downloadsRatio: 0.08310651682685861,
humanDownloadsLast30Days: '11m',
Expand Down Expand Up @@ -129,6 +128,9 @@ For every single NPM package, we create a record in the Algolia index. The resul
popularName: 'babel-core',
downloadsMagnitude: 8,
jsDelivrPopularity: 5,
alternativeNames: [
// alternative versions of this name, to show up on confused searches
],
},
}
```
Expand Down
12 changes: 5 additions & 7 deletions src/__tests__/__snapshots__/formatPkg.test.js.snap
Expand Up @@ -6,9 +6,9 @@ Object {
"alternativeNames": Array [
"atlaskitinput",
" atlaskit input",
"@atlaskit/input.js",
"@atlaskit/input",
],
"concatenatedName": "atlaskitinput",
},
"bin": undefined,
"computedKeywords": Array [],
Expand Down Expand Up @@ -201,9 +201,9 @@ Object {
"alternativeNames": Array [
"atomicpackagetab",
" atomic package tab",
"@atomic-package/tab.js",
"@atomic-package/tab",
],
"concatenatedName": "atomicpackagetab",
},
"bin": undefined,
"computedKeywords": Array [],
Expand Down Expand Up @@ -314,9 +314,9 @@ Object {
"alternativeNames": Array [
"createinstantsearchapp",
"create instantsearch app",
"create-instantsearch-app.js",
"create-instantsearch-app",
],
"concatenatedName": "createinstantsearchapp",
},
"bin": Object {
"create-instantsearch-app": "src/cli/index.js",
Expand Down Expand Up @@ -431,10 +431,8 @@ Object {
"_searchInternal": Object {
"alternativeNames": Array [
"indexof",
"indexof",
"indexof",
"indexof.js",
],
"concatenatedName": "indexof",
},
"bin": undefined,
"computedKeywords": Array [],
Expand Down Expand Up @@ -515,9 +513,9 @@ Object {
"alternativeNames": Array [
"longboy",
"long boy",
"long-boy.js",
"long-boy",
],
"concatenatedName": "longboy",
},
"bin": undefined,
"computedKeywords": Array [],
Expand Down
74 changes: 69 additions & 5 deletions src/__tests__/formatPkg.test.js
Expand Up @@ -25,10 +25,10 @@ it('keeps .bin intact', () => {
);
const formatted = formatPkg(createInstantSearchApp);
expect(formatted.bin).toMatchInlineSnapshot(`
Object {
"create-instantsearch-app": "src/cli/index.js",
}
`);
Object {
"create-instantsearch-app": "src/cli/index.js",
}
`);
});

it('truncates long readmes', () => {
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('adds TypeScript information', () => {
});
});

describe('test getRepositoryInfo', () => {
describe('getRepositoryInfo', () => {
const getRepositoryInfo = formatPkg.__RewireAPI__.__get__(
'getRepositoryInfo'
);
Expand Down Expand Up @@ -377,3 +377,67 @@ describe('test getRepositoryInfo', () => {
expect(getRepositoryInfo('aaaaaaaa')).toBe(null);
});
});

describe('alternative names', () => {
test('name not yet ending in .js', () => {
const original = {
name: 'places',
lastPublisher: { name: 'unknown' },
};
expect(formatPkg(original)._searchInternal.alternativeNames)
.toMatchInlineSnapshot(`
Array [
"places",
"places.js",
]
`);
});

test('name ending in .js', () => {
const original = {
name: 'places.js',
lastPublisher: { name: 'unknown' },
};
expect(formatPkg(original)._searchInternal.alternativeNames)
.toMatchInlineSnapshot(`
Array [
"placesjs",
"places js",
"places",
"places.js",
]
`);
});

test('scoped package', () => {
const original = {
name: '@algolia/places.js',
lastPublisher: { name: 'unknown' },
};
expect(formatPkg(original)._searchInternal.alternativeNames)
.toMatchInlineSnapshot(`
Array [
"algoliaplacesjs",
" algolia places js",
"@algolia/places",
"@algolia/places.js",
]
`);
});

test('name with - and _', () => {
const original = {
name: 'this-is_a-dumb-name',
lastPublisher: { name: 'unknown' },
};
expect(formatPkg(original)._searchInternal.alternativeNames)
.toMatchInlineSnapshot(`
Array [
"thisisadumbname",
"this is a dumb name",
"this-is_a-dumb-name.js",
"this-is_a-dumb-name",
]
`);
});
});
22 changes: 18 additions & 4 deletions src/formatPkg.js
Expand Up @@ -59,8 +59,7 @@ export default function formatPkg(pkg) {

const dependencies = cleaned.dependencies || {};
const devDependencies = cleaned.devDependencies || {};
const concatenatedName = cleaned.name.replace(/[-/@_.]+/g, '');
const splitName = cleaned.name.replace(/[-/@_.]+/g, ' ');
const alternativeNames = getAlternativeNames(cleaned.name);

const tags = pkg['dist-tags'];

Expand Down Expand Up @@ -97,8 +96,7 @@ export default function formatPkg(pkg) {
types,
lastCrawl: new Date().toISOString(),
_searchInternal: {
concatenatedName,
alternativeNames: [concatenatedName, splitName, cleaned.name],
alternativeNames,
},
};

Expand Down Expand Up @@ -417,3 +415,19 @@ function getTypes(pkg) {
ts: false,
};
}

/**
* @param {string} name
*/
function getAlternativeNames(name) {
const concatenatedName = name.replace(/[-/@_.]+/g, '');
const splitName = name.replace(/[-/@_.]+/g, ' ');
const dotJSName = name.endsWith('.js')
? name.substring(0, name.length - 3)
: `${name}.js`;
const normalName = name;

return Array.from(
new Set([concatenatedName, splitName, dotJSName, normalName])
);
}

0 comments on commit 8463308

Please sign in to comment.