shim.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function () {
  2. "use strict";
  3. // modified from https://github.com/kriskowal/es5-shim
  4. var has = Object.prototype.hasOwnProperty,
  5. toString = Object.prototype.toString,
  6. forEach = require('./foreach'),
  7. isArgs = require('./isArguments'),
  8. hasDontEnumBug = !({'toString': null}).propertyIsEnumerable('toString'),
  9. hasProtoEnumBug = (function () {}).propertyIsEnumerable('prototype'),
  10. dontEnums = [
  11. "toString",
  12. "toLocaleString",
  13. "valueOf",
  14. "hasOwnProperty",
  15. "isPrototypeOf",
  16. "propertyIsEnumerable",
  17. "constructor"
  18. ],
  19. keysShim;
  20. keysShim = function keys(object) {
  21. var isObject = object !== null && typeof object === 'object',
  22. isFunction = toString.call(object) === '[object Function]',
  23. isArguments = isArgs(object),
  24. theKeys = [];
  25. if (!isObject && !isFunction && !isArguments) {
  26. throw new TypeError("Object.keys called on a non-object");
  27. }
  28. if (isArguments) {
  29. forEach(object, function (value) {
  30. theKeys.push(value);
  31. });
  32. } else {
  33. var name,
  34. skipProto = hasProtoEnumBug && isFunction;
  35. for (name in object) {
  36. if (!(skipProto && name === 'prototype') && has.call(object, name)) {
  37. theKeys.push(name);
  38. }
  39. }
  40. }
  41. if (hasDontEnumBug) {
  42. var ctor = object.constructor,
  43. skipConstructor = ctor && ctor.prototype === object;
  44. forEach(dontEnums, function (dontEnum) {
  45. if (!(skipConstructor && dontEnum === 'constructor') && has.call(object, dontEnum)) {
  46. theKeys.push(dontEnum);
  47. }
  48. });
  49. }
  50. return theKeys;
  51. };
  52. module.exports = keysShim;
  53. }());