-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.js
134 lines (124 loc) · 4.05 KB
/
Table.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
class Table {
constructor(arr) {
if (arr && arr[0] && (arr[0].id === undefined || arr[0].data === undefined)) {
let normArray = []
let i = 0;
for (const data of arr) {
normArray.push({
id: i,
data: data
})
i++;
}
this.select = normArray;
} else {
if (arr !== undefined) {
this.select = arr;
} else {
throw new Error('Error: An array must be passed to create the table. Try creating it using something like this: "new Table([]);"')
}
}
}
delete(target) {
target.select.forEach(targetRow => {
this.select = this.select.filter(obj => obj != targetRow)
})
return { select: this.select }
}
whereId(id) {
return new Table(this.select.filter(obj => obj.id === id))
}
static getDataFromNestedColumns(obj, columns) {
if (!obj || !columns) {
throw new Error("Please check again the arguments!");
}
const args = columns.split('.');
let objData = obj.data;
for (let arg of args) {
if (objData[arg]) {
objData = objData[arg];
} else {
return false;
}
}
return objData;
}
whereEquals(column, value) {
return new Table(this.select.filter(obj => {
if (Table.getDataFromNestedColumns(obj, column)) {
return Table.getDataFromNestedColumns(obj, column) == value;
}
return false;
}));
}
whereNotEquals(column, value) {
return new Table(this.select.filter(obj => {
if (Table.getDataFromNestedColumns(obj, column)) {
return Table.getDataFromNestedColumns(obj, column) != value;
}
return false;
}));
}
insert(columns, data) {
let newRow = {};
let i = 0;
for (let col of columns) {
if (this.select && this.select[0]) {
if (this.select[0].data[col]) {
newRow[col] = data[i];
i++;
} else throw new Error('Error -1: Column does not exist on table.')
} else {
newRow[col] = data[i];
i++;
}
}
const lastId = this.select && this.select.length !== 0?this.select[this.select.length - 1].id:-1;
this.select?this.select.push({ id: lastId + 1, data: newRow }):this.select = [{ id: lastId + 1, data: newRow }];
}
update(columns, data) {
this.select.forEach(row => {
let i = 0;
for (let col of columns) {
if (row.data[col]) {
row.data[col] = data[i];
} else throw new Error('Error -2: Column does not exist on a row.')
i++;
}
})
}
updateFirst(columns, data) {
for (const row of this.select) {
let i = 0;
let isColFound = false
for (let col of columns) {
if (row.data[col]) {
row.data[col] = data[i];
isColFound = true;
} else throw new Error('Error -2: Column does not exist on a row.')
i++;
}
if (isColFound) break;
}
}
unique() {
let uniqueItems = [];
if (this.select) {
for (const item of this.select) {
if (!uniqueItems.some(i => {
let isUnique = true;
for (let [index, data] of Object.entries(i.data)) {
if (item.data[index] !== data) {
isUnique = false;
}
}
return isUnique;
})) {
uniqueItems.push(item)
}
}
}
return new Table(uniqueItems);
}
}
module.exports = Table;