• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/58_index

  1 The `index()` function locates an element within a given array or a substring
  2 position within a given string, depending on the type of arguments given.
  3 
  4 Returns `null` if the given haystack argument is neither an array nor a string,
  5 returns `-1` if the element was not found within the array or the substring was
  6 not found within the string.
  7 
  8 Returns the first found index position in all other cases.
  9 
 10 -- Testcase --
 11 {%
 12         let o = {};
 13 
 14         printf("%.J\n", [
 15                 index([ 1, 2, "abc", 3, "abc", 1, 2 ], "abc"),  // should return 2
 16                 index([ 1, 2, 3 ], 4),                                                  // should return -1
 17                 index([ [], {} ], {}),                                                  // should return -1 (strict equality)
 18                 index([ [], o ], o),                                                    // should return 1 (strict equality)
 19 
 20                 index("foobarfoobarfoobar", "arf"),                             // should return 4
 21                 index("test", "hello"),                                                 // should return -1
 22                 index("test", "test"),                                                  // should return 0 (needle = haystack length special case)
 23                 index("test", ""),                                                              // should return 0 (zero length needle special case)
 24                 index("", ""),                                                                  // should return 0 (zero length special case)
 25                 index("foo\0foo\0foo", "o\0f"),                                 // should return 2 (binary safe)
 26 
 27                 index({ test: true }, true),                                    // should return null
 28                 index(1234, 3),                                                                 // should return null
 29         ]);
 30 %}
 31 -- End --
 32 
 33 -- Expect stdout --
 34 [
 35         2,
 36         -1,
 37         -1,
 38         1,
 39         4,
 40         -1,
 41         0,
 42         0,
 43         0,
 44         2,
 45         null,
 46         null
 47 ]
 48 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt