d1fa8c6b47acd2f408fbc731cbdd4227e3457632aa5a34186d3ae98ac9218a5150a93f5fe3b3bf0320398936d87552d8cdb469a2c8c1c6878d98ca4917d3ec 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var Call = require('./Call');
  4. var SameValue = require('./SameValue');
  5. var ToNumber = require('./ToNumber');
  6. var isNaN = require('math-intrinsics/isNaN');
  7. // https://262.ecma-international.org/14.0/#sec-comparetypedarrayelements
  8. module.exports = function CompareTypedArrayElements(x, y, compareFn) {
  9. if ((typeof x !== 'number' && typeof x !== 'bigint') || typeof x !== typeof y) {
  10. throw new $TypeError('Assertion failed: `x` and `y` must be either a BigInt or a Number, and both must be the same type');
  11. }
  12. if (typeof compareFn !== 'function' && typeof compareFn !== 'undefined') {
  13. throw new $TypeError('Assertion failed: `compareFn` must be a function or undefined');
  14. }
  15. if (typeof compareFn !== 'undefined') { // step 2
  16. var v = ToNumber(Call(compareFn, void undefined, [x, y])); // step 2.a
  17. if (isNaN(v)) {
  18. return 0; // step 2.b
  19. }
  20. return v; // step 2.c
  21. }
  22. var xNaN = isNaN(x);
  23. var yNaN = isNaN(y);
  24. if (xNaN && yNaN) {
  25. return 0; // step 3
  26. }
  27. if (xNaN) {
  28. return 1; // step 4
  29. }
  30. if (yNaN) {
  31. return -1; // step 5
  32. }
  33. if (x < y) {
  34. return -1; // step 6
  35. }
  36. if (x > y) {
  37. return 1; // step 7
  38. }
  39. if (SameValue(x, -0) && SameValue(y, 0)) {
  40. return -1; // step 8
  41. }
  42. if (SameValue(x, 0) && SameValue(y, -0)) {
  43. return 1; // step 9
  44. }
  45. return 0; // step 10
  46. };