[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 |
|
---|
| 10 | var IsStringPrefix = require('./IsStringPrefix');
|
---|
| 11 | var StringToBigInt = require('./StringToBigInt');
|
---|
| 12 | var ToNumeric = require('./ToNumeric');
|
---|
| 13 | var ToPrimitive = require('./ToPrimitive');
|
---|
| 14 |
|
---|
| 15 | var BigIntLessThan = require('./BigInt/lessThan');
|
---|
| 16 | var NumberLessThan = require('./Number/lessThan');
|
---|
| 17 |
|
---|
| 18 | // https://262.ecma-international.org/13.0/#sec-islessthan
|
---|
| 19 |
|
---|
| 20 | // eslint-disable-next-line max-statements, max-lines-per-function
|
---|
| 21 | module.exports = function IsLessThan(x, y, LeftFirst) {
|
---|
| 22 | if (typeof LeftFirst !== 'boolean') {
|
---|
| 23 | throw new $TypeError('Assertion failed: LeftFirst argument must be a Boolean');
|
---|
| 24 | }
|
---|
| 25 | var px;
|
---|
| 26 | var py;
|
---|
| 27 | if (LeftFirst) {
|
---|
| 28 | px = ToPrimitive(x, $Number);
|
---|
| 29 | py = ToPrimitive(y, $Number);
|
---|
| 30 | } else {
|
---|
| 31 | py = ToPrimitive(y, $Number);
|
---|
| 32 | px = ToPrimitive(x, $Number);
|
---|
| 33 | }
|
---|
| 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 | /*
|
---|
| 43 | c. Let k be the smallest non-negative integer such that the code unit at index k within px is different from the code unit at index k within py. (There must be such a k, for neither String is a prefix of the other.)
|
---|
| 44 | d. Let m be the integer that is the numeric value of the code unit at index k within px.
|
---|
| 45 | e. Let n be the integer that is the numeric value of the code unit at index k within py.
|
---|
| 46 | f. If m < n, return true. Otherwise, return false.
|
---|
| 47 | */
|
---|
| 48 | return px < py; // both strings, neither a prefix of the other. shortcut for steps 3 c-f
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | var nx;
|
---|
| 52 | var ny;
|
---|
| 53 | if (typeof px === 'bigint' && typeof py === 'string') {
|
---|
| 54 | ny = StringToBigInt(py);
|
---|
| 55 | if (typeof ny === 'undefined') {
|
---|
| 56 | return void undefined;
|
---|
| 57 | }
|
---|
| 58 | return BigIntLessThan(px, ny);
|
---|
| 59 | }
|
---|
| 60 | if (typeof px === 'string' && typeof py === 'bigint') {
|
---|
| 61 | nx = StringToBigInt(px);
|
---|
| 62 | if (typeof nx === 'undefined') {
|
---|
| 63 | return void undefined;
|
---|
| 64 | }
|
---|
| 65 | return BigIntLessThan(nx, py);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | nx = ToNumeric(px);
|
---|
| 69 | ny = ToNumeric(py);
|
---|
| 70 |
|
---|
| 71 | if (typeof nx === typeof ny) {
|
---|
| 72 | return typeof nx === 'number' ? NumberLessThan(nx, ny) : BigIntLessThan(nx, ny);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if ($isNaN(nx) || $isNaN(ny)) {
|
---|
| 76 | return void undefined;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (nx === -Infinity || ny === Infinity) {
|
---|
| 80 | return true;
|
---|
| 81 | }
|
---|
| 82 | if (nx === Infinity || ny === -Infinity) {
|
---|
| 83 | return false;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | return nx < ny; // by now, these are both finite, and the same type
|
---|
| 87 | };
|
---|