Skip to content

Commit

Permalink
feat: Throw when an undefined is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
bebraw committed Nov 12, 2020
1 parent 07e21c1 commit 01cd2ba
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mergeWith from "./merge-with";
import joinArrays from "./join-arrays";
import unique from "./unique";
import { CustomizeRule, ICustomizeOptions, Key } from "./types";
import { isPlainObject } from "./utils";
import { isPlainObject, isUndefined } from "./utils";

function merge<Configuration extends object>(
firstConfiguration: Configuration | Configuration[],
Expand All @@ -22,23 +22,31 @@ function mergeWithCustomize<Configuration extends object>(
firstConfiguration: Configuration | Configuration[],
...configurations: Configuration[]
): Configuration {
// No configuration at all
if (!firstConfiguration) {
return {} as Configuration;
if (isUndefined(firstConfiguration) || configurations.some(isUndefined)) {
throw new TypeError("Merging undefined is not supported");
}

// @ts-ignore
if (firstConfiguration.then) {
throw new TypeError("Promises are not supported");
}

// No configuration at all
if (!firstConfiguration) {
return {} as Configuration;
}

if (configurations.length === 0) {
if (Array.isArray(firstConfiguration)) {
// Empty array
if (firstConfiguration.length === 0) {
return {} as Configuration;
}

if (firstConfiguration.some(isUndefined)) {
throw new TypeError("Merging undefined is not supported");
}

// @ts-ignore
if (firstConfiguration[0].then) {
throw new TypeError("Promises are not supported");
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ function isPlainObject(a) {
return typeof a === "object";
}

export { isRegex, isFunction, isPlainObject };
function isUndefined(a) {
return typeof a === "undefined";
}

export { isRegex, isFunction, isPlainObject, isUndefined };
59 changes: 57 additions & 2 deletions test/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,63 @@ function normalMergeTests(merge) {
}

function normalMergeTest(merge, loadersKey) {
it("should work with an empty configuration", function() {
assert.deepStrictEqual(merge(), {});
it("should throw with an empty configuration", function() {
assert.throws(() => merge(), {
name: "TypeError",
message: "Merging undefined is not supported"
});
});

it("should throw with undefined 1/4", function() {
assert.throws(() => merge(undefined), {
name: "TypeError",
message: "Merging undefined is not supported"
});
});

it("should throw with undefined 2/4", function() {
assert.throws(() => merge([undefined]), {
name: "TypeError",
message: "Merging undefined is not supported"
});
});

it("should throw with undefined 2/4", function() {
const result = { devServer: null };

assert.throws(
() =>
merge(
{
devServer: { base: true }
},
undefined,
result
),
{
name: "TypeError",
message: "Merging undefined is not supported"
}
);
});

it("should throw with undefined 3/4", function() {
const result = { devServer: null };

assert.throws(
() =>
merge(
undefined,
{
devServer: { base: true }
},
result
),
{
name: "TypeError",
message: "Merging undefined is not supported"
}
);
});

it("should work with an empty array", function() {
Expand Down

0 comments on commit 01cd2ba

Please sign in to comment.