_methodFingerprint.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. define(['exports', './_getLength', './isFunction', './allKeys'], function (exports, _getLength, isFunction, allKeys) {
  2. // Since the regular `Object.prototype.toString` type tests don't work for
  3. // some types in IE 11, we use a fingerprinting heuristic instead, based
  4. // on the methods. It's not great, but it's the best we got.
  5. // The fingerprint method lists are defined below.
  6. function ie11fingerprint(methods) {
  7. var length = _getLength(methods);
  8. return function(obj) {
  9. if (obj == null) return false;
  10. // `Map`, `WeakMap` and `Set` have no enumerable keys.
  11. var keys = allKeys(obj);
  12. if (_getLength(keys)) return false;
  13. for (var i = 0; i < length; i++) {
  14. if (!isFunction(obj[methods[i]])) return false;
  15. }
  16. // If we are testing against `WeakMap`, we need to ensure that
  17. // `obj` doesn't have a `forEach` method in order to distinguish
  18. // it from a regular `Map`.
  19. return methods !== weakMapMethods || !isFunction(obj[forEachName]);
  20. };
  21. }
  22. // In the interest of compact minification, we write
  23. // each string in the fingerprints only once.
  24. var forEachName = 'forEach',
  25. hasName = 'has',
  26. commonInit = ['clear', 'delete'],
  27. mapTail = ['get', hasName, 'set'];
  28. // `Map`, `WeakMap` and `Set` each have slightly different
  29. // combinations of the above sublists.
  30. var mapMethods = commonInit.concat(forEachName, mapTail),
  31. weakMapMethods = commonInit.concat(mapTail),
  32. setMethods = ['add'].concat(commonInit, forEachName, hasName);
  33. exports.ie11fingerprint = ie11fingerprint;
  34. exports.mapMethods = mapMethods;
  35. exports.setMethods = setMethods;
  36. exports.weakMapMethods = weakMapMethods;
  37. Object.defineProperty(exports, '__esModule', { value: true });
  38. });