pax_global_header00006660000000000000000000000064124077411130014512gustar00rootroot0000000000000052 comment=0c73e8e9df9bb82ac721a6ab849382ffd835bb88 foreach-2.0.5/000077500000000000000000000000001240774111300131255ustar00rootroot00000000000000foreach-2.0.5/.gitignore000066400000000000000000000000371240774111300151150ustar00rootroot00000000000000components build node_modules/ foreach-2.0.5/.npmignore000066400000000000000000000000351240774111300151220ustar00rootroot00000000000000node_modules components buildforeach-2.0.5/LICENSE000066400000000000000000000020621240774111300141320ustar00rootroot00000000000000The MIT License Copyright (c) 2013 Manuel Stofer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. foreach-2.0.5/Makefile000066400000000000000000000002271240774111300145660ustar00rootroot00000000000000 build: components @component build components: component.json @component install --dev clean: rm -fr build components template.js .PHONY: clean foreach-2.0.5/Readme.md000066400000000000000000000011211240774111300146370ustar00rootroot00000000000000 # foreach Iterate over the key value pairs of either an array-like object or a dictionary like object. [![browser support][1]][2] ## API ### foreach(object, function, [context]) ```js var each = require('foreach'); each([1,2,3], function (value, key, array) { // value === 1, 2, 3 // key === 0, 1, 2 // array === [1, 2, 3] }); each({0:1,1:2,2:3}, function (value, key, object) { // value === 1, 2, 3 // key === 0, 1, 2 // object === {0:1,1:2,2:3} }); ``` [1]: https://ci.testling.com/manuelstofer/foreach.png [2]: https://ci.testling.com/manuelstofer/foreach foreach-2.0.5/component.json000066400000000000000000000003431240774111300160220ustar00rootroot00000000000000{ "name": "foreach", "description": "foreach component + npm package", "version": "2.0.5", "keywords": [], "dependencies": {}, "scripts": [ "index.js" ], "repo": "manuelstofer/foreach" } foreach-2.0.5/index.js000066400000000000000000000010531240774111300145710ustar00rootroot00000000000000 var hasOwn = Object.prototype.hasOwnProperty; var toString = Object.prototype.toString; module.exports = function forEach (obj, fn, ctx) { if (toString.call(fn) !== '[object Function]') { throw new TypeError('iterator must be a function'); } var l = obj.length; if (l === +l) { for (var i = 0; i < l; i++) { fn.call(ctx, obj[i], i, obj); } } else { for (var k in obj) { if (hasOwn.call(obj, k)) { fn.call(ctx, obj[k], k, obj); } } } }; foreach-2.0.5/package.json000066400000000000000000000024211240774111300154120ustar00rootroot00000000000000{ "name": "foreach", "description": "foreach component + npm package", "version": "2.0.5", "author": "Manuel Stofer ", "contributors": [ { "name": "Manuel Stofer" }, { "name": "Jordan Harband", "url": "https://github.com/ljharb" } ], "license": "MIT", "main": "index.js", "scripts": { "test": "node test.js", "coverage": "covert test.js", "coverage-quiet": "covert --quiet test.js" }, "repository": { "type": "git", "url": "git://github.com/manuelstofer/foreach" }, "keywords": [ "shim", "Array.prototype.forEach", "forEach", "Array#forEach", "each" ], "dependencies": {}, "devDependencies": { "tape": "*", "covert": "*" }, "testling": { "files": "test.js", "browsers": [ "iexplore/6.0..latest", "firefox/3.0", "firefox/15.0..latest", "firefox/nightly", "chrome/4.0", "chrome/22.0..latest", "chrome/canary", "opera/10.0..latest", "opera/next", "safari/5.0.5..latest", "ipad/6.0..latest", "iphone/6.0..latest", "android-browser/4.2" ] } } foreach-2.0.5/test.js000066400000000000000000000112001240774111300144340ustar00rootroot00000000000000var test = require('tape'); var forEach = require('./index.js'); test('second argument: iterator', function (t) { var arr = []; t.throws(function () { forEach(arr); }, TypeError, 'undefined is not a function'); t.throws(function () { forEach(arr, null); }, TypeError, 'null is not a function'); t.throws(function () { forEach(arr, ''); }, TypeError, 'string is not a function'); t.throws(function () { forEach(arr, /a/); }, TypeError, 'regex is not a function'); t.throws(function () { forEach(arr, true); }, TypeError, 'true is not a function'); t.throws(function () { forEach(arr, false); }, TypeError, 'false is not a function'); t.throws(function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function'); t.throws(function () { forEach(arr, 42); }, TypeError, '42 is not a function'); t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function'); t.end(); }); test('array', function (t) { var arr = [1, 2, 3]; t.test('iterates over every item', function (st) { var index = 0; forEach(arr, function () { index += 1; }); st.equal(index, arr.length, 'iterates ' + arr.length + ' times'); st.end(); }); t.test('first iterator argument', function (st) { var index = 0; st.plan(arr.length); forEach(arr, function (item) { st.equal(arr[index], item, 'item ' + index + ' is passed as first argument'); index += 1; }); st.end(); }); t.test('second iterator argument', function (st) { var counter = 0; st.plan(arr.length); forEach(arr, function (item, index) { st.equal(counter, index, 'index ' + index + ' is passed as second argument'); counter += 1; }); st.end(); }); t.test('third iterator argument', function (st) { st.plan(arr.length); forEach(arr, function (item, index, array) { st.deepEqual(arr, array, 'array is passed as third argument'); }); st.end(); }); t.test('context argument', function (st) { var context = {}; st.plan(1); forEach([1], function () { st.equal(this, context, '"this" is the passed context'); }, context); st.end(); }); t.end(); }); test('object', function (t) { var obj = { a: 1, b: 2, c: 3 }; var keys = ['a', 'b', 'c']; var F = function () { this.a = 1; this.b = 2; }; F.prototype.c = 3; var fKeys = ['a', 'b']; t.test('iterates over every object literal key', function (st) { var counter = 0; forEach(obj, function () { counter += 1; }); st.equal(counter, keys.length, 'iterated ' + counter + ' times'); st.end(); }); t.test('iterates only over own keys', function (st) { var counter = 0; forEach(new F(), function () { counter += 1; }); st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times'); st.end(); }); t.test('first iterator argument', function (st) { var index = 0; st.plan(keys.length); forEach(obj, function (item) { st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument'); index += 1; }); st.end(); }); t.test('second iterator argument', function (st) { var counter = 0; st.plan(keys.length); forEach(obj, function (item, key) { st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument'); counter += 1; }); st.end(); }); t.test('third iterator argument', function (st) { st.plan(keys.length); forEach(obj, function (item, key, object) { st.deepEqual(obj, object, 'object is passed as third argument'); }); st.end(); }); t.test('context argument', function (st) { var context = {}; st.plan(1); forEach({ a: 1 }, function () { st.equal(this, context, '"this" is the passed context'); }, context); st.end(); }); t.end(); }); test('string', function (t) { var str = 'str'; t.test('second iterator argument', function (st) { var counter = 0; st.plan(str.length * 2 + 1); forEach(str, function (item, index) { st.equal(counter, index, 'index ' + index + ' is passed as second argument'); st.equal(str.charAt(index), item); counter += 1; }); st.equal(counter, str.length, 'iterates ' + str.length + ' times'); }); t.end(); });