npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

a.div

v0.1.0

Published

## Templates

Downloads

17

Readme

a.div

Templates

Frameworks often use HTML-templates as a basis for views. Custom attributes are introduced for looping, branching etc. At some point this HTML must be compiled into javascript, in a way that keeps the user safe from cross site scripting. This compiling takes time, everytime.

What if we drop the HTML alltogether?

<div class="form">
	<div>
		<label for="user">Username</label>
		<input name="user" placeholder="your name" />
	</div>
	<div>
		<label for="pass">Password</label>
		<input name="pass" type="password" />
	</div>
	<button onclick="loginUser()">Login</button>
</div>
a.div( { class:'form' },
	a.div(
		a.label( { for:'user' }, 'Username' ),
		a.input( { name:'user', placeholder:'your name' } )
	),
	a.div(
		a.label( { for:'pass' }, 'Password' ),
		a.input( { name:'pass', type:'password' } )
	)
	a.button('Login').on('click', loginUser);
)

It takes a little getting used to, but actually looks quite similar. It has the following benefits:

  • It's already javascript, no compilation needed
  • There is no HTML-parsing so we should be safe from XSS attacks

How does it work

a.div() //creates <div/>
a.div().toBody() //creates <div/> and appends it to the document body
a.div('hello world') //creates <div>Hello World</div>
a.div( { id:'test', class:'header' } ) //creates <div id="test" class="header"></div>
a.table(
	a.tr(
		a.td('col 1'),
		a.td('col 2') )
	)
)
/* creates:
<table>
	<tr>
		<td>col 1</td>
		<td>col 2</td>
	</tr>
</table>
*/
a('header',
	a.p('semantics')
)
/* creates:
<header>
	<p>semantics</p>
</header>
*/

Code size comparison

Disclaimer

In no way is the following a fair comparison between vue and a.div. It merely shows that similar end goals can be achieved using only a.div.

minified

vue.runtime.min.js (2.4.2) 58398 bytes

adiv.min.js 1536 bytes


Vue app1 (110 bytes)

<div id="app">
	{{ message }}
</div>

var app = new Vue({
	el: '#app',
	data: {
		message: 'Hello Vue!'
	}
})

adiv app1 (86 bytes)

a.div('Hello Vue!', { id:'app', message:function(str) {
	this.text(str);
}}).toBody()

Vue app2 (249 bytes)

<div id="app-2">
	<span v-bind:title="message">
		Hover your mouse over me for a few seconds
		to see my dynamically bound title!
	</span>
</div>

var app2 = new Vue({
	el: '#app-2',
	data: {
		message: 'You loaded this page on ' + new Date()
	}
})

adiv app2 (255 bytes)

a.div(
	{ id:'app-2' },
	a.span( { title: 'You loaded this page on ' + new Date() },
		`Hover your mouse over me for a few seconds
		to see my dynamically bound title!` ),
	function title(str) {
		this.qs('span').setAttribute('title', str);
	}
).toBody()

Vue app3 (124 bytes)

<div id="app-3">
	<p v-if="seen">Now you see me</p>
</div>

var app3 = new Vue({
	el: '#app-3',
	data: {
		seen: true
	}
})

adiv app3 (143 bytes)

var app3 = a.div( { id:'app-3' },
	a.p('Now you see me'),
	function seen(b) {
		this.q('p').el.style.display = b ? '' : 'none';
	}
).toBody()


Vue app4 (255 bytes)

<div id="app-4">
	<ol>
		<li v-for="todo in todos">
			{{ todo.text }}
		</li>
	</ol>
</div>

var app4 = new Vue({
	el: '#app-4',
	data: {
		todos: [
		{ text: 'Learn JavaScript' },
		{ text: 'Learn Vue' },
		{ text: 'Build something awesome' }
		]
	}
})

adiv app4 (291 bytes)

var app4 = a.div(
	{ id:'app-4' },
	a.ol(),
	function todos(arr) {
		var ol = this.q('ol');
		ol.empty()
		arr.forEach( (todo) => {
			ol.add( a.li(todo.text) );
		})
	}
).toBody();

app4.todos([
	{ text: 'Learn JavaScript' },
	{ text: 'Learn Vue' },
	{ text: 'Build something awesome' }
])

Vue app5 (299 bytes)

<div id="app-5">
	<p>{{ message }}</p>
	<button v-on:click="reverseMessage">Reverse Message</button>
</div>

var app5 = new Vue({
	el: '#app-5',
	data: {
		message: 'Hello Vue.js!'
	},
	methods: {
		reverseMessage: function () {
			this.message = this.message.split('').reverse().join('')
		}
	}
})

adiv app5 (199 bytes)

a.div( { id:'app-5' },
	a.p('Hello Vue.js!'),
	a.button('Reverse message').on('click', function() {
		var p = this.parent().q('p');
		p.text( p.text().split('').reverse().join('') );
	})
 ).toBody()

Vue app6 (149 bytes)

<div id="app-6">
	<p>{{ message }}</p>
	<input v-model="message">
</div>

var app6 = new Vue({
	el: '#app-6',
	data: {
		message: 'Hello Vue!'
	}
})

adiv app6 (161 bytes)

a.div( { id:'app-6' },
	a.p(),
	a.input( { value:'Hello Vue!' }).on('input', function() {
		this.parent().q('p').text( this.el.value );
	}).trigger()
).toBody()