Skip to content

Instantly share code, notes, and snippets.

@HasanHuseyinDemir
Last active September 9, 2023 20:37
Show Gist options
  • Save HasanHuseyinDemir/ba781df027f4271ff9aa9b2e978a0afd to your computer and use it in GitHub Desktop.
Save HasanHuseyinDemir/ba781df027f4271ff9aa9b2e978a0afd to your computer and use it in GitHub Desktop.
JSX Element Namer for Template Literals , used it in frameworks I wrote
const namer=(item)=>{
let data = item;
let last="/>";
let first="<";
let count=data.split(last).length-1;
let counted =0;
while(count>counted){
let lastplac=data.search(last)-1;
let i=0;
let deval="";
while(lastplac-i>=-1){
if(data.charAt(lastplac-i)==first){
deval=deval.split("").reverse().join("");
let name=deval.split(" ")[0];
let props=deval.replace(name,"");
let completed=`<${name} ${props?props:""}></${name}>`;
data=data.slice(0,lastplac-i)+completed+data.slice(lastplac+3,data.length);
counted++;
lastplac="";
i=0;
deval="";
}else{
deval=deval+data.charAt(lastplac-i);
}
i++;
}
}
return data;}
/*
before:
<p>Hello World</p>
<Test attribute="test"/>
----------------------------
after:
<p>Hello World</p>
<Test attribute="test"></Test>
*/
//<Hello/> => <hello></hello>
//<element attr="hello"/> => <element attr="hello"></element>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment