-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (144 loc) · 5.68 KB
/
index.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var query = function query() {
var status = arguments.length <= 0 || arguments[0] === undefined ? { data: [] } : arguments[0];
// INNER JOIN
var innerJoin = function innerJoin(a, b, f) {
var m = a.length,
n = b.length,
c = [];
for (var i = 0; i < m; i++) {
var x = a[i];
for (var j = 0; j < n; j++) {
// cartesian product - all combinations
var y = void 0;
if (f) {
y = f([x, b[j]]) ? [x, b[j]] : undefined;
} else {
y = [x, b[j]];
}
if (y) c.push(y); // if a row is returned add it to the table
}
}
return c;
};
var where = function where(fns, data) {
// APPLY MULTIPLE WHERE CLASES WITH 'OR'
var whereOR = function whereOR(fns, data) {
var res = [];
fns.forEach(function (f) {
return res = res.concat(data.filter(f).reverse());
});
return res;
};
// APPLY MULTIPLE WHERE CLASES WITH 'OR'
var whereAND = function whereAND(fns, data) {
var res = [];
fns.forEach(function (f) {
return res = data.filter(f);
});
status.whereLogic = 'OR';
return res;
};
return status.whereLogic === 'AND' ? whereAND(fns, data) : whereOR(fns, data);
};
// GROUP DATASET BY ATTR
var groupBy = function groupBy(fns, data) {
// GROUP FUNCTION WHEN DATASET INST GROUPED
var group = function group(f, data) {
if (!data) return [];
var groupArray = function groupArray(f, data) {
var groupsMap = {};
var groups = [];
data.forEach(function (d) {
if (groupsMap[f(d)]) {
groupsMap[f(d)].push(d);
} else {
groupsMap[f(d)] = [d];
}
});
Object.keys(groupsMap).forEach(function (groupName) {
var items = groupsMap[groupName];
if (!Number.isNaN(Number.parseInt(groupName))) groupName = Number.parseInt(groupName);
groups.push([groupName, items]);
});
return groups;
};
if (_typeof(data[0][0]) != 'object' && _typeof(data[0][1]) === 'object') {
return regroup(f, data);
} else {
return groupArray(f, data);
}
};
// GROUP FUNCTION WHEN DATASET IS ALREADY GROUPED
var regroup = function regroup(f, data) {
for (var i = 0; i < data.length; i++) {
var g = data[i];
data[i][1] = group(f, g[1]);
}
return data;
};
fns.forEach(function (f) {
return data = group(f, data);
});
return data;
};
return {
select: function select(f) {
if (status.hasOwnProperty('select')) throw new Error('Duplicate SELECT');
status.select = f;
return query(status);
},
from: function from() {
for (var _len = arguments.length, data = Array(_len), _key = 0; _key < _len; _key++) {
data[_key] = arguments[_key];
}
if (status.hasOwnProperty('from')) throw new Error('Duplicate FROM');
status.from = data;
status.data = data;
return query(status);
},
where: function where() {
for (var _len2 = arguments.length, f = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
f[_key2] = arguments[_key2];
}
status.where = status.where ? status.where.concat(f) : f;
return query(status);
},
groupBy: function groupBy() {
for (var _len3 = arguments.length, f = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
f[_key3] = arguments[_key3];
}
if (status.hasOwnProperty('groupBy')) throw new Error('Duplicate GROUPBY');
status.groupBy = f;
return query(status);
},
orderBy: function orderBy(f) {
if (status.hasOwnProperty('orderBy')) throw new Error('Duplicate ORDERBY');
status.orderBy = f;
return query(status);
},
having: function having(f) {
status.having = f;
return query(status);
},
execute: function execute() {
if (status.data.length === 2) {
status.data = status.where ? innerJoin(status.data[0], status.data[1], status.where[0]) : innerJoin(status.data[0], status.data[1]);
status.whereLogic = 'AND';
} else {
status.data = status.data[0] || [];
}
if (status.where) status.data = where(status.where, status.data);
if (status.groupBy) status.data = groupBy(status.groupBy, status.data);
if (status.having) status.data = status.data.filter(status.having);
if (status.select) status.data = status.data.map(status.select);
if (status.orderBy) status.data = status.data.sort(status.orderBy);
return status.data;
}
};
};
exports.default = query;