Sha256: 80f78810d49544373d7836b2732ebddc8035c1dc07d662a1bf6a6efa2c9ead90

Contents?: true

Size: 1.72 KB

Versions: 52

Compression:

Stored size: 1.72 KB

Contents

'use strict';

var isCallable = require('is-callable');

var toStr = Object.prototype.toString;
var hasOwnProperty = Object.prototype.hasOwnProperty;

var forEachArray = function forEachArray(array, iterator, receiver) {
    for (var i = 0, len = array.length; i < len; i++) {
        if (hasOwnProperty.call(array, i)) {
            if (receiver == null) {
                iterator(array[i], i, array);
            } else {
                iterator.call(receiver, array[i], i, array);
            }
        }
    }
};

var forEachString = function forEachString(string, iterator, receiver) {
    for (var i = 0, len = string.length; i < len; i++) {
        // no such thing as a sparse string.
        if (receiver == null) {
            iterator(string.charAt(i), i, string);
        } else {
            iterator.call(receiver, string.charAt(i), i, string);
        }
    }
};

var forEachObject = function forEachObject(object, iterator, receiver) {
    for (var k in object) {
        if (hasOwnProperty.call(object, k)) {
            if (receiver == null) {
                iterator(object[k], k, object);
            } else {
                iterator.call(receiver, object[k], k, object);
            }
        }
    }
};

var forEach = function forEach(list, iterator, thisArg) {
    if (!isCallable(iterator)) {
        throw new TypeError('iterator must be a function');
    }

    var receiver;
    if (arguments.length >= 3) {
        receiver = thisArg;
    }

    if (toStr.call(list) === '[object Array]') {
        forEachArray(list, iterator, receiver);
    } else if (typeof list === 'string') {
        forEachString(list, iterator, receiver);
    } else {
        forEachObject(list, iterator, receiver);
    }
};

module.exports = forEach;

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
immosquare-cleaner-0.1.62 node_modules/for-each/index.js
immosquare-cleaner-0.1.61 node_modules/for-each/index.js
immosquare-cleaner-0.1.60 node_modules/for-each/index.js
immosquare-cleaner-0.1.59 node_modules/for-each/index.js
immosquare-cleaner-0.1.58 node_modules/for-each/index.js
immosquare-cleaner-0.1.57 node_modules/for-each/index.js
immosquare-cleaner-0.1.56 node_modules/for-each/index.js
immosquare-cleaner-0.1.55 node_modules/for-each/index.js
immosquare-cleaner-0.1.54 node_modules/for-each/index.js
immosquare-cleaner-0.1.53 node_modules/for-each/index.js
immosquare-cleaner-0.1.52 node_modules/for-each/index.js
immosquare-cleaner-0.1.51 node_modules/for-each/index.js
immosquare-cleaner-0.1.50 node_modules/for-each/index.js
immosquare-cleaner-0.1.49 node_modules/for-each/index.js
immosquare-cleaner-0.1.48 node_modules/for-each/index.js
immosquare-cleaner-0.1.47 node_modules/for-each/index.js
immosquare-cleaner-0.1.46 node_modules/for-each/index.js
immosquare-cleaner-0.1.45 node_modules/for-each/index.js
immosquare-cleaner-0.1.44 node_modules/for-each/index.js
immosquare-cleaner-0.1.43 node_modules/for-each/index.js