es.array.to-spliced.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var addToUnscopables = require('../internals/add-to-unscopables');
  4. var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
  5. var lengthOfArrayLike = require('../internals/length-of-array-like');
  6. var toAbsoluteIndex = require('../internals/to-absolute-index');
  7. var toIndexedObject = require('../internals/to-indexed-object');
  8. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  9. var $Array = Array;
  10. var max = Math.max;
  11. var min = Math.min;
  12. // `Array.prototype.toSpliced` method
  13. // https://tc39.es/ecma262/#sec-array.prototype.tospliced
  14. $({ target: 'Array', proto: true }, {
  15. toSpliced: function toSpliced(start, deleteCount /* , ...items */) {
  16. var O = toIndexedObject(this);
  17. var len = lengthOfArrayLike(O);
  18. var actualStart = toAbsoluteIndex(start, len);
  19. var argumentsLength = arguments.length;
  20. var k = 0;
  21. var insertCount, actualDeleteCount, newLen, A;
  22. if (argumentsLength === 0) {
  23. insertCount = actualDeleteCount = 0;
  24. } else if (argumentsLength === 1) {
  25. insertCount = 0;
  26. actualDeleteCount = len - actualStart;
  27. } else {
  28. insertCount = argumentsLength - 2;
  29. actualDeleteCount = min(max(toIntegerOrInfinity(deleteCount), 0), len - actualStart);
  30. }
  31. newLen = doesNotExceedSafeInteger(len + insertCount - actualDeleteCount);
  32. A = $Array(newLen);
  33. for (; k < actualStart; k++) A[k] = O[k];
  34. for (; k < actualStart + insertCount; k++) A[k] = arguments[k - actualStart + 2];
  35. for (; k < newLen; k++) A[k] = O[k + actualDeleteCount - insertCount];
  36. return A;
  37. }
  38. });
  39. addToUnscopables('toSpliced');