| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
|---|
| 4 |
|
|---|
| 5 | var $Number = GetIntrinsic('%Number%');
|
|---|
| 6 | var $TypeError = require('es-errors/type');
|
|---|
| 7 |
|
|---|
| 8 | var $isNaN = require('math-intrinsics/isNaN');
|
|---|
| 9 |
|
|---|
| 10 | var IsStringPrefix = require('./IsStringPrefix');
|
|---|
| 11 | var StringToBigInt = require('./StringToBigInt');
|
|---|
| 12 | var ToNumeric = require('./ToNumeric');
|
|---|
| 13 | var ToPrimitive = require('./ToPrimitive');
|
|---|
| 14 |
|
|---|
| 15 | var BigIntLessThan = require('./BigInt/lessThan');
|
|---|
| 16 | var NumberLessThan = require('./Number/lessThan');
|
|---|
| 17 |
|
|---|
| 18 | var isSameType = require('../helpers/isSameType');
|
|---|
| 19 |
|
|---|
| 20 | // https://262.ecma-international.org/11.0/#sec-abstract-relational-comparison
|
|---|
| 21 |
|
|---|
| 22 | // eslint-disable-next-line max-statements, max-lines-per-function
|
|---|
| 23 | module.exports = function AbstractRelationalComparison(x, y, LeftFirst) {
|
|---|
| 24 | if (typeof LeftFirst !== 'boolean') {
|
|---|
| 25 | throw new $TypeError('Assertion failed: LeftFirst argument must be a Boolean');
|
|---|
| 26 | }
|
|---|
| 27 | var px;
|
|---|
| 28 | var py;
|
|---|
| 29 | if (LeftFirst) {
|
|---|
| 30 | px = ToPrimitive(x, $Number);
|
|---|
| 31 | py = ToPrimitive(y, $Number);
|
|---|
| 32 | } else {
|
|---|
| 33 | py = ToPrimitive(y, $Number);
|
|---|
| 34 | px = ToPrimitive(x, $Number);
|
|---|
| 35 | }
|
|---|
| 36 | if (typeof px === 'string' && typeof py === 'string') {
|
|---|
| 37 | if (IsStringPrefix(py, px)) {
|
|---|
| 38 | return false;
|
|---|
| 39 | }
|
|---|
| 40 | if (IsStringPrefix(px, py)) {
|
|---|
| 41 | return true;
|
|---|
| 42 | }
|
|---|
| 43 | return px < py; // both strings, neither a prefix of the other. shortcut for steps 3 c-f
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | var nx;
|
|---|
| 47 | var ny;
|
|---|
| 48 | if (typeof px === 'bigint' && typeof py === 'string') {
|
|---|
| 49 | ny = StringToBigInt(py);
|
|---|
| 50 | if ($isNaN(ny)) {
|
|---|
| 51 | return void undefined;
|
|---|
| 52 | }
|
|---|
| 53 | return BigIntLessThan(px, ny);
|
|---|
| 54 | }
|
|---|
| 55 | if (typeof px === 'string' && typeof py === 'bigint') {
|
|---|
| 56 | nx = StringToBigInt(px);
|
|---|
| 57 | if ($isNaN(nx)) {
|
|---|
| 58 | return void undefined;
|
|---|
| 59 | }
|
|---|
| 60 | return BigIntLessThan(nx, py);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | nx = ToNumeric(px);
|
|---|
| 64 | ny = ToNumeric(py);
|
|---|
| 65 | if (isSameType(nx, ny)) {
|
|---|
| 66 | return typeof nx === 'number' ? NumberLessThan(nx, ny) : BigIntLessThan(nx, ny);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | if ($isNaN(nx) || $isNaN(ny)) {
|
|---|
| 70 | return void undefined;
|
|---|
| 71 | }
|
|---|
| 72 | if (nx === -Infinity || ny === Infinity) {
|
|---|
| 73 | return true;
|
|---|
| 74 | }
|
|---|
| 75 | if (nx === Infinity || ny === -Infinity) {
|
|---|
| 76 | return false;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return nx < ny; // by now, these are both nonzero, finite, and not equal
|
|---|
| 80 | };
|
|---|