es.array.splice.js 2.8 KB

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