/**
* @external Array
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
*/
(function($$) {
var Break = $$.ns('util.control.Break');
// var End = $$.ns('util.control.End');
/**
* add one or more items/arrays for concat in Array.
* empty Arrays and undefined items are ignored
*
* @function external:Array.prototype.xAdd
*
* @param {...AnyItem} item one or more of args
*
* @this module:EventJS
* @return {module:EventJS}
*
* @example
* // standard
* [].xAdd('a'); // ['a']
* [].xAdd( 'a', 'b' ); // ['a','b']
* [].xAdd([ 'a', 'b' ]); // ['a','b']
* [].xAdd(['a'], ['b']); // ['a','b']
*
* // empty items
* [].xAdd(); // []
* [].xAdd([]); // []
* [].xAdd(['a'], []); // ['a']
*
* // undefined items
* [].xAdd( undefined ); // []
* [].xAdd( undefined, 'b' ); // ['b']
* [].xAdd([ 'a', undefined ]); // ['a']
* [].xAdd(['a'], [ undefined ]); // ['a']
*/
function xAddArray() {
var i = 0,
m = arguments.length,
a;
for (; i<m; i+=1 ) {
a = arguments[i];
if ( $$.isType( a, 'Array' ) ) {
xAddArray.apply( this, a );
}
else if ( a !== undefined ) {
this.push(a);
}
}
return this;
}
Object.defineProperty( Array.prototype, 'xAdd', {
value: xAddArray
});
/**
* call of each Array items with {@linkcode module:BaseJS.utilTick|(async) Crisp.utilTick}
* and execute <code>option.success</code> and/or <code>option.complete</code> with {@linkcode module:BaseJS.utilTick|(async) Crisp.utilTick}
*
* @function external:Array.prototype.xEach
*
* @param {external:Object} option
* @param {util.utilTickCallback} option.success callback function for execute each item with {@linkcode module:BaseJS.utilTick|(async) Crisp.utilTick}
* @param {AnyItem} [option.self] use Object for .call() the <code>option.success</code> an <code>option.complete</code> function
* @param {util.utilTickCallback} [option.complete] callback function for exeute on the end of xEach with {@linkcode module:BaseJS.utilTick|(async) Crisp.utilTick}
* @param {external:Boolean} [option.async] enable asynchronus for call of each Array items with {@linkcode module:BaseJS.utilTick|(async) Crisp.utilTick}
* @param {external:Number} [option.start=0] start index of each
* @param {external:Number} [option.limit=length] limit items of each
*
* @this external:Array
* @return {external:Array}
*
* @tutorial {@link http://opencrisp.wca.at/tutorials/BaseJS-xEach_test.html#array|use Array.xEach}
* @tutorial {@link http://opencrisp.wca.at/tutorials/BaseJS-xEach_test.html#array-option-start|use Array.xEach( start )}
* @tutorial {@link http://opencrisp.wca.at/tutorials/BaseJS-xEach_test.html#array-option-limit|use Array.xEach( limit )}
*
* @example
* ['A','B'].xEach({
* success: function( item, index ) {
* // return; got to the next item
* // throw new Break(); stop each of items
* console.log('success:', index, item );
* },
* complete: function() {
* console.log('complete');
* }
* });
* console.log('end');
* // logs:
* // success: 0 A
* // success: 1 B
* // complete
* // end
*
*
* @example
* // async
* ['A','B'].xEach({
* async: true,
* success: function( item, index ) {
* console.log('success:', index, item );
* },
* complete: function() {
* console.log('complete');
* }
* });
* console.log('end');
* // logs:
* // end
* // success: 0 A
* // success: 1 B
* // complete
*/
function xEachArray( option ) {
var i = 0,
length = this.length,
start = option.start ? Number( option.start ) : 0,
limit = option.limit === undefined ? length : Number( option.limit );
if ( limit <= 0 ) {
limit = length;
}
if ( start < 0 ) {
start = length + start;
}
if ( start + limit > length ) {
limit -= start + limit - length;
}
if ( start < 0 ) {
start = 0;
limit = length;
}
try {
for (; i<limit; i+=1 ) {
option.success.call( option.self, this[ i + start ], i + start );
}
} catch (e) { if ( e instanceof Break ) {} else { throw e; } }
return this;
}
Object.defineProperty( Array.prototype, 'xEach', {
value: function( option ) {
return $$.utilTick( this, xEachArray, option, option.async );
}
});
/**
* @function external:Array.prototype.xTo
* @implements {module:BaseJS.to}
*
* @param {external:String} [type="json"]
*
* @this external:Array
* @return {external:String}
*
* @example
* ['a'].xTo(); // '["a"]'
*/
// Object.defineProperty( Array.prototype, 'xTo', {
// value: $$.to
// });
})(Crisp);