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

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.1 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('../helpers/isNaN');
9
10var IsStringPrefix = require('./IsStringPrefix');
11var StringToBigInt = require('./StringToBigInt');
12var ToNumeric = require('./ToNumeric');
13var ToPrimitive = require('./ToPrimitive');
14var Type = require('./Type');
15
16var BigIntLessThan = require('./BigInt/lessThan');
17var 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
22module.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};
Note: See TracBrowser for help on using the repository browser.