Comments (4)
Fair enough, but the readme clearly states:
merge-options
considers plain objects as Option Objects, everything else as Option Values.
As undefined
is not a plain object, your examples describe the expected behavior.
Please note: the more recent (non-recursive) spread operator works the same way:
const defaults = { port: 443 };
const options = { port: process.env.PORT };
mergeOptions(defaults, options)
// => { port: undefined }
{...defaults, ...options}
// => { port: undefined }
Hence, in your case, I would suggest to make your environment variables more useful:
// index.js
const mergeOptions = require('merge-options');
const envKeys = [ 'PORT' ];
const env = Object.fromEntries(Object.entries(process.env)
.filter(([k]) => envKeys.includes(k))
.map(([k, v]) => [k.toLowerCase(), v])
);
console.log('env:', env)
const options = mergeOptions({ port: 443 }, env);
console.log('options:', options);
$ PORT=8080 node index.js
# env: { port: '8080' }
# options: { port: '8080' }
$ node index.js
# env: {}
# options: { port: 443 }
from merge-options.
Would you accept a PR that added this behaviour as a config option similar to concatArrays
called ignoreUndefined
or similar?
I run into situations where this would be useful all the time but don't think the world needs Yet Another Options Merging Module.
from merge-options.
👍 @achingbrain I don't see a problem with an option – ignoreUndefined
sounds good to me.
from merge-options.
Thanks to @achingbrain (merge-options@^2.0.0
):
const mergeOptions = require('merge-options').bind({ignoreUndefined: true});
const result = mergeOptions({port: 443}, {port: process.env.PORT});
console.log(result);
$ PORT=8080 node index.js
# { port: '8080' }
$ node index.js
# { port: 443 }
from merge-options.
Related Issues (15)
- Is this different in results than lodash.merge? HOT 1
- CVE HOT 1
- [Request] Keep first param reactive like Object.assign HOT 1
- npm audit reports this package as problematic for prototype pollution HOT 6
- Can a stable release be published? HOT 1
- TypeScript is not supported HOT 5
- node 12 support? HOT 2
- cjs/esm support breaks IntelliSense of VSCode HOT 1
- Rollup complains
- Breaking in webpack when ignore node_modules HOT 9
- Fails to build on create-react-app: Publish ES5 to NPM HOT 6
- [production build] Unexpected token: operator (>) [scripts/main_d82bc1a8.js:7487,44] HOT 1
- Snyk vulnerability report HOT 3
- Consideration for Alternate Array Handling HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from merge-options.