Skip to content

Commit

Permalink
Add functions: resetTable, getNumberOfItemInTable, resetFrequencies, …
Browse files Browse the repository at this point in the history
…resetAgencies, resetStops, resetTransfers
  • Loading branch information
Laurent Petit authored and Laurent Petit committed Mar 15, 2018
1 parent 14a449b commit a9c8edf
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ typings/
.env

.idea/

# VS code configuration
.vscode
99 changes: 80 additions & 19 deletions gtfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function addItems(gtfs, tableName, items) {
});
}
}

/**
* 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).
Expand Down Expand Up @@ -150,6 +149,35 @@ function getSizeOfMap(map) {
return (map instanceof Map) ? map.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) {
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 deepness = gtfs._schema.deepnessByTableName[tableName];

if (deepness !== 1) {
throw new Error(`'${tableName}' has a deepness of ${deepness}. \
getNumberOfItemsInTable can only apply on a first level table.`);
}

const indexedTable = gtfs.getIndexedTable(tableName);

return getSizeOfMap(indexedTable);
}

class Gtfs {
/**
* Constructor of the GTFS
Expand Down Expand Up @@ -335,6 +363,13 @@ class Gtfs {
*/
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.
*
Expand All @@ -360,6 +395,14 @@ class Gtfs {
*/
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.
*
Expand All @@ -368,7 +411,6 @@ class Gtfs {
*/
setIndexedItemsAsTable(indexedItems, tableName) { setIndexedItems(this, tableName, indexedItems); }


/* agency.txt */

/**
Expand Down Expand Up @@ -439,6 +481,11 @@ class Gtfs {
*/
removeAgencies(agencies) { removeItems(this, 'agency', agencies); }

/**
* Reset the map of indexed agencies.
*/
resetAgencies() { resetTable(this, 'agency'); }

/**
* Set the map of indexed agencies.
*
Expand Down Expand Up @@ -519,6 +566,11 @@ class Gtfs {
*/
removeStops(stops) { removeItems(this, 'stops', stops); }

/**
* Reset the map of indexed stops.
*/
resetStops() { resetTable(this, 'stops'); }

/**
* Set the map of indexed stops.
*
Expand Down Expand Up @@ -564,7 +616,7 @@ class Gtfs {
*
* @returns {number}
*/
getNumberOfRoutes() { return getIndexedTable(this, 'routes').size; }
getNumberOfRoutes() { return getNumberOfItemsInTable(this, 'routes'); }

/**
* Get the route using one of its grand child stopTime.
Expand Down Expand Up @@ -623,6 +675,11 @@ class Gtfs {
*/
removeRoutes(routes) { removeItems(this, 'routes', routes); }

/**
* Reset the map of indexed routes.
*/
resetRoutes() { resetTable(this, 'routes'); }

/**
* Set the map of indexed routes.
*
Expand All @@ -632,12 +689,6 @@ class Gtfs {
*/
setIndexedRoutes(indexedRoutes) { setIndexedItems(this, 'routes', indexedRoutes); }

/**
* Reset the map of indexed routes.
*/
resetRoutes() { setIndexedItems(this, 'routes', new Map()); }


/* trips.txt */

/**
Expand Down Expand Up @@ -736,7 +787,7 @@ class Gtfs {
/**
* Reset the map of indexed trips.
*/
resetTrips() { setIndexedItems(this, 'trips', new Map()); }
resetTrips() { resetTable(this, 'trips'); }


/* stop_times.txt */
Expand Down Expand Up @@ -865,7 +916,7 @@ class Gtfs {
/**
* Reset the map of indexed stop times.
*/
resetStopTimes() { setIndexedItems(this, 'stop_times', new Map()); }
resetStopTimes() { resetTable(this, 'stop_times'); }


/* calendar.txt */
Expand Down Expand Up @@ -949,7 +1000,7 @@ class Gtfs {
/**
* Reset the map of indexed calendars.
*/
resetCalendars() { setIndexedItems(this, 'calendar', new Map()); }
resetCalendars() { resetTable(this, 'calendar'); }

/**
* Set the map of indexed calendars.
Expand Down Expand Up @@ -990,7 +1041,7 @@ class Gtfs {
* @param {Object} trip Trip scoping the calendar dates in which to enumerate.
* @param {function} iterator Function which will be applied on every calendar dates of the trip.
*/
forEachGtfsCalendarDateOfTrip(trip, iterator) {
forEachCalendarDateOfTrip(trip, iterator) {
const calendarDateByDate = this.getCalendarDateByDateOfTrip(trip);

if (calendarDateByDate instanceof Map) {
Expand All @@ -1004,7 +1055,7 @@ class Gtfs {
* @param {string} serviceId Service id scoping the calendar dates in which to enumerate.
* @param {function} iterator Function which will be applied on every calendar dates of the service.
*/
forEachGtfsCalendarDateWithServiceId(serviceId, iterator) {
forEachCalendarDateWithServiceId(serviceId, iterator) {
const calendarDateByDateByServiceId = this.getIndexedCalendarDates();
const calendarDateByDate = calendarDateByDateByServiceId.get(serviceId);

Expand Down Expand Up @@ -1055,7 +1106,7 @@ class Gtfs {
* @param {string} serviceId Service id to for which to check if calendar dates are defined.
* @returns {boolean}
*/
hasGtfsCalendarDatesWithServiceId(serviceId) {
hasCalendarDatesWithServiceId(serviceId) {
const calendarDateByDateByServiceId = this.getIndexedCalendarDates();
const calendarDateByDate = calendarDateByDateByServiceId.get(serviceId);

Expand All @@ -1077,9 +1128,9 @@ class Gtfs {
removeCalendarDates(calendarDates) { removeItems(this, 'calendar_dates', calendarDates); }

/**
* Reset the map of indexed calendars.
* Reset the map of indexed calendars dates.
*/
resetCalendarDates() { setIndexedItems(this, 'calendar_dates', new Map()); }
resetCalendarDates() { resetTable(this, 'calendar_dates'); }

/**
* Set the map of indexed calendarDates.
Expand Down Expand Up @@ -1130,7 +1181,7 @@ class Gtfs {
* @param {string} shapeId Service id scoping the calendar dates in which to enumerate.
* @param {function} iterator Function which will be applied on every calendar dates of the service.
*/
forEachGtfsShapePointOfShapeId(shapeId, iterator) {
forEachShapePointOfShapeId(shapeId, iterator) {
const shapePointByShapePointSequenceByShapeId = this.getIndexedShapePoints();
const shapePointByShapePointSequence = shapePointByShapePointSequenceByShapeId.get(shapeId);

Expand Down Expand Up @@ -1192,7 +1243,7 @@ class Gtfs {
/**
* Reset the shape points.
*/
resetShapePoints() { setIndexedItems(this, 'shapes', new Map()); }
resetShapePoints() { resetTable(this, 'shapes'); }

/**
* Set the map of indexed shapePoints.
Expand Down Expand Up @@ -1259,6 +1310,11 @@ class Gtfs {
*/
removeFrequencies(frequencies) { removeItems(this, 'frequencies', frequencies); }

/**
* Reset the map of indexed frequencies.
*/
resetFrequencies() { resetTable(this, 'frequencies'); }

/**
* Set the map of indexed frequencies.
*
Expand Down Expand Up @@ -1324,6 +1380,11 @@ class Gtfs {
*/
removeTransfers(transfers) { removeItems(this, 'transfers', transfers); }

/**
* Reset the map of indexed transfers.
*/
resetTransfers() { resetTable(this, 'transfers'); }

/**
* Set the map of indexed transfers.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/test_calendar_dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ describe('Tests on GTFS calendar dates', () => {
done();
});

it('Tests on gtfs.forEachCalendarDateWithServiceId(serviceId, iterator)', (done) => {
it('Tests on gtfs.hasCalendarDatesWithServiceId(serviceId, iterator)', (done) => {
const gtfs = new Gtfs(`${__dirname}/samples/1`);

expect(gtfs.hasGtfsCalendarDatesWithServiceId('service_1')).to.equal(false);
expect(gtfs.hasCalendarDatesWithServiceId('service_1')).to.equal(false);

gtfs.setIndexedCalendarDates(new Map([
['service_0', new Map([
Expand All @@ -126,7 +126,7 @@ describe('Tests on GTFS calendar dates', () => {
])],
]));

expect(gtfs.forEachCalendarDateWithServiceId('service_1')).to.equal(true);
expect(gtfs.hasCalendarDatesWithServiceId('service_1')).to.equal(true);

done();
});
Expand Down
1 change: 0 additions & 1 deletion tests/test_generic_table_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ describe('Tests on GTFS generic table functions', () => {
expect(() => gtfs.getNumberOfItemsInTable('stop_times')).to.throw();

done();
done();
});
});

Expand Down
2 changes: 1 addition & 1 deletion tests/test_shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Tests on GTFS shapes', () => {

const shapePointSequencesForShape1 = [];
gtfs.forEachShapePointOfShapeId('shape_1', (calendarDates) => {
shapePointSequencesForShape1.push(calendarDates.exception_type);
shapePointSequencesForShape1.push(calendarDates.shape_pt_sequence);
});
expect(shapePointSequencesForShape1.sort()).to.deep.equal(['6', '7']);

Expand Down

0 comments on commit a9c8edf

Please sign in to comment.