implementation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. var keysShim;
  3. if (!Object.keys) {
  4. // modified from https://github.com/es-shims/es5-shim
  5. var has = Object.prototype.hasOwnProperty;
  6. var toStr = Object.prototype.toString;
  7. var isArgs = require('./isArguments'); // eslint-disable-line global-require
  8. var isEnumerable = Object.prototype.propertyIsEnumerable;
  9. var hasDontEnumBug = !isEnumerable.call({ toString: null }, 'toString');
  10. var hasProtoEnumBug = isEnumerable.call(function () {}, 'prototype');
  11. var dontEnums = [
  12. 'toString',
  13. 'toLocaleString',
  14. 'valueOf',
  15. 'hasOwnProperty',
  16. 'isPrototypeOf',
  17. 'propertyIsEnumerable',
  18. 'constructor'
  19. ];
  20. var equalsConstructorPrototype = function (o) {
  21. var ctor = o.constructor;
  22. return ctor && ctor.prototype === o;
  23. };
  24. var excludedKeys = {
  25. $applicationCache: true,
  26. $console: true,
  27. $external: true,
  28. $frame: true,
  29. $frameElement: true,
  30. $frames: true,
  31. $innerHeight: true,
  32. $innerWidth: true,
  33. $outerHeight: true,
  34. $outerWidth: true,
  35. $pageXOffset: true,
  36. $pageYOffset: true,
  37. $parent: true,
  38. $scrollLeft: true,
  39. $scrollTop: true,
  40. $scrollX: true,
  41. $scrollY: true,
  42. $self: true,
  43. $webkitIndexedDB: true,
  44. $webkitStorageInfo: true,
  45. $window: true
  46. };
  47. var hasAutomationEqualityBug = (function () {
  48. /* global window */
  49. if (typeof window === 'undefined') { return false; }
  50. for (var k in window) {
  51. try {
  52. if (!excludedKeys['$' + k] && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
  53. try {
  54. equalsConstructorPrototype(window[k]);
  55. } catch (e) {
  56. return true;
  57. }
  58. }
  59. } catch (e) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }());
  65. var equalsConstructorPrototypeIfNotBuggy = function (o) {
  66. /* global window */
  67. if (typeof window === 'undefined' || !hasAutomationEqualityBug) {
  68. return equalsConstructorPrototype(o);
  69. }
  70. try {
  71. return equalsConstructorPrototype(o);
  72. } catch (e) {
  73. return false;
  74. }
  75. };
  76. keysShim = function keys(object) {
  77. var isObject = object !== null && typeof object === 'object';
  78. var isFunction = toStr.call(object) === '[object Function]';
  79. var isArguments = isArgs(object);
  80. var isString = isObject && toStr.call(object) === '[object String]';
  81. var theKeys = [];
  82. if (!isObject && !isFunction && !isArguments) {
  83. throw new TypeError('Object.keys called on a non-object');
  84. }
  85. var skipProto = hasProtoEnumBug && isFunction;
  86. if (isString && object.length > 0 && !has.call(object, 0)) {
  87. for (var i = 0; i < object.length; ++i) {
  88. theKeys.push(String(i));
  89. }
  90. }
  91. if (isArguments && object.length > 0) {
  92. for (var j = 0; j < object.length; ++j) {
  93. theKeys.push(String(j));
  94. }
  95. } else {
  96. for (var name in object) {
  97. if (!(skipProto && name === 'prototype') && has.call(object, name)) {
  98. theKeys.push(String(name));
  99. }
  100. }
  101. }
  102. if (hasDontEnumBug) {
  103. var skipConstructor = equalsConstructorPrototypeIfNotBuggy(object);
  104. for (var k = 0; k < dontEnums.length; ++k) {
  105. if (!(skipConstructor && dontEnums[k] === 'constructor') && has.call(object, dontEnums[k])) {
  106. theKeys.push(dontEnums[k]);
  107. }
  108. }
  109. }
  110. return theKeys;
  111. };
  112. }
  113. module.exports = keysShim;