2021-02-26 04:58:33 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var GetIntrinsic = require('get-intrinsic');
|
|
|
|
|
|
|
|
var $Number = GetIntrinsic('%Number%');
|
|
|
|
var $TypeError = GetIntrinsic('%TypeError%');
|
|
|
|
|
|
|
|
var $isNaN = require('../helpers/isNaN');
|
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
var IsStringPrefix = require('./IsStringPrefix');
|
|
|
|
var StringToBigInt = require('./StringToBigInt');
|
|
|
|
var ToNumeric = require('./ToNumeric');
|
2021-02-26 04:58:33 +01:00
|
|
|
var ToPrimitive = require('./ToPrimitive');
|
|
|
|
var Type = require('./Type');
|
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
var BigIntLessThan = require('./BigInt/lessThan');
|
|
|
|
var NumberLessThan = require('./Number/lessThan');
|
2021-02-26 04:58:33 +01:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
// https://262.ecma-international.org/9.0/#sec-abstract-relational-comparison
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-statements, max-lines-per-function
|
2021-02-26 04:58:33 +01:00
|
|
|
module.exports = function AbstractRelationalComparison(x, y, LeftFirst) {
|
|
|
|
if (Type(LeftFirst) !== 'Boolean') {
|
|
|
|
throw new $TypeError('Assertion failed: LeftFirst argument must be a Boolean');
|
|
|
|
}
|
|
|
|
var px;
|
|
|
|
var py;
|
|
|
|
if (LeftFirst) {
|
|
|
|
px = ToPrimitive(x, $Number);
|
|
|
|
py = ToPrimitive(y, $Number);
|
|
|
|
} else {
|
|
|
|
py = ToPrimitive(y, $Number);
|
|
|
|
px = ToPrimitive(x, $Number);
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if (Type(px) === 'String' && Type(py) === 'String') {
|
|
|
|
if (IsStringPrefix(py, px)) {
|
2021-02-26 04:58:33 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if (IsStringPrefix(px, py)) {
|
2021-02-26 04:58:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
return px < py; // both strings, neither a prefix of the other. shortcut for steps 3 c-f
|
|
|
|
}
|
|
|
|
|
|
|
|
var pxType = Type(px);
|
|
|
|
var pyType = Type(py);
|
|
|
|
var nx;
|
|
|
|
var ny;
|
|
|
|
if (pxType === 'BigInt' && pyType === 'String') {
|
|
|
|
ny = StringToBigInt(py);
|
|
|
|
if ($isNaN(ny)) {
|
|
|
|
return void undefined;
|
2021-02-26 04:58:33 +01:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
return BigIntLessThan(px, ny);
|
|
|
|
}
|
|
|
|
if (pxType === 'String' && pyType === 'BigInt') {
|
|
|
|
nx = StringToBigInt(px);
|
|
|
|
if ($isNaN(nx)) {
|
|
|
|
return void undefined;
|
2021-02-26 04:58:33 +01:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
return BigIntLessThan(px, ny);
|
2021-02-26 04:58:33 +01:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
|
|
|
|
nx = ToNumeric(px);
|
|
|
|
ny = ToNumeric(py);
|
|
|
|
var nxType = Type(nx);
|
|
|
|
if (nxType === Type(ny)) {
|
|
|
|
return nxType === 'Number' ? NumberLessThan(nx, ny) : BigIntLessThan(nx, ny);
|
2021-02-26 04:58:33 +01:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
|
|
|
|
if ($isNaN(nx) || $isNaN(ny)) {
|
|
|
|
return void undefined;
|
|
|
|
}
|
|
|
|
if (nx === -Infinity || ny === Infinity) {
|
2021-02-26 04:58:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if (nx === Infinity || ny === -Infinity) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nx < ny; // by now, these are both nonzero, finite, and not equal
|
2021-02-26 04:58:33 +01:00
|
|
|
};
|