source: frontend/node_modules/es-abstract/2020/AbstractRelationalComparison.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.0 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $Number = GetIntrinsic('%Number%');
6var $TypeError = require('es-errors/type');
7
8var $isNaN = require('math-intrinsics/isNaN');
9
10var IsStringPrefix = require('./IsStringPrefix');
11var StringToBigInt = require('./StringToBigInt');
12var ToNumeric = require('./ToNumeric');
13var ToPrimitive = require('./ToPrimitive');
14
15var BigIntLessThan = require('./BigInt/lessThan');
16var NumberLessThan = require('./Number/lessThan');
17
18var 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
23module.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};
Note: See TracBrowser for help on using the repository browser.