Permalink
Browse files

add array.js and range.js

  • Loading branch information...
marcuswestin committed May 29, 2013
1 parent 5bce572 commit 2e01c47e861e642781e3f4ec904c1a682f65c424
Showing with 33 additions and 0 deletions.
  1. +15 −0 array.js
  2. +18 −0 range.js
View
@@ -0,0 +1,15 @@
+var range = require('./range')
+var each = require('./each')
+var identity = require('./identity')
+
+module.exports = function(obj, iteratorFn) {
+ if (typeof obj == 'number') {
+ obj = range(obj)
+ }
+ if (!iteratorFn) { iteratorFn = identity }
+ var result = []
+ each(obj, function(value, key) {
+ result.push(iteratorFn(value, key))
+ })
+ return result
+}
View
@@ -0,0 +1,18 @@
+module.exports = function range(start, stop, step) {
+ if (arguments.length <= 1) {
+ stop = start || 0
+ start = 0
+ }
+ step = arguments[2] || 1
+
+ var length = Math.max(Math.ceil((stop - start) / step), 0)
+ var index = 0
+ var result = new Array(length)
+
+ while (index < length) {
+ result[index++] = start
+ start += step
+ }
+
+ return result
+}

0 comments on commit 2e01c47

Please sign in to comment.