Skip to content

Commit

Permalink
fix(alternative names): remove prismjs -> prismjs.js
Browse files Browse the repository at this point in the history
also add the js without dot form to other keys
  • Loading branch information
Haroenv committed Apr 16, 2020
1 parent c37d6d6 commit a1bad34
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/__tests__/__snapshots__/formatPkg.test.js.snap
Expand Up @@ -7,6 +7,7 @@ Object {
"atlaskitinput",
" atlaskit input",
"@atlaskit/input.js",
"@atlaskit/inputjs",
"@atlaskit/input",
],
},
Expand Down Expand Up @@ -202,6 +203,7 @@ Object {
"atomicpackagetab",
" atomic package tab",
"@atomic-package/tab.js",
"@atomic-package/tabjs",
"@atomic-package/tab",
],
},
Expand Down Expand Up @@ -318,6 +320,7 @@ Object {
"createinstantsearchapp",
"create instantsearch app",
"create-instantsearch-app.js",
"create-instantsearch-appjs",
"create-instantsearch-app",
],
},
Expand Down Expand Up @@ -438,6 +441,7 @@ Object {
"alternativeNames": Array [
"indexof",
"indexof.js",
"indexofjs",
],
},
"bin": undefined,
Expand Down Expand Up @@ -521,7 +525,7 @@ Object {
"_searchInternal": Object {
"alternativeNames": Array [
"prismjs",
"prismjs.js",
"prism",
],
},
"bin": undefined,
Expand Down Expand Up @@ -719,6 +723,7 @@ Object {
"longboy",
"long boy",
"long-boy.js",
"long-boyjs",
"long-boy",
],
},
Expand Down
24 changes: 20 additions & 4 deletions src/__tests__/formatPkg.test.js
Expand Up @@ -30,10 +30,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 @@ -390,6 +390,7 @@ describe('alternative names', () => {
Array [
"places",
"places.js",
"placesjs",
]
`);
});
Expand All @@ -410,6 +411,20 @@ describe('alternative names', () => {
`);
});

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

test('scoped package', () => {
const original = {
name: '@algolia/places.js',
Expand Down Expand Up @@ -437,6 +452,7 @@ describe('alternative names', () => {
"thisisadumbname",
"this is a dumb name",
"this-is_a-dumb-name.js",
"this-is_a-dumb-namejs",
"this-is_a-dumb-name",
]
`);
Expand Down
27 changes: 20 additions & 7 deletions src/formatPkg.js
Expand Up @@ -425,16 +425,29 @@ function getTypes(pkg) {
* @param {string} name
*/
function getAlternativeNames(name) {
const alternativeNames = new Set();

const concatenatedName = name.replace(/[-/@_.]+/g, '');
alternativeNames.add(concatenatedName);

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

const isDotJs = name.endsWith('.js');
const isJsSuffix = name.match(/\.?js$/);

if (isDotJs) {
alternativeNames.add(name.substring(0, name.length - 3));
} else if (isJsSuffix) {
alternativeNames.add(name.substring(0, name.length - 2));
} else {
alternativeNames.add(`${name}.js`);
alternativeNames.add(`${name}js`);
}

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

return Array.from(alternativeNames);
}

export function getMains(pkg) {
Expand Down

0 comments on commit a1bad34

Please sign in to comment.