Skip to content

Commit

Permalink
Major bug fix and small updates
Browse files Browse the repository at this point in the history
- Used const instead of let for endIndex variable in script.js#showPage (oops)
  • Loading branch information
cheeezburga committed Oct 15, 2024
1 parent c3fe25a commit f67a47a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
12 changes: 6 additions & 6 deletions js/combinator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export default class Combinator {
case 'ChoiceNode':
let results = [];
for (const choice of node.choices) {
results = results.concat(this.generate(choice));
const choiceResults = this.generate(choice);
results = results.concat(choiceResults);
}
return results;
return results.length > 0 ? results : [{ text: '', tags: [] }];
case 'ParseTagNode':
const parseResults = this.generate(node.node);
for (const result of parseResults) {
Expand All @@ -30,13 +31,12 @@ export default class Combinator {
}

combineNodes(nodes) {
if (nodes.length === 0) {
if (nodes.length === 0)
return [{ text: '', tags: [] }];
}

const [firstNode, ...restNodes] = nodes;
const firstResults = this.generate(firstNode);
const restResults = this.combineNodes(restNodes);
const firstResults = this.generate(firstNode) || [{ text: '', tags: [] }];
const restResults = this.combineNodes(restNodes) || [{ text: '', tags: [] }];

const combinedResults = [];

Expand Down
18 changes: 7 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let currentPage = 1;
let totalPages = 1;
let resultsPerPage = 100;
let patternsContent;
let statsContent;
let prevPageButton;
let nextPageButton;
let currentPageInput;
Expand All @@ -21,6 +22,7 @@ document.addEventListener('DOMContentLoaded', () => {
const patternInput = document.getElementById('pattern');
const themeToggleButton = document.getElementById('theme-toggle');
patternsContent = document.getElementById('patternsContent');
statsContent = document.getElementById('statsContent');
prevPageButton = document.getElementById('prev-page');
nextPageButton = document.getElementById('next-page');
currentPageInput = document.getElementById('current-page');
Expand Down Expand Up @@ -68,7 +70,6 @@ function debounce(func, delay) {

function parsePattern() {
const pattern = document.getElementById('pattern').value;
const statsContent = document.getElementById('statsContent');

// no pattern :(
if (!pattern.trim()) {
Expand All @@ -87,10 +88,12 @@ function parsePattern() {

const combinator = new Combinator();
const estimatedCombinations = combinator.estimate(ast);
console.log('Estimated combinations: ', estimatedCombinations);

const MAX_COMBINATIONS = 100000; // should this be configurable?

if (estimatedCombinations > MAX_COMBINATIONS) {
console.log('Too many combinations!');
displayError('Too many combinations!');
resetPagination();
return;
Expand All @@ -113,6 +116,7 @@ function parsePattern() {
displayStatistics();
showPage(currentPage);
} catch (error) {
console.error('Invalid syntax pattern: ', error);
displayError('Invalid syntax pattern!');
resetPagination();
}
Expand Down Expand Up @@ -148,11 +152,7 @@ function updatePaginationControls() {
nextPageButton.disabled = currentPage >= totalPages;

const paginationDiv = document.querySelector('.pagination');
if (allResults && allResults.length > 0) {
paginationDiv.style.display = 'flex';
} else {
paginationDiv.style.display = 'none';
}
paginationDiv.style.display = 'flex';
}

function displayStatistics() {
Expand All @@ -164,7 +164,6 @@ function displayStatistics() {
uniqueParseTags,
} = allData;

const statsContent = document.getElementById('statsContent');
statsContent.innerHTML = '';

const stats = [
Expand All @@ -191,7 +190,7 @@ function showPage(page) {
updatePaginationControls();

const startIndex = (currentPage - 1) * resultsPerPage;
const endIndex = startIndex + resultsPerPage;
let endIndex = startIndex + resultsPerPage;
if (endIndex > allResults.length)
endIndex = allResults.length;
const pageResults = allResults.slice(startIndex, endIndex);
Expand Down Expand Up @@ -261,9 +260,6 @@ function positionTooltip(e) {
}

function displayError(message) {
const patternsContent = document.getElementById('patternsContent');
const statsContent = document.getElementById('statsContent');

patternsContent.innerHTML = `<p class="error">${message}</p>`;
statsContent.innerHTML = '';

Expand Down

0 comments on commit f67a47a

Please sign in to comment.