[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 | var $isFinite = require('../helpers/isFinite');
|
---|
| 10 | var isPrefixOf = require('../helpers/isPrefixOf');
|
---|
| 11 |
|
---|
| 12 | var ToNumber = require('./ToNumber');
|
---|
| 13 | var ToPrimitive = require('./ToPrimitive');
|
---|
| 14 |
|
---|
| 15 | // https://262.ecma-international.org/5.1/#sec-11.8.5
|
---|
| 16 |
|
---|
| 17 | // eslint-disable-next-line max-statements
|
---|
| 18 | module.exports = function AbstractRelationalComparison(x, y, LeftFirst) {
|
---|
| 19 | if (typeof LeftFirst !== 'boolean') {
|
---|
| 20 | throw new $TypeError('Assertion failed: LeftFirst argument must be a Boolean');
|
---|
| 21 | }
|
---|
| 22 | var px;
|
---|
| 23 | var py;
|
---|
| 24 | if (LeftFirst) {
|
---|
| 25 | px = ToPrimitive(x, $Number);
|
---|
| 26 | py = ToPrimitive(y, $Number);
|
---|
| 27 | } else {
|
---|
| 28 | py = ToPrimitive(y, $Number);
|
---|
| 29 | px = ToPrimitive(x, $Number);
|
---|
| 30 | }
|
---|
| 31 | var bothStrings = typeof px === 'string' && typeof py === 'string';
|
---|
| 32 | if (!bothStrings) {
|
---|
| 33 | var nx = ToNumber(px);
|
---|
| 34 | var ny = ToNumber(py);
|
---|
| 35 | if ($isNaN(nx) || $isNaN(ny)) {
|
---|
| 36 | return undefined;
|
---|
| 37 | }
|
---|
| 38 | if ($isFinite(nx) && $isFinite(ny) && nx === ny) {
|
---|
| 39 | return false;
|
---|
| 40 | }
|
---|
| 41 | if (nx === Infinity) {
|
---|
| 42 | return false;
|
---|
| 43 | }
|
---|
| 44 | if (ny === Infinity) {
|
---|
| 45 | return true;
|
---|
| 46 | }
|
---|
| 47 | if (ny === -Infinity) {
|
---|
| 48 | return false;
|
---|
| 49 | }
|
---|
| 50 | if (nx === -Infinity) {
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 | return nx < ny; // by now, these are both nonzero, finite, and not equal
|
---|
| 54 | }
|
---|
| 55 | if (isPrefixOf(py, px)) {
|
---|
| 56 | return false;
|
---|
| 57 | }
|
---|
| 58 | if (isPrefixOf(px, py)) {
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 | return px < py; // both strings, neither a prefix of the other. shortcut for steps c-f
|
---|
| 62 | };
|
---|