a17931219b0c5020d41f1d255222d1eead847dfd27b4922e6a87d056871faa1c2c09c76ad430c9bcc73c0abaf094e246087d6155d568928d8670e67106a636 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var aCallable = require('../internals/a-callable');
  5. var toObject = require('../internals/to-object');
  6. var lengthOfArrayLike = require('../internals/length-of-array-like');
  7. var deletePropertyOrThrow = require('../internals/delete-property-or-throw');
  8. var toString = require('../internals/to-string');
  9. var fails = require('../internals/fails');
  10. var internalSort = require('../internals/array-sort');
  11. var arrayMethodIsStrict = require('../internals/array-method-is-strict');
  12. var FF = require('../internals/engine-ff-version');
  13. var IE_OR_EDGE = require('../internals/engine-is-ie-or-edge');
  14. var V8 = require('../internals/engine-v8-version');
  15. var WEBKIT = require('../internals/engine-webkit-version');
  16. var test = [];
  17. var nativeSort = uncurryThis(test.sort);
  18. var push = uncurryThis(test.push);
  19. // IE8-
  20. var FAILS_ON_UNDEFINED = fails(function () {
  21. test.sort(undefined);
  22. });
  23. // V8 bug
  24. var FAILS_ON_NULL = fails(function () {
  25. test.sort(null);
  26. });
  27. // Old WebKit
  28. var STRICT_METHOD = arrayMethodIsStrict('sort');
  29. var 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. var result = '';
  36. var code, chr, value, index;
  37. // generate an array with more 512 elements (Chakra and old V8 fails only in this case)
  38. for (code = 65; code < 76; code++) {
  39. chr = String.fromCharCode(code);
  40. switch (code) {
  41. case 66: case 69: case 70: case 72: value = 3; break;
  42. case 68: case 71: value = 4; break;
  43. default: value = 2;
  44. }
  45. for (index = 0; index < 47; index++) {
  46. test.push({ k: chr + index, v: value });
  47. }
  48. }
  49. test.sort(function (a, b) { return b.v - a.v; });
  50. for (index = 0; index < test.length; index++) {
  51. chr = test[index].k.charAt(0);
  52. if (result.charAt(result.length - 1) !== chr) result += chr;
  53. }
  54. return result !== 'DGBEFHACIJK';
  55. });
  56. var FORCED = FAILS_ON_UNDEFINED || !FAILS_ON_NULL || !STRICT_METHOD || !STABLE_SORT;
  57. var getSortCompare = function (comparefn) {
  58. return function (x, y) {
  59. if (y === undefined) return -1;
  60. if (x === undefined) return 1;
  61. if (comparefn !== undefined) return +comparefn(x, y) || 0;
  62. return toString(x) > toString(y) ? 1 : -1;
  63. };
  64. };
  65. // `Array.prototype.sort` method
  66. // https://tc39.es/ecma262/#sec-array.prototype.sort
  67. $({ target: 'Array', proto: true, forced: FORCED }, {
  68. sort: function sort(comparefn) {
  69. if (comparefn !== undefined) aCallable(comparefn);
  70. var array = toObject(this);
  71. if (STABLE_SORT) return comparefn === undefined ? nativeSort(array) : nativeSort(array, comparefn);
  72. var items = [];
  73. var arrayLength = lengthOfArrayLike(array);
  74. var itemsLength, index;
  75. for (index = 0; index < arrayLength; index++) {
  76. if (index in array) push(items, array[index]);
  77. }
  78. internalSort(items, getSortCompare(comparefn));
  79. itemsLength = lengthOfArrayLike(items);
  80. index = 0;
  81. while (index < itemsLength) array[index] = items[index++];
  82. while (index < arrayLength) deletePropertyOrThrow(array, index++);
  83. return array;
  84. }
  85. });