source: trip-planner-front/node_modules/core-js/modules/es.array.sort.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var aFunction = require('../internals/a-function');
4var toObject = require('../internals/to-object');
5var toLength = require('../internals/to-length');
6var toString = require('../internals/to-string');
7var fails = require('../internals/fails');
8var internalSort = require('../internals/array-sort');
9var arrayMethodIsStrict = require('../internals/array-method-is-strict');
10var FF = require('../internals/engine-ff-version');
11var IE_OR_EDGE = require('../internals/engine-is-ie-or-edge');
12var V8 = require('../internals/engine-v8-version');
13var WEBKIT = require('../internals/engine-webkit-version');
14
15var test = [];
16var nativeSort = test.sort;
17
18// IE8-
19var FAILS_ON_UNDEFINED = fails(function () {
20 test.sort(undefined);
21});
22// V8 bug
23var FAILS_ON_NULL = fails(function () {
24 test.sort(null);
25});
26// Old WebKit
27var STRICT_METHOD = arrayMethodIsStrict('sort');
28
29var STABLE_SORT = !fails(function () {
30 // feature detection can be too slow, so check engines versions
31 if (V8) return V8 < 70;
32 if (FF && FF > 3) return;
33 if (IE_OR_EDGE) return true;
34 if (WEBKIT) return WEBKIT < 603;
35
36 var result = '';
37 var code, chr, value, index;
38
39 // generate an array with more 512 elements (Chakra and old V8 fails only in this case)
40 for (code = 65; code < 76; code++) {
41 chr = String.fromCharCode(code);
42
43 switch (code) {
44 case 66: case 69: case 70: case 72: value = 3; break;
45 case 68: case 71: value = 4; break;
46 default: value = 2;
47 }
48
49 for (index = 0; index < 47; index++) {
50 test.push({ k: chr + index, v: value });
51 }
52 }
53
54 test.sort(function (a, b) { return b.v - a.v; });
55
56 for (index = 0; index < test.length; index++) {
57 chr = test[index].k.charAt(0);
58 if (result.charAt(result.length - 1) !== chr) result += chr;
59 }
60
61 return result !== 'DGBEFHACIJK';
62});
63
64var FORCED = FAILS_ON_UNDEFINED || !FAILS_ON_NULL || !STRICT_METHOD || !STABLE_SORT;
65
66var getSortCompare = function (comparefn) {
67 return function (x, y) {
68 if (y === undefined) return -1;
69 if (x === undefined) return 1;
70 if (comparefn !== undefined) return +comparefn(x, y) || 0;
71 return toString(x) > toString(y) ? 1 : -1;
72 };
73};
74
75// `Array.prototype.sort` method
76// https://tc39.es/ecma262/#sec-array.prototype.sort
77$({ target: 'Array', proto: true, forced: FORCED }, {
78 sort: function sort(comparefn) {
79 if (comparefn !== undefined) aFunction(comparefn);
80
81 var array = toObject(this);
82
83 if (STABLE_SORT) return comparefn === undefined ? nativeSort.call(array) : nativeSort.call(array, comparefn);
84
85 var items = [];
86 var arrayLength = toLength(array.length);
87 var itemsLength, index;
88
89 for (index = 0; index < arrayLength; index++) {
90 if (index in array) items.push(array[index]);
91 }
92
93 items = internalSort(items, getSortCompare(comparefn));
94 itemsLength = items.length;
95 index = 0;
96
97 while (index < itemsLength) array[index] = items[index++];
98 while (index < arrayLength) delete array[index++];
99
100 return array;
101 }
102});
Note: See TracBrowser for help on using the repository browser.