|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* |
| 4 | + * The MIT License (MIT) |
| 5 | + * Copyright (c) 2015 Nathan Houle |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * Module dependencies. |
| 26 | + */ |
| 27 | + |
| 28 | +var keys = require('@ndhoule/keys'); |
| 29 | + |
| 30 | +var objToString = Object.prototype.toString; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests if a value is a number. |
| 34 | + * |
| 35 | + * @name isNumber |
| 36 | + * @api private |
| 37 | + * @param {*} val The value to test. |
| 38 | + * @return {boolean} Returns `true` if `val` is a number, otherwise `false`. |
| 39 | + */ |
| 40 | + |
| 41 | +var isNumber = function isNumber(val) { |
| 42 | + var type = typeof val; |
| 43 | + return ( |
| 44 | + type === 'number' || |
| 45 | + (type === 'object' && objToString.call(val) === '[object Number]') |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests if a value is an array. |
| 51 | + * |
| 52 | + * @name isArray |
| 53 | + * @api private |
| 54 | + * @param {*} val The value to test. |
| 55 | + * @return {boolean} Returns `true` if the value is an array, otherwise `false`. |
| 56 | + */ |
| 57 | + |
| 58 | +var isArray = |
| 59 | + typeof Array.isArray === 'function' |
| 60 | + ? Array.isArray |
| 61 | + : function isArray(val) { |
| 62 | + return objToString.call(val) === '[object Array]'; |
| 63 | + }; |
| 64 | + |
| 65 | +/** |
| 66 | + * Tests if a value is array-like. Array-like means the value is not a function and has a numeric |
| 67 | + * `.length` property. |
| 68 | + * |
| 69 | + * @name isArrayLike |
| 70 | + * @api private |
| 71 | + * @param {*} val |
| 72 | + * @return {boolean} |
| 73 | + */ |
| 74 | + |
| 75 | +var isArrayLike = function isArrayLike(val) { |
| 76 | + return ( |
| 77 | + val != null && |
| 78 | + (isArray(val) || (val !== 'function' && isNumber(val.length))) |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +/** |
| 83 | + * Internal implementation of `each`. Works on arrays and array-like data structures. |
| 84 | + * |
| 85 | + * @name arrayEach |
| 86 | + * @api private |
| 87 | + * @param {Function(value, key, collection)} iterator The function to invoke per iteration. |
| 88 | + * @param {Array} array The array(-like) structure to iterate over. |
| 89 | + * @return {undefined} |
| 90 | + */ |
| 91 | +var arrayEach = function arrayEach(iterator, array) { |
| 92 | + for (var i = 0; i < array.length; i += 1) { |
| 93 | + // Break iteration early if `iterator` returns `false` |
| 94 | + if (iterator(array[i], i, array) === false) { |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * Internal implementation of `each`. Works on objects. |
| 102 | + * |
| 103 | + * @name baseEach |
| 104 | + * @api private |
| 105 | + * @param {Function(value, key, collection)} iterator The function to invoke per iteration. |
| 106 | + * @param {Object} object The object to iterate over. |
| 107 | + * @return {undefined} |
| 108 | + */ |
| 109 | +var baseEach = function baseEach(iterator, object) { |
| 110 | + var ks = keys(object); |
| 111 | + |
| 112 | + for (var i = 0; i < ks.length; i += 1) { |
| 113 | + // Break iteration early if `iterator` returns `false` |
| 114 | + if (iterator(object[ks[i]], ks[i], object) === false) { |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +/** |
| 121 | + * Iterate over an input collection, invoking an `iterator` function for each element in the |
| 122 | + * collection and passing to it three arguments: `(value, index, collection)`. The `iterator` |
| 123 | + * function can end iteration early by returning `false`. |
| 124 | + * |
| 125 | + * @name each |
| 126 | + * @api public |
| 127 | + * @param {Function(value, key, collection)} iterator The function to invoke per iteration. |
| 128 | + * @param {Array|Object|string} collection The collection to iterate over. |
| 129 | + * @return {undefined} Because `each` is run only for side effects, always returns `undefined`. |
| 130 | + * @example |
| 131 | + * var log = console.log.bind(console); |
| 132 | + * |
| 133 | + * each(log, ['a', 'b', 'c']); |
| 134 | + * //-> 'a', 0, ['a', 'b', 'c'] |
| 135 | + * //-> 'b', 1, ['a', 'b', 'c'] |
| 136 | + * //-> 'c', 2, ['a', 'b', 'c'] |
| 137 | + * //=> undefined |
| 138 | + * |
| 139 | + * each(log, 'tim'); |
| 140 | + * //-> 't', 2, 'tim' |
| 141 | + * //-> 'i', 1, 'tim' |
| 142 | + * //-> 'm', 0, 'tim' |
| 143 | + * //=> undefined |
| 144 | + * |
| 145 | + * // Note: Iteration order not guaranteed across environments |
| 146 | + * each(log, { name: 'tim', occupation: 'enchanter' }); |
| 147 | + * //-> 'tim', 'name', { name: 'tim', occupation: 'enchanter' } |
| 148 | + * //-> 'enchanter', 'occupation', { name: 'tim', occupation: 'enchanter' } |
| 149 | + * //=> undefined |
| 150 | + */ |
| 151 | +var each = function each(iterator, collection) { |
| 152 | + return (isArrayLike(collection) ? arrayEach : baseEach).call( |
| 153 | + this, |
| 154 | + iterator, |
| 155 | + collection |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +/* |
| 160 | + * Exports. |
| 161 | + */ |
| 162 | + |
| 163 | +module.exports = each; |
0 commit comments