source: imaps-frontend/node_modules/for-each/index.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.7 KB
Line 
1'use strict';
2
3var isCallable = require('is-callable');
4
5var toStr = Object.prototype.toString;
6var hasOwnProperty = Object.prototype.hasOwnProperty;
7
8var forEachArray = function forEachArray(array, iterator, receiver) {
9 for (var i = 0, len = array.length; i < len; i++) {
10 if (hasOwnProperty.call(array, i)) {
11 if (receiver == null) {
12 iterator(array[i], i, array);
13 } else {
14 iterator.call(receiver, array[i], i, array);
15 }
16 }
17 }
18};
19
20var forEachString = function forEachString(string, iterator, receiver) {
21 for (var i = 0, len = string.length; i < len; i++) {
22 // no such thing as a sparse string.
23 if (receiver == null) {
24 iterator(string.charAt(i), i, string);
25 } else {
26 iterator.call(receiver, string.charAt(i), i, string);
27 }
28 }
29};
30
31var forEachObject = function forEachObject(object, iterator, receiver) {
32 for (var k in object) {
33 if (hasOwnProperty.call(object, k)) {
34 if (receiver == null) {
35 iterator(object[k], k, object);
36 } else {
37 iterator.call(receiver, object[k], k, object);
38 }
39 }
40 }
41};
42
43var forEach = function forEach(list, iterator, thisArg) {
44 if (!isCallable(iterator)) {
45 throw new TypeError('iterator must be a function');
46 }
47
48 var receiver;
49 if (arguments.length >= 3) {
50 receiver = thisArg;
51 }
52
53 if (toStr.call(list) === '[object Array]') {
54 forEachArray(list, iterator, receiver);
55 } else if (typeof list === 'string') {
56 forEachString(list, iterator, receiver);
57 } else {
58 forEachObject(list, iterator, receiver);
59 }
60};
61
62module.exports = forEach;
Note: See TracBrowser for help on using the repository browser.