[njs] Array.prototype.findIndex() method.

Igor Sysoev igor at sysoev.ru
Tue Apr 4 10:24:37 UTC 2017


details:   http://hg.nginx.org/njs/rev/26e0ebd97454
branches:  
changeset: 331:26e0ebd97454
user:      Andrey Zelenkov <zelenkov at nginx.com>
date:      Tue Apr 04 06:16:49 2017 +0300
description:
Array.prototype.findIndex() method.

diffstat:

 njs/njs_array.c          |  58 ++++++++++++++++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  44 ++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+), 0 deletions(-)

diffs (136 lines):

diff -r 52d53653ac52 -r 26e0ebd97454 njs/njs_array.c
--- a/njs/njs_array.c	Tue Apr 04 06:10:10 2017 +0300
+++ b/njs/njs_array.c	Tue Apr 04 06:16:49 2017 +0300
@@ -100,6 +100,8 @@ static njs_ret_t njs_array_prototype_fil
     njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
 static njs_ret_t njs_array_prototype_find_continuation(njs_vm_t *vm,
     njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t njs_array_prototype_find_index_continuation(njs_vm_t *vm,
+    njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
 static njs_ret_t njs_array_prototype_map_continuation(njs_vm_t *vm,
     njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
 static nxt_noinline uint32_t njs_array_prototype_map_index(njs_array_t *array,
@@ -1531,6 +1533,54 @@ njs_array_prototype_find_continuation(nj
 }
 
 
+static njs_ret_t
+njs_array_prototype_find_index(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+    nxt_int_t         ret;
+    njs_array_iter_t  *iter;
+
+    ret = njs_array_iterator_args(vm, args, nargs);
+    if (nxt_slow_path(ret != NXT_OK)) {
+        return ret;
+    }
+
+    iter = njs_vm_continuation(vm);
+    iter->u.cont.function = njs_array_prototype_find_index_continuation;
+    iter->retval.data.truth = 0;
+
+    return njs_array_prototype_find_index_continuation(vm, args, nargs, unused);
+}
+
+
+static njs_ret_t
+njs_array_prototype_find_index_continuation(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+    double             index;
+    njs_array_iter_t   *iter;
+
+    iter = njs_vm_continuation(vm);
+    index = iter->index;
+
+    if (!njs_is_true(&iter->retval)) {
+        iter->index++;
+
+        if (iter->index < iter->length
+            && iter->index < args[0].data.u.array->length)
+        {
+            return njs_array_prototype_find_apply(vm, iter, args, nargs);
+        }
+
+        index = -1;
+    }
+
+    njs_number_set(&vm->retval, index);
+
+    return NXT_OK;
+}
+
+
 static nxt_noinline njs_ret_t
 njs_array_prototype_find_apply(njs_vm_t *vm, njs_array_iter_t *iter,
     njs_value_t *args, nxt_uint_t nargs)
@@ -2147,6 +2197,14 @@ static const njs_object_prop_t  njs_arra
                      njs_continuation_size(njs_array_find_t), 0),
     },
 
+    /* ES6. */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("findIndex"),
+        .value = njs_native_function(njs_array_prototype_find_index,
+                     njs_continuation_size(njs_array_iter_t), 0),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("map"),
diff -r 52d53653ac52 -r 26e0ebd97454 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Apr 04 06:10:10 2017 +0300
+++ b/njs/test/njs_unit_test.c	Tue Apr 04 06:16:49 2017 +0300
@@ -2927,6 +2927,50 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("undefined") },
 
     { nxt_string("var a = [];"
+                 "a.findIndex(function(v, i, a) { return v > 1 })"),
+      nxt_string("-1") },
+
+    { nxt_string("var a = [,NaN,0,-1];"
+                 "a.findIndex(function(v, i, a) { return v > 1 })"),
+      nxt_string("-1") },
+
+    { nxt_string("var a = [,NaN,0,-1,2];"
+                 "a.findIndex(function(v, i, a) { return v > 1 })"),
+     nxt_string("4") },
+
+    { nxt_string("var a = [1,2,3,-1,5];"
+                 "a.findIndex(function(v, i, a) { return v > 1 })"),
+      nxt_string("1") },
+
+    { nxt_string("var a = [,1,,-1,5];"
+                 "a.findIndex(function(v, i, a) { return v > 1 })"),
+      nxt_string("4") },
+
+    { nxt_string("var a = [,1,,-1,5,6];"
+                 "a.findIndex(function(v, i, a) { return v > 1 })"),
+      nxt_string("4") },
+
+    { nxt_string("[].findIndex(function(v) { return (v === undefined) })"),
+      nxt_string("-1") },
+
+    { nxt_string("[,].findIndex(function(v) { return (v === undefined) })"),
+      nxt_string("0") },
+
+    { nxt_string("[1,2,,3].findIndex(function(el){return el === undefined})"),
+      nxt_string("2") },
+
+    { nxt_string("[,2,,3].findIndex(function(el){return el === undefined})"),
+      nxt_string("0") },
+
+    { nxt_string("var a = [1,2,3,4,5,6];"
+                 "a.findIndex(function(v, i, a) { a.shift(); return v == 3 })"),
+      nxt_string("1") },
+
+    { nxt_string("var a = [1,2,3,4,5,6];"
+                 "a.findIndex(function(v, i, a) { a.shift(); return v == 4 })"),
+      nxt_string("-1") },
+
+    { nxt_string("var a = [];"
                  "a.map(function(v, i, a) { return v + 1 })"),
       nxt_string("") },
 


More information about the nginx-devel mailing list