This project is archived and is in readonly mode.

#130 ✓resolved
Samori Gorse

Update Specs for Array#extend / Array#concat

Reported by Samori Gorse | June 14th, 2008 @ 08:15 PM | in 1.2.1

Hi there, I was reviewing Array methods in mootools and I notice that Array#extend is just the same than Array#concat (Native which exists since Javascript 1.2. According to dev-moz documenation

Specs will still run if you change mootools-core source code so Array.prototype.extend would be a reference Array.prototype.concat.

Also, you may notice that Array#concat (contrary to Array#extend) allows you to concat n arrays to the subject :

var resultArray = array.concat(value1, value2, ..., valueN);

Comments and changes to this ticket

  • digitarald

    digitarald June 14th, 2008 @ 09:48 PM

    Nope, take a closer look.

    http://developer.mozilla.org/en/...

    Array::extend is a MUTATOR method, it combined the original array with the given argument-array and returns the original.

    Array::concat is an ACCESSOR method and returns a new array comprised of this array joined with other array(s) and/or value(s).

  • Samori Gorse

    Samori Gorse June 14th, 2008 @ 10:16 PM

    Hi there,

    Yep, I didn't notice that the method modified the array in place :-)

    I guess not creating another array when you just wanted to add values at its end (especially when it has considerable size) is faster than the other solution.

    Silly me :-)

    Sorry for disturbing you for nothing!

  • Jan Kassens

    Jan Kassens June 14th, 2008 @ 10:25 PM

    • State changed from “new” to “open”

    if the specs still run when concat is used the specs are incomplete.

  • Samori Gorse

    Samori Gorse June 15th, 2008 @ 12:50 AM

    Code proposal in mootools-core/Specs/Native/Array.js

    
    describe('Array.extend', {
    
            'should extend an array': function(){
                    var arr = [1,2,3,4];
                    arr.extend([1,2,3,4,5,6,7]);
                    value_of(arr).should_be([1,2,3,4,1,2,3,4,5,6,7]);
            }
    
    });
    
    
  • Samori Gorse

    Samori Gorse June 15th, 2008 @ 01:00 AM

    Optionally, this spec documents the difference between Array#concat and Array#extend

    
            'should not extend the array in place': function() {
                    var arr = [1,2,3,4];
                    arr.extend = Array.prototype.concat;
                    arr.extend([1,2,3,4,5,6,7]);
                    value_of(arr).should_be([1,2,3,4]);
                    value_of(arr).should_not_be([1,2,3,4,1,2,3,4,5,6,7]);
    
            }
    
    
    
  • Thomas Aylott

    Thomas Aylott June 16th, 2008 @ 05:14 PM

    • Milestone changed from 2.0 to 1.2.1
    • Title changed from “Array#extend is the same than Array#concat” to “Update Specs for Array#extend / Array#concat”
  • Jan Kassens

    Jan Kassens July 19th, 2008 @ 10:42 AM

    • State changed from “open” to “resolved”
    • Tag set to array, specs

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Tags

Pages