Array.prototype-generic-functions.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. load("test-common.js");
  2. try {
  3. const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
  4. ["every"].forEach(name => {
  5. const visited = [];
  6. Array.prototype[name].call(o, function (value) {
  7. visited.push(value);
  8. return true;
  9. });
  10. assert(visited.length === 3);
  11. assert(visited[0] === "foo");
  12. assert(visited[1] === "bar");
  13. assert(visited[2] === "baz");
  14. });
  15. ["find", "findIndex"].forEach(name => {
  16. const visited = [];
  17. Array.prototype[name].call(o, function (value) {
  18. visited.push(value);
  19. return false;
  20. });
  21. assert(visited.length === 5);
  22. assert(visited[0] === "foo");
  23. assert(visited[1] === "bar");
  24. assert(visited[2] === undefined);
  25. assert(visited[3] === "baz");
  26. assert(visited[4] === undefined);
  27. });
  28. ["filter", "forEach", "map", "some"].forEach(name => {
  29. const visited = [];
  30. Array.prototype[name].call(o, function (value) {
  31. visited.push(value);
  32. return false;
  33. });
  34. assert(visited.length === 3);
  35. assert(visited[0] === "foo");
  36. assert(visited[1] === "bar");
  37. assert(visited[2] === "baz");
  38. });
  39. console.log("PASS");
  40. } catch (e) {
  41. console.log("FAIL: " + e);
  42. }