Skip to content

Commit

Permalink
feat: provide the filledDataToObject util function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumball12 committed Mar 2, 2024
1 parent f2f2ecf commit a932665
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
66 changes: 65 additions & 1 deletion src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { JWT } from 'google-auth-library';
import { GoogleSpreadsheet } from 'google-spreadsheet';
import PublicGoogleSheetsParser from 'public-google-sheets-parser';

import { publicGoogleSheetsParser, googleSpreadsheet } from '../parser';
import {
publicGoogleSheetsParser,
googleSpreadsheet,
filledDataToObject,
} from '../parser';

const expected = {
click_conversation_data: {
Expand Down Expand Up @@ -100,3 +104,63 @@ describe.skip('GoogleSpreadsheet', () => {
expect(parsed).toEqual(expected);
});
});

describe('filledDataToObject', () => {
it('Common forms', () => {
const filledData = [
{
Key: 'click_conversation_data',
Property: 'conversation_id',
Type: 'string',
},
{
Key: 'click_conversation_data',
Property: 'created_at',
Type: 'Date',
},
{
Key: 'click_conversation_data',
Property: 'agent_type',
Type: 'string',
},
{
Key: 'click_conversation_data',
Property: 'status',
Type: 'StatusEnum',
},
{
Key: 'click_conversation_data',
Property: 'generate_position',
Type: "conversation' | 'playground'",
},
{
Key: 'click_message_feedback_button',
Property: 'conversation_id',
Type: 'string',
},
{
Key: 'click_message_feedback_button',
Property: 'message_id',
Type: 'string',
},
{
Key: 'click_message_feedback_button',
Property: 'generate_position',
Type: '"conversation" | "playground"',
},
{
Key: 'click_message_feedback_button',
Property: 'my_test',
Type: "string | 'string'",
},
];

const transformed = filledDataToObject(
filledData,
['Key', 'Property'],
'Type',
);

expect(transformed).toEqual(expected);
});
});
46 changes: 46 additions & 0 deletions src/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,49 @@ declare global {
}
}
```

### `parser/filledDataToObject` Utility function

This utility function takes `FilledData` type data and converts it to an `object` type. It can be useful when creating parsers.

```ts
import { filledDataToObject } from 'google-spreadsheet-dts/parser';

filledDataToObject(
[
{ Key: 'key1', Property: 'property1', Type: 'type1' },
{ Key: 'key2', Property: 'property2', Type: 'type2', Other: 'other' },
{ Key: 'key3', Property: 'property3', Type: 'MyType' },
],
['Key', 'Property'],
'Type',
);

// Returns
({
key1: {
property1: 'type1',
}
key2: {
property2: 'type2',
}
key3: {
property3: 'MyType',
}
})
```

**Type definition:**

```ts
type FilledData = Record<string, string>[];
type Result = {
[key: string]: string | Result;
};

function filledDataToObject(
filledData: FilledData,
path: string[],
typeName: string,
): Result;
```
3 changes: 3 additions & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { publicGoogleSheetsParser } from './publicGoogleSheetsParser';
export { googleSpreadsheet } from './googleSpreadsheet';

// Utils
export { filledDataToObject } from './utils/filledDataToObject';

0 comments on commit a932665

Please sign in to comment.