-
Notifications
You must be signed in to change notification settings - Fork 0
/
mig.js
93 lines (86 loc) · 2.91 KB
/
mig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Mig JS
* @description - Modular utility Javascript library. Made mostly for fun and personal projects but free to another to use and distribute as they see fit
* @author Will Busby
* @version 0.2
*/
;(function(win, doc, arr) {
'use strict';
/**
* Main selector function
* @param {String} b String selector
* @param {undefined} c Placeholder variable to use for the start of the b variable string.
* @param {undefined} i Placeholder to use for selector length
* @param {undefined} d Placeholder to use for selector incrementor
* @return {mixed} Mixed return of either single Element, HTMLCollection, or NodeList
*/
function _lookup(b, c, i, d) {
c = b[0];
i = b.length;
d = 0;
while (i-- && c !== 'q') {
switch(b[i]) {
case '.':
d++;
break;
case '#':
d++;
break;
case ' ' :
case ':' :
case '*' :
case '[' :
case '>' :
c = 'q';
break;
}
}
if (d > 1) {
c = 'q';
}
switch(c) {
case '#' :
return [doc.getElementById(b.substr(1))];
case '.' :
return doc.getElementsByClassName(b.substr(1));
default :
return doc.querySelectorAll(b);
}
}
/**
* The main Mig function
* @param {Mixed} selector either a CSS string selector or DOM element(s)
* @param {undefined} els placeholder variable for the future DOM array
* @return {Object} Mig instance Returns an instance of Mig with prototype functions and Array functions
*/
function Mig(selector, els) {
els = '' + selector === selector && _lookup(selector) || selector[0] && selector || [selector];
return this.push.apply(this, els);
}
/**
* The selector function exposed to the global scope. Takes the same selector param as above but returns a new instance of Mig
*/
win.m = function(a) {
// console.time('find');
// var b = new Mig(a);
// console.timeEnd('find');
// return b;
return new Mig(a);
};
//This is key, it adds all Array prototype functions and acts like an array instead of an object. You can still assign functions to the prototype like an object.
Mig.prototype = Object.create([]);
/**
* The main extension function
*/
Mig.prototype.extend = function(name, fn) {
Object.defineProperty(Mig.prototype, name, {
configurable: false,
value: fn,
writable: false
});
};
/**
* Create a static Object of Mig so static functions can be used without having to select an element. Useful when registering global events with Mig.on('event', callback); and then Mig.on.trigger('event');
*/
win.Mig = Object.create(Mig.prototype);
}(window, document, []));