X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=node_modules%2Ffor-each%2Findex.js;fp=node_modules%2Ffor-each%2Findex.js;h=93843da9ae1ed03fa47bbc43330526b7468c1fab;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/node_modules/for-each/index.js b/node_modules/for-each/index.js new file mode 100644 index 000000000..93843da9a --- /dev/null +++ b/node_modules/for-each/index.js @@ -0,0 +1,46 @@ +var isFunction = require('is-function') + +module.exports = forEach + +var toString = Object.prototype.toString +var hasOwnProperty = Object.prototype.hasOwnProperty + +function forEach(list, iterator, context) { + if (!isFunction(iterator)) { + throw new TypeError('iterator must be a function') + } + + if (arguments.length < 3) { + context = this + } + + if (toString.call(list) === '[object Array]') + forEachArray(list, iterator, context) + else if (typeof list === 'string') + forEachString(list, iterator, context) + else + forEachObject(list, iterator, context) +} + +function forEachArray(array, iterator, context) { + for (var i = 0, len = array.length; i < len; i++) { + if (hasOwnProperty.call(array, i)) { + iterator.call(context, array[i], i, array) + } + } +} + +function forEachString(string, iterator, context) { + for (var i = 0, len = string.length; i < len; i++) { + // no such thing as a sparse string. + iterator.call(context, string.charAt(i), i, string) + } +} + +function forEachObject(object, iterator, context) { + for (var k in object) { + if (hasOwnProperty.call(object, k)) { + iterator.call(context, object[k], k, object) + } + } +}