shim.js 668 B

12345678910111213141516171819202122
  1. 'use strict';
  2. var isPlainArray = require('../../is-plain-array')
  3. , callable = require('../../../object/valid-callable')
  4. , isArray = Array.isArray, filter = Array.prototype.filter
  5. , forEach = Array.prototype.forEach, call = Function.prototype.call;
  6. module.exports = function (callbackFn/*, thisArg*/) {
  7. var result, thisArg, i;
  8. if (!this || !isArray(this) || isPlainArray(this)) {
  9. return filter.apply(this, arguments);
  10. }
  11. callable(callbackFn);
  12. thisArg = arguments[1];
  13. result = new this.constructor();
  14. i = 0;
  15. forEach.call(this, function (val, j, self) {
  16. if (call.call(callbackFn, thisArg, val, j, self)) result[i++] = val;
  17. });
  18. return result;
  19. };