Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Unshift after last executed element

  • Loading branch information...
commit b7da78f9f2203f364aa8a1c2e8c9124d5586683d 1 parent d8c433b
Worms David authored March 31, 2013
7  lib/each.js
@@ -69,15 +69,16 @@ module.exports = function(elements) {
69 69
     return eacher;
70 70
   };
71 71
   eacher.unshift = function(item) {
72  
-    var l;
  72
+    var index, l;
73 73
     l = arguments.length;
  74
+    index = Math.floor(eacher.started / times);
74 75
     if (l === 1) {
75  
-      elements.unshift(arguments[0]);
  76
+      elements.splice(index, 0, arguments[0]);
76 77
     } else if (l === 2) {
77 78
       if (!keys) {
78 79
         keys = [];
79 80
       }
80  
-      keys.unshift(arguments[0]);
  81
+      keys.splice(index, 0, arguments[0]);
81 82
       elements[arguments[0]] = arguments[1];
82 83
     }
83 84
     eacher.total++;
8  src/each.coffee
@@ -56,11 +56,15 @@ module.exports = (elements) ->
56 56
     eacher
57 57
   eacher.unshift = (item) ->
58 58
     l = arguments.length
  59
+    index = Math.floor(eacher.started / times)
  60
+    # console.log index
59 61
     if l is 1
60  
-      elements.unshift arguments[0]
  62
+      # elements.unshift arguments[0]
  63
+      elements.splice index, 0, arguments[0]
61 64
     else if l is 2
62 65
       keys = [] if not keys
63  
-      keys.unshift arguments[0]
  66
+      # keys.unshift arguments[0]
  67
+      keys.splice index, 0, arguments[0]
64 68
       elements[arguments[0]] = arguments[1]
65 69
     eacher.total++
66 70
     eacher
16  test/unshift.coffee
@@ -25,3 +25,19 @@ describe 'Unshift', ->
25 25
     .on 'end', (count) ->
26 26
       count.should.eql 2
27 27
       next()
  28
+
  29
+  it 'should place the next element', (next) ->
  30
+    last = null
  31
+    e = each(['a','b','c'])
  32
+    .on 'item', (value, next) ->
  33
+      if value is 'a'
  34
+        e.unshift 'aa'
  35
+      if last is 'a'
  36
+        value.should.eql 'aa'
  37
+      if last is 'aa'
  38
+        value.should.eql 'b'
  39
+      last = value
  40
+      next()
  41
+    .on 'end', (count) ->
  42
+      count.should.eql 4
  43
+      next()

0 notes on commit b7da78f

Please sign in to comment.
Something went wrong with that request. Please try again.