IsLessThan.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $Number = GetIntrinsic('%Number%');
  4. var $TypeError = require('es-errors/type');
  5. var $isNaN = require('../helpers/isNaN');
  6. var IsStringPrefix = require('./IsStringPrefix');
  7. var StringToBigInt = require('./StringToBigInt');
  8. var ToNumeric = require('./ToNumeric');
  9. var ToPrimitive = require('./ToPrimitive');
  10. var BigIntLessThan = require('./BigInt/lessThan');
  11. var NumberLessThan = require('./Number/lessThan');
  12. // https://262.ecma-international.org/13.0/#sec-islessthan
  13. // eslint-disable-next-line max-statements, max-lines-per-function
  14. module.exports = function IsLessThan(x, y, LeftFirst) {
  15. if (typeof LeftFirst !== 'boolean') {
  16. throw new $TypeError('Assertion failed: LeftFirst argument must be a Boolean');
  17. }
  18. var px;
  19. var py;
  20. if (LeftFirst) {
  21. px = ToPrimitive(x, $Number);
  22. py = ToPrimitive(y, $Number);
  23. } else {
  24. py = ToPrimitive(y, $Number);
  25. px = ToPrimitive(x, $Number);
  26. }
  27. if (typeof px === 'string' && typeof py === 'string') {
  28. if (IsStringPrefix(py, px)) {
  29. return false;
  30. }
  31. if (IsStringPrefix(px, py)) {
  32. return true;
  33. }
  34. /*
  35. c. Let k be the smallest non-negative integer such that the code unit at index k within px is different from the code unit at index k within py. (There must be such a k, for neither String is a prefix of the other.)
  36. d. Let m be the integer that is the numeric value of the code unit at index k within px.
  37. e. Let n be the integer that is the numeric value of the code unit at index k within py.
  38. f. If m < n, return true. Otherwise, return false.
  39. */
  40. return px < py; // both strings, neither a prefix of the other. shortcut for steps 3 c-f
  41. }
  42. var nx;
  43. var ny;
  44. if (typeof px === 'bigint' && typeof py === 'string') {
  45. ny = StringToBigInt(py);
  46. if (typeof ny === 'undefined') {
  47. return void undefined;
  48. }
  49. return BigIntLessThan(px, ny);
  50. }
  51. if (typeof px === 'string' && typeof py === 'bigint') {
  52. nx = StringToBigInt(px);
  53. if (typeof nx === 'undefined') {
  54. return void undefined;
  55. }
  56. return BigIntLessThan(nx, py);
  57. }
  58. nx = ToNumeric(px);
  59. ny = ToNumeric(py);
  60. if (typeof nx === typeof ny) {
  61. return typeof nx === 'number' ? NumberLessThan(nx, ny) : BigIntLessThan(nx, ny);
  62. }
  63. if ($isNaN(nx) || $isNaN(ny)) {
  64. return void undefined;
  65. }
  66. if (nx === -Infinity || ny === Infinity) {
  67. return true;
  68. }
  69. if (nx === Infinity || ny === -Infinity) {
  70. return false;
  71. }
  72. return nx < ny; // by now, these are both finite, and the same type
  73. };