Commits

Andrews Medina committed 601cbe2

implemented [].shift

Comments (0)

Files changed (2)

js/builtins/array.py

 
     put_native_function(w_ArrayPrototype, u'lastIndexOf', last_index_of)
 
+    put_native_function(w_ArrayPrototype, u'shift', shift)
+
 
 # 15.4.4.7
 @w_return
         return element
 
 
+@w_return
+def shift(this, args):
+    o = this.ToObject()
+    l = o.get(u'length').ToUInt32()
+
+    if l == 0:
+        o.put(u'length', _w(0))
+        return newundefined()
+    else:
+        new_length = l - 1
+        element = o.get(u"0")
+        for i in xrange(0, new_length):
+            indx = unicode(str(i))
+            next_indx = unicode(str(i + 1))
+            o.put(indx, o.get(next_indx))
+        o.put(u'length', _w(new_length))
+        return element
+
+
 # 15.4.4.8
 @w_return
 def reverse(this, args):

test/test_array.py

 from test.test_interp import assertv, assertp
 
 
+def test_array_shift(capsys):
+    assertp("var a = [2, 5, 9, 2]; print(a.shift());", "2", capsys)
+    assertp("var a = [2, 5, 9, 2]; a.shift(); print(a);", "5,9,2", capsys)
+    assertp("var a = [2, 5, 9, 2]; a.shift(); print(a.length);", "3", capsys)
+
+
 def test_array_last_index_of(capsys):
     assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(2));", "3", capsys)
     assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(7));", "-1", capsys)