utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. // Returns "Type(value) is Object" in ES terminology.
  3. function isObject(value) {
  4. return typeof value === "object" && value !== null || typeof value === "function";
  5. }
  6. function hasOwn(obj, prop) {
  7. return Object.prototype.hasOwnProperty.call(obj, prop);
  8. }
  9. const getOwnPropertyDescriptors = typeof Object.getOwnPropertyDescriptors === "function" ?
  10. Object.getOwnPropertyDescriptors :
  11. // Polyfill exists until we require Node.js v8.x
  12. // https://tc39.github.io/ecma262/#sec-object.getownpropertydescriptors
  13. obj => {
  14. if (obj === undefined || obj === null) {
  15. throw new TypeError("Cannot convert undefined or null to object");
  16. }
  17. obj = Object(obj);
  18. const ownKeys = Reflect.ownKeys(obj);
  19. const descriptors = {};
  20. for (const key of ownKeys) {
  21. const descriptor = Reflect.getOwnPropertyDescriptor(obj, key);
  22. if (descriptor !== undefined) {
  23. Reflect.defineProperty(descriptors, key, {
  24. value: descriptor,
  25. writable: true,
  26. enumerable: true,
  27. configurable: true
  28. });
  29. }
  30. }
  31. return descriptors;
  32. };
  33. const wrapperSymbol = Symbol("wrapper");
  34. const implSymbol = Symbol("impl");
  35. const sameObjectCaches = Symbol("SameObject caches");
  36. function getSameObject(wrapper, prop, creator) {
  37. if (!wrapper[sameObjectCaches]) {
  38. wrapper[sameObjectCaches] = Object.create(null);
  39. }
  40. if (prop in wrapper[sameObjectCaches]) {
  41. return wrapper[sameObjectCaches][prop];
  42. }
  43. wrapper[sameObjectCaches][prop] = creator();
  44. return wrapper[sameObjectCaches][prop];
  45. }
  46. function wrapperForImpl(impl) {
  47. return impl ? impl[wrapperSymbol] : null;
  48. }
  49. function implForWrapper(wrapper) {
  50. return wrapper ? wrapper[implSymbol] : null;
  51. }
  52. function tryWrapperForImpl(impl) {
  53. const wrapper = wrapperForImpl(impl);
  54. return wrapper ? wrapper : impl;
  55. }
  56. function tryImplForWrapper(wrapper) {
  57. const impl = implForWrapper(wrapper);
  58. return impl ? impl : wrapper;
  59. }
  60. const iterInternalSymbol = Symbol("internal");
  61. const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
  62. function isArrayIndexPropName(P) {
  63. if (typeof P !== "string") {
  64. return false;
  65. }
  66. const i = P >>> 0;
  67. if (i === Math.pow(2, 32) - 1) {
  68. return false;
  69. }
  70. const s = `${i}`;
  71. if (P !== s) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. const supportsPropertyIndex = Symbol("supports property index");
  77. const supportedPropertyIndices = Symbol("supported property indices");
  78. const supportsPropertyName = Symbol("supports property name");
  79. const supportedPropertyNames = Symbol("supported property names");
  80. const indexedGet = Symbol("indexed property get");
  81. const indexedSetNew = Symbol("indexed property set new");
  82. const indexedSetExisting = Symbol("indexed property set existing");
  83. const namedGet = Symbol("named property get");
  84. const namedSetNew = Symbol("named property set new");
  85. const namedSetExisting = Symbol("named property set existing");
  86. const namedDelete = Symbol("named property delete");
  87. module.exports = exports = {
  88. isObject,
  89. hasOwn,
  90. getOwnPropertyDescriptors,
  91. wrapperSymbol,
  92. implSymbol,
  93. getSameObject,
  94. wrapperForImpl,
  95. implForWrapper,
  96. tryWrapperForImpl,
  97. tryImplForWrapper,
  98. iterInternalSymbol,
  99. IteratorPrototype,
  100. isArrayIndexPropName,
  101. supportsPropertyIndex,
  102. supportedPropertyIndices,
  103. supportsPropertyName,
  104. supportedPropertyNames,
  105. indexedGet,
  106. indexedSetNew,
  107. indexedSetExisting,
  108. namedGet,
  109. namedSetNew,
  110. namedSetExisting,
  111. namedDelete
  112. };