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.
		
		
		
		
		
			
		
			
				
					84 lines
				
				2.0 KiB
			
		
		
			
		
	
	
					84 lines
				
				2.0 KiB
			| 
											3 years ago
										 | /** | ||
|  |  * Obliterator ForEachWithNullKeys Function | ||
|  |  * ========================================= | ||
|  |  * | ||
|  |  * Helper function used to easily iterate over mixed values. | ||
|  |  */ | ||
|  | var support = require('./support.js'); | ||
|  | 
 | ||
|  | var ARRAY_BUFFER_SUPPORT = support.ARRAY_BUFFER_SUPPORT; | ||
|  | var SYMBOL_SUPPORT = support.SYMBOL_SUPPORT; | ||
|  | 
 | ||
|  | /** | ||
|  |  * Same function as the `forEach` but will yield `null` when the target | ||
|  |  * does not have keys. | ||
|  |  * | ||
|  |  * @param  {any}      iterable - Iterable value. | ||
|  |  * @param  {function} callback - Callback function. | ||
|  |  */ | ||
|  | module.exports = function forEachWithNullKeys(iterable, callback) { | ||
|  |   var iterator, k, i, l, s; | ||
|  | 
 | ||
|  |   if (!iterable) | ||
|  |     throw new Error('obliterator/forEachWithNullKeys: invalid iterable.'); | ||
|  | 
 | ||
|  |   if (typeof callback !== 'function') | ||
|  |     throw new Error('obliterator/forEachWithNullKeys: expecting a callback.'); | ||
|  | 
 | ||
|  |   // The target is an array or a string or function arguments
 | ||
|  |   if ( | ||
|  |     Array.isArray(iterable) || | ||
|  |     (ARRAY_BUFFER_SUPPORT && ArrayBuffer.isView(iterable)) || | ||
|  |     typeof iterable === 'string' || | ||
|  |     iterable.toString() === '[object Arguments]' | ||
|  |   ) { | ||
|  |     for (i = 0, l = iterable.length; i < l; i++) callback(iterable[i], null); | ||
|  |     return; | ||
|  |   } | ||
|  | 
 | ||
|  |   // The target is a Set
 | ||
|  |   if (iterable instanceof Set) { | ||
|  |     iterable.forEach(function (value) { | ||
|  |       callback(value, null); | ||
|  |     }); | ||
|  |     return; | ||
|  |   } | ||
|  | 
 | ||
|  |   // The target has a #.forEach method
 | ||
|  |   if (typeof iterable.forEach === 'function') { | ||
|  |     iterable.forEach(callback); | ||
|  |     return; | ||
|  |   } | ||
|  | 
 | ||
|  |   // The target is iterable
 | ||
|  |   if ( | ||
|  |     SYMBOL_SUPPORT && | ||
|  |     Symbol.iterator in iterable && | ||
|  |     typeof iterable.next !== 'function' | ||
|  |   ) { | ||
|  |     iterable = iterable[Symbol.iterator](); | ||
|  |   } | ||
|  | 
 | ||
|  |   // The target is an iterator
 | ||
|  |   if (typeof iterable.next === 'function') { | ||
|  |     iterator = iterable; | ||
|  |     i = 0; | ||
|  | 
 | ||
|  |     while (((s = iterator.next()), s.done !== true)) { | ||
|  |       callback(s.value, null); | ||
|  |       i++; | ||
|  |     } | ||
|  | 
 | ||
|  |     return; | ||
|  |   } | ||
|  | 
 | ||
|  |   // The target is a plain object
 | ||
|  |   for (k in iterable) { | ||
|  |     if (iterable.hasOwnProperty(k)) { | ||
|  |       callback(iterable[k], k); | ||
|  |     } | ||
|  |   } | ||
|  | 
 | ||
|  |   return; | ||
|  | }; |