Home > database >  loadash isElement equivalent in pure JS
loadash isElement equivalent in pure JS

Time:04-25

Right now I am using isElement by loadash to check if an element is DOM likely. I want to get rid of this library so I am looking for a pure JS implementation of this function.

I'm using this, but I don't know if it's the right implementation and if I'm ignoring any edge cases:

const isDOMElement = el => el instanceof HTMLElement

CodePudding user response:

Look at the implementation from library source code:

https://github.com/lodash/lodash/blob/master/isElement.js

CodePudding user response:

This is the lodash util:

function isElement(value) {
  return isObjectLike(value) && value.nodeType === 1 && !isPlainObject(value)
}

I prefer using your check:

const isDOMElement = el => el instanceof HTMLElement
//or
const isDOMElement = el => el instanceof Node

  • Related