You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

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