Skip to content

Commit

Permalink
fix: alphabetical sort method now handles missing data better (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: the_mcmurder <the_mcmurder@users.noreply.github.com>
  • Loading branch information
TheMcMurder and the_mcmurder committed Oct 22, 2021
1 parent ba802c8 commit ad02a26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
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
? 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;
}
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

0 comments on commit ad02a26

Please sign in to comment.