Code Monkey home page Code Monkey logo

Comments (4)

schnittstabil avatar schnittstabil commented on July 23, 2024

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.

achingbrain avatar achingbrain commented on July 23, 2024

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.

schnittstabil avatar schnittstabil commented on July 23, 2024

👍 @achingbrain I don't see a problem with an option – ignoreUndefined sounds good to me.

from merge-options.

schnittstabil avatar schnittstabil commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.