Skip to content

Properly merge array config values#128

Merged
shakyShane merged 2 commits intoBrowserSync:masterfrom
probertson:master
Apr 9, 2014
Merged

Properly merge array config values#128
shakyShane merged 2 commits intoBrowserSync:masterfrom
probertson:master

Conversation

@probertson
Copy link
Copy Markdown
Contributor

_.merge works if an array contains objects (that is, it merges the properties of the objects). However, if the array contains simple values (such as strings in the case of browser-sync config values), it overwrites the values in the default array with values in the user config.

For example, suppose I call browser-sync with a config object like this (in my case it's in a gulpfile):

browserSync.init(["output/**.*"], {
  excludedFileTypes: ["ejs"]
});

The default config contains these values:

excludedFileTypes: [
  "js",
  "css",
  "pdf",
  "map",
  "svg",
  "ico",
  "woff",
  "json",
  "eot",
  "ttf",
  "png",
  "jpg",
  "jpeg",
  "webp",
  "gif",
  "mp4",
  "mp3",
  "3gp",
  "ogg",
  "ogv",
  "webm",
  "m4a",
  "flv",
  "wmv",
  "avi",
  "swf",
  "scss"
]

The result of the _merge call in index.js is:

excludedFileTypes: [
  "ejs",
  "css",
  "pdf",
  "map",
  "svg",
  "ico",
  "woff",
  "json",
  "eot",
  "ttf",
  "png",
  "jpg",
  "jpeg",
  "webp",
  "gif",
  "mp4",
  "mp3",
  "3gp",
  "ogg",
  "ogv",
  "webm",
  "m4a",
  "flv",
  "wmv",
  "avi",
  "swf",
  "scss"
]

Note that the user-provided value "ejs" replaces the first value ("js") rather than being added to the array.

This pull request changes the behavior of _.merge so that for arrays, the arrays are concatenated. Note that a more robust solution would be needed if there were config parameters that accepted arrays of objects, but as long as the only array config parameters use simple values (string, number, etc.) this approach works.

Extend _.merge so that Array config values are concatenated instead of replacing default values with user-provided values.
Change the merge mechanism to use _.union instead of Array.concat(), so duplicates between the default config and the user config are only included once.
shakyShane added a commit that referenced this pull request Apr 9, 2014
Properly merge array config values
@shakyShane shakyShane merged commit 4417ea1 into BrowserSync:master Apr 9, 2014
@shakyShane
Copy link
Copy Markdown
Contributor

ouch - good catch & thanks!

I need to add tests around the config merging, it's getting messy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants