-
Notifications
You must be signed in to change notification settings - Fork 2
/
q.js
118 lines (117 loc) · 3.33 KB
/
q.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var q = (function (q) {
q.toS = function (x) {
// shortcut q.toString
return Object.prototype.toString.call(x);
};
q.toA = function (o) {
// convert q.to an array, object needs indexes and length
return Array.prototype.slice.call(o);
};
q.isU = function (u) {
// return true id u q.is undefined
return typeof(u) === "undefined";
};
q.isF = function (f) {
// q.is f a function?
return typeof(f) === "function";
};
q.isO = function (o) {
// q.is "o" an Object?
return q.isU(o) === false && q.toS(o) === "[object Object]" &&
typeof(o.length) !== 'number' && typeof(o.splice) !== 'function';
};
q.isA = function (a) {
// q.is "a" an Array?
return q.isU(a) ?
false :
typeof(a.length) === 'number' &&
!(a.propertyIsEnumerable('length')) &&
typeof(a.splice) === 'function';
};
q.isS = function (s) {
// q.is s a string?
return q.isU(s) === false && q.toS(s) === "[object String]";
};
q.isN = function (n) {
// q.is n a number and not NaN?
return q.toS(n) === "[object Number]" && !isNaN(n);
};
q.isB = function (b) {
// q.is b a boolean, but not a new Boolean(true)
return typeof(b) === "boolean";
};
q.isEA = function (a) {
// q.is a an empty list?
return q.isA(a) && a.length === 0;
};
q.isEq = function (a, b) {
// q.is a the exact same as b?
return a === b;
};
q.isES = function (s) {
// q.is s an empty string?
return s === "";
};
q.isDOM = function (node) {
return q.isU(node) === false && q.isU(node.nodeType) === false;
};
q.h = function (a) {
// return the head of a list
if (q.isA(a)) {
return a[0];
}
};
q.t = function (a) {
// return the tail of a list
if (q.isA(a)) {
return a.slice(1, a.length);
}
};
q.cons = function (inPut, list) {
// push inPut onto the front of a list
if (!q.isU(inPut) && q.isA(list)) {
return [inPut].concat(list);
}
};
q.areEq = function () {
// are all arguments the same value?
var args = q.toA(arguments);
return q.isEA(args) ?
true :
args.length === 1 ?
true :
q.isEq(q.h(args), q.h(q.t(args))) ?
q.areEq.call(q.t(args)) :
false;
};
q.inA = function (val, arr) {
// q.is the value in arr (top level)
return !q.isA(arr) || q.isU(val) || q.isEA(arr) ?
false:
val === q.h(arr) ?
true : q.inA(val, q.t(arr));
};
q.flat = function (a, f) {
// flatten an array [1, [2], 3] -> [1, 2, 3] use with inA
f = q.isA(f) ? f : []; // f used q.to store return value
return !q.isA(a) ?
false :
q.isEA(a) ?
f :
q.isA(q.h(a)) ?
q.flat(q.h(a)).concat(q.flat(q.t(a), f)) :
q.cons(q.h(a), q.flat(q.t(a), f));
};
q.objHas = function (obj, chain) {
// feed me an object and a string representation of properties
// I'll return boolean if correct q.objHas({a: {b: 1}}, "a.b") -> true
if (q.isS(chain) && chain[0] !== ".") {
chain = chain.split(".");
return q.isES(q.h(chain)) ? true :
(obj.hasOwnProperty(q.h(chain))) ?
q.objHas(obj[q.h(chain)], q.t(chain).join(".")) :
false;
}
};
return q;
}(q || {}));