Playing Around With A Personal JavaScript Framework
April 2025
Giving It A Shot
This is from a while back. I was playing around with making my own basic Vanilla JavaScript framework (which I was calling YAGNI-JS because things are easier to talk about when they have a name).
This is the code in its most recent state (which I'm not currently using because I'm messing with an overhaul as I write this)
////////////////////////////////////////////////////////////
// yagni-js
// TODO: Rename 'parent' to 'target'
// and internal 'target' to 'el'
functionpageAddClassTo(target,className){constel=pageGetEl(target)if(el){el.classList.add(className)returnel}}functionpageAddListenerTo(target,event,func){constel=pageGetEl(target)if(el){el.addEventListener(event,func)returnel}}functionpageAddListenersTo(selector,event,func){constels=document.querySelectorAll(selector)els.forEach((el)=>{el.addEventListener(event,func)})}functionpageAddSvgTo(target,tag,attrs={}){constel=pageGetEl(target)if(el){constsvg=document.createElementNS('http://www.w3.org/2000/svg',tag)pageUpdateSvgAttrs(svg,attrs)el.appendChild(svg)returnsvg}}functionpageAddTo(target,tag,attrs={}){constel=pageGetEl(target)if(el){constnewEl=document.createElement(tag)pageUpdateAttrs(newEl,attrs)el.appendChild(newEl)returnnewEl}}functionpageAddToFront(target,tag,attrs={}){constel=pageGetEl(target)if(el){constnewEl=document.createElement(tag)pageUpdateAttrs(newEl,attrs)if(el.hasChildNodes()){constfirst_child=el.firstChildel.insertBefore(newEl,first_child)}else{el.appendChild(newEl)}returnnewEl}}functionpageAppendText(target,text){constel=pageGetEl(target)if(el){el.innerText=el.innerText+text}}functionpageGetData(target,key){// TODO: get a data attribute value from an element
// with JSON stringify
}functionpageGetEl(target){if(typeoftarget==='string'){constel=document.querySelector(target)if(el){returnel}else{pageLogError(`Could not find querySelector for: ${target}`)returnundefined}}else if(target){returntarget}else{pageLogError(`Could not get element: ${target}`)return
To Be Continued...
I'll probably play around with it again at some point. Leaving it here for now as a reference.