UNPKG

5.83 kBJavaScriptView Raw
1
2// Generic javascript utilities.
3var Type = {};
4//Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }}
5Type.fns = Type.fn = {is: function(fn){ return (!!fn && 'function' == typeof fn) }}
6Type.bi = {is: function(b){ return (b instanceof Boolean || typeof b == 'boolean') }}
7Type.num = {is: function(n){ return !list_is(n) && ((n - parseFloat(n) + 1) >= 0 || Infinity === n || -Infinity === n) }}
8Type.text = {is: function(t){ return (typeof t == 'string') }}
9Type.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}
14Type.text.random = function(l, c){
15 var s = '';
16 l = l || 24; // you are not going to make a 0 length random number, so no need to check type
17 c = c || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz';
18 while(l > 0){ s += c.charAt(Math.floor(Math.random() * c.length)); l-- }
19 return s;
20}
21Type.text.match = function(t, o){ var r = false;
22 t = t || '';
23 o = Type.text.is(o)? {'=': o} : o || {}; // {'~', '=', '*', '<', '>', '+', '-', '?', '!'} // ignore case, exactly equal, anything after, lexically larger, lexically lesser, added in, subtacted from, questionable fuzzy match, and ends with.
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 } // via http://stackoverflow.com/questions/9206013/javascript-fuzzy-search
41 if(Type.obj.has(o,'?')){ if(fuzzy(t, o['?'])){ r = true } else { return false }} // change name!
42 return r;
43}
44Type.list = {is: function(l){ return (l instanceof Array) }}
45Type.list.slit = Array.prototype.slice;
46Type.list.sort = function(k){ // creates a new sort function based off some field
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}
53Type.list.map = function(l, c, _){ return obj_map(l, c, _) }
54Type.list.index = 1; // change this to 0 if you want non-logical, non-mathematical, non-matrix, non-convenient array notation
55Type.obj = {is: function(o){ return o? (o instanceof Object && o.constructor === Object) || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === 'Object' : false }}
56Type.obj.put = function(o, f, v){ return (o||{})[f] = v, o }
57Type.obj.has = function(o, f){ return o && Object.prototype.hasOwnProperty.call(o, f) }
58Type.obj.del = function(o, k){
59 if(!o){ return }
60 o[k] = null;
61 delete o[k];
62 return o;
63}
64Type.obj.as = function(o, f, v, u){ return o[f] = o[f] || (u === v? {} : v) }
65Type.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}());
82Type.obj.copy = function(o){ // because http://web.archive.org/web/20140328224025/http://jsperf.com/cloning-an-object/2
83 return !o? o : JSON.parse(JSON.stringify(o)); // is shockingly faster than anything else, and our data has to be a subset of JSON anyways!
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 //if(Type.test.is(c,l[i])){ return ii } // should implement deep equality testing!
120 if(c === l[lle? ll[i] : i]){ return ll? ll[i] : ii } // use this for now
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 //if(a.test.is(c,l[i])){ return i } // should implement deep equality testing!
132 if(c === l[i]){ return i } // use this for now
133 }
134 }
135 }
136 return f? t.r : Type.list.index? 0 : -1;
137 }
138}());
139Type.time = {};
140Type.time.is = function(t){ return t? t instanceof Date : (+new Date().getTime()) }
141
142var fn_is = Type.fn.is;
143var list_is = Type.list.is;
144var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map;
145module.exports = Type;
146
\No newline at end of file