es.array.flat.js 865 B

1234567891011121314151617181920
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var flattenIntoArray = require('../internals/flatten-into-array');
  4. var toObject = require('../internals/to-object');
  5. var lengthOfArrayLike = require('../internals/length-of-array-like');
  6. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  7. var arraySpeciesCreate = require('../internals/array-species-create');
  8. // `Array.prototype.flat` method
  9. // https://tc39.es/ecma262/#sec-array.prototype.flat
  10. $({ target: 'Array', proto: true }, {
  11. flat: function flat(/* depthArg = 1 */) {
  12. var depthArg = arguments.length ? arguments[0] : undefined;
  13. var O = toObject(this);
  14. var sourceLen = lengthOfArrayLike(O);
  15. var A = arraySpeciesCreate(O, 0);
  16. A.length = flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toIntegerOrInfinity(depthArg));
  17. return A;
  18. }
  19. });