You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					47 lines
				
				836 B
			
		
		
			
		
	
	
					47 lines
				
				836 B
			| 
								 
											3 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Obliterator Chain Function
							 | 
						||
| 
								 | 
							
								 * ===========================
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * Variadic function combining the given iterables.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								var Iterator = require('./iterator.js');
							 | 
						||
| 
								 | 
							
								var iter = require('./iter.js');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Chain.
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param  {...Iterator} iterables - Target iterables.
							 | 
						||
| 
								 | 
							
								 * @return {Iterator}
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								module.exports = function chain() {
							 | 
						||
| 
								 | 
							
								  var iterables = arguments;
							 | 
						||
| 
								 | 
							
								  var current = null;
							 | 
						||
| 
								 | 
							
								  var i = -1;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  /* eslint-disable no-constant-condition */
							 | 
						||
| 
								 | 
							
								  return new Iterator(function next() {
							 | 
						||
| 
								 | 
							
								    var step = null;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    do {
							 | 
						||
| 
								 | 
							
								      if (current === null) {
							 | 
						||
| 
								 | 
							
								        i++;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (i >= iterables.length) return {done: true};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        current = iter(iterables[i]);
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      step = current.next();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      if (step.done === true) {
							 | 
						||
| 
								 | 
							
								        current = null;
							 | 
						||
| 
								 | 
							
								        continue;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      break;
							 | 
						||
| 
								 | 
							
								    } while (true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return step;
							 | 
						||
| 
								 | 
							
								  });
							 | 
						||
| 
								 | 
							
								};
							 |