Skip to content

Commit

Permalink
Turn Visitor into a file and import.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Sep 11, 2024
1 parent 0abe081 commit 1d7dadd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
33 changes: 33 additions & 0 deletions bin/Visitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* Copyright (c) 2024 Digital Bazaar, Inc.
*/
export class Visitor {
/**
* A Visitor traverses nodes in a graph.
*
* @param {object} options - Options for the Class.
* @param {Function<boolean>} options.condition - The condition a node must
* pass to be included in the accumulator.
* @param {Array} options.accumulator - An accumulator to store matching
* nodes in.
* @param {Array<string>} options.props - The props to recur on.
*/
constructor({condition, accumulator, props}) {
this.condition = condition;
this.accumulator = accumulator;
this.props = props;
}
visit({nodes}) {
for(const node of nodes) {
if(this.condition(node)) {
this.accumulator.push(node);
}
for(const prop of this.props) {
if(node[prop]) {
this.visit({nodes: node[prop]});
}
}
}
}
}

24 changes: 2 additions & 22 deletions bin/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,7 @@
*/
import {parse, TextNode} from 'node-html-parser';
import {readFile} from 'node:fs/promises';

class Visitor {
constructor({condition, accumulator, props}) {
this.condition = condition;
this.accumulator = accumulator;
this.props = props;
}
visit({nodes}) {
for(const node of nodes) {
if(this.condition(node)) {
this.accumulator.push(node);
}
for(const prop of this.props) {
if(node[prop]) {
this.visit({nodes: node[prop]});
}
}
}
}
}
import {Visitor} from './Visitor.js';

export async function checkSpecText({specUrl, suiteLog}) {
const specUrls = Array.isArray(specUrl) ? specUrl : [specUrl];
Expand All @@ -45,13 +26,12 @@ export async function checkSpecText({specUrl, suiteLog}) {
if(suiteLog) {
log = JSON.parse(await readFile(suiteLog));
}
//console.log(accumulator);
if(log) {
testVisitor.visit({nodes: log.suites});
testVisitor.visit({nodes: log.tests});
}
const testTitles = new Set(tests.map(test => test?.title));
const normStatements = new Set(statements);
const normStatements = new Set(statements.map(s => s.text));
console.log(`Test Title Count ${testTitles.size}`);
console.log(`Normative Statement Count ${normStatements.size}`);
}
Expand Down

0 comments on commit 1d7dadd

Please sign in to comment.