1 |
|
2 |
|
3 | var Type = {};
|
4 |
|
5 | Type.fns = Type.fn = {is: function(fn){ return (!!fn && 'function' == typeof fn) }}
|
6 | Type.bi = {is: function(b){ return (b instanceof Boolean || typeof b == 'boolean') }}
|
7 | Type.num = {is: function(n){ return !list_is(n) && ((n - parseFloat(n) + 1) >= 0 || Infinity === n || -Infinity === n) }}
|
8 | Type.text = {is: function(t){ return (typeof t == 'string') }}
|
9 | Type.text.ify = function(t){
|
10 | if(Type.text.is(t)){ return t }
|
11 | if(typeof JSON !== "undefined"){ return JSON.stringify(t) }
|
12 | return (t && t.toString)? t.toString() : t;
|
13 | }
|
14 | Type.text.random = function(l, c){
|
15 | var s = '';
|
16 | l = l || 24;
|
17 | c = c || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz';
|
18 | while(l > 0){ s += c.charAt(Math.floor(Math.random() * c.length)); l-- }
|
19 | return s;
|
20 | }
|
21 | Type.text.match = function(t, o){ var r = false;
|
22 | t = t || '';
|
23 | o = Type.text.is(o)? {'=': o} : o || {};
|
24 | if(Type.obj.has(o,'~')){ t = t.toLowerCase(); o['='] = (o['='] || o['~']).toLowerCase() }
|
25 | if(Type.obj.has(o,'=')){ return t === o['='] }
|
26 | if(Type.obj.has(o,'*')){ if(t.slice(0, o['*'].length) === o['*']){ r = true; t = t.slice(o['*'].length) } else { return false }}
|
27 | if(Type.obj.has(o,'!')){ if(t.slice(-o['!'].length) === o['!']){ r = true } else { return false }}
|
28 | if(Type.obj.has(o,'+')){
|
29 | if(Type.list.map(Type.list.is(o['+'])? o['+'] : [o['+']], function(m){
|
30 | if(t.indexOf(m) >= 0){ r = true } else { return true }
|
31 | })){ return false }
|
32 | }
|
33 | if(Type.obj.has(o,'-')){
|
34 | if(Type.list.map(Type.list.is(o['-'])? o['-'] : [o['-']], function(m){
|
35 | if(t.indexOf(m) < 0){ r = true } else { return true }
|
36 | })){ return false }
|
37 | }
|
38 | if(Type.obj.has(o,'>')){ if(t > o['>']){ r = true } else { return false }}
|
39 | if(Type.obj.has(o,'<')){ if(t < o['<']){ r = true } else { return false }}
|
40 | function fuzzy(t,f){ var n = -1, i = 0, c; for(;c = f[i++];){ if(!~(n = t.indexOf(c, n+1))){ return false }} return true }
|
41 | if(Type.obj.has(o,'?')){ if(fuzzy(t, o['?'])){ r = true } else { return false }}
|
42 | return r;
|
43 | }
|
44 | Type.list = {is: function(l){ return (l instanceof Array) }}
|
45 | Type.list.slit = Array.prototype.slice;
|
46 | Type.list.sort = function(k){
|
47 | return function(A,B){
|
48 | if(!A || !B){ return 0 } A = A[k]; B = B[k];
|
49 | if(A < B){ return -1 }else if(A > B){ return 1 }
|
50 | else { return 0 }
|
51 | }
|
52 | }
|
53 | Type.list.map = function(l, c, _){ return obj_map(l, c, _) }
|
54 | Type.list.index = 1;
|
55 | Type.obj = {is: function(o){ return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false }}
|
56 | Type.obj.put = function(o, f, v){ return (o||{})[f] = v, o }
|
57 | Type.obj.has = function(o, f){ return o && Object.prototype.hasOwnProperty.call(o, f) }
|
58 | Type.obj.del = function(o, k){
|
59 | if(!o){ return }
|
60 | o[k] = null;
|
61 | delete o[k];
|
62 | return o;
|
63 | }
|
64 | Type.obj.as = function(o, f, v, u){ return o[f] = o[f] || (u === v? {} : v) }
|
65 | Type.obj.ify = function(o){
|
66 | if(obj_is(o)){ return o }
|
67 | try{o = JSON.parse(o);
|
68 | }catch(e){o={}};
|
69 | return o;
|
70 | }
|
71 | ;(function(){ var u;
|
72 | function map(v,f){
|
73 | if(obj_has(this,f) && u !== this[f]){ return }
|
74 | this[f] = v;
|
75 | }
|
76 | Type.obj.to = function(from, to){
|
77 | to = to || {};
|
78 | obj_map(from, map, to);
|
79 | return to;
|
80 | }
|
81 | }());
|
82 | Type.obj.copy = function(o){
|
83 | return !o? o : JSON.parse(JSON.stringify(o));
|
84 | }
|
85 | ;(function(){
|
86 | function empty(v,i){ var n = this.n;
|
87 | if(n && (i === n || (obj_is(n) && obj_has(n, i)))){ return }
|
88 | if(i){ return true }
|
89 | }
|
90 | Type.obj.empty = function(o, n){
|
91 | if(!o){ return true }
|
92 | return obj_map(o,empty,{n:n})? false : true;
|
93 | }
|
94 | }());
|
95 | ;(function(){
|
96 | function t(k,v){
|
97 | if(2 === arguments.length){
|
98 | t.r = t.r || {};
|
99 | t.r[k] = v;
|
100 | return;
|
101 | } t.r = t.r || [];
|
102 | t.r.push(k);
|
103 | };
|
104 | var keys = Object.keys;
|
105 | Type.obj.map = function(l, c, _){
|
106 | var u, i = 0, x, r, ll, lle, f = fn_is(c);
|
107 | t.r = null;
|
108 | if(keys && obj_is(l)){
|
109 | ll = Object.keys(l); lle = true;
|
110 | }
|
111 | if(list_is(l) || ll){
|
112 | x = (ll || l).length;
|
113 | for(;i < x; i++){
|
114 | var ii = (i + Type.list.index);
|
115 | if(f){
|
116 | r = lle? c.call(_ || this, l[ll[i]], ll[i], t) : c.call(_ || this, l[i], ii, t);
|
117 | if(r !== u){ return r }
|
118 | } else {
|
119 |
|
120 | if(c === l[lle? ll[i] : i]){ return ll? ll[i] : ii }
|
121 | }
|
122 | }
|
123 | } else {
|
124 | for(i in l){
|
125 | if(f){
|
126 | if(obj_has(l,i)){
|
127 | r = _? c.call(_, l[i], i, t) : c(l[i], i, t);
|
128 | if(r !== u){ return r }
|
129 | }
|
130 | } else {
|
131 |
|
132 | if(c === l[i]){ return i }
|
133 | }
|
134 | }
|
135 | }
|
136 | return f? t.r : Type.list.index? 0 : -1;
|
137 | }
|
138 | }());
|
139 | Type.time = {};
|
140 | Type.time.is = function(t){ return t? t instanceof Date : (+new Date().getTime()) }
|
141 |
|
142 | var fn_is = Type.fn.is;
|
143 | var list_is = Type.list.is;
|
144 | var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map;
|
145 | module.exports = Type;
|
146 | |
\ | No newline at end of file |