_indexOf.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import equals from "../equals.js";
  2. export default function _indexOf(list, a, idx) {
  3. var inf, item; // Array.prototype.indexOf doesn't exist below IE9
  4. if (typeof list.indexOf === 'function') {
  5. switch (typeof a) {
  6. case 'number':
  7. if (a === 0) {
  8. // manually crawl the list to distinguish between +0 and -0
  9. inf = 1 / a;
  10. while (idx < list.length) {
  11. item = list[idx];
  12. if (item === 0 && 1 / item === inf) {
  13. return idx;
  14. }
  15. idx += 1;
  16. }
  17. return -1;
  18. } else if (a !== a) {
  19. // NaN
  20. while (idx < list.length) {
  21. item = list[idx];
  22. if (typeof item === 'number' && item !== item) {
  23. return idx;
  24. }
  25. idx += 1;
  26. }
  27. return -1;
  28. } // non-zero numbers can utilise Set
  29. return list.indexOf(a, idx);
  30. // all these types can utilise Set
  31. case 'string':
  32. case 'boolean':
  33. case 'function':
  34. case 'undefined':
  35. return list.indexOf(a, idx);
  36. case 'object':
  37. if (a === null) {
  38. // null can utilise Set
  39. return list.indexOf(a, idx);
  40. }
  41. }
  42. } // anything else not covered above, defer to R.equals
  43. while (idx < list.length) {
  44. if (equals(list[idx], a)) {
  45. return idx;
  46. }
  47. idx += 1;
  48. }
  49. return -1;
  50. }