es.array.splice.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var toObject = require('../internals/to-object');
  4. var toAbsoluteIndex = require('../internals/to-absolute-index');
  5. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  6. var lengthOfArrayLike = require('../internals/length-of-array-like');
  7. var setArrayLength = require('../internals/array-set-length');
  8. var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
  9. var arraySpeciesCreate = require('../internals/array-species-create');
  10. var createProperty = require('../internals/create-property');
  11. var deletePropertyOrThrow = require('../internals/delete-property-or-throw');
  12. var arrayMethodHasSpeciesSupport = require('../internals/array-method-has-species-support');
  13. var HAS_SPECIES_SUPPORT = arrayMethodHasSpeciesSupport('splice');
  14. var max = Math.max;
  15. var min = Math.min;
  16. // `Array.prototype.splice` method
  17. // https://tc39.es/ecma262/#sec-array.prototype.splice
  18. // with adding support of @@species
  19. $({ target: 'Array', proto: true, forced: !HAS_SPECIES_SUPPORT }, {
  20. splice: function splice(start, deleteCount /* , ...items */) {
  21. var O = toObject(this);
  22. var len = lengthOfArrayLike(O);
  23. var actualStart = toAbsoluteIndex(start, len);
  24. var argumentsLength = arguments.length;
  25. var insertCount, actualDeleteCount, A, k, from, to;
  26. if (argumentsLength === 0) {
  27. insertCount = actualDeleteCount = 0;
  28. } else if (argumentsLength === 1) {
  29. insertCount = 0;
  30. actualDeleteCount = len - actualStart;
  31. } else {
  32. insertCount = argumentsLength - 2;
  33. actualDeleteCount = min(max(toIntegerOrInfinity(deleteCount), 0), len - actualStart);
  34. }
  35. doesNotExceedSafeInteger(len + insertCount - actualDeleteCount);
  36. A = arraySpeciesCreate(O, actualDeleteCount);
  37. for (k = 0; k < actualDeleteCount; k++) {
  38. from = actualStart + k;
  39. if (from in O) createProperty(A, k, O[from]);
  40. }
  41. A.length = actualDeleteCount;
  42. if (insertCount < actualDeleteCount) {
  43. for (k = actualStart; k < len - actualDeleteCount; k++) {
  44. from = k + actualDeleteCount;
  45. to = k + insertCount;
  46. if (from in O) O[to] = O[from];
  47. else deletePropertyOrThrow(O, to);
  48. }
  49. for (k = len; k > len - actualDeleteCount + insertCount; k--) deletePropertyOrThrow(O, k - 1);
  50. } else if (insertCount > actualDeleteCount) {
  51. for (k = len - actualDeleteCount; k > actualStart; k--) {
  52. from = k + actualDeleteCount - 1;
  53. to = k + insertCount - 1;
  54. if (from in O) O[to] = O[from];
  55. else deletePropertyOrThrow(O, to);
  56. }
  57. }
  58. for (k = 0; k < insertCount; k++) {
  59. O[k + actualStart] = arguments[k + 2];
  60. }
  61. setArrayLength(O, len - actualDeleteCount + insertCount);
  62. return A;
  63. }
  64. });