Andrews Medina avatar Andrews Medina committed b90fd00

implemented [].lastIndexOf

Comments (0)

Files changed (2)

js/builtins/array.py

 
     put_native_function(w_ArrayPrototype, u'indexOf', index_of)
 
+    put_native_function(w_ArrayPrototype, u'lastIndexOf', last_index_of)
+
 
 # 15.4.4.7
 @w_return
 
 
 @w_return
+def last_index_of(this, args):
+    obj = this
+    elem = get_arg(args, 0)
+    length = this.get(u'length').ToUInt32()
+    from_index = length
+
+    if len(args) > 1:
+        findex = get_arg(args, 1).ToInt32()
+        if findex < 0:
+            from_index = length + findex
+        else:
+            from_index = findex
+
+    from js.jsobj import W_IntNumber
+    for i in xrange(from_index, -1, -1):
+        y = obj.get(unicode(i))
+        if elem == y:
+            return W_IntNumber(i)
+    return W_IntNumber(-1)
+
+
+@w_return
 def index_of(this, args):
     obj = this
     length = this.get(u'length').ToUInt32()

test/test_array.py

 from test.test_interp import assertv, assertp
 
 
+def test_arrya_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)
+    assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(2, 3));", "3", capsys)
+    assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(2, 2));", "0", capsys)
+    assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(2, -2));", "0", capsys)
+    assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(2, -1));", "3", capsys)
+
+
 def test_array_index_of(capsys):
     assertp("var a = [1,2,3]; print(a.indexOf(1));", "0", capsys)
     assertp("var a = [1,2,3]; print(a.indexOf(3));", "2", capsys)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.