_shouldPolyfillES6Collection.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. * @preventMunge
  9. *
  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) {
  16. var 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. var 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;