source: imaps-frontend/node_modules/es-abstract/2023/CompareArrayElements.js@ d565449

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var Call = require('./Call');
6var IsLessThan = require('./IsLessThan');
7var ToNumber = require('./ToNumber');
8var ToString = require('./ToString');
9
10var isNaN = require('../helpers/isNaN');
11
12// https://262.ecma-international.org/14.0/#sec-comparearrayelements
13
14module.exports = function CompareArrayElements(x, y, compareFn) {
15 if (typeof compareFn !== 'function' && typeof compareFn !== 'undefined') {
16 throw new $TypeError('Assertion failed: `compareFn` must be a function or undefined');
17 }
18
19 if (typeof x === 'undefined' && typeof y === 'undefined') {
20 return 0; // step 1
21 }
22
23 if (typeof x === 'undefined') {
24 return 1; // step 2
25 }
26
27 if (typeof y === 'undefined') {
28 return -1; // step 3
29 }
30
31 if (typeof compareFn !== 'undefined') { // step 4
32 var v = ToNumber(Call(compareFn, void undefined, [x, y])); // step 4.a
33 if (isNaN(v)) {
34 return 0; // step 4.b
35 }
36 return v; // step 4.c
37 }
38
39 var xString = ToString(x); // step 5
40 var yString = ToString(y); // step 6
41 var xSmaller = IsLessThan(xString, yString, true); // step 7
42 if (xSmaller) {
43 return -1; // step 8
44 }
45 var ySmaller = IsLessThan(yString, xString, true); // step 9
46 if (ySmaller) {
47 return 1; // step 10
48 }
49 return 0; // step 11
50};
Note: See TracBrowser for help on using the repository browser.