index.js 869 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict'
  2. module.exports = isElement
  3. // Check if if `node` is an `element` and, if `tagNames` is given, `node`
  4. // matches them `tagNames`.
  5. function isElement(node, tagNames) {
  6. var name
  7. if (
  8. !(
  9. tagNames === null ||
  10. tagNames === undefined ||
  11. typeof tagNames === 'string' ||
  12. (typeof tagNames === 'object' && tagNames.length !== 0)
  13. )
  14. ) {
  15. throw new Error(
  16. 'Expected `string` or `Array.<string>` for `tagNames`, not `' +
  17. tagNames +
  18. '`'
  19. )
  20. }
  21. if (
  22. !node ||
  23. typeof node !== 'object' ||
  24. node.type !== 'element' ||
  25. typeof node.tagName !== 'string'
  26. ) {
  27. return false
  28. }
  29. if (tagNames === null || tagNames === undefined) {
  30. return true
  31. }
  32. name = node.tagName
  33. if (typeof tagNames === 'string') {
  34. return name === tagNames
  35. }
  36. return tagNames.indexOf(name) !== -1
  37. }