shim.js 698 B

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