-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.js
319 lines (270 loc) · 7.36 KB
/
data.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
function addData(p5, fn){
fn.touchStarted = function (...args) {
return this.mousePressed(...args);
};
fn.touchEnded = function (...args) {
return this.mouseReleased(...args);
};
fn.touchMoved = function (...args) {
return this.mouseDragged(...args);
};
fn.append = function (array, value) {
array.push(value);
return array;
};
fn.arrayCopy = function (src, srcPosition, dst, dstPosition, length) {
// the index to begin splicing from dst array
let start;
let end;
if (typeof length !== 'undefined') {
end = Math.min(length, src.length);
start = dstPosition;
src = src.slice(srcPosition, end + srcPosition);
} else {
if (typeof dst !== 'undefined') {
// src, dst, length
// rename so we don't get confused
end = dst;
end = Math.min(end, src.length);
} else {
// src, dst
end = src.length;
}
start = 0;
// rename so we don't get confused
dst = srcPosition;
src = src.slice(0, end);
}
// Since we are not returning the array and JavaScript is pass by reference
// we must modify the actual values of the array
// instead of reassigning arrays
Array.prototype.splice.apply(dst, [start, end].concat(src));
};
fn.concat = (list0, list1) => list0.concat(list1);
fn.reverse = list => list.reverse();
fn.shorten = function (list) {
list.pop();
return list;
};
fn.sort = function (list, count) {
let arr = count ? list.slice(0, Math.min(count, list.length)) : list;
const rest = count ? list.slice(Math.min(count, list.length)) : [];
if (typeof arr[0] === 'string') {
arr = arr.sort();
} else {
arr = arr.sort((a, b) => a - b);
}
return arr.concat(rest);
};
fn.splice = function (list, value, index) {
// note that splice returns spliced elements and not an array
Array.prototype.splice.apply(list, [index, 0].concat(value));
return list;
};
fn.subset = function (list, start, count) {
if (typeof count !== 'undefined') {
return list.slice(start, start + count);
} else {
return list.slice(start, list.length);
}
};
fn.join = function(list, separator) {
return list.join(separator);
};
fn.match = function(str, reg) {
return str.match(reg);
};
fn.matchAll = function(str, reg) {
const re = new RegExp(reg, 'g');
let match = re.exec(str);
const matches = [];
while (match !== null) {
matches.push(match);
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
match = re.exec(str);
}
return matches;
};
fn.split = function(str, delim) {
return str.split(delim);
};
fn.trim = function(str) {
if (str instanceof Array) {
return str.map(this.trim);
} else {
return str.trim();
}
};
fn.createStringDict = function (key, value) {
return new p5.StringDict(key, value);
};
fn.createNumberDict = function (key, value) {
return new p5.NumberDict(key, value);
};
p5.TypedDict = class TypedDict {
constructor(key, value) {
if (key instanceof Object) {
this.data = key;
} else {
this.data = {};
this.data[key] = value;
}
return this;
}
size() {
return Object.keys(this.data).length;
}
hasKey(key) {
return this.data.hasOwnProperty(key);
}
get(key) {
if (this.data.hasOwnProperty(key)) {
return this.data[key];
} else {
console.log(`${key} does not exist in this Dictionary`);
}
}
set(key, value) {
if (this._validate(value)) {
this.data[key] = value;
} else {
console.log('Those values dont work for this dictionary type.');
}
}
_addObj(obj) {
for (const key in obj) {
this.set(key, obj[key]);
}
}
create(key, value) {
if (key instanceof Object && typeof value === 'undefined') {
this._addObj(key);
} else if (typeof key !== 'undefined') {
this.set(key, value);
} else {
console.log(
'In order to create a new Dictionary entry you must pass ' +
'an object or a key, value pair'
);
}
}
clear() {
this.data = {};
}
remove(key) {
if (this.data.hasOwnProperty(key)) {
delete this.data[key];
} else {
throw new Error(`${key} does not exist in this Dictionary`);
}
}
print() {
for (const item in this.data) {
console.log(`key:${item} value:${this.data[item]}`);
}
}
saveTable(filename) {
let output = '';
for (const key in this.data) {
output += `${key},${this.data[key]}\n`;
}
const blob = new Blob([output], { type: 'text/csv' });
fn.downloadFile(blob, filename || 'mycsv', 'csv');
}
saveJSON(filename, opt) {
fn.saveJSON(this.data, filename, opt);
}
_validate(value) {
return true;
}
};
p5.StringDict = class StringDict extends p5.TypedDict {
constructor(...args) {
super(...args);
}
_validate(value) {
return typeof value === 'string';
}
};
p5.NumberDict = class NumberDict extends p5.TypedDict {
constructor(...args) {
super(...args);
}
_validate(value) {
return typeof value === 'number';
}
add(key, amount) {
if (this.data.hasOwnProperty(key)) {
this.data[key] += amount;
} else {
console.log(`The key - ${key} does not exist in this dictionary.`);
}
}
sub(key, amount) {
this.add(key, -amount);
}
mult(key, amount) {
if (this.data.hasOwnProperty(key)) {
this.data[key] *= amount;
} else {
console.log(`The key - ${key} does not exist in this dictionary.`);
}
}
div(key, amount) {
if (this.data.hasOwnProperty(key)) {
this.data[key] /= amount;
} else {
console.log(`The key - ${key} does not exist in this dictionary.`);
}
}
_valueTest(flip) {
if (Object.keys(this.data).length === 0) {
throw new Error(
'Unable to search for a minimum or maximum value on an empty NumberDict'
);
} else if (Object.keys(this.data).length === 1) {
return this.data[Object.keys(this.data)[0]];
} else {
let result = this.data[Object.keys(this.data)[0]];
for (const key in this.data) {
if (this.data[key] * flip < result * flip) {
result = this.data[key];
}
}
return result;
}
}
minValue() {
return this._valueTest(1);
}
maxValue() {
return this._valueTest(-1);
}
_keyTest(flip) {
if (Object.keys(this.data).length === 0) {
throw new Error('Unable to use minValue on an empty NumberDict');
} else if (Object.keys(this.data).length === 1) {
return Object.keys(this.data)[0];
} else {
let result = Object.keys(this.data)[0];
for (let i = 1; i < Object.keys(this.data).length; i++) {
if (Object.keys(this.data)[i] * flip < result * flip) {
result = Object.keys(this.data)[i];
}
}
return result;
}
}
minKey() {
return this._keyTest(1);
}
maxKey() {
return this._keyTest(-1);
}
};
}
if (typeof p5 !== undefined) {
p5.registerAddon(addData);
}