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

fix: alphabetical sort method now handles missing data better #125

Merged
merged 1 commit into from
Oct 22, 2021
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
19 changes: 11 additions & 8 deletions src/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ exports.modifyImportMap = function (env, newValues) {
const { services, scopes } = newValues;

const alphabetical = !!getConfig().alphabetical;
const newImports =
services && typeof services === "object" && alphabetical
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added all this last time. I could've saved myself a fair amount of pain by making sortObjectAlphabeticallyByKeys handle unexpected data better, which is what this PR does.

? sortObjectAlphabeticallyByKeys(services)
: services;
const newScopes =
scopes && typeof scopes === "object" && alphabetical
? sortObjectAlphabeticallyByKeys(scopes)
: scopes;
const newImports = alphabetical
? sortObjectAlphabeticallyByKeys(services)
: services;
const newScopes = alphabetical
? sortObjectAlphabeticallyByKeys(scopes)
: scopes;

// either imports or scopes have to be defined
if (newImports || newScopes) {
Expand Down Expand Up @@ -160,10 +158,15 @@ exports.modifyService = function (
exports.getEmptyManifest = getEmptyManifest;

function sortObjectAlphabeticallyByKeys(unordered) {
if (!unordered) {
return unordered;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to have this return the falsey value so that if your source import-map.json doesn't have scopes it doesn't get magically added be defaulting it to an empty object.

return Object.keys(unordered)
.sort()
.reduce((obj, key) => {
obj[key] = unordered[key];
return obj;
}, {});
}

exports.sortObjectAlphabeticallyByKeys = sortObjectAlphabeticallyByKeys;
7 changes: 7 additions & 0 deletions test/alphabetical-import-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { app, setConfig } = require("../src/web-server");
const {
resetManifest: resetMemoryManifest,
} = require("../src/io-methods/memory");
const { sortObjectAlphabeticallyByKeys } = require("../src/modify.js");

describe(`alphabetically sorted`, () => {
beforeAll(() => {
Expand Down Expand Up @@ -58,6 +59,12 @@ describe(`alphabetically sorted`, () => {
`{"a":"/a-1-updated.mjs","b":"/b-1.mjs","c":"/c-1.mjs"}`
);
});

it("should return undefined or null if you pass them in and not throw an error", () => {
expect(sortObjectAlphabeticallyByKeys(undefined)).toBe(undefined);
expect(sortObjectAlphabeticallyByKeys(null)).toBe(null);
expect(JSON.stringify(sortObjectAlphabeticallyByKeys({}))).toBe("{}");
});
});

describe(`not alphabetically sorted`, () => {
Expand Down