1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $Number = GetIntrinsic('%Number%');
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 | var min = GetIntrinsic('%Math.min%');
|
---|
8 |
|
---|
9 | var $isNaN = require('../helpers/isNaN');
|
---|
10 | var $charCodeAt = require('call-bind/callBound')('String.prototype.charCodeAt');
|
---|
11 |
|
---|
12 | var StringToBigInt = require('./StringToBigInt');
|
---|
13 | var ToNumeric = require('./ToNumeric');
|
---|
14 | var ToPrimitive = require('./ToPrimitive');
|
---|
15 |
|
---|
16 | var BigIntLessThan = require('./BigInt/lessThan');
|
---|
17 | var NumberLessThan = require('./Number/lessThan');
|
---|
18 |
|
---|
19 | // https://262.ecma-international.org/14.0/#sec-islessthan
|
---|
20 |
|
---|
21 | // eslint-disable-next-line max-statements, max-lines-per-function
|
---|
22 | module.exports = function IsLessThan(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 |
|
---|
36 | if (typeof px === 'string' && typeof py === 'string') { // step 3
|
---|
37 | // a. Let lx be the length of px.
|
---|
38 | // b. Let ly be the length of py.
|
---|
39 | // c. For each integer i starting with 0 such that i < min(lx, ly), in ascending order, do
|
---|
40 | // i. Let cx be the integer that is the numeric value of the code unit at index i within px.
|
---|
41 | // ii. Let cy be the integer that is the numeric value of the code unit at index i within py.
|
---|
42 | // iii. If cx < cy, return true.
|
---|
43 | // iv. If cx > cy, return false.
|
---|
44 | // d. If lx < ly, return true. Otherwise, return false.
|
---|
45 |
|
---|
46 | var lx = px.length; // step 3.a
|
---|
47 | var ly = py.length; // step 3.b
|
---|
48 | for (var i = 0; i < min(lx, ly); i++) { // step 3.c
|
---|
49 | var cx = $charCodeAt(px, i); // step 3.c.i
|
---|
50 | var cy = $charCodeAt(py, i); // step 3.c.ii
|
---|
51 | if (cx < cy) {
|
---|
52 | return true; // step 3.c.iii
|
---|
53 | }
|
---|
54 | if (cx > cy) {
|
---|
55 | return false; // step 3.c.iv
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return lx < ly; // step 3.d
|
---|
59 | }
|
---|
60 |
|
---|
61 | var nx;
|
---|
62 | var ny;
|
---|
63 | if (typeof px === 'bigint' && typeof py === 'string') {
|
---|
64 | ny = StringToBigInt(py);
|
---|
65 | if (typeof ny === 'undefined') {
|
---|
66 | return void undefined;
|
---|
67 | }
|
---|
68 | return BigIntLessThan(px, ny);
|
---|
69 | }
|
---|
70 | if (typeof px === 'string' && typeof py === 'bigint') {
|
---|
71 | nx = StringToBigInt(px);
|
---|
72 | if (typeof nx === 'undefined') {
|
---|
73 | return void undefined;
|
---|
74 | }
|
---|
75 | return BigIntLessThan(nx, py);
|
---|
76 | }
|
---|
77 |
|
---|
78 | nx = ToNumeric(px);
|
---|
79 | ny = ToNumeric(py);
|
---|
80 |
|
---|
81 | if (typeof nx === typeof ny) {
|
---|
82 | return typeof nx === 'number' ? NumberLessThan(nx, ny) : BigIntLessThan(nx, ny);
|
---|
83 | }
|
---|
84 |
|
---|
85 | if ($isNaN(nx) || $isNaN(ny)) {
|
---|
86 | return void undefined;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (nx === -Infinity || ny === Infinity) {
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 | if (nx === Infinity || ny === -Infinity) {
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | return nx < ny; // by now, these are both finite, and the same type
|
---|
97 | };
|
---|