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