[5.4] Add new conjoin() method to collection#19318
[5.4] Add new conjoin() method to collection#19318taylorotwell merged 5 commits intolaravel:5.4from mikebronner:patch-2
conjoin() method to collection#19318Conversation
conJoin() method to collectionconJoin() method to collection
|
|
Using merge will overwrite existing values for existing keys. I haven't added tests for string keys, because the key is irrelevant, although I should probably add tests to prove that they are irrelevant. Also, I was returning the original object, because other methods did the same, and I wanted to keep with that.
The aim was to provide an easy way to join collections without having to figure it out each time. using the approach in bullet 3 works, but has to be thought through each time, and isn't as obvious as other collection methods. |
|
Upon further thought in the matter, example 3 above would only satisfy a one-off, but not conJoining of multiple collections or traversibles where some have string keys and other have index keys. The index keys would always be overwriten. I will update the unit tests to include string keys. |
|
I reviewed the other methods in the collection class again, and saw what you meant by the original collection not being mutated. In those instances where it returned I made the appropriate changes. Thanks @JosephSilber. |
|
Why are you using the camel-cased name "conJoin"? It seems rather strange seeing that "conjoin" is not a compound word. Also wouldn't "concat" be a more appropriate name? |
|
@thecrypticace Open to naming suggestions. :) I originally had it as conjoin, then for some reason made it camel-case. I'm not sure about concat, as that seems to be used predominantly for strings. I'll remove the camel-casing. Thanks for the feedback. :) |
|
For reference: concat is the name of the operation in javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat?v=control) and ruby (https://ruby-doc.org/core-2.4.0/Array.html#method-i-concat) and probably several other languages. |
|
I was looking at the PHP method http://php.net/manual/en/internals2.opcodes.concat.php as well as Excel (which is not related, of course, but its the first thing that comes to my mind). |
|
|
|
Ah, you're right, of course, thanks for correcting me on it not being a method. I just did a quick google search and just skimmed it. If @taylorotwell is open to merging this, I'll defer to him for the naming, and certainly encourage him to take |
conJoin() method to collectionconjoin() method to collection
|
Renamed to concat. |
This function allows joining of a traversable object with the current collection, ignoring the keys. This is useful when stacking collections based on different models or sources to be used for a singular purpose, for example generating of combined lists based off of multiple collections without overwriting values of existing keys.
Internals discussion: laravel/ideas#568