//
//
// here's some common Array operations you might find usefull and familiar:
//
// .each( callback, boolFlgFromLast )
// # iterate an array runing given callback-fn for every array item,
// # pass it curent array key and value, respectively,
// # set context of the callback to host array,
// # if boolFlgFromLast argument is ( === )true, iterate from last element,
// # break iteration if callback returns ( === )false,
// # return iterated array
//
// .not( callback )
// # remove items for which callback returns ( === )true
// # keep others
// # return host array
//
// .keep( callback )
// # keep items for which callback returns ( === )true
// # remove others
// # return host array
//
// .desparse()
// # 'desparse' host array in place
// # return host array
//
//
// var
// a = [
// [317, 193],
// [110, 334],
// [390, 347],
// ];
//
//
// a
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
// .not( function ( k, v ) { console.log(' * '); return ( v[0] == 317 ) && ( v[1] == 193 ); } )
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
// .keep( function ( k, v ) { console.log(' * '); return Math.random() > .1; } )
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v + ', [ this === a ] -> '+ ( this === a ) ); } );
//
// // make sparse array
// a[5] = [0,0];
// console.log('sparse array: ', a);
//
// a
// .desparse()
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
//
//
//
;( function( _a ) {
var
t = !0,
f = !t;
_a.each = function ( fn ) {
var
len = this.length,
i = 0;
for( ; i < len ; i++ ) {
if ( fn.call( this, i, this[i] ) === f ) break;
}
return this;
};
overload( 'each', _a, function ( fn, flgIterateBackwards ) {
if ( flgIterateBackwards === t ) {
var
i = this.length - 1;
for ( ; i >= 0 ; i-- ) {
if ( fn.call( this, i, this[i] ) === f ) break;
}
return this;
} else {
return this.each( fn );
}
}
);
_a.not = function ( callback ) {
return this.each( function ( k, v ) {
( callback.call( this, k, v ) === t ) && this.splice( k, 1 );
}, t );
};
_a.keep = function ( callback ) {
return this.each( function ( k, v ) {
( callback.call( this, k, v ) === t ) || this.splice( k, 1 );
}, t );
};
_a.desparse = function () {
return this.not( function ( k, v ) { return k in this === f; } );
};
// helper fn-s
function overload ( fn, obj, newfn ) {
return ( function ( origfn ) {
obj[fn] = function () {
var
args = _a.slice.call( arguments );
if ( newfn.length == arguments.length ) {
return newfn.apply( this, args )
} else if ( isfn ( origfn ) ) {
return origfn.apply( this, args )
} else {
// ignore non method props
}
};
} )( obj[fn] );
}
function isfn ( o ) {
return ( typeof o === 'function' ) &&
( Object.prototype.toString.call( o ) === '[object Function]' );
}
} )( Array.prototype );
//