es.array.flat.js 802 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 toLength = require('../internals/to-length');
  6. var toInteger = require('../internals/to-integer');
  7. var arraySpeciesCreate = require('../internals/array-species-create');
  8. // `Array.prototype.flat` method
  9. // https://github.com/tc39/proposal-flatMap
  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 = toLength(O.length);
  15. var A = arraySpeciesCreate(O, 0);
  16. A.length = flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
  17. return A;
  18. }
  19. });