_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. } // The iterator protocol depends on `Symbol.iterator`. If a collection is
  20. // implemented, but `Symbol` is not, it's going to break iteration because
  21. // we'll be using custom "@@iterator" instead, which is not implemented on
  22. // native collections.
  23. if (typeof global.Symbol !== 'function') {
  24. return true;
  25. }
  26. var proto = Collection.prototype; // These checks are adapted from es6-shim: https://fburl.com/34437854
  27. // NOTE: `isCallableWithoutNew` and `!supportsSubclassing` are not checked
  28. // because they make debugging with "break on exceptions" difficult.
  29. return Collection == null || typeof Collection !== 'function' || typeof proto.clear !== 'function' || new Collection().size !== 0 || typeof proto.keys !== 'function' || typeof proto.forEach !== 'function';
  30. }
  31. module.exports = shouldPolyfillES6Collection;