Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatPkg): add .js to alternative names #383

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Contributor

@bodinsamuel bodinsamuel Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are specifically testing getAlternativeNames you could use this function directly, that way the test would be clear about the expectation. You already have snapshot in place to be sure that this function is already called in the main method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, but would leave it for a major reorganise, since these functions aren't exported :)

.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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a .trim() would clean that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, however it isn't really an issue for the engine, so I left it as-is

"@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
Original file line number Diff line number Diff line change
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])
);
}