Skip to content

Commit

Permalink
ensure undefined values are converted to nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
butlerx committed Feb 20, 2021
1 parent 69f084f commit 77e918c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-google-spreadsheets",
"version": "3.0.7",
"version": "3.0.8",
"description": "A source plugin for Gatsby that allows reading data from Google Sheets.",
"main": "index.js",
"scripts": {
Expand Down
27 changes: 27 additions & 0 deletions src/fetchSheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ if (withPrivateKey == it.skip) {
);
}

declare global {
namespace jest {
interface Matchers<R> {
toBeBoolean(): R;
}
}
}

expect.extend({
toBeBoolean(received) {
return {
message: () => `expected ${received} to be boolean or null`,
pass: typeof received === 'boolean',
};
},
});

describe('fetching remote sheet from google', () => {
withAPIKey('public sheets require API credential', async () => {
const sheet = await fetchSheet(SPREADSHEET_ID, undefined, GOOGLE_API_KEY);
Expand Down Expand Up @@ -56,4 +73,14 @@ describe('fetching remote sheet from google', () => {
expect(sheet.id).toBeDefined();
expect(sheet.inventory).toBeDefined();
});

withAPIKey('undefineds on sheets to be correct', async () => {
const sheet = await fetchSheet(
'1QfQ-DvRe1O-w2t87AIA_XcQFgMEwylrVVeBetZW9plI',
undefined,
GOOGLE_API_KEY,
);
expect(sheet).toBeDefined();
expect(sheet.id).toBeDefined();
});
});
36 changes: 31 additions & 5 deletions src/fetchSheet/cleanRows.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,45 @@ describe('cleaning rows from GSheets response', () => {
},
{
number: '2',
string: 'nothing',
string: '',
null: null,
'boolean-value': null,
},
{
number: null,
string: null,
null: null,
'boolean-value': null,
},
{
number: undefined,
string: undefined,
null: undefined,
'boolean-value': undefined,
},
];
const cleaned = cleanRows(rows);
expect(cleaned.map(row => row.number)).toEqual([0, 1, 2]);
expect(cleaned.map(row => row.number)).toEqual([0, 1, 2, null, null]);
expect(cleaned.map(row => row.string)).toEqual([
'something',
'anything',
'nothing',
null,
null,
null,
]);
expect(cleaned.map(row => row.null)).toEqual([
null,
null,
null,
null,
null,
]);
expect(cleaned.map(row => row.booleanValue)).toEqual([
true,
false,
false,
false,
false,
]);
expect(cleaned.map(row => row.null)).toEqual([null, null, null]);
expect(cleaned.map(row => row.booleanValue)).toEqual([true, false, false]);
});
});
11 changes: 8 additions & 3 deletions src/fetchSheet/cleanRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ export const cleanRows = (rows: SpreadsheetRow[]): SpreadsheetRow[] => {
function convertCell(columnTypes: ColumnTypes, key: string, val: any): any {
switch (columnTypes[key]) {
case 'number':
return Number(val.replace(/,/g, ''));
return isNull(val) ? null : Number(val.replace(/,/g, ''));
case 'boolean':
// when column contains null we return false, otherwise check boolean value
return val === null ? false : val === 'TRUE';
return isNull(val) ? false : val === 'TRUE';
default:
return val === '' ? null : val;
// We cast all possible null types to actually be null
return isNull(val) ? null : val;
}
}

function isNull(val: any): boolean {
return val === null || val === undefined || val === '';
}

0 comments on commit 77e918c

Please sign in to comment.