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