Code
window.addEventListener("DOMContentLoaded", () => { const publicProps = /\bpublic\b.*?\s+?([_a-zA-Z0-9<>\[\]\?]+)\s+([_a-zA-Z0-9]+?)\s*[;{=]/gm; const fallbackProps = /\b([_a-zA-Z0-9<>\[\]\?]+)\s+([_a-zA-Z0-9]+?)\s*[;{=]/gm; const reClass = /\b(class|interface|struct)\s*?([^\s]+)/gi; let className = "Unknown"; const tab = "\t"; const line = "\r\n"; const getMatches = (re, str) => { let match, matches = []; while (match = re.exec(str)) { matches.push(match); } return matches; }; const filterProps = (prop) => { console.log(prop.name, prop.type); const reserved = ["void", "return", "class", "struct", "enum", "using" ]; if (reserved.indexOf(prop.type) === -1) { return true; } }; const convertPublicProperty = (prop, prefix) => { let p = prop.name; let c = prop.last ? "" : ","; return `public ${prop.type} ${p} { get; set; }`; // ${prop.type} }; const convertProperty = (prop, prefix) => { let p = prop.name; let c = prop.last ? "" : ","; return `${prop.type} ${p} { get; set; }`; // ${prop.type} }; const mapTabs = (arr, tabCount) => { const tabs = tab.repeat(tabCount); return Array.from(arr).map(a => tabs + a); } const mapProps = (match, i, arr) => { return { type: match[1], name: match[2], last: arr.length - 1 === i }; }; const convertAll = (str) => { let props = getMatches(publicProps, str).map(mapProps).filter(filterProps); if (props.length < 1) { props = getMatches(fallbackProps, str).map(mapProps).filter(filterProps); } const props1 = mapTabs(props.map(convertPublicProperty), 1); const props2 = mapTabs(props.map(convertProperty), 1); const mapToClass = mapTabs([ `public class ${className}`, `{`, ...props1, `}` ], 1); const mapToInterface = mapTabs([ `public interface ${className}`, `{`, ...props2, `}` ], 1); const fullMapper = [ `namespace Extensions`, `{`, ...mapToInterface, ...mapToClass, `}` ]; return fullMapper.join(line); }; const run = () => { const input = document.querySelector("#input").value; className = (reClass.exec(input) || [])[2] || null; if (className) { console.log(input); const output = convertAll(input); document.querySelector("#output").value = output; } }; document.querySelector("#btnRun").addEventListener("click", run); });
286910cookie-checkC# create domain class/interface