diff --git a/src/modify.js b/src/modify.js index a79f9e5..0c334c8 100644 --- a/src/modify.js +++ b/src/modify.js @@ -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) { @@ -160,6 +158,9 @@ exports.modifyService = function ( exports.getEmptyManifest = getEmptyManifest; function sortObjectAlphabeticallyByKeys(unordered) { + if (!unordered) { + return unordered; + } return Object.keys(unordered) .sort() .reduce((obj, key) => { @@ -167,3 +168,5 @@ function sortObjectAlphabeticallyByKeys(unordered) { return obj; }, {}); } + +exports.sortObjectAlphabeticallyByKeys = sortObjectAlphabeticallyByKeys; diff --git a/test/alphabetical-import-map.test.js b/test/alphabetical-import-map.test.js index 2300d85..f1c1633 100644 --- a/test/alphabetical-import-map.test.js +++ b/test/alphabetical-import-map.test.js @@ -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(() => { @@ -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`, () => {