I’m looking for a way to merge two configuration objects together, something like:
var developmentConfig = {
url: "localhost",
port: 80
};
var productionConfig = {
url: "example.com"
};
var config = isDevelopment ? developmentConfig : jQuery.extend(developmentConfig, productionConfig);
However, this is a Node.js app and I don’t want to include jQuery and am looking for something similar but standalone. I know I could write something similar myself but I’d rather use something tested and proven (there are edge cases, complications when the config objects use richer hierarchies etc.)
Edit: Simple iteration is not enough because that does not handle hierarchical structures. Neither does Underscore’s extend.
If all you need is extend, then it’s pretty simple to write that in a couple of lines. If you want recursive extension, it’s tricky to do that completely generically if you want have circular structures, objects with complex prototype chains, etc. If it’s just some nested plain objects, then this should work:
If you’re looking for a lightweight library that does this (minus the recursion, for the reasons listed above) and other similar functions not provided by javascript, look at Underscore which is available via NPM for node too.