-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
209 lines (204 loc) · 4.61 KB
/
index.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
export default class Query {
private data: any[];
private key: string;
private current: any[];
private _limit: number | null;
private old: any[];
constructor(data: any[]) {
this.old = JSON.parse(JSON.stringify(data));
this.data = data.map((v, i) => ({ ...v, _$current: v, _$old: this.old[i] }));
this.key = "";
this.current = this.data.map((v, i) => ({ ...v, _$current: v, _$old: this.old[i] }));
this._limit = null;
}
/**
* Add a number to the selected value
*/
public add(val: number) {
this.current.forEach((d) => {
d._$current[this.key] += val;
});
return this;
}
/**
* Gets back to the top level on the data
*/
public clearQuery() {
this.current = this.data.map((v, i) => ({ ...v, _$current: v, _$old: this.old[i] }));
return this;
}
/**
* Delete the key
*/
public delete(key: string) {
this.current = this.current.map((v, i) => {
delete v._$current[key];
v._$old = v;
return (v = v);
});
return this;
}
/**
* Divide the number
*/
public divide(val: number) {
this.current.forEach((d) => {
d._$current[this.key] /= val;
});
return this;
}
/**
* Check if the value is equal
*/
public equals(val: any) {
this.current = this.current.filter((v, i) => v._$current[this.key] === val);
return this;
}
/**
* Check if the data exists on every object
*/
public exists(key: string) {
return this.current.every((v) => v[key]);
}
/**
* Find the key and matching value
*/
public find(key: string, val: any) {
const res: any[] = [];
return this.current.forEach((v) => {
if (v._$current[key] === val) res.push(v);
});
}
/**
* Check if a number value is greater
*/
public gt(val: number) {
this.current = this.current.filter((v, i) => v._$current[this.key] > val);
return this;
}
/**
* Check if a number value is greater or equal
*/
public gte(val: number) {
this.current = this.current.filter((v, i) => v._$current[this.key] >= val);
return this;
}
/**
* Limit the number of outputs
*/
public limit(val: number) {
this._limit = val;
return this;
}
/**
* Check if a number value is lesser
*/
public lt(val: number) {
this.current = this.current.filter((v, i) => v._$current[this.key] < val);
return this;
}
/**
* Check if a number value is greater or equal
*/
public lte(val: number) {
this.current = this.current.filter((v, i) => v._$current[this.key] <= val);
return this;
}
/**
* Multiply the number
*/
public multiply(val: number) {
this.current.forEach((d) => {
d._$current[this.key] *= val;
});
return this;
}
/**
* Push a element to the selected array
*/
public push(val: any) {
this.current.forEach((d) => {
if (Array.isArray(d._$current[this.key])) d._$current[this.key].push(val);
});
return this;
}
/**
* Delete elements and replace it by a value
*/
public splice(start: number, deleteCount?: number, items?: any[]) {
this.current.forEach((d) => {
if (Array.isArray(d._$current[this.key])) d._$current[this.key].splice(start, deleteCount, items);
});
return this;
}
/**
* Get the raw data
*/
public raw() {
const data = this.current.map((v) => (v = v._$current));
return data;
}
/**
* Update the selected value
*/
public update(val: any) {
this.current.forEach((d) => {
d._$current[this.key] = val;
});
return this;
}
/**
* Get into a deeper object
* @param {String} key
*/
public select(key: string) {
this.current.map((v, i) => (this.current[i]._$current = v[key]));
return this;
}
/**
* Subtract the number
*/
public subtract(val: number) {
this.current.forEach((d) => {
d._$current[this.key] -= val;
});
return this;
}
/**
* Saves the queries on the data
*/
public save() {
this.current = this.current.map((v, i) => (v = { ...v._$current }));
this.data = this.current;
const res = this.data.map((v, i) => {
delete v["_$old"]["_$current"];
delete v["_$current"];
return (v = { ...v });
});
return res;
}
/**
* Get the Latest data
*/
public toJSON() {
return this.data.map((v, i) => ({ ...v }));
}
/**
* Get the last selected query
*/
public toValue() {
return this.current.map((v, i) => {
v = { ...v._$current };
delete v["_$old"];
return v;
});
}
/**
* Select a object to query next
* @param {string} key
*/
public where(key: string) {
this.key = `${key}`;
return this;
}
}