_shouldPolyfillES6Collection.js.flow 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule _shouldPolyfillES6Collection
  8. * @preventMunge
  9. * @flow
  10. */
  11. /**
  12. * Checks whether a collection name (e.g. "Map" or "Set") has a native polyfill
  13. * that is safe to be used.
  14. */
  15. function shouldPolyfillES6Collection(collectionName: string): boolean {
  16. const Collection = global[collectionName];
  17. if (Collection == null) {
  18. return true;
  19. }
  20. // The iterator protocol depends on `Symbol.iterator`. If a collection is
  21. // implemented, but `Symbol` is not, it's going to break iteration because
  22. // we'll be using custom "@@iterator" instead, which is not implemented on
  23. // native collections.
  24. if (typeof global.Symbol !== 'function') {
  25. return true;
  26. }
  27. const proto = Collection.prototype;
  28. // These checks are adapted from es6-shim: https://fburl.com/34437854
  29. // NOTE: `isCallableWithoutNew` and `!supportsSubclassing` are not checked
  30. // because they make debugging with "break on exceptions" difficult.
  31. return Collection == null || typeof Collection !== 'function' || typeof proto.clear !== 'function' || new Collection().size !== 0 || typeof proto.keys !== 'function' || typeof proto.forEach !== 'function';
  32. }
  33. module.exports = shouldPolyfillES6Collection;