arraySlice.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Check from "./Check.js";
  2. import defined from "./defined.js";
  3. import FeatureDetection from "./FeatureDetection.js";
  4. /**
  5. * Create a shallow copy of an array from begin to end.
  6. *
  7. * @param {Array} array The array to fill.
  8. * @param {Number} [begin=0] The index to start at.
  9. * @param {Number} [end=array.length] The index to end at which is not included.
  10. *
  11. * @returns {Array} The resulting array.
  12. * @private
  13. */
  14. function arraySlice(array, begin, end) {
  15. //>>includeStart('debug', pragmas.debug);
  16. Check.defined("array", array);
  17. if (defined(begin)) {
  18. Check.typeOf.number("begin", begin);
  19. }
  20. if (defined(end)) {
  21. Check.typeOf.number("end", end);
  22. }
  23. //>>includeEnd('debug');
  24. if (typeof array.slice === "function") {
  25. return array.slice(begin, end);
  26. }
  27. let copy = Array.prototype.slice.call(array, begin, end);
  28. const typedArrayTypes = FeatureDetection.typedArrayTypes;
  29. const length = typedArrayTypes.length;
  30. for (let i = 0; i < length; ++i) {
  31. if (array instanceof typedArrayTypes[i]) {
  32. copy = new typedArrayTypes[i](copy);
  33. break;
  34. }
  35. }
  36. return copy;
  37. }
  38. export default arraySlice;