Permalink
Browse files

use native forEach and map if available

  • Loading branch information...
1 parent 87d8a49 commit f6c338280abedb662ab792b13fbe8728f32d8e28 @marcuswestin committed May 18, 2013
Showing with 8 additions and 2 deletions.
  1. +6 −2 each.js
  2. +2 −0 map.js
View
@@ -3,8 +3,12 @@ var isList = require('./isList')
module.exports = function each(items, fn) {
if (!items) { return }
if (isList(items)) {
- for (var i=0; i < items.length; i++) {
- fn(items[i], i)
+ if (items.forEach) {
+ items.forEach(fn)
+ } else {
+ for (var i=0; i < items.length; i++) {
+ fn(items[i], i)
+ }
}
} else {
for (var key in items) {
View
@@ -1,7 +1,9 @@
var each = require('./each')
var isList = require('./isList')
+var isArray = require('./isArray')
module.exports = function map(obj, fn) {
+ if (isArray(obj) && obj.map) { return obj.map(fn) }
var result = isList(obj) ? new Array(obj.length) : {}
each(obj, function(val, key) {
result[key] = fn(val, key)

0 comments on commit f6c3382

Please sign in to comment.