|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// MIT License |
| 4 | + |
| 5 | +// Copyright (c) 2024 Furqonflynn |
| 6 | + |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | + |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | + |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | + |
| 25 | +const fs = require('fs'); |
| 26 | +const readline = require('readline'); |
| 27 | + |
| 28 | +const rl = readline.createInterface({ |
| 29 | + input: process.stdin, |
| 30 | + output: process.stdout |
| 31 | +}); |
| 32 | + |
| 33 | +// Coloring (Opstional), you can use chalk instead |
| 34 | +const color = { |
| 35 | + green: '\x1b[32m', |
| 36 | + red: '\x1b[31m', |
| 37 | + blue: '\x1b[34m', |
| 38 | + yellow: '\x1b[33m', |
| 39 | + underLine: '\x1b[4m', |
| 40 | + reset: '\x1b[0m' |
| 41 | +}; |
| 42 | + |
| 43 | +function banner() { |
| 44 | + console.log(` |
| 45 | +╭━━━╮╱╱╱╱╱╱╱╱╱╱╱╱╱╭━━━┳╮ |
| 46 | +┃╭━━╯╱╱╱╱╱╱╱╱╱╱╱╱╱┃╭━━┫┃${color.yellow}RegExp${color.green} |
| 47 | +┃╰━━┳╮╭┳━┳━━┳━━┳━╮┃╰━━┫┃╭╮╱╭┳━╮╭━╮ |
| 48 | +┃╭━━┫┃┃┃╭┫╭╮┃╭╮┃╭╮┫╭━━┫┃┃┃╱┃┃╭╮┫╭╮╮ |
| 49 | +┃┃╱╱┃╰╯┃┃┃╰╯┃╰╯┃┃┃┃┃╱╱┃╰┫╰━╯┃┃┃┃┃┃┃ |
| 50 | +╰╯╱╱╰━━┻╯╰━╮┣━━┻╯╰┻╯╱╱╰━┻━╮╭┻╯╰┻╯╰╯ |
| 51 | +╱╱╱╱╱╱╱╱╱╱╱┃┃╱╱╱╱╱╱╱╱╱╱╱╭━╯┃${color.red}Tester${color.reset} |
| 52 | +╱╱╱╱╱╱╱╱╱╱╱╰╯╱╱╱╱╱╱╱╱╱╱╱╰━━╯ |
| 53 | + `); |
| 54 | +} |
| 55 | + |
| 56 | +const menu = ` |
| 57 | +1. ${color.green}${color.underLine}Check Regex${color.reset} |
| 58 | +2. ${color.underLine}Examples${color.reset} |
| 59 | +3. ${color.reset}Settings |
| 60 | +4. ${color.red}Exit${color.reset} |
| 61 | +\nEnter Your choice: `; |
| 62 | + |
| 63 | +function promptUser() { |
| 64 | + rl.question(menu, (choice) => { |
| 65 | + switch (choice) { |
| 66 | + case '1': |
| 67 | + checkRegex(); |
| 68 | + break; |
| 69 | + case '2': |
| 70 | + showExamples(); |
| 71 | + break; |
| 72 | + case '3': |
| 73 | + console.clear(); |
| 74 | + settingsMenu(); |
| 75 | + break; |
| 76 | + case '4': |
| 77 | + rl.close(); |
| 78 | + break; |
| 79 | + default: |
| 80 | + console.log('Invalid choice. Please try again.'); |
| 81 | + promptUser(); |
| 82 | + } |
| 83 | + }); |
| 84 | +} |
| 85 | +//Checking input string type Before Execute Regextester |
| 86 | +function checkRegex() { |
| 87 | + rl.question(`${color.yellow}Enter the regex pattern${color.reset}: `, (pattern) => { |
| 88 | + const settings = JSON.parse(fs.readFileSync('settings.json', 'utf8')); |
| 89 | + if (settings.TestStringsInput) { |
| 90 | + console.log(`${color.blue}=> Multiple Test Strings${color.reset} `); |
| 91 | + enterTestStrings(pattern); |
| 92 | + } else { |
| 93 | + console.log(`${color.blue}=> Single Test Strings${color.reset} `); |
| 94 | + rl.question(`${color.yellow}Enter the string to test${color.reset}: `, (testString) => { |
| 95 | + executeRegex(pattern, testString, settings); |
| 96 | + }); |
| 97 | + } |
| 98 | + }); |
| 99 | +} |
| 100 | +//Executing String for testing |
| 101 | +function executeRegex(pattern, testString, settings) { |
| 102 | + try { |
| 103 | + const flags = `${settings.caseInsensitive ? 'i' : ''}${settings.global ? 'g' : ''}${settings.multiline ? 'm' : ''}${settings.dotMatchesAll ? 's' : ''}`; |
| 104 | + const regex = new RegExp(pattern, flags); |
| 105 | + const matches = testString.match(regex); |
| 106 | + const result = matches ? `${color.blue}True${color.reset} Found ${matches.length} matches.` : `${color.red}False${color.reset} Not Found matches`; |
| 107 | + console.log(`${color.yellow}Result${color.reset}: ${result}`); |
| 108 | + if (matches) { |
| 109 | + const highlightedString = testString.replace(regex, (match) => `${color.green}${color.underLine}${match}${color.reset}`); |
| 110 | + console.log(`${color.yellow}Matches are Underline below${color.reset}:\n${highlightedString}\n`); |
| 111 | + } |
| 112 | + } catch (e) { |
| 113 | + console.log(`${color.red}Invalid regex pattern. Please try again.${color.reset}`); |
| 114 | + } |
| 115 | + continueOrExit(); |
| 116 | +} |
| 117 | + |
| 118 | +function continueOrExit() { |
| 119 | + rl.question('Do you want to continue using the script? (y/n): ', (answer) => { |
| 120 | + if (answer.toLowerCase() === 'y') { |
| 121 | + checkRegex(); |
| 122 | + } else { |
| 123 | + console.log(`${color.green}Thank you!${color.reset}`); |
| 124 | + rl.close(); |
| 125 | + } |
| 126 | + }); |
| 127 | +} |
| 128 | +// Showing Examples |
| 129 | +function showExamples() { |
| 130 | + console.log(` |
| 131 | +Examples: |
| 132 | +1. Pattern: ^[a-z]+$ | String: hello | Result: true |
| 133 | +2. Pattern: \\d+ | String: 123abc | Result: true |
| 134 | +3. Pattern: \\w+@\\w+\\.\\w+ | String: [email protected] | Result: true |
| 135 | +`); |
| 136 | + continueOrExit(); |
| 137 | +} |
| 138 | +// Setting Flag and Input String Type |
| 139 | +function settingsMenu() { |
| 140 | + console.log(` |
| 141 | +(1 for true/ 0 for false)\n |
| 142 | + i - Case-insensitive |
| 143 | + m - Multiline |
| 144 | + g - Global (don't stop at first match) |
| 145 | + s - Dot matches all INCLUDING line breaks\n |
| 146 | + Test Strings (1 for Multiple Input) \n `); |
| 147 | + |
| 148 | + rl.question('Case-insensitive (1/0): ', (caseInsensitive) => { |
| 149 | + rl.question('Multiline (1/0): ', (multiline) => { |
| 150 | + rl.question('Global (1/0): ', (global) => { |
| 151 | + rl.question('Dot matches all (1/0): ', (dotMatchesAll) => { |
| 152 | + rl.question('\nTestStringsInput (1/0): ', (TestStringsInput) => { |
| 153 | + const settings = { |
| 154 | + caseInsensitive: caseInsensitive === '1', |
| 155 | + multiline: multiline === '1', |
| 156 | + global: global === '1', |
| 157 | + dotMatchesAll: dotMatchesAll === '1', |
| 158 | + TestStringsInput: TestStringsInput === '1', |
| 159 | + }; |
| 160 | + fs.writeFileSync('settings.json', JSON.stringify(settings, null, 2)); |
| 161 | + console.clear(); |
| 162 | + console.log('Settings saved to settings.json'); |
| 163 | + promptUser(); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +} |
| 170 | +// Multiple Input String Test |
| 171 | +function enterTestStrings(pattern) { |
| 172 | + const testStrings = []; |
| 173 | + console.log(`${color.yellow}Enter the string to test${color.blue} (or type "done" to finish)${color.reset}:`); |
| 174 | + |
| 175 | + function getTestString() { |
| 176 | + rl.question(`${color.green}(>${color.reset} `, (testString) => { |
| 177 | + if (testString.toLowerCase() === 'done') { |
| 178 | + const settings = JSON.parse(fs.readFileSync('settings.json', 'utf8')); |
| 179 | + const combinedTestString = testStrings.join('\n'); |
| 180 | + executeRegex(pattern, combinedTestString, settings); |
| 181 | + continueOrExit(); |
| 182 | + } else { |
| 183 | + testStrings.push(testString); |
| 184 | + getTestString(); |
| 185 | + } |
| 186 | + }); |
| 187 | + } |
| 188 | + getTestString(); |
| 189 | +} |
| 190 | + |
| 191 | +if (!fs.existsSync('settings.json')) { |
| 192 | + console.clear(); |
| 193 | + console.log(`${color.red}settings.json not found. Defaulting to settings option.${color.reset}`); |
| 194 | + settingsMenu(); |
| 195 | +} else { |
| 196 | + console.clear(); |
| 197 | + banner(); |
| 198 | + promptUser(); |
| 199 | +} |
| 200 | + |
0 commit comments