is-element.js 685 B

123456789101112131415161718192021222324252627
  1. "use strict";
  2. var div = typeof document !== "undefined" && document.createElement("div");
  3. /**
  4. * @name samsam.isElement
  5. * @param Object object
  6. *
  7. * Returns ``true`` if ``object`` is a DOM element node. Unlike
  8. * Underscore.js/lodash, this function will return ``false`` if ``object``
  9. * is an *element-like* object, i.e. a regular object with a ``nodeType``
  10. * property that holds the value ``1``.
  11. */
  12. function isElement(object) {
  13. if (!object || object.nodeType !== 1 || !div) {
  14. return false;
  15. }
  16. try {
  17. object.appendChild(div);
  18. object.removeChild(div);
  19. } catch (e) {
  20. return false;
  21. }
  22. return true;
  23. }
  24. module.exports = isElement;