JavaScript array_pad
Returns a copy of input array padded with pad_value to size pad_size
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 | function array_pad ( input, pad_size, pad_value ) { // Returns a copy of input array padded with pad_value to size pad_size // // version: 1008.1718 // discuss at: http://phpjs.org/functions/array_pad // + original by: Waldo Malqui Silva // * example 1: array_pad([ 7, 8, 9 ], 2, 'a'); // * returns 1: [ 7, 8, 9] // * example 2: array_pad([ 7, 8, 9 ], 5, 'a'); // * returns 2: [ 7, 8, 9, 'a', 'a'] // * example 3: array_pad([ 7, 8, 9 ], 5, 2); // * returns 3: [ 7, 8, 9, 2, 2] // * example 4: array_pad([ 7, 8, 9 ], -5, 'a'); // * returns 4: [ 'a', 'a', 7, 8, 9 ] var pad = [], newArray = [], newLength, i=0; if ( input instanceof Array && !isNaN( pad_size ) ) { newLength = ( ( pad_size < 0 ) ? ( pad_size * -1 ) : pad_size ); if ( newLength > input.length ) { for ( i = 0; i < ( newLength - input.length ); i++ ) { newArray [ i ] = pad_value; } pad = ( ( pad_size < 0 ) ? newArray.concat( input ) : input.concat( newArray ) ); } else { pad = input; } } return pad; } |
Examples
» Example 1
Running
1 | array_pad([ 7, 8, 9 ], 2, 'a'); |
Should return
1 | [ 7, 8, 9] |
» Example 2
Running
1 | array_pad([ 7, 8, 9 ], 5, 'a'); |
Should return
1 | [ 7, 8, 9, 'a', 'a'] |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have array_pad goodness in JavaScript.
// Even quicker function is possible!
Array.prototype.pad = function(pad_size, pad_value){
var tail_length = Math.abs(pad_size) - this.length;
if(tail_length <= 0){
return this; // replace "this" with "this.slice(0)" if you want a copy
}
var tail = [];
var subTail = [pad_value];
for( ; ; ){
if(tail_length % 2){
tail = tail.concat(subTail);
}
if(tail_length >>= 1){
subTail = subTail.concat(subTail);
}
else{
break;
}
}
return pad_size < 0 ? tail.concat(this) : this.concat(tail);
}
// Is for loop the best way to fill an array with same value in JavaScript?
// No! Let's steal the idea from here: http://en.wikipedia.org/wiki/Exponentiation_by_squaring.
// We get a PHP-like array_pad function:
function array_pad(array, pad_size, pad_value){
var tail_length = Math.abs(pad_size) - array.length;
if(tail_length <= 0){
return array;
}
var tail = [];
var subTail = [pad_value];
while(tail_length > 0){
if(tail_length % 2){
tail = tail.concat(subTail);
}
subTail = subTail.concat(subTail);
tail_length >>= 1;
}
return pad_size < 0 ? tail.concat(array) : array.concat(tail);
}


Rafał Kukawski
Jul 5th
function array_pad(input, pad_size, pad_value){ try { pad_size |= 0; // force pad_size to be int var diff = Math.abs(pad_size) - input.length, pad = []; if(diff <= 0){ return input.slice(0); } while(diff--){ pad.push(pad_value); } return pad_size < 0 ? pad.concat(input) : input.concat(pad); } catch(e) { return null; // PHP throws a warning and returns null when first param isn't an array } }I wrapped the functions body with try..catch, in case the input isn't an array or anything else that has concat and slice methods (the function works also with strings, cause both methods are included in String prototype :-P). To be 99.9% compatible with PHP and not let this function handle strings, just add a condition
if(!(input instanceOf Array)){ throw "Not an array"; }or
if(!(input instanceOf Array)){ return null; }Some people prefer also "better" checking for arrays
Another problem is handling arrays with non-numeric indexes. I don't see a stable strategy along the whole phpjs project, that is why I didn't implement support for it.