-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgtfs.js
1712 lines (1474 loc) · 55.2 KB
/
gtfs.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
'use strict';
/* eslint-disable no-underscore-dangle */
const infoLog = require('debug')('gtfsNodeLib:i');
const fs = require('fs-extra');
const forEachWithLog = require('./helpers/logging_iterator_wrapper');
const { exportGtfs } = require('./helpers/export');
const getters = require('./helpers/getters');
const { importTable, createGtfsObjectFromSimpleObject } = require('./helpers/import');
const defaultSchema = require('./helpers/schema');
/**
* Table-generic function to add items in a table of a GTFS.
*
* @param {Gtfs} gtfs GTFS object in which to add the items.
* @param {string} tableName Name of the table of the GTFS in which the objects should be added.
* @param {Array.<Object>} items Array of items to add to the GTFS.
*/
function addItems(gtfs, tableName, items) {
if (items instanceof Array === false) {
throw new Error(`items must be an array instead of: ${items}`);
}
items = items.map((item) => {
// GtfsRow object should have a clone method
if (item.clone === undefined) {
return gtfs.createGtfsObjectFromSimpleObject(item);
}
return item;
});
const indexedTable = gtfs.getIndexedTable(tableName);
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
if (indexKeys.setOfItems) {
items.forEach(item => indexedTable.add(item));
return;
}
if (indexKeys.indexKey) {
items.forEach(item => indexedTable.set(item[indexKeys.indexKey], item));
return;
}
if (indexKeys.firstIndexKey && indexKeys.secondIndexKey) {
items.forEach((item) => {
if (indexedTable.has(item[indexKeys.firstIndexKey]) === false) {
indexedTable.set(item[indexKeys.firstIndexKey], new Map());
}
indexedTable.get(item[indexKeys.firstIndexKey]).set(item[indexKeys.secondIndexKey], item);
});
}
}
/**
* Table-generic function to get an indexed table of a GTFS. The indexation depends of the table, and is defined in
* the schema (see schema.js).
*
* @param {Gtfs} gtfs GTFS object containing the table to get.
* @param {string} tableName Name of the table of the GTFS to get.
* @return {
* Object|
* Map.<string, Object>|
* Map.<string, Map.<string, Object>>
* } Indexed table returned
*/
function getIndexedTable(gtfs, tableName) {
if (gtfs._tables.has(tableName) === false) {
importTable(gtfs, tableName);
infoLog(`[Importation] Table ${tableName} has been imported.`);
}
return gtfs._tables.get(tableName);
}
/**
* Check if an object is already in a table. Can only be used for not indexed table.
*
* @param {Gtfs} gtfs - GTFS object containing the table to check
* @param {Object} item - Item which will be searched in the table
* @param {string} tableName - Name of the table of the GTFS in which to search the object.
* @returns {Boolean} Whether or not the item is in the table.
*/
function hasItemInTable(gtfs, item, tableName) {
const items = gtfs.getIndexedTable(tableName);
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
if (indexKeys.setOfItems) {
return items.has(item);
}
throw new Error('hasItemInTable can only be used on table stored as Set. Use the key in other cases.');
}
/**
* Table-generic function to get an iterate in a table of a GTFS.
*
* @param {Gtfs} gtfs GTFS object containing the table to iterate on.
* @param {string} tableName Name of the table of the GTFS to enumerate.
* @param {function} iterator Function which will be applied on every item of the table.
*/
function forEachItem(gtfs, tableName, iterator) {
if (typeof iterator !== 'function') {
throw new Error(`iterator must be a function, instead of a ${typeof iterator}.`);
}
const indexedTable = gtfs.getIndexedTable(tableName);
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
const deepness = gtfs._schema.deepnessByTableName[tableName];
if (indexKeys.singleton) {
iterator(indexedTable);
return;
}
if (deepness === 0 || deepness === 1) {
forEachWithLog(`Iterating:${tableName}`, indexedTable, (item) => {
iterator(item);
});
return;
}
if (deepness === 2) {
forEachWithLog(`Iterating:${tableName}`, indexedTable, (indexedSubTable) => {
indexedSubTable.forEach(iterator);
});
}
}
/**
* Table-generic function to remove items in a table of a GTFS.
*
* @param {Gtfs} gtfs GTFS object containing the table in which the object should be removed.
* @param {string} tableName Name of the table of the GTFS in which to add the items.
* @param {Array.<Object>} items Array of items to remove of the GTFS.
*/
function removeItems(gtfs, tableName, items) {
if (items instanceof Array === false) {
throw new Error(`items must be an array instead of: ${items}`);
}
const indexedTable = gtfs.getIndexedTable(tableName);
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
if (indexKeys.setOfItems) {
items.forEach(item => indexedTable.delete(item));
return;
}
if (indexKeys.indexKey) {
items.forEach(item => indexedTable.delete(item[indexKeys.indexKey]));
return;
}
if (indexKeys.firstIndexKey && indexKeys.secondIndexKey) {
items.forEach((item) => {
if (indexedTable.has(item[indexKeys.firstIndexKey]) === true) {
indexedTable.get(item[indexKeys.firstIndexKey]).delete(item[indexKeys.secondIndexKey]);
}
if (indexedTable.get(item[indexKeys.firstIndexKey]).size === 0) {
indexedTable.delete(item[indexKeys.firstIndexKey]);
}
});
}
}
/**
* Table-generic function to set an indexed table in the GTFS.
*
* @param {Gtfs} gtfs GTFS object in which the table will be set.
* @param {string} tableName Name of the table of the GTFS to set.
* @param {Object|Map|Set} table Object properly indexed as the schema requires the table to be (see schema.js).
*/
function setTable(gtfs, tableName, table) {
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
if (indexKeys.setOfItems && table instanceof Set === true) {
gtfs._tables.set(tableName, table);
} else if (indexKeys.singleton && table instanceof Set === false && table instanceof Map === false) {
gtfs._tables.set(tableName, table);
} else if (indexKeys.setOfItems !== true && indexKeys.singleton !== true && table instanceof Map === true) {
gtfs._tables.set(tableName, table);
} else {
throw new Error(`setTable cannot be used with the table: ${table}`);
}
}
/**
* Return the size of the Map if define, otherwise 0
*
* @param {Map} map
* @returns {number}
*/
function getSizeOfMap(map) {
return (map instanceof Map) ? map.size : 0;
}
/**
* Return the size of the Set if define, otherwise 0
*
* @param {Set} set
* @returns {number}
*/
function getSizeOfSet(set) {
return (set instanceof Set) ? set.size : 0;
}
/**
* Table-generic function to reset a table of a GTFS (ie, create a new Map)
*
* @param {Gtfs} gtfs GTFS object containing the table to be reset.
* @param {string} tableName Name of the table to be reset.
*/
function resetTable(gtfs, tableName) {
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
if (indexKeys.setOfItems) {
gtfs._tables.set(tableName, new Set());
} else {
gtfs._tables.set(tableName, new Map());
}
}
/**
* Table-generic function to get the number of items in the provided GTFS table
*
* @param {Gtfs} gtfs GTFS object containing the table.
* @param {string} tableName Name of the table to be get the number of item from.
*/
function getNumberOfItemsInTable(gtfs, tableName) {
const indexKeys = gtfs._schema.indexKeysByTableName[tableName];
const deepness = gtfs._schema.deepnessByTableName[tableName];
if (deepness === 1) {
const indexedTable = gtfs.getIndexedTable(tableName);
return getSizeOfMap(indexedTable);
}
if (deepness === 0 && indexKeys.setOfItems) {
const items = gtfs.getIndexedTable(tableName);
return getSizeOfSet(items);
}
throw new Error(
`'${tableName}' has a deepness of ${deepness}.` +
'getNumberOfItemsInTable can only apply on a first level table or a Set.'
);
}
class Gtfs {
/**
* Constructor of the GTFS
*
* # options.regexPatternObjectsByTableName
*
* Optional ad-hoc list of regex to fix the tables. The keys are the tableName like defined in schema.js, the value
* are arrays containing pairs of regex and pattern to be applied on the raw table, before parsing. The goal is to fix
* some bad CSV to make them readable.
*
* Example
*
* The following raw is invalid according to the CSV specification:
*
* > something,something else,a field "not" properly escaped,one last thing
*
* Assuming it is in someTable.txt, it could be fixed with the following regexPatternObjectsByTableName:
*
* regexPatternObjectsByTableName = {
* nameOfTheTable: [{
* regex: /,a field "not" properly escaped,/g,
* pattern: ',a field ""not"" properly escaped,',
* }]
* };
*
*
* # options.throws
*
* Optional ad-hoc boolean. Default is true. Will force the parser to ignore invalid rows in the tables.
*
*
* # options.postImportTableFunction
*
* Optional ad-hoc function which will be applied on every item of every table after importation.
*
*
* # options.forcedSchema
*
* Will overwrite the default schema by the value passed.
*
*
* @param {string} [path] Path to the folder that contains the GTFS text files.
* @param {{
* regexPatternObjectsByTableName: Map.<string, Array.<{regex: RegExp, pattern: string}>>,
* throws: boolean,
* forcedSchema,
* postImportItemFunction: function,
* preExportItemFunction: function,
* }} [options] Optional. See list above.
* @return {Gtfs} gtfs Instanciated GTFS object.
*/
constructor(
path,
{
regexPatternObjectsByTableName = new Map(),
throws = true,
forcedSchema,
postImportItemFunction,
preExportItemFunction,
} = {}
) {
if (path !== undefined) {
if (typeof path !== 'string' || path.length === 0) {
throw new Error(`Gtfs need a valid input path as string, instead of: "${path}".`);
}
path = (path[path.length - 1] === '/') ? path : `${path}/`;
if (fs.existsSync(path) === false) {
throw new Error(`inputPath: "${path}" is not a valid folder.`);
}
}
this.isGtfs = true;
this._path = path;
this._regexPatternObjectsByTableName = regexPatternObjectsByTableName;
this._shouldThrow = throws;
this._postImportItemFunction = postImportItemFunction;
this._preExportItemFunction = preExportItemFunction;
this._tables = new Map();
this._schema = forcedSchema || defaultSchema;
}
/* Input/Output */
/**
* Async function exporting the GTFS at a specific path.
*
* @param {string} path Path to the folder which will contain the GTFS. The folder will be created if needed.
* @param {function} callback Function called when the export will be done.
*/
exportAtPath(path, callback) { return exportGtfs(this, path, callback); }
/**
* Getter returning the path of the GTFS when it was imported.
*
* @return {string} Path to the imported GTFS.
*/
getPath() { return this._path; }
/* Generic table & item manipulation */
/**
* Table-generic function to add an item in a table of the GTFS.
*
* @param {Object} item Item to add in the GTFS.
* @param {string} tableName Name of the table of the GTFS in which the item will be added.
*/
addItemInTable(item, tableName) { addItems(this, tableName, [item]); }
/**
* Table-generic function to add items in a table of the GTFS.
*
* @param {Array.<Map>} items Array of items to add in the GTFS.
* @param {string} tableName Name of the table of the GTFS in which the item will be added.
*/
addItemsInTable(items, tableName) { addItems(this, tableName, items); }
/**
* Table-generic function to get an iterate in a table of the GTFS.
*
* @param {string} tableName Name of the table of the GTFS to enumerate.
* @param {function} iterator Function which will be applied on every item of the table.
*/
forEachItemInTable(tableName, iterator) { forEachItem(this, tableName, iterator); }
/**
* Enumerate through every table name of the GTFS.
*
* @param {function} iterator Function which will be applied on every table name.
*/
forEachTableName(iterator) { this.getTableNames().forEach(iterator); }
/**
* Table-generic function to get an indexed table of the GTFS. The indexation depends of the table, and is defined in
* the schema (see schema.js).
*
* @param {string} tableName Name of the table of the GTFS to get.
* @return {Object} Indexed table returned
*/
getIndexedTable(tableName) { return getIndexedTable(this, tableName); }
/**
* Get an item of a table using its index.
*
* WARNING: Will work only for the tables in which such unique indexing value exists (see schema.js for an
* exhaustive list)
*
* @param {string} index Index of the item
* @param {string} tableName Name of the table
*/
getItemWithIndexInTable(index, tableName) { return getters.getItemWithIndex(index, tableName, this); }
/**
* Get a Set containing every table names, either defined as part of the GTFS specification, or already loaded in
* the GTFS.
*
* @return {Set.<string>} Array of the table names
*/
getTableNames() { return new Set([...this._schema.tableNames, ...this._tables.keys()]); }
/**
* Get the default schema of the GTFS. For safety, the schema is cloned before being passed.
*
* @return {Object} The schema
*/
static getDefaultSchema() { return JSON.parse(JSON.stringify(defaultSchema)); }
/**
* Get the schema of the GTFS. For safety, the schema is cloned before being passed.
*
* @return {Object} The schema
*/
getSchema() { return JSON.parse(JSON.stringify(this._schema)); }
/**
* Build the list of the keys used in a table of the GTFS. Since the GTFS specification allows any additional field,
* this function allows to explore those additional values.
*
* @param {string} tableName Table of the GTFS of which we want to key.
* @return {Array.<string>} Keys used by the items of the table.
*/
getActualKeysForTable(tableName) { return getters.getActualKeysForTable(this, tableName); }
/**
* Table-generic function returning the number of items in the provided GTFS table.
*
* @param {string} tableName Name of the table of the GTFS
*/
getNumberOfItemsInTable(tableName) { return getNumberOfItemsInTable(this, tableName); }
/**
* Get the parent item using one of its child.
*
* @param {Object} item The child item.
* @param {string} tableName The name of the table containing the parent item.
* @return {Object} The parent item.
*/
getParentItem(item, tableName) { return getters.getParentItem(item, tableName, this); }
/**
* Table-generic function to remove one item in a table of the GTFS.
*
* @param {Object} item Item to remove of the GTFS.
* @param {string} tableName Name of the table of the GTFS in which to remove the items.
*/
removeItemInTable(item, tableName) { removeItems(this, tableName, [item]); }
/**
* Table-generic function to remove items in a table of the GTFS.
*
* @param {Array.<Object>} items Array of items to remove of the GTFS.
* @param {string} tableName Name of the table of the GTFS in which to remove the items.
*/
removeItemsInTable(items, tableName) { removeItems(this, tableName, items); }
/**
* Table-generic function to reset a GTFS table (ie, create a new Map)
*
* @param {Gtfs} gtfs GTFS object containing the table to be reset.
* @param {string} tableName Name of the table to be reset.
*/
resetTable(tableName) { resetTable(this, tableName); }
/**
* Table-generic function to set an indexed table in the GTFS.
*
* @param {Map} indexedItems Object properly indexed as the schema requires the table to be (see schema.js).
* @param {string} tableName Name of the table of the GTFS to set.
*/
setIndexedItemsAsTable(indexedItems, tableName) { setTable(this, tableName, indexedItems); }
/* agency.txt */
/**
* Adds an agency in the GTFS.
*
* @param {Object} agency Agency to add in the GTFS.
*/
addAgency(agency) { addItems(this, 'agency', [agency]); }
/**
* Adds a list of agencies in the GTFS.
*
* @param {Array.<Object>} agencies Array of agencies to add in the GTFS.
*/
addAgencies(agencies) { addItems(this, 'agency', agencies); }
/**
* Apply a function to each agency in the GTFS.
*
* @param {function} iterator Function which will be applied on every agency.
*/
forEachAgency(iterator) { forEachItem(this, 'agency', iterator); }
/**
* Get the agency using one of its child route.
*
* WARNING: Will return the agency which is indexed with the route.agency_id of the route passed as argument. If the
* internal value of the agency's agency_id has been changed but not it's indexing, the result will be wrong.
*
* @param {Object} route A route of the GTFS.
* @return {Object} The agency indexed by the route.agency_id of the route passed as parameter.
*/
getAgencyOfRoute(route) { return getters.getParentItem(route, 'agency', this); }
/**
* Get the agency using its agency_id.
*
* WARNING: Will return the agency which is indexed with the agency_id passed as argument. If the internal value of
* the agency's agency_id has been changed but not it's indexing, the result will be wrong.
*
* @param {string} agencyId Index of the agency.
* @return {Object} The agency indexed by the agency_id passed as parameter.
*/
getAgencyWithId(agencyId) { return getters.getItemWithIndex(agencyId, 'agency', this); }
/**
* Get the indexed agencies of the GTFS. The indexation is defined in the schema (see schema.js).
*
* @return {Map.<string, Object>} Indexed agencies.
*/
getIndexedAgencies() { return getIndexedTable(this, 'agency'); }
/**
* Removes an agency of the GTFS.
*
* WARNING: It will remove the agency indexed by the `agency.agency_id` of the agency passed as parameter.
*
* @param {Object} agency Agency to remove of the GTFS.
*/
removeAgency(agency) { removeItems(this, 'agency', [agency]); }
/**
* Removes a list of agencies of the GTFS.
*
* WARNING: It will remove the agencies indexed by the `agency.agency_id` of the agencies passed as parameter.
*
* @param {Array.<Object>} agencies Agencies to remove of the GTFS.
*/
removeAgencies(agencies) { removeItems(this, 'agency', agencies); }
/**
* Reset the map of indexed agencies.
*/
resetAgencies() { resetTable(this, 'agency'); }
/**
* Set the map of indexed agencies.
*
* WARNING: The Map should be indexed as defined in schema.js
*
* @param {Map.<string, Object>} indexedAgencies Map of agencies properly indexed (see schema.js).
*/
setIndexedAgencies(indexedAgencies) { setTable(this, 'agency', indexedAgencies); }
/* stops.txt */
/**
* Adds a stop in the GTFS.
*
* @param {Object} stop Stop to add in the GTFS.
*/
addStop(stop) { addItems(this, 'stops', [stop]); }
/**
* Adds a list of stops in the GTFS.
*
* @param {Array.<Object>} stops Array of stops to add in the GTFS.
*/
addStops(stops) { addItems(this, 'stops', stops); }
/**
* Apply a function to each stop in the GTFS.
*
* @param {function} iterator Function which will be applied on every stop.
*/
forEachStop(iterator) { forEachItem(this, 'stops', iterator); }
/**
* Get the indexed stops of the GTFS. The indexation is defined in the schema (see schema.js).
*
* @return {Map.<string, Object>} Indexed stops.
*/
getIndexedStops() { return getIndexedTable(this, 'stops'); }
/**
* Get the number of stops defined
*
* @returns {number}
*/
getNumberOfStops() { return getNumberOfItemsInTable(this, 'stops'); }
/**
* Get the stop using one of its child stopTime.
*
* WARNING: Will return the stop which is indexed with the stopTime.stop_id of the stopTime passed as argument. If the
* internal value of the stop's stop_id has been changed but not it's indexing, the result will be wrong.
*
* @param {Object} stopTime A stopTime of the GTFS.
* @return {Object} The stop indexed by the stopTime.stop_id of the stopTime passed as parameter.
*/
getStopOfStopTime(stopTime) { return getters.getParentItem(stopTime, 'stops', this); }
/**
* Get the stop using its stop_id.
*
* WARNING: Will return the stop which is indexed with the stop_id passed as argument. If the internal value of
* the stop's stop_id has been changed but not it's indexing, the result will be wrong.
*
* @param {string} stopId Index of the stop.
* @return {Object} The stop indexed by the stop_id passed as parameter.
*/
getStopWithId(stopId) { return getters.getItemWithIndex(stopId, 'stops', this); }
/**
* Removes a stop of the GTFS.
*
* WARNING: It will remove the stop indexed by the `stop.stop_id` of the stop passed as parameter.
*
* @param {Object} stop Stop to remove of the GTFS.
*/
removeStop(stop) { removeItems(this, 'stops', [stop]); }
/**
* Removes a list of stops of the GTFS.
*
* WARNING: It will remove the stops indexed by the `stop.stop_id` of the stops passed as parameter.
*
* @param {Array.<Object>} stops Stops to remove of the GTFS.
*/
removeStops(stops) { removeItems(this, 'stops', stops); }
/**
* Reset the map of indexed stops.
*/
resetStops() { resetTable(this, 'stops'); }
/**
* Set the map of indexed stops.
*
* WARNING: The Map should be indexed as defined in schema.js
*
* @param {Map.<string, Object>} indexedStops Map of stops properly indexed (see schema.js).
*/
setIndexedStops(indexedStops) { setTable(this, 'stops', indexedStops); }
/* routes.txt */
/**
* Adds a route in the GTFS.
*
* @param {Object} route Route to add in the GTFS.
*/
addRoute(route) { addItems(this, 'routes', [route]); }
/**
* Adds a list of routes in the GTFS.
*
* @param {Array.<Object>} routes Array of routes to add in the GTFS.
*/
addRoutes(routes) { addItems(this, 'routes', routes); }
/**
* Apply a function to each route in the GTFS.
*
* @param {function} iterator Function which will be applied on every route.
*/
forEachRoute(iterator) { forEachItem(this, 'routes', iterator); }
/**
* Get the indexed routes of the GTFS. The indexation is defined in the schema (see schema.js).
*
* @return {Map.<string, Object>} Indexed routes.
*/
getIndexedRoutes() { return getIndexedTable(this, 'routes'); }
/**
* Get the number of routes defined
*
* @returns {number}
*/
getNumberOfRoutes() { return getNumberOfItemsInTable(this, 'routes'); }
/**
* Get the route using one of its grand child stopTime.
*
* @param {Object} stopTime A stopTime of the GTFS.
* @return {Object} The grand parent route of the stopTime.
*/
getRouteOfStopTime(stopTime) { return getters.getGrandParentItem(stopTime, 'trips', 'routes', this); }
/**
* Get a sample route.
*
* WARNING: This is not a random route. With the current implementation, it will be the first one in inserting order.
*
* @returns {Object}
*/
getSampleRoute() { return getIndexedTable(this, 'routes').values().next().value; }
/**
* Get the route using one of its child trip.
*
* WARNING: Will return the route which is indexed with the trip.route_id of the trip passed as argument. If the
* internal value of the route's route_id has been changed but not it's indexing, the result will be wrong.
*
* @param {Object} trip A trip of the GTFS.
* @return {Object} The route indexed by the trip.route_id of the trip passed as parameter.
*/
getRouteOfTrip(trip) { return getters.getParentItem(trip, 'routes', this); }
/**
* Get the route using its route_id.
*
* WARNING: Will return the route which is indexed with the route_id passed as argument. If the internal value of
* the route's route_id has been changed but not it's indexing, the result will be wrong.
*
* @param {string} routeId Index of the route.
* @return {Object} The route indexed by the route_id passed as parameter.
*/
getRouteWithId(routeId) { return getters.getItemWithIndex(routeId, 'routes', this); }
/**
* Removes a route of the GTFS.
*
* WARNING: It will remove the route indexed by the `route.route_id` of the route passed as parameter.
*
* @param {Object} route Route to remove of the GTFS.
*/
removeRoute(route) { removeItems(this, 'routes', [route]); }
/**
* Removes a list of routes of the GTFS.
*
* WARNING: It will remove the routes indexed by the `route.route_id` of the routes passed as parameter.
*
* @param {Array.<Object>} routes Routes to remove of the GTFS.
*/
removeRoutes(routes) { removeItems(this, 'routes', routes); }
/**
* Reset the map of indexed routes.
*/
resetRoutes() { resetTable(this, 'routes'); }
/**
* Set the map of indexed routes.
*
* WARNING: The Map should be indexed as defined in schema.js
*
* @param {Map.<string, Object>} indexedRoutes Map of routes properly indexed (see schema.js).
*/
setIndexedRoutes(indexedRoutes) { setTable(this, 'routes', indexedRoutes); }
/* trips.txt */
/**
* Adds a trip in the GTFS.
*
* @param {Object} trip Trip to add in the GTFS.
*/
addTrip(trip) { addItems(this, 'trips', [trip]); }
/**
* Adds a list of trips in the GTFS.
*
* @param {Array.<Object>} trips Array of trips to add in the GTFS.
*/
addTrips(trips) { addItems(this, 'trips', trips); }
/**
* Apply a function to each trip in the GTFS.
*
* @param {function} iterator Function which will be applied on every trip.
*/
forEachTrip(iterator) { forEachItem(this, 'trips', iterator); }
/**
* Get the indexed trips of the GTFS. The indexation is defined in the schema (see schema.js).
*
* @return {Map.<string, Object>} Indexed trips.
*/
getIndexedTrips() { return getIndexedTable(this, 'trips'); }
/**
* Get the number of trips defined
*
* @returns {number}
*/
getNumberOfTrips() { return getNumberOfItemsInTable(this, 'trips'); }
/**
* Get a sample trip.
*
* WARNING: This is not a random trip. With the current implementation, it will be the first one in inserting order.
*
* @returns {Object}
*/
getSampleTrip() { return getIndexedTable(this, 'trips').values().next().value; }
/**
* Get the trip using one of its child stopTime.
*
* WARNING: Will return the trip which is indexed with the stopTime.trip_id of the stopTime passed as argument. If the
* internal value of the trip's trip_id has been changed but not it's indexing, the result will be wrong.
*
* @param {Object} stopTime A stopTime of the GTFS.
* @return {Object} The trip indexed by the stopTime.trip_id of the stopTime passed as parameter.
*/
getTripOfStopTime(stopTime) { return getters.getParentItem(stopTime, 'trips', this); }
/**
* Get the trip using its trip_id.
*
* WARNING: Will return the trip which is indexed with the trip_id passed as argument. If the internal value of
* the trip's trip_id has been changed but not it's indexing, the result will be wrong.
*
* @param {string} tripId Index of the trip.
* @return {Object} The trip indexed by the trip_id passed as parameter.
*/
getTripWithId(tripId) { return getters.getItemWithIndex(tripId, 'trips', this); }
/**
* Removes a trip of the GTFS.
*
* WARNING: It will remove the trip indexed by the `trip.trip_id` of the trip passed as parameter.
*
* @param {Object} trip Trip to remove of the GTFS.
*/
removeTrip(trip) { removeItems(this, 'trips', [trip]); }
/**
* Removes a list of trips of the GTFS.
*
* WARNING: It will remove the trips indexed by the `trip.trip_id` of the trips passed as parameter.
*
* @param {Array.<Object>} trips Trips to remove of the GTFS.
*/
removeTrips(trips) { removeItems(this, 'trips', trips); }
/**
* Set the map of indexed trips.
*
* WARNING: The Map should be indexed as defined in schema.js
*
* @param {Map.<string, Object>} indexedTrips Map of trips properly indexed (see schema.js).
*/
setIndexedTrips(indexedTrips) { setTable(this, 'trips', indexedTrips); }
/**
* Reset the map of indexed trips.
*/
resetTrips() { resetTable(this, 'trips'); }
/* stop_times.txt */
/**
* Adds a stopTime in the GTFS.
*
* @param {Object} stopTime StopTime to add in the GTFS.
*/
addStopTime(stopTime) { addItems(this, 'stop_times', [stopTime]); }
/**
* Adds a list of stopTimes in the GTFS.
*
* @param {Array.<Object>} stopTimes Array of stopTimes to add in the GTFS.
*/
addStopTimes(stopTimes) { addItems(this, 'stop_times', stopTimes); }
/**
* Apply a function to each stopTime in the GTFS.
*
* @param {function} iterator Function which will be applied on every stopTime.
*/
forEachStopTime(iterator) { forEachItem(this, 'stop_times', iterator); }
/**
* Apply a function to each stopTime of a specific trip in the GTFS.
*
* @param {Object} trip Trip scoping the stopTime in which to enumerate.
* @param {function} iterator Function which will be applied on every stopTime of the trip.
*/
forEachStopTimeOfTrip(trip, iterator) {
const stopTimeByStopSequence = this.getStopTimeByStopSequenceOfTrip(trip);
if (stopTimeByStopSequence instanceof Map) {
stopTimeByStopSequence.forEach(iterator);
}
}
/**
* Apply a function to each stopTime of a specific trip id in the GTFS.
*
* @param {string} tripId Trip id scoping the stopTime in which to enumerate.
* @param {function} iterator Function which will be applied on every stopTime of the trip.
*/
forEachStopTimeOfTripId(tripId, iterator) {
const stopTimeByStopSequenceByTripId = this.getIndexedStopTimes();
const stopTimeByStopSequence = stopTimeByStopSequenceByTripId.get(tripId);
if (stopTimeByStopSequence instanceof Map) {
stopTimeByStopSequence.forEach(iterator);
}
}
/**
* Get the indexed stopTimes of the GTFS. The indexation is defined in the schema (see schema.js).
*
* @return {Map.<string, Map.<string, Object>>} Indexed stopTimes.
*/
getIndexedStopTimes() { return getIndexedTable(this, 'stop_times'); }
/**
* Get the number of stop time defined for one specific trip
*
* @param {Object} trip Trip scoping the stop times to count.
* @returns {number}
*/
getNumberOfStopTimeOfTrip(trip) {
const stopTimeByStopSequence = this.getStopTimeByStopSequenceOfTrip(trip);
return (stopTimeByStopSequence instanceof Map) ? stopTimeByStopSequence.size : 0;
}
/**
* Get the number of stop time defined for one specific trip id
*
* @param {string} tripId Trip id scoping the stop times to count.
* @returns {number}
*/
getNumberOfStopTimeOfTripId(tripId) {
const stopTimeByStopSequenceByTripId = this.getIndexedStopTimes();
const stopTimeByStopSequence = stopTimeByStopSequenceByTripId.get(tripId);
return (stopTimeByStopSequence instanceof Map) ? stopTimeByStopSequence.size : 0;
}
/**
* Get the child stopTimes of a trip.
*
* @param {Object} trip The parent trip.
* @return {Map.<string, Object>} Indexed child stopTimes.
*/
getStopTimeByStopSequenceOfTrip(trip) { return getters.getIndexedItemsWithParent(trip, 'stop_times', this); }
/**
* Get a stopTime using its indexes: the tripId and the stopSequence.
*
* @param {string} tripId First index of the stopTime
* @param {string} stopSequence Second index of the stopTime
* @return {Object} StopTime object
*/
getStopTimeWithTripIdAndStopSequence(tripId, stopSequence) {
return getters.getItemWithIndexes(tripId, stopSequence, 'stop_times', this);
}
/**
* Removes a stopTime of the GTFS.
*
* @param {Object} stopTime StopTime to remove of the GTFS.
*/
removeStopTime(stopTime) { removeItems(this, 'stop_times', [stopTime]); }
/**
* Removes a list of stopTimes of the GTFS.
*
* @param {Array.<Object>} stopTimes StopTimes to remove of the GTFS.
*/
removeStopTimes(stopTimes) { removeItems(this, 'stop_times', stopTimes); }
/**
* Set the map of indexed stopTimes.
*
* WARNING: The Map should be indexed as defined in schema.js
*
* @param {Map.<string, Map.<string, Object>>} indexedStopTimes Map of stopTimes properly indexed (see schema.js).
*/
setIndexedStopTimes(indexedStopTimes) { setTable(this, 'stop_times', indexedStopTimes); }
/**
* Reset the map of indexed stop times.
*/
resetStopTimes() { resetTable(this, 'stop_times'); }