I'm creating a small JavaScript library for node to manage basic web authentication workflows. When the user configures the Express middleware he could provide or not a json object with his preferred configuration e.g.
web: {
login: {
enabled: true,
customView: myDir/myCustomLogin.html
},
logout: {
enabled: true
},
forgotPassword: {
enabled: false
},
...someOtherProperties
If he doesn't provide it the library should use the fallback config json. But if he provides it the library should use the fallback plus the attributes he overrode. What would be the best way to merge the user's config with the app's config file? I guess I could iterate over each property and check which are the ones the user wants to override. Is there a better way to do that? perhaps a library that handles those use cases? how are these scenarios handled by production libraries?
EDIT: My solution was based on the answer below and this useful blog post:
https://blog.mariusschulz.com/2015/03/30/combining-settings-objects-with-lodash-assign-or-merge
If you want to know more about the different methods lodash provides for cloning objects check this question:
Lodash - difference between .extend() / .assign() and .merge()