Oops.
I got bored watching a video about the proposal and was like;
“bro wth, just do it yourself”
and so;
// Match Keyword Example :)
/* Unfinished; but ready!
OG:
- https://github.com/tc39/proposal-pattern-matching
*/
let match = (value, cb = "") => {
// Get cb.toString()
let cbStr = cb.toString() || "";
// console.log(cbStr);
let ASTList = []
let pointer = ""
let dimTree = {}
let arrOfTokens = {
"when": "case",
"end": "break",
"def": "default"
}
let curToken = ''
let idx = 0;
// Loop through cbStr, split at match keywords
dv: for (let i = 0; i < cbStr.length; i++) {
for (let key in arrOfTokens) {
if (curToken.trim().includes(key)) {
ASTList.push(curToken.slice(0, curToken.length - 1 - key.length - 1))
curToken = ''
continue dv
}
else {
// console.log(curToken)
continue
}
}
// console.log(dimTree)
curToken += cbStr[i]
if (
cbStr[i] == "{"
|| cbStr[i] == "("
|| cbStr[i] == "["
) {
idx++
}
if (
cbStr[i] == "}"
|| cbStr[i] == ")"
|| cbStr[i] == "]"
) {
idx--
}
}
if (curToken.length > 0) {
ASTList.push(curToken)
}
let transpiled = ``;
let formattedList = ASTList.slice(1, ASTList.length - 1) || [];
for (let i = 0; i < formattedList.length; i++) {
let formedStr = formattedList[i]
.trim() || ""
let tagName = formedStr
.split?.(/\({1}/)?.[1]
?.split?.(/\){1}/)
|| "";
if (tagName) {
tagName
? tagName
: tagName = formedStr
|| "";
let body = tagName[1]
.split("end")[0]
.trim()
|| "";
tagName = tagName[0]
.trim()
switch (typeof value) {
case "string":
value = `"${value}"`
break;
}
if (tagName.startsWith("{") && tagName.endsWith("}")) {
let _resu = eval(`(()=>{
var __Gross__ = ${tagName};
// console.log(__Gross__)
return __Gross__;
})()`)
if (typeof _resu === "object") {
for (let __key in _resu) {
let bod = _resu[__key]
globalThis[__key] = globalThis[__key] ?? ""
let domr = ` && (${JSON.stringify(value)}?.["${__key}"] == ${JSON.stringify(value)}?.["${__key}"])`
transpiled += `if (Object.keys(${JSON.stringify(value)}).includes("${__key}")${bod ? domr : ""}) {
${body}
}`
}
}
} else {
transpiled += `
if (${tagName} === ${value}) {
${body}
}
`
}
// console.log(transpiled)
}
}
let __return = eval(`(()=>{
${transpiled}
})()`)
return __return
}
var __return = match(
{
"a": "b",
"c": "d"
},
() => {
when ({ a: "b" })
var bs = "hello"
globalThis.bs = bs
end
}
)
I have no use for this, but knock yourself out.