Skip to content

undefined values are merged #13

@kcwitt

Description

@kcwitt

Based on the following code:

    const merge = require('merge-options');
    const result = merge({value: 10}, {value: undefined});

result.value should be '10', but it is 'undefined'.

Whether it really should return 10 is arguable, so my argument is that {} is functionally equivalent to {value: undefined} when getting a value from on object, so as inputs to merge they should both provide equivalent output. Where they are not functionally equivalent is when iterating the objects keys, but when dealing with options (and being mindful if the distinction between undefined and null) the rule of least surprise (for me at least) is to completely ignore undefined values (but still allow null values to supersede values of lower precedence).

The practical application of this is putting environment variables (which may or may not be defined) as the highest precedence item in the merge, but only if it has been defined.
const port = merge(defaults, {port: process.env.PORT});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions