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

Handle """ + add test #6

Merged
merged 1 commit into from
May 23, 2018
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
8 changes: 7 additions & 1 deletion helpers/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ function fromCsvStringToArray(string, tableName) {
}

if (!reValid.test(string)) {
if (string.match(/""/)) {
if (string.match(/,"""/)) { // Handle the case with 3 " at start/end of string
string = string.replace(/,"""/g, ',"\\"');
return fromCsvStringToArray(string, tableName);
} else if (string.match(/""",/)) {
string = string.replace(/""",/g, '\\"",');
return fromCsvStringToArray(string, tableName);
} else if (string.match(/""/)) {
string = string.replace(/""/g, '\\"');
return fromCsvStringToArray(string, tableName);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/samples/1/routes.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
agency_id,route_id,route_short_name,route_long_name,route_type
agency_0,route_0,R0,Route 0,3
agency_0,route_0,R0,Route 0,3
agency_0,route_x,RX,"""Route X""",3
10 changes: 5 additions & 5 deletions tests/test_generic_table_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ describe('Tests on GTFS generic table functions', () => {
const route0 = gtfs.getRouteWithId('route_0');

gtfs.addItemInTable({ route_id: 'route_1', route_long_name: 'Route 1' }, ROUTE_TABLE_NAME);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_x']);

gtfs.addItemsInTable([
{ route_id: 'route_2', route_long_name: 'Route 2' },
{ route_id: 'route_3', route_long_name: 'Route 3' },
], ROUTE_TABLE_NAME);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3', 'route_x']);

gtfs.removeItemInTable(gtfs.getRouteWithId('route_2'), ROUTE_TABLE_NAME);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3', 'route_x']);

gtfs.removeItemsInTable([gtfs.getRouteWithId('route_0'), gtfs.getRouteWithId('route_3')], ROUTE_TABLE_NAME);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1', 'route_x']);

gtfs.setIndexedItemsAsTable(new Map([['route_0', route0]]), ROUTE_TABLE_NAME);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0']);
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('Tests on GTFS generic table functions', () => {
it('Test on gtfs.getNumberOfItemsInTable(tableName)', (done) => {
const gtfs = new Gtfs(`${__dirname}/samples/1`);

expect(gtfs.getNumberOfItemsInTable('routes')).to.equal(1);
expect(gtfs.getNumberOfItemsInTable('routes')).to.equal(2);

gtfs.resetRoutes();
expect(gtfs.getNumberOfItemsInTable('routes')).to.equal(0);
Expand Down
6 changes: 4 additions & 2 deletions tests/test_gtfs_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ describe('Tests on GTFS constructor options', () => {
expect(String(routesTxt)).to.equal(
'route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,' +
'route_text_color,route_sort_order,temp\n' +
'route_0,agency_0,R0,Route 0,,3,,,,,some value\n'
'route_0,agency_0,R0,Route 0,,3,,,,,some value\n' +
'route_x,agency_0,RX,"""Route X""",,3,,,,,some value\n'
);

fs.remove(outputPath, (removeError) => {
Expand Down Expand Up @@ -90,7 +91,8 @@ describe('Tests on GTFS constructor options', () => {
expect(String(routesTxt)).to.equal(
'route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,' +
'route_text_color,route_sort_order,temp\n' +
'route_0,agency_0,R0,Route 0,,3,,,,,"{""key"":""value""}"\n'
'route_0,agency_0,R0,Route 0,,3,,,,,"{""key"":""value""}"\n' +
'route_x,agency_0,RX,"""Route X""",,3,,,,,"{""key"":""value""}"\n'
);

fs.remove(outputPath, (removeError) => {
Expand Down
23 changes: 13 additions & 10 deletions tests/test_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ describe('Tests on GTFS routes', () => {
const path = `${__dirname}/samples/1`;
const gtfs = new Gtfs(path);

expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_x']);

const route0 = gtfs.getRouteWithId('route_0');
expect(route0.route_long_name).to.equal('Route 0');

gtfs.addRoute({route_id: 'route_1', route_long_name: 'Route 1'});
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1']);
const routeX = gtfs.getRouteWithId('route_x');
expect(routeX.route_long_name).to.equal('"Route X"');

gtfs.addRoute({ route_id: 'route_1', route_long_name: 'Route 1' });
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_x']);

gtfs.addRoutes([
{route_id: 'route_2', route_long_name: 'Route 2'},
{route_id: 'route_3', route_long_name: 'Route 3'},
{ route_id: 'route_2', route_long_name: 'Route 2' },
{ route_id: 'route_3', route_long_name: 'Route 3' },
]);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3', 'route_x']);

gtfs.removeRoute(gtfs.getRouteWithId('route_2'));
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3', 'route_x']);

gtfs.removeRoutes([gtfs.getRouteWithId('route_0'), gtfs.getRouteWithId('route_3')]);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1']);
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1', 'route_x']);

gtfs.setIndexedRoutes(new Map([['route_0', route0]]));
expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0']);
Expand All @@ -51,7 +54,7 @@ describe('Tests on GTFS routes', () => {
it('Tests on gtfs.getNumberOfRoutes()', (done) => {
const gtfs = new Gtfs(`${__dirname}/samples/1`);

expect(gtfs.getNumberOfRoutes()).to.equal(1);
expect(gtfs.getNumberOfRoutes()).to.equal(2);

gtfs.resetRoutes();

Expand All @@ -74,7 +77,7 @@ describe('Tests on GTFS routes', () => {
it('Tests on gtfs.resetRoutes()', (done) => {
const gtfs = new Gtfs(`${__dirname}/samples/1`);

expect(gtfs.getIndexedRoutes().size).to.equal(1);
expect(gtfs.getIndexedRoutes().size).to.equal(2);

gtfs.resetRoutes();

Expand Down
3 changes: 2 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ describe('Tests on GTFS', () => {
expect(String(routesTxt)).to.equal(
'route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,' +
'route_text_color,route_sort_order,some_extra_route_field\n' +
'route_0,agency_0,R0,Route 0,Some new description,3,,,,,some_extra_route_value\n'
'route_0,agency_0,R0,Route 0,Some new description,3,,,,,some_extra_route_value\n' +
'route_x,agency_0,RX,"""Route X""",Some new description,3,,,,,some_extra_route_value\n'
);

fs.readFile(`${outputPath}feed_info.txt`, (readFeedInfoError, feedInfoTxt) => {
Expand Down