Soucouyant selector engine
by Julien Etienne
HTML
<div id='thing1'></div>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<div class='thing2'>Class 1</div>
<div class='thing2'>Class 2</div>
<div class='thing2'>Class 3</div>
JavaScript
// 0, all, col, last
o0(['thing1', 'id', 'thing1'], ['useableCol', 'tag', 'p', 'col'], ['targetCol', 'tag', 'p', 'col', 0], ['allTags', 'tag', 'p', 'all'], ['secLast', 'tag', 'p', 'last', -1], ['last', 'tag', 'p', 'last'], ['getClass', 'class', 'thing2', 'col', 2]);
function selector() {
var elTemp;
for (var i = 0; i < arguments.length; i++) {
// id
if (arguments[i][1] === 'id' || arguments[i][1] === '#') {
o0[arguments[i][0]] = document.getElementById(arguments[i][2]);
} else {
// Collections getter
var getEl;
if (arguments[i][1] === 'tag' || arguments[i][1] === '@') {
getel = 'getElementsByTagName';
}
if (arguments[i][1] === 'class' || arguments[i][1] === '.') {
getel = 'getElementsByClassName';
}
// Collections
if (arguments[i].length === 3) { // no index
o0[arguments[i][0]] = document[getel](arguments[i][2])[0];
} else if (arguments[i][3] === 'all') { // all for API only
o0[arguments[i][0]] = [document[getel](arguments[i][2]), 'all'];
} else if (arguments[i][3] === 'col') { // Col with eq
if (arguments[i].length === 5) {
var eq = arguments[i][4];
o0[arguments[i][0]] = document[getel](arguments[i][2])[eq];
} else {
o0[arguments[i][0]] = document[getel](arguments[i][2]);
}
} else if (arguments[i][3] === 'last') { // Last with eq
var collection = document[getel](arguments[i][2]);
if (arguments[i].length === 5) {
var eq = arguments[i][4] - 1;
o0[arguments[i][0]] = collection[collection.length + eq];
} else {
o0[arguments[i][0]] = collection[collection.length - 1];
}
}
...