1.0.9 • Published 4 years ago

dom-reducer v1.0.9

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

dom-reducer

CircleCI

Reduce designed for DOM crawling. It can be used for node info collection.

It can behave like Visitor pattern modifying nodes, and if you only need that functionality or breadth-first traversal, then use dom-visitor instead:

DOMReduce takes 3 arguments:

  • startingNode ex.: document.body,

  • reducer, a function called on every node with accumulator, ex.: (accu, node) => accu + (node.tagName === "DIV" ? 1 : 0)

  • initialValue can be anything but it's usually: 0, {}, [], ""

DOMReduce supports Browser, Node.js & AMD.

USAGE

Refer to index.spec.js for Node.js fakeDOM example

In browser: inject DOMReduce code unto a website through devtools or script tag, then:

const tagCount = (tag, node) =>
  DOMReduce(
    node, 
    (count, node) => count + (node.tagName === tag ? 1 : 0), 
    0
  );

tagCount("DIV", document.body);

Happy DOM crawling!

1.0.9

4 years ago

1.0.8

5 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago