Skip to content

Commit

Permalink
Expose function getActualKeysForTable on the gtfs object (with tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoFrachet committed Jan 22, 2018
1 parent d0ab992 commit b5a78ec
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 34 deletions.
9 changes: 9 additions & 0 deletions gtfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ class Gtfs {
*/
getTableNames() { return new Set([...schema.tableNames, ...this._tables.keys()]); }

/**
* 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); }

/**
* Get the parent item using one of its child.
*
Expand Down
35 changes: 1 addition & 34 deletions helpers/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ const schema = require('./schema');
* Private functions
*/

function getSample(iterable) {
return (iterable && iterable.values().next()) ? iterable.values().next().value : undefined;
}

function getHHmmss() {
const date = new Date();
return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
Expand Down Expand Up @@ -70,37 +66,8 @@ function copyUntouchedTable(inputPath, outputPath, tableName, callback) {
});
}

function getActualKeysForTable(gtfs, tableName) {
const keys = [...schema.keysByTableName[tableName]];
const deepness = schema.deepnessByTableName[tableName];
const table = gtfs.getIndexedTable(tableName);
let sampleItem;

if (deepness === 0) {
sampleItem = table;
} else if (deepness === 1) {
sampleItem = getSample(table);
} else if (deepness === 2) {
sampleItem = getSample(getSample(table));
}

if (sampleItem) {
Object.keys(sampleItem).forEach((key) => {
if (schema.keysByTableName[tableName].includes(key) === false) {
keys.push(key);
}
});
}

if (keys.length === 0) {
throw new Error(`No keys found for table ${tableName}`);
}

return keys;
}

function exportTable(tableName, gtfs, outputPath, callback) {
const keys = getActualKeysForTable(gtfs, tableName);
const keys = gtfs.getActualKeysForTable(tableName);
const outputFullPath = `${outputPath + tableName}.txt`;
const firstRow = `${keys.join(',')}\n`;

Expand Down
39 changes: 39 additions & 0 deletions helpers/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@

const schema = require('./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 {Gtfs} gtfs GTFS containing the table.
* @param {string} tableName Table of the GTFS of which we want to key.
* @return {Array.<string>} Keys used by the items of the table.
*/
function getActualKeysForTable(gtfs, tableName) {
const getSample = iterable => ((iterable && iterable.values().next()) ? iterable.values().next().value : undefined);
const keys = [...schema.keysByTableName[tableName]];
const deepness = schema.deepnessByTableName[tableName];
const table = gtfs.getIndexedTable(tableName);
let sampleItem;

if (deepness === 0) {
sampleItem = table;
} else if (deepness === 1) {
sampleItem = getSample(table);
} else if (deepness === 2) {
sampleItem = getSample(getSample(table));
}

if (sampleItem) {
Object.keys(sampleItem).forEach((key) => {
if (schema.keysByTableName[tableName].includes(key) === false) {
keys.push(key);
}
});
}

if (keys.length === 0) {
throw new Error(`No keys found for table ${tableName}`);
}

return keys;
}

/**
* Get the grand parent item using one of its child.
*
Expand Down Expand Up @@ -177,6 +215,7 @@ function getParentItem(itemWithForeignIndexId, tableName, gtfs) {
}

module.exports = {
getActualKeysForTable,
getGrandParentItem,
getIndexedItemsWithParent,
getIndexedItemsWithParentIndex,
Expand Down
20 changes: 20 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,26 @@ describe('Tests on GTFS', () => {

done();
});

// eslint-disable-next-line no-undef
it('Test getters helpers: getActualKeysForTable', (done) => {
const gtfs = new Gtfs();
const funkyStop = {};
gtfs.addStop(funkyStop);

expect(Gtfs.getSchema().keysByTableName.stops).to.deep.equal(gtfs.getActualKeysForTable('stops'));

funkyStop.route_funky_name = 'Tshboom tshboom';
funkyStop.route_esoteric_float = 120.37;
const standardRouteKeys = Gtfs.getSchema().keysByTableName.stops;
const actualRouteKeys = gtfs.getActualKeysForTable('stops');

expect(standardRouteKeys).to.not.deep.equal(actualRouteKeys);

expect([...standardRouteKeys, 'route_funky_name', 'route_esoteric_float']).to.deep.equal(actualRouteKeys);

done();
});
});

function sortedKeys(map) {
Expand Down

0 comments on commit b5a78ec

Please sign in to comment.