Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for timeframes.txt #158

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ export function getPathways(
options?: QueryOptions,
): SqlResults;

/**
* Returns an array of timeframes that match query parameters.
*/
export function getTimeframes(
query?: SqlWhere,
fields?: SqlSelect,
sortBy?: SqlOrderBy,
options?: QueryOptions,
): SqlResults;

/**
* Returns an array of transfers that match query parameters.
*/
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,17 @@ import { getPathways } from 'gtfs';
const pathways = getPathways();
```

#### getTimeframes(query, fields, sortBy, options)

Returns an array of timeframes that match query parameters. [Details on timeframes.txt](https://gtfs.org/schedule/reference/#timeframestxt)

```js
import { getTimeframes } from 'gtfs';

// Get all timeframes
const timeframes = getTimeframes();
```

#### getTransfers(query, fields, sortBy, options)

Returns an array of transfers that match query parameters. [Details on transfers.txt](https://gtfs.org/schedule/reference/#transferstxt)
Expand Down
4 changes: 4 additions & 0 deletions lib/gtfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getShapes, getShapesAsGeoJSON } from './gtfs/shapes.js';
import { getStopAreas } from './gtfs/stop-areas.js';
import { getStops, getStopsAsGeoJSON } from './gtfs/stops.js';
import { getStoptimes } from './gtfs/stop-times.js';
import { getTimeframes } from './gtfs/timeframes.js';
import { getTransfers } from './gtfs/transfers.js';
import { getTranslations } from './gtfs/translations.js';
import { getTrips } from './gtfs/trips.js';
Expand Down Expand Up @@ -134,6 +135,9 @@ export { _getStopsAsGeoJSON as getStopsAsGeoJSON };
const _getStoptimes = getStoptimes;
export { _getStoptimes as getStoptimes };

const _getTimeframes = getTimeframes;
export { _getTimeframes as getTimeframes };

const _getTransfers = getTransfers;
export { _getTransfers as getTransfers };

Expand Down
27 changes: 27 additions & 0 deletions lib/gtfs/timeframes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sqlString from 'sqlstring-sqlite';

import { openDb } from '../db.js';

import {
formatOrderByClause,
formatSelectClause,
formatWhereClauses,
} from '../utils.js';
import timeframes from '../../models/gtfs/timesframes.js';

/*
* Returns an array of all timeframes that match the query
*/
export function getTimeframes(query = {}, fields = [], orderBy = [], options = {}) {
const db = options.db ?? openDb();
const tableName = sqlString.escapeId(timeframes.filenameBase);
const selectClause = formatSelectClause(fields);
const whereClause = formatWhereClauses(query);
const orderByClause = formatOrderByClause(orderBy);

return db
.prepare(
`${selectClause} FROM ${tableName} ${whereClause} ${orderByClause};`
)
.all();
}
28 changes: 28 additions & 0 deletions models/gtfs/timesframes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const model = {
filenameBase: 'timesframes',
schema: [
{
name: 'timeframe_group_id',
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'start_time',
type: 'varchar(255)',
default: null,
},
{
name: 'end_time',
type: 'varchar(255)',
default: null,
},
{
name: 'service_id',
type: 'varchar(255)',
},
],
};

export default model;
2 changes: 2 additions & 0 deletions models/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import shapes from '../models/gtfs/shapes.js';
import stopAreas from '../models/gtfs/stop-areas.js';
import stopTimes from '../models/gtfs/stop-times.js';
import stops from '../models/gtfs/stops.js';
import timeframes from '../models/gtfs/timesframes.js';
import transfers from '../models/gtfs/transfers.js';
import translations from '../models/gtfs/translations.js';
import trips from '../models/gtfs/trips.js';
Expand Down Expand Up @@ -71,6 +72,7 @@ const models = [
stopAreas,
stopTimes,
stops,
timeframes,
transfers,
translations,
trips,
Expand Down
28 changes: 28 additions & 0 deletions test/mocha/get-timeframes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env mocha */

import should from 'should';

import config from '../test-config.js';
import { openDb, closeDb, importGtfs, getTimeframes } from '../../index.js';

describe('getTimeframes():', () => {
before(async () => {
openDb(config);
await importGtfs(config);
});

after(() => {
const db = openDb(config);
closeDb(db);
});

it('should return empty array if no timeframes', () => {
const timeframeGroupId = 'not_real';

const results = getTimeframes({
timeframe_group_id: timeframeGroupId,
});
should.exists(results);
results.should.have.length(0);
});
Comment on lines +19 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the included test dataset does not include timeframes.txt, so I was not able to write a test that confirms the actual database output of getTimeframes.

})