-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathp5.Table.js
1328 lines (1292 loc) · 34.7 KB
/
p5.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module IO
* @submodule Table
* @requires core
*/
import p5 from '../core/main';
/**
* Table Options
* Generic class for handling tabular data, typically from a
* CSV, TSV, or other sort of spreadsheet file.
* CSV files are
* <a href="http://en.wikipedia.org/wiki/Comma-separated_values">
* comma separated values</a>, often with the data in quotes. TSV
* files use tabs as separators, and usually don't bother with the
* quotes.
* File names should end with .csv if they're comma separated.
* A rough "spec" for CSV can be found
* <a href="http://tools.ietf.org/html/rfc4180">here</a>.
* To load files, use the <a href="#/p5/loadTable">loadTable</a> method.
* To save tables to your computer, use the <a href="#/p5/save">save</a> method
* or the <a href="#/p5/saveTable">saveTable</a> method.
*
* Possible options include:
* <ul>
* <li>csv - parse the table as comma-separated values
* <li>tsv - parse the table as tab-separated values
* <li>header - this table has a header (title) row
* </ul>
*/
/**
* <a href="#/p5.Table">Table</a> objects store data with multiple rows and columns, much
* like in a traditional spreadsheet. Tables can be generated from
* scratch, dynamically, or using data from an existing file.
*
* @class p5.Table
* @constructor
* @param {p5.TableRow[]} [rows] An array of p5.TableRow objects
*/
p5.Table = class {
/**
* An array containing the names of the columns in the table, if the "header" the table is
* loaded with the "header" parameter.
* @type {String[]}
* @property columns
* @name columns
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //print the column names
* for (let c = 0; c < table.getColumnCount(); c++) {
* print('column ' + c + ' is named ' + table.columns[c]);
* }
* }
* </code>
* </div>
*/
constructor(rows = []) {
this.columns = [];
/**
* An array containing the <a href="#/p5.Table">p5.TableRow</a> objects that make up the
* rows of the table. The same result as calling <a href="#/p5/getRows">getRows()</a>
* @type {p5.TableRow[]}
* @property rows
* @name rows
*/
this.rows = rows;
}
/**
* Use <a href="#/p5/addRow">addRow()</a> to add a new row of data to a <a href="#/p5.Table">p5.Table</a> object. By default,
* an empty row is created. Typically, you would store a reference to
* the new row in a TableRow object (see newRow in the example above),
* and then set individual values using <a href="#/p5/set">set()</a>.
*
* If a <a href="#/p5.TableRow">p5.TableRow</a> object is included as a parameter, then that row is
* duplicated and added to the table.
*
* @method addRow
* @param {p5.TableRow} [row] row to be added to the table
* @return {p5.TableRow} the row that was added
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //add a row
* let newRow = table.addRow();
* newRow.setString('id', table.getRowCount() - 1);
* newRow.setString('species', 'Canis Lupus');
* newRow.setString('name', 'Wolf');
*
* //print the results
* for (let r = 0; r < table.getRowCount(); r++)
* for (let c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
*
* describe('no image displayed');
* }
* </code>
* </div>
*/
addRow (row) {
// make sure it is a valid TableRow
const r = row || new p5.TableRow();
if (typeof r.arr === 'undefined' || typeof r.obj === 'undefined') {
//r = new p5.prototype.TableRow(r);
throw new Error(`invalid TableRow: ${r}`);
}
r.table = this;
this.rows.push(r);
return r;
}
/**
* Removes a row from the table object.
*
* @method removeRow
* @param {Integer} id ID number of the row to remove
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //remove the first row
* table.removeRow(0);
*
* //print the results
* for (let r = 0; r < table.getRowCount(); r++)
* for (let c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
*
* describe('no image displayed');
* }
* </code>
* </div>
*/
removeRow (id) {
this.rows[id].table = null; // remove reference to table
const chunk = this.rows.splice(id + 1, this.rows.length);
this.rows.pop();
this.rows = this.rows.concat(chunk);
}
/**
* Returns a reference to the specified <a href="#/p5.TableRow">p5.TableRow</a>. The reference
* can then be used to get and set values of the selected row.
*
* @method getRow
* @param {Integer} rowID ID number of the row to get
* @return {p5.TableRow} <a href="#/p5.TableRow">p5.TableRow</a> object
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* let row = table.getRow(1);
* //print it column by column
* //note: a row is an object, not an array
* for (let c = 0; c < table.getColumnCount(); c++) {
* print(row.getString(c));
* }
*
* describe('no image displayed');
* }
* </code>
* </div>
*/
getRow (r) {
return this.rows[r];
}
/**
* Gets all rows from the table. Returns an array of <a href="#/p5.TableRow">p5.TableRow</a>s.
*
* @method getRows
* @return {p5.TableRow[]} Array of <a href="#/p5.TableRow">p5.TableRow</a>s
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* let rows = table.getRows();
*
* //warning: rows is an array of objects
* for (let r = 0; r < rows.length; r++) {
* rows[r].set('name', 'Unicorn');
* }
*
* //print the results
* for (let r = 0; r < table.getRowCount(); r++)
* for (let c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
*
* describe('no image displayed');
* }
* </code>
* </div>
*/
getRows () {
return this.rows;
}
/**
* Finds the first row in the Table that contains the value
* provided, and returns a reference to that row. Even if
* multiple rows are possible matches, only the first matching
* row is returned. The column to search may be specified by
* either its ID or title.
*
* @method findRow
* @param {String} value The value to match
* @param {Integer|String} column ID number or title of the
* column to search
* @return {p5.TableRow}
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //find the animal named zebra
* let row = table.findRow('Zebra', 'name');
* //find the corresponding species
* print(row.getString('species'));
* describe('no image displayed');
* }
* </code>
* </div>
*/
findRow (value, column) {
// try the Object
if (typeof column === 'string') {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[column] === value) {
return this.rows[i];
}
}
} else {
// try the Array
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column] === value) {
return this.rows[j];
}
}
}
// otherwise...
return null;
}
/**
* Finds the rows in the Table that contain the value
* provided, and returns references to those rows. Returns an
* Array, so for must be used to iterate through all the rows,
* as shown in the example above. The column to search may be
* specified by either its ID or title.
*
* @method findRows
* @param {String} value The value to match
* @param {Integer|String} column ID number or title of the
* column to search
* @return {p5.TableRow[]} An Array of TableRow objects
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //add another goat
* let newRow = table.addRow();
* newRow.setString('id', table.getRowCount() - 1);
* newRow.setString('species', 'Scape Goat');
* newRow.setString('name', 'Goat');
*
* //find the rows containing animals named Goat
* let rows = table.findRows('Goat', 'name');
* print(rows.length + ' Goats found');
* describe('no image displayed');
* }
* </code>
* </div>
*/
findRows (value, column) {
const ret = [];
if (typeof column === 'string') {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[column] === value) {
ret.push(this.rows[i]);
}
}
} else {
// try the Array
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column] === value) {
ret.push(this.rows[j]);
}
}
}
return ret;
}
/**
* Finds the first row in the Table that matches the regular
* expression provided, and returns a reference to that row.
* Even if multiple rows are possible matches, only the first
* matching row is returned. The column to search may be
* specified by either its ID or title.
*
* @method matchRow
* @param {String|RegExp} regexp The regular expression to match
* @param {String|Integer} column The column ID (number) or
* title (string)
* @return {p5.TableRow} TableRow object
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //Search using specified regex on a given column, return TableRow object
* let mammal = table.matchRow(new RegExp('ant'), 1);
* print(mammal.getString(1));
* //Output "Panthera pardus"
* }
* </code>
* </div>
*/
matchRow (regexp, column) {
if (typeof column === 'number') {
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column].match(regexp)) {
return this.rows[j];
}
}
} else {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[column].match(regexp)) {
return this.rows[i];
}
}
}
return null;
}
/**
* Finds the rows in the Table that match the regular expression provided,
* and returns references to those rows. Returns an array, so for must be
* used to iterate through all the rows, as shown in the example. The
* column to search may be specified by either its ID or title.
*
* @method matchRows
* @param {String} regexp The regular expression to match
* @param {String|Integer} [column] The column ID (number) or
* title (string)
* @return {p5.TableRow[]} An Array of TableRow objects
* @example
* <div class="norender">
* <code>
* let table;
*
* function setup() {
* table = new p5.Table();
*
* table.addColumn('name');
* table.addColumn('type');
*
* let newRow = table.addRow();
* newRow.setString('name', 'Lion');
* newRow.setString('type', 'Mammal');
*
* newRow = table.addRow();
* newRow.setString('name', 'Snake');
* newRow.setString('type', 'Reptile');
*
* newRow = table.addRow();
* newRow.setString('name', 'Mosquito');
* newRow.setString('type', 'Insect');
*
* newRow = table.addRow();
* newRow.setString('name', 'Lizard');
* newRow.setString('type', 'Reptile');
*
* let rows = table.matchRows('R.*', 'type');
* for (let i = 0; i < rows.length; i++) {
* print(rows[i].getString('name') + ': ' + rows[i].getString('type'));
* }
* }
* // Sketch prints:
* // Snake: Reptile
* // Lizard: Reptile
* </code>
* </div>
*/
matchRows (regexp, column) {
const ret = [];
if (typeof column === 'number') {
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column].match(regexp)) {
ret.push(this.rows[j]);
}
}
} else {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[column].match(regexp)) {
ret.push(this.rows[i]);
}
}
}
return ret;
}
/**
* Retrieves all values in the specified column, and returns them
* as an array. The column may be specified by either its ID or title.
*
* @method getColumn
* @param {String|Number} column String or Number of the column to return
* @return {Array} Array of column values
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* //getColumn returns an array that can be printed directly
* print(table.getColumn('species'));
* //outputs ["Capra hircus", "Panthera pardus", "Equus zebra"]
* describe('no image displayed');
* }
* </code>
* </div>
*/
getColumn (value) {
const ret = [];
if (typeof value === 'string') {
for (let i = 0; i < this.rows.length; i++) {
ret.push(this.rows[i].obj[value]);
}
} else {
for (let j = 0; j < this.rows.length; j++) {
ret.push(this.rows[j].arr[value]);
}
}
return ret;
}
/**
* Removes all rows from a Table. While all rows are removed,
* columns and column titles are maintained.
*
* @method clearRows
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* table.clearRows();
* print(table.getRowCount() + ' total rows in table');
* print(table.getColumnCount() + ' total columns in table');
* describe('no image displayed');
* }
* </code>
* </div>
*/
clearRows () {
delete this.rows;
this.rows = [];
}
/**
* Use <a href="#/p5/addColumn">addColumn()</a> to add a new column to a <a href="#/p5.Table">Table</a> object.
* Typically, you will want to specify a title, so the column
* may be easily referenced later by name. (If no title is
* specified, the new column's title will be null.)
*
* @method addColumn
* @param {String} [title] title of the given column
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* table.addColumn('carnivore');
* table.set(0, 'carnivore', 'no');
* table.set(1, 'carnivore', 'yes');
* table.set(2, 'carnivore', 'no');
*
* //print the results
* for (let r = 0; r < table.getRowCount(); r++)
* for (let c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
*
* describe('no image displayed');
* }
* </code>
* </div>
*/
addColumn (title) {
const t = title || null;
this.columns.push(t);
}
/**
* Returns the total number of columns in a Table.
*
* @method getColumnCount
* @return {Integer} Number of columns in this table
* @example
* <div>
* <code>
* // given the cvs file "blobs.csv" in /assets directory
* // ID, Name, Flavor, Shape, Color
* // Blob1, Blobby, Sweet, Blob, Pink
* // Blob2, Saddy, Savory, Blob, Blue
*
* let table;
*
* function preload() {
* table = loadTable('assets/blobs.csv');
* }
*
* function setup() {
* createCanvas(200, 100);
* textAlign(CENTER);
* background(255);
* }
*
* function draw() {
* let numOfColumn = table.getColumnCount();
* text('There are ' + numOfColumn + ' columns in the table.', 100, 50);
* }
* </code>
* </div>
*/
getColumnCount () {
return this.columns.length;
}
/**
* Returns the total number of rows in a Table.
*
* @method getRowCount
* @return {Integer} Number of rows in this table
* @example
* <div>
* <code>
* // given the cvs file "blobs.csv" in /assets directory
* //
* // ID, Name, Flavor, Shape, Color
* // Blob1, Blobby, Sweet, Blob, Pink
* // Blob2, Saddy, Savory, Blob, Blue
*
* let table;
*
* function preload() {
* table = loadTable('assets/blobs.csv');
* }
*
* function setup() {
* createCanvas(200, 100);
* textAlign(CENTER);
* background(255);
* }
*
* function draw() {
* text('There are ' + table.getRowCount() + ' rows in the table.', 100, 50);
* }
* </code>
* </div>
*/
getRowCount () {
return this.rows.length;
}
/**
* Removes any of the specified characters (or "tokens").
*
* If no column is specified, then the values in all columns and
* rows are processed. A specific column may be referenced by
* either its ID or title.
*
* @method removeTokens
* @param {String} chars String listing characters to be removed
* @param {String|Integer} [column] Column ID (number)
* or name (string)
*
* @example
* <div class="norender"><code>
* function setup() {
* let table = new p5.Table();
*
* table.addColumn('name');
* table.addColumn('type');
*
* let newRow = table.addRow();
* newRow.setString('name', ' $Lion ,');
* newRow.setString('type', ',,,Mammal');
*
* newRow = table.addRow();
* newRow.setString('name', '$Snake ');
* newRow.setString('type', ',,,Reptile');
*
* table.removeTokens(',$ ');
* print(table.getArray());
* }
*
* // prints:
* // 0 "Lion" "Mamal"
* // 1 "Snake" "Reptile"
* </code></div>
*/
removeTokens (chars, column) {
const escape = s => s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
const charArray = [];
for (let i = 0; i < chars.length; i++) {
charArray.push(escape(chars.charAt(i)));
}
const regex = new RegExp(charArray.join('|'), 'g');
if (typeof column === 'undefined') {
for (let c = 0; c < this.columns.length; c++) {
for (let d = 0; d < this.rows.length; d++) {
let s = this.rows[d].arr[c];
s = s.replace(regex, '');
this.rows[d].arr[c] = s;
this.rows[d].obj[this.columns[c]] = s;
}
}
} else if (typeof column === 'string') {
for (let j = 0; j < this.rows.length; j++) {
let val = this.rows[j].obj[column];
val = val.replace(regex, '');
this.rows[j].obj[column] = val;
const pos = this.columns.indexOf(column);
this.rows[j].arr[pos] = val;
}
} else {
for (let k = 0; k < this.rows.length; k++) {
let str = this.rows[k].arr[column];
str = str.replace(regex, '');
this.rows[k].arr[column] = str;
this.rows[k].obj[this.columns[column]] = str;
}
}
}
/**
* Trims leading and trailing whitespace, such as spaces and tabs,
* from String table values. If no column is specified, then the
* values in all columns and rows are trimmed. A specific column
* may be referenced by either its ID or title.
*
* @method trim
* @param {String|Integer} [column] Column ID (number)
* or name (string)
* @example
* <div class="norender"><code>
* function setup() {
* let table = new p5.Table();
*
* table.addColumn('name');
* table.addColumn('type');
*
* let newRow = table.addRow();
* newRow.setString('name', ' Lion ,');
* newRow.setString('type', ' Mammal ');
*
* newRow = table.addRow();
* newRow.setString('name', ' Snake ');
* newRow.setString('type', ' Reptile ');
*
* table.trim();
* print(table.getArray());
* }
*
* // prints:
* // 0 "Lion" "Mamal"
* // 1 "Snake" "Reptile"
* </code></div>
*/
trim (column) {
const regex = new RegExp(' ', 'g');
if (typeof column === 'undefined') {
for (let c = 0; c < this.columns.length; c++) {
for (let d = 0; d < this.rows.length; d++) {
let s = this.rows[d].arr[c];
s = s.replace(regex, '');
this.rows[d].arr[c] = s;
this.rows[d].obj[this.columns[c]] = s;
}
}
} else if (typeof column === 'string') {
for (let j = 0; j < this.rows.length; j++) {
let val = this.rows[j].obj[column];
val = val.replace(regex, '');
this.rows[j].obj[column] = val;
const pos = this.columns.indexOf(column);
this.rows[j].arr[pos] = val;
}
} else {
for (let k = 0; k < this.rows.length; k++) {
let str = this.rows[k].arr[column];
str = str.replace(regex, '');
this.rows[k].arr[column] = str;
this.rows[k].obj[this.columns[column]] = str;
}
}
}
/**
* Use <a href="#/p5/removeColumn">removeColumn()</a> to remove an existing column from a Table
* object. The column to be removed may be identified by either
* its title (a String) or its index value (an int).
* removeColumn(0) would remove the first column, removeColumn(1)
* would remove the second column, and so on.
*
* @method removeColumn
* @param {String|Integer} column columnName (string) or ID (number)
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* table.removeColumn('id');
* print(table.getColumnCount());
* describe('no image displayed');
* }
* </code>
* </div>
*/
removeColumn (c) {
let cString;
let cNumber;
if (typeof c === 'string') {
// find the position of c in the columns
cString = c;
cNumber = this.columns.indexOf(c);
} else {
cNumber = c;
cString = this.columns[c];
}
const chunk = this.columns.splice(cNumber + 1, this.columns.length);
this.columns.pop();
this.columns = this.columns.concat(chunk);
for (let i = 0; i < this.rows.length; i++) {
const tempR = this.rows[i].arr;
const chip = tempR.splice(cNumber + 1, tempR.length);
tempR.pop();
this.rows[i].arr = tempR.concat(chip);
delete this.rows[i].obj[cString];
}
}
/**
* Stores a value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @method set
* @param {Integer} row row ID
* @param {String|Integer} column column ID (Number)
* or title (String)
* @param {String|Number} value value to assign
*
* @example
* <div class="norender">
* <code>
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable('assets/mammals.csv', 'csv', 'header');
* }
*
* function setup() {
* table.set(0, 'species', 'Canis Lupus');
* table.set(0, 'name', 'Wolf');
*
* //print the results
* for (let r = 0; r < table.getRowCount(); r++)
* for (let c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
*
* describe('no image displayed');
* }
* </code>
* </div>
*/
set (row, column, value) {
this.rows[row].set(column, value);
}
/**
* Stores a Float value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified