-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-sheets.js
271 lines (231 loc) · 7.42 KB
/
js-sheets.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Created by rodik on 07/03/2017.
*/
"use strict";
const fs = require('fs');
// const handler = require('./proxy-handler');
class JSONSheet extends Array {
/**
*
* @param {Array} [arr] Array to create the sheet from. If an integer is passed, a normal Array will be returned.
* @param options
* @returns {*}
*/
constructor(arr = [], options = {}) {
if (typeof arr === "number" && Object.keys(options).length === 0) {
return new Array(arr);
}
super();
if (arr && arr.length) {
Array.prototype.push.apply(this, arr.map(a => Object.assign({}, a)));
}
this.options = options;
this.createindices(options.indices);
// return new Proxy(this, handler);
}
//// Index creation ////
createindices() {
this.indices = {};
if (!this.options.indices || !this.options.indices.length) {
return;
}
this.options.indices.forEach(this.createIndex.bind(this));
}
createIndex(indexName) {
let index = this.indices[indexName];
if (!index) {
index = this.indices[indexName] = {};
}
this.forEach((item) => {
let itemValues = item[indexName];
if (itemValues === undefined) {
return;
}
if (!Array.isArray(itemValues)) {
itemValues = [itemValues];
}
itemValues.forEach(itemValue => {
if (index[itemValue]) {
index[itemValue].push(item);
} else {
index[itemValue] = [item];
}
});
});
}
//// END: Index creation ////
//// Adding and removing from indices ////
addToIndices(item) {
if (!Object.keys(this.indices).length) {
return;
}
if (Array.isArray(item)) {
return item.forEach(arrItem=>{
this.addToIndices(arrItem);
});
}
Object.keys(this.indices).forEach((key) => {
let keyIndex = this.indices[key];
if (item.hasOwnProperty(key)) {
let itemValues = item[key];
if (!Array.isArray(itemValues)) {
this.addItemToIndex(keyIndex, itemValues, item);
} else {
itemValues.forEach(itemValue => {
this.addItemToIndex(keyIndex, itemValue, item);
});
}
}
});
}
removeFromIndices(item) {
if (!Object.keys(this.indices).length) {
return;
}
if (Array.isArray(item)) {
return item.forEach(arrItem=>{
this.removeFromIndices(arrItem);
});
}
Object.keys(this.indices).forEach((key) => {
let keyIndex = this.indices[key];
if (item.hasOwnProperty(key)) {
let value = item[key];
if(Array.isArray(value)){
value.forEach(innerValue=>{
this.removeItemFromIndex(keyIndex, innerValue, item);
});
}else{
this.removeItemFromIndex(keyIndex, value, item);
}
}
});
}
addItemToIndex(keyIndex, itemValues, item) {
if (keyIndex[itemValues]) {
keyIndex[itemValues].push(item);
} else {
keyIndex[itemValues] = [item];
}
}
removeItemFromIndex(keyIndex, value, item) {
if (keyIndex[value]) {
if (keyIndex[value].length < 2) {
delete keyIndex[value];
} else {
let indexOf = keyIndex[value].indexOf(item);
if (indexOf !== -1) {
keyIndex[value].splice(indexOf, 1);
}
}
}
}
//// END: Adding and removing from indices ////
//// File commands ////
save(path) {
if (!path) {
path = this.path;
}
if (!path) {
return false;
}
fs.writeFileSync(path, JSON.stringify(this, null, 4));
return true;
}
static fromFile(path, options) {
let fArr = new JSONSheet(JSON.parse(fs.readFileSync(path)), options);
fArr.path = path;
return fArr;
}
//// END: File commands ////
sortByField(field, dir = 1, options = {sortCopy:false}) {
let arrToSort = this;
if (options.sortCopy) {
arrToSort = this.slice();
}
// TODO: add cases for numeric and lexical
return arrToSort.sort((a, b) => {
if (dir === -1) {
return parseInt(b[field], 10) - parseInt(a[field], 10);
} else {
return parseInt(a[field], 10) - parseInt(b[field], 10);
}
});
}
findByField(fieldName, value) {
if (this.indices[fieldName]) {
return this.indices[fieldName][value] || [];
}
console.warn('js-sheets: searching without index');
return this.filter((item) => {
return item[fieldName] === value;
});
}
// union(fObj, field) {
// // let union = new Set([...fObj.indices[field], ...this.indices[field]]);
// // fObj.indices
// }
//
// intersect(fObj, field) {
// // let a = fObj.indices[field];
// // this.indices[field];
// // let intersection = new Set([...a].filter(x => b.has(x)));
// }
merge(fObj, field, otherField = field, fieldsToCopy = false, renames = false) {
fObj.forEach((extItem) => {
if (this.indices[field][extItem[otherField]]) {
if (!fieldsToCopy) {
Object.assign(this.indices[field][extItem[otherField]], extItem);
} else {
Object.keys(extItem).filter((key) => fieldsToCopy.indexOf(key) !== -1).forEach((key) => {
if (renames && renames[key]) {
this.indices[field][extItem[otherField]][renames[key]] = extItem[key];
} else {
this.indices[field][extItem[otherField]][key] = extItem[key];
}
});
}
}
});
return this;
}
query(queryObj) {
let queries = Object.keys(queryObj);
queries.forEach(key => {
if (key in queryMethods) {
}
});
// TODO: use indices and fallback to manual
}
}
['push', 'unshift'].forEach((method) => { // additive
JSONSheet.prototype[method] = function () {
let rt = Array.prototype[method].call(this, ...arguments);
this.addToIndices(...arguments);
return rt;
};
});
['pop', 'shift'].forEach((method) => { // subtractive
JSONSheet.prototype[method] = function () {
let rt = Array.prototype[method].call(this, ...arguments);
this.removeFromIndices(rt);
return rt;
};
});
['splice'].forEach((method) => {
JSONSheet.prototype[method] = function () {
let rt = Array.prototype[method].call(this, ...arguments);
this.removeFromIndices(rt);
if (arguments.length > 2) {
this.addToIndices(Array.from(arguments).slice(2));
}
return rt;
};
});
const queryMethods = {
"$sum": function (subQuery) {
},
"$avg": function (subQuery) {
}
};
module.exports = JSONSheet;