Skip to content

Helpful methods for HTMLCollection #194

Closed
@japgolly

Description

@japgolly

Would you be interested in adding these useful functions the library?
They make element.children act a bit more Scala-idiomatically.

@inline implicit class PatchHTMLCollection(private val c: raw.HTMLCollection) extends AnyVal {
  def nonEmpty: Boolean =
    c.length > 0

  def isEmpty: Boolean =
    !nonEmpty

  def headOption: Option[Element] =
    if (nonEmpty) Some(head) else None

  def lastOption: Option[Element] =
    if (nonEmpty) Some(last) else None

  def head: Element =
    c(0)

  def last: Element =
    c(c.length - 1)

  def iterator: Iterator[Element] =
    (0 until c.length).iterator.map(c.apply)

  def deepIteratorDepthFirst: Iterator[Element] =
    iterator.flatMap(e => Iterator.single(e) ++ e.children.deepIteratorDepthFirst)

  def deepIteratorBreadthFirst: Iterator[Element] =
    iterator ++ iterator.flatMap(_.children.deepIteratorBreadthFirst)
}

If interested, please tell me where you'd like me to put them and how.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions