News for hackers. Stuff that matters.
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by synergy20 on Wednesday October 02, 2024 @03:36PM (#41724287)
This is the whole code:

    (() => {
    const assignDeep = (elm, props) => Object.entries(props).forEach(([key, value]) =>
    typeof value === 'object' ? assignDeep(elm[key], value) : Object.assign(elm, {[key]: value}))

    const tagNames = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption',     'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del',     'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset',     'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5',     'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins',    'kbd', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'meta',     'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output',     'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's',     'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong',     'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template',     'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u',     'ul', 'var', 'video', 'wbr'  ].forEach(tag => window[tag] = function(...args) { 
    const props = typeof args[0] == 'object' && !(args[0] instanceof HTMLElement) ? args.shift() : null
    const elm = document.createElement(tag)
    props && assignDeep(elm, props)
    elm.append(...args.map(a => typeof a == 'string' ? document.createTextNode(a) : a))
    return elm
    })
    window['$'] = selector => document.querySelector(selector)
    window['$$'] = selector => Array.from(document.querySelectorAll(selector))
})()
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by synergy20 on Wednesday October 02, 2024 @03:47PM (#41724381)

    window['$'] = selector => document.querySelector(selector)
    window['$$'] = selector => Array.from(document.querySelectorAll(selector))
is really what I want to have
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by iudqnolq on Wednesday October 02, 2024 @04:05PM (#41724518)
I'm quite fond of a little helper like so:

    createElement({
        className: 'p-2 flex justify-end',
        contents: createElement({
          tag: 'img',
          src: '/os-logo-maps.svg'
        }),
      });
I got the idea from Lea Verou's blissfuljs. When you're creating a bunch of nested elements it comes in handy.
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by meow_catrix on Wednesday October 02, 2024 @04:29PM (#41724707)
That’s React without the JSX sugar.
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by SahAssar on Wednesday October 02, 2024 @07:34PM (#41725924)
The idea is older than react and react contains much more cruft (both wanted and unwanted).
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by iudqnolq on Wednesday October 02, 2024 @04:38PM (#41724768)
Yes, and?
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by smusamashah on Wednesday October 02, 2024 @04:05PM (#41724520)
What's the point of the rest of the code? Looks useless.
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by cies on Wednesday October 02, 2024 @05:53PM (#41725313)
Looks difficult. I have no clue what I'm reading there tbh.
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by abdusco on Wednesday October 02, 2024 @07:23PM (#41725860)
It's creating global helper funcs for creating elements. E.g. `a(...)` for an anchor
Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$ by jazzypants on Wednesday October 02, 2024 @06:40PM (#41725601)
Why do you pollute the global scope with all this crap? It's 2024, dude. Just make an ESM module.