justmoon / node-extend

Simple function to extend objects
MIT License
341 stars 68 forks source link

Add ability to combine arrays #28

Closed ctate closed 9 years ago

ctate commented 9 years ago

Hi all - I've used this module for a while now and have often needed the ability to combine arrays in a deep merge (instead of replacing array elements by index).

I tried to keep the code/syntax style the same by simply adding a second boolean argument to enable this feature:

var extend = require('extend');
var x = extend(
    true,
    true,
    { y: [42] },
    { y: [4, 15, 16] },
    { y: [8, 23] }
);

// x = { y: [ 4, 8, 15, 16, 23, 42 ] }

All of the current tests pass and I'm willing to write more tests if needed. This feature only applies to plain arrays (arrays of strings or numbers only) and sorts the array once combined.

I hope others find this useful. Please consider this PR and/or provide feedback if you'd like to see a different implementation.

Thank you for your time and consideration!

ljharb commented 9 years ago

This module needs to retain the algorithm from jQuery, and as such, no new features like this can ever be added.

In addition, I recommend using a distinct module for a different purpose - if we weren't conforming to jQuery's API, we wouldn't have a "deep" argument, we'd have two different functions, assign (since the language has Object.assign for this now) and deepAssign.

Thanks for your contribution though!