UNPKG

parsix

Version:

A lightweight and customizable JavaScript parser framework that processes strings based on user-defined keywords and actions. Perfect for building flexible, extensible parsing solutions.

101 lines (84 loc) 3.24 kB
// Import the Parser class import Parser from './index.js'; // Utility function to run test cases function runTest(testName, testFunction) { try { testFunction(); console.log(`✅ ${testName}: Passed`); } catch (error) { console.error(`❌ ${testName}: Failed`); console.error(error.message); } } // Test suite runTest("Basic keyword parsing", () => { const parser = new Parser({ 'hello': (actions) => actions.set('Hi'), 'world': (actions) => actions.set('Earth') }); const result = parser.parse("hello world").join(''); if (result !== "HiEarth") { throw new Error(`Expected "HiEarth", but got "${result}"`); } }); runTest("Default keyword handling", () => { const parser = new Parser({}, (actions) => {actions.set(actions.keyword.toUpperCase()); actions.skip();}); const result = parser.parse("hello world").join(''); if (result !== "HELLO WORLD") { throw new Error(`Expected "HELLO WORLD", but got "${result}"`); } }); runTest("Static method: Parser.sames", () => { const parser = new Parser(Parser.sames("yes", "no", "maybe")); const result = parser.parse("yesno maybe").join(''); if (result !== "yesnomaybe") { throw new Error(`Expected "yesnomaybe", but got "${result}"`); } }); runTest("Replacing a keyword with a string", () => { const parser = new Parser({ '!': 'STOP' }, (actions) => {actions.set(actions.keyword).skip();}); const result = parser.parse("hello!").join(''); if (result !== "helloSTOP") { throw new Error(`Expected "helloSTOP", but got "${result}"`); } }); runTest("Skipping unmatched characters", () => { const parser = new Parser({ 'a': (actions) => actions.set('A'), }, (actions) => {actions.set(actions.keyword).skip();}); // Retain unmatched characters const result = parser.parse("abc").join(''); if (result !== "Abc") { throw new Error(`Expected "Abc", but got "${result}"`); } }); runTest("Stopping parsing", () => { const parser = new Parser({ 'a': (actions) => actions.set('A'), 'b': (actions) => actions.stop() }); const result = parser.parse("abc").join(''); if (result !== "A") { throw new Error(`Expected "A", but got "${result}"`); } }); runTest("Nested keyword parsing", () => { const parser = new Parser({ 'start': '[Start]', 'end': '[End]' }, (actions) => {actions.set(actions.keyword).skip();}); // Retain unmatched content const result = parser.parse("start middle end").join(''); if (result !== "[Start] middle [End]") { throw new Error(`Expected "[Start] middle [End]", but got "${result}"`); } }); runTest("Mixed keywords and unmatched characters", () => { const parser = new Parser({ 'hello': 'Hi' }, (actions) => {actions.set(actions.keyword).skip();}); // Retain unmatched characters const result = parser.parse("hello, world!").join(''); if (result !== "Hi, world!") { throw new Error(`Expected "Hi, world!", but got "${result}"`); } });