IsLessThan.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $Number = GetIntrinsic('%Number%');
  4. var $TypeError = require('es-errors/type');
  5. var min = GetIntrinsic('%Math.min%');
  6. var $isNaN = require('../helpers/isNaN');
  7. var $charCodeAt = require('call-bind/callBound')('String.prototype.charCodeAt');
  8. var StringToBigInt = require('./StringToBigInt');
  9. var ToNumeric = require('./ToNumeric');
  10. var ToPrimitive = require('./ToPrimitive');
  11. var BigIntLessThan = require('./BigInt/lessThan');
  12. var NumberLessThan = require('./Number/lessThan');
  13. // https://262.ecma-international.org/14.0/#sec-islessthan
  14. // eslint-disable-next-line max-statements, max-lines-per-function
  15. module.exports = function IsLessThan(x, y, LeftFirst) {
  16. if (typeof LeftFirst !== 'boolean') {
  17. throw new $TypeError('Assertion failed: LeftFirst argument must be a Boolean');
  18. }
  19. var px;
  20. var py;
  21. if (LeftFirst) {
  22. px = ToPrimitive(x, $Number);
  23. py = ToPrimitive(y, $Number);
  24. } else {
  25. py = ToPrimitive(y, $Number);
  26. px = ToPrimitive(x, $Number);
  27. }
  28. if (typeof px === 'string' && typeof py === 'string') { // step 3
  29. // a. Let lx be the length of px.
  30. // b. Let ly be the length of py.
  31. // c. For each integer i starting with 0 such that i < min(lx, ly), in ascending order, do
  32. // i. Let cx be the integer that is the numeric value of the code unit at index i within px.
  33. // ii. Let cy be the integer that is the numeric value of the code unit at index i within py.
  34. // iii. If cx < cy, return true.
  35. // iv. If cx > cy, return false.
  36. // d. If lx < ly, return true. Otherwise, return false.
  37. var lx = px.length; // step 3.a
  38. var ly = py.length; // step 3.b
  39. for (var i = 0; i < min(lx, ly); i++) { // step 3.c
  40. var cx = $charCodeAt(px, i); // step 3.c.i
  41. var cy = $charCodeAt(py, i); // step 3.c.ii
  42. if (cx < cy) {
  43. return true; // step 3.c.iii
  44. }
  45. if (cx > cy) {
  46. return false; // step 3.c.iv
  47. }
  48. }
  49. return lx < ly; // step 3.d
  50. }
  51. var nx;
  52. var ny;
  53. if (typeof px === 'bigint' && typeof py === 'string') {
  54. ny = StringToBigInt(py);
  55. if (typeof ny === 'undefined') {
  56. return void undefined;
  57. }
  58. return BigIntLessThan(px, ny);
  59. }
  60. if (typeof px === 'string' && typeof py === 'bigint') {
  61. nx = StringToBigInt(px);
  62. if (typeof nx === 'undefined') {
  63. return void undefined;
  64. }
  65. return BigIntLessThan(nx, py);
  66. }
  67. nx = ToNumeric(px);
  68. ny = ToNumeric(py);
  69. if (typeof nx === typeof ny) {
  70. return typeof nx === 'number' ? NumberLessThan(nx, ny) : BigIntLessThan(nx, ny);
  71. }
  72. if ($isNaN(nx) || $isNaN(ny)) {
  73. return void undefined;
  74. }
  75. if (nx === -Infinity || ny === Infinity) {
  76. return true;
  77. }
  78. if (nx === Infinity || ny === -Infinity) {
  79. return false;
  80. }
  81. return nx < ny; // by now, these are both finite, and the same type
  82. };