Skip to content

Commit

Permalink
feat: make publicGoogleSheetsParser() available for an instance or op…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Gumball12 committed Mar 10, 2024
1 parent 79a721c commit ebabf2c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 36 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ generateDtsFile({
}

parser: publicGoogleSheetsParser(
new PublicGoogleSheetsParser(/* ... */),
{
spreadsheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo',
},
{
path: ['Key', 'Property'],
typeName: 'Type',
Expand Down Expand Up @@ -156,7 +158,9 @@ generateDtsFile({
}

parser: publicGoogleSheetsParser(
new PublicGoogleSheetsParser(/* ... */),
{
spreadsheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo',
},
{
path: ['Key', 'Property'],
typeName: 'Type',
Expand Down
17 changes: 16 additions & 1 deletion src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('publicGoogleSheetsParser', () => {
},
);

it('Common forms', async () => {
it('Common forms :: Pass instance', async () => {
const parsed = await publicGoogleSheetsParser(
publicGoogleSheetsParserInstance,
{
Expand All @@ -50,6 +50,21 @@ describe('publicGoogleSheetsParser', () => {
expect(parsed).toEqual(expected);
});

it('Common forms :: Pass options', async () => {
const parsed = await publicGoogleSheetsParser(
{
spreadsheetId: SPREADSHEET_ID,
sheetInfo: SHEET_NAME,
},
{
path: ['Key', 'Property'],
typeName: 'Type',
},
)();

expect(parsed).toEqual(expected);
});

it('With empty lines', async () => {
const parsed = await publicGoogleSheetsParser(
publicGoogleSheetsParserInstance,
Expand Down
73 changes: 46 additions & 27 deletions src/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import PublicGoogleSheetsParser from 'public-google-sheets-parser';

// Define parser
const parser = publicGoogleSheetsParser(
new PublicGoogleSheetsParser(/* ... */),
{
spreadsheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo',
},
{
path: ['Key', 'Property'],
typeName: 'Type',
Expand All @@ -38,15 +40,32 @@ generateDtsFile({

```ts
function publicGoogleSheetsParser(
publicGoogleSheetsParserInstance: PublicGoogleSheetsParser,
instanceOrOptions: PublicGoogleSheetsParser | PublicGoogleSheetsParserOptions,
params: {
path: string[];
typeName: string;
},
): Parser;

interface PublicGoogleSheetsParserOptions {
spreadsheetId?: string;
sheetInfo?:
| SheetName
| {
sheetName?: SheetName;
sheetId?: GID;
};
}

type SheetName = string;
type GID = string;
```

- `publicGoogleSheetsParserInstance`: An instance of [`PublicGoogleSheetsParser`](https://github.com/fureweb-com/public-google-sheets-parser?tab=readme-ov-file#usage-example)
- `instanceOrOptions`: An instance of [`PublicGoogleSheetsParser`](https://github.com/fureweb-com/public-google-sheets-parser?tab=readme-ov-file#usage-example) or an object with the following properties:
- `spreadsheetId`: The ID of the Google Sheets
- `sheetInfo`: The sheet name or an object with the following properties:
- `sheetName`: The sheet name
- `sheetId`: The sheet ID
- `path`: List of column names where object property names exists
- `typeName`: Column name where the type name exists

Expand All @@ -61,17 +80,17 @@ For example, given the following Google Sheets:
If `path` is `['Key', 'Property']` and `typeName` is `Type`, the return will look like this:

```ts
{
({
key1: {
property1: "'type1'";
}
property1: "'type1'",
},
key2: {
property2: "'type2'";
}
property2: "'type2'",
},
key3: {
property3: 'MyType';
}
}
property3: 'MyType',
},
});
```

## google-spreadsheet (node-google-spreadsheet)
Expand Down Expand Up @@ -139,11 +158,11 @@ The object property value becomes the type name. This can also be arbitrarily as
If you want the generated type to point to an actual type, just pass the type name as a string.

```ts
{
({
key: {
property: 'MyType' | 'YourType';
}
}
property: 'MyType' | 'YourType',
},
});
```

```ts
Expand All @@ -161,11 +180,11 @@ declare global {
If you want the generated type to be a string type, pass a string wrapped in quotes (`''` or `""`).

```ts
{
({
key: {
property: "'MyType' | 'YourType'";
}
}
property: "'MyType' | 'YourType'",
},
});
```

```ts
Expand All @@ -183,11 +202,11 @@ declare global {
These can be mixed and matched.

```ts
{
({
key: {
property: 'MyType' | "'YourType'";
}
}
property: 'MyType' | "'YourType'",
},
});
```

```ts
Expand Down Expand Up @@ -223,14 +242,14 @@ filledDataToObject(
({
key1: {
property1: 'type1',
}
},
key2: {
property2: 'type2',
}
},
key3: {
property3: 'MyType',
}
})
},
});
```

**Type definition:**
Expand Down
28 changes: 22 additions & 6 deletions src/parser/publicGoogleSheetsParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Parser for public-google-sheets-parser
// https://github.com/fureweb-com/public-google-sheets-parser

import type PublicGoogleSheetsParser from 'public-google-sheets-parser';
import PublicGoogleSheetsParser from 'public-google-sheets-parser';
import { FilledData } from './types/data';
import { filledDataToObject } from './utils/filledDataToObject';

Expand All @@ -10,13 +7,21 @@ type PublicGoogleSheetsParserParams = {
typeName: string;
};

type PublicGoogleSheetsParserOptions = {
spreadsheetId: ConstructorParameters<typeof PublicGoogleSheetsParser>[0];
sheetInfo: ConstructorParameters<typeof PublicGoogleSheetsParser>[1];
};

export const publicGoogleSheetsParser =
(
publicGoogleSheetsParserInstance: PublicGoogleSheetsParser,
instanceOrOptions:
| PublicGoogleSheetsParser
| PublicGoogleSheetsParserOptions,
{ path, typeName }: PublicGoogleSheetsParserParams,
) =>
async (): Promise<object> => {
const parsedData = await publicGoogleSheetsParserInstance.parse();
const instance = getPublicGoogleSheetsParserInstance(instanceOrOptions);
const parsedData = await instance.parse();
const filledData = parsedData.map<FilledData[number]>(
(item, index, data) => {
if (index === 0) {
Expand All @@ -35,3 +40,14 @@ export const publicGoogleSheetsParser =

return filledDataToObject(filledData, path, typeName);
};

const getPublicGoogleSheetsParserInstance = (
instanceOrOptions: PublicGoogleSheetsParser | PublicGoogleSheetsParserOptions,
) => {
if (instanceOrOptions instanceof PublicGoogleSheetsParser) {
return instanceOrOptions;
}

const { spreadsheetId, sheetInfo } = instanceOrOptions;
return new PublicGoogleSheetsParser(spreadsheetId, sheetInfo);
};

0 comments on commit ebabf2c

Please sign in to comment.