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

feat: adding keywords to autocast #457

Merged
merged 12 commits into from
Apr 3, 2024
5 changes: 5 additions & 0 deletions .changeset/lovely-dogs-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-autocast': patch
---

Adding keywords
carlbrugger marked this conversation as resolved.
Show resolved Hide resolved
118 changes: 114 additions & 4 deletions plugins/autocast/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,117 @@
# @flatfile/autocast
# @flatfile/plugin-autocast
**This plugin automatically casts values in Flatfile to their appropriate types.**

carlbrugger marked this conversation as resolved.
Show resolved Hide resolved
This package automatically converts the initial user-inputted data to the correct type needed for that field so it can be processed.

## Get Started
The `@flatfile/plugin-autocast` plugin is an opinionated transformer that will
automatically convert the data in the Sheet to match the type defined by the
Blueprint.

Follow [this guide](https://flatfile.com/docs/plugins/transform/autocast) to learn how to use the plugin.

**Event Type:**
`listener.on('commit:created')`


**Supported field types:**
`number`, `boolean`, `date`


## Parameters

#### `sheetSlug` *string* (required)

The `sheetSlug` indicates the slug name of the sheet you want to monitor.

#### `fieldFilters` *string[]*

Use the `fieldFilters` parameter to select specific fields to monitor. Without
any specified `fieldFilters`, the plugin will automatically monitor
castable-field (number, boolean & Date).
carlbrugger marked this conversation as resolved.
Show resolved Hide resolved


#### `options.chunkSize` default="10_000" *number*
ashleygmulligan2 marked this conversation as resolved.
Show resolved Hide resolved
ashleygmulligan2 marked this conversation as resolved.
Show resolved Hide resolved
carlbrugger marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start the sentence with an uppercase letter for consistency.

- #### `options.chunkSize` default="10_000" *number*
+ #### `Options.chunkSize` default="10_000" *number*

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
#### `options.chunkSize` default="10_000" *number*
#### `Options.chunkSize` default="10_000" *number*


The `chunkSize` parameter allows you to specify the quantity of records to in
each chunk.

#### `options.parallel` default="1" *number*
ashleygmulligan2 marked this conversation as resolved.
Show resolved Hide resolved
ashleygmulligan2 marked this conversation as resolved.
Show resolved Hide resolved
carlbrugger marked this conversation as resolved.
Show resolved Hide resolved
ashleygmulligan2 marked this conversation as resolved.
Show resolved Hide resolved

The `parallel` parameter allows you to specify the number of chunks to process
in parallel.


## API Calls

- `api.sheets.get`

## Imported NPM Packages

- [`@flatfile/[email protected]+`](https://www.npmjs.com/package/@flatfile/api)
- [`@flatfile/[email protected]+`](https://www.npmjs.com/package/@flatfile/listener)
- [`@flatfile/[email protected]+`](./record-hook)
- `@flatfile/[email protected]+`

## Usage

The `autocast` plugin will listen for the `commit:created` event and cast numbers, booleans,
carlbrugger marked this conversation as resolved.
Show resolved Hide resolved
and dates to the appropriate Blueprint type. Note that the `recordHook` and `bulkRecordHook` plugins
listen for the same event type. Plugins will fire in the order they are placed in the listener.

### Strings

Numbers and booleans are transformed from strings to their respective types (i.e., `'1'` to `1`, `"true"` to `true`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify the transformation of booleans to strings for consistency.

- Numbers and booleans are transformed from strings to their respective types (i.e., `'1'` to `1`, `"true"` to `true`).
+ Numbers and booleans are cast from strings to their respective types (i.e., `'1'` to `1`, `"true"` to `true`).

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
Numbers and booleans are transformed from strings to their respective types (i.e., `'1'` to `1`, `"true"` to `true`).
Numbers and booleans are cast from strings to their respective types (i.e., `'1'` to `1`, `"true"` to `true`).


### Numbers

String numbers (i.e `'1'`), string decimals (i.e `'1.1'`), and string numbers with commas (i.e `'1,000'`)
are interpreted as numbers.

### Booleans

`'1'`, `'yes'`, `'true'`, `'on'`, `'t'`, `'y'`, and `1` are interpreted as truthy values.

`'-1'`, `'0'`, `'no'`, `'false'`, `'off'`, `'f'`, `'n'`, `0`, `-1` are interpreted as falsy values.

### Dates

Date strings and numbers are cast to a UTC string. These are examples of dates that will be cast (note: `YYYY-MM-DD...` is interpreted as an ISO 8601 date and is treated as UTC, while other formats are treated as local time and converted to UTC):

- `'2023-08-16'` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
- `'08-16-2023'` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
- `'08/16/2023'` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
- `'Aug 16, 2023'` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
- `'August 16, 2023'` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
- `'2023-08-16T00:00:00.000Z'` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
- `1692144000000` => `'Wed, 16 Aug 2023 00:00:00 GMT'`
ashleygmulligan2 marked this conversation as resolved.
Show resolved Hide resolved

**install**
```bash
npm i @flatfile/plugin-autocast
```

**import**
```js
import { autocast } from "@flatfile/plugin-autocast";
```

**listener.js**
```js
listener.use(autocast("sheetSlug"));
```
**listener.js w/ fieldFilters**
```js
listener.use(autocast("sheetSlug", ["numberField", "dateField"]));
```
**listener.js w/ fieldFilters & options**
```js
listener.use(
autocast("sheetSlug", ["numberField", "dateField"], {
chunkSize: 10_000,
parallel: 2,
})
);
```


## See the code

https://github.com/FlatFilers/flatfile-plugins/tree/main/plugins/autocast
2 changes: 1 addition & 1 deletion plugins/autocast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"check": "tsc ./**/*.ts --noEmit --esModuleInterop",
"test": "jest ./**/*.spec.ts --config=../../jest.config.js --runInBand"
},
"keywords": [],
"keywords": ["flatfile-plugins", "category-transform", "featured"],
"author": "Alex Hollenbeck",
"repository": {
"type": "git",
Expand Down
Loading