Skip to content

Commit

Permalink
Extended rename function arguments. 🧮
Browse files Browse the repository at this point in the history
Allows for content specific renaming. 🔬
  • Loading branch information
michaelbeutler committed Jun 28, 2022
1 parent 7ea127e commit f4603b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@iperka/scan-categorizer",
"description": "Categorize your scanned documents and organize them in folders.",
"version": "2.0.0",
"version": "2.0.2",
"main": "index.js",
"license": "MIT",
"author": {
Expand Down
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ namespace ScanCategorizer {
const rename =
typeof category.rename === 'string'
? category.rename
: category.rename({getName: () => 'TEMP'} as any);
: category.rename({
getName: () => 'TEMP',
getId: () => 'TEMP',
text: 'Lorem Ipsum',
} as any);

// Check if rename is valid.
if (!/\.pdf$/.test(rename)) {
Expand Down Expand Up @@ -163,7 +167,7 @@ namespace ScanCategorizer {

// Handle first category matched.
Logger.log('Apply category: ' + sorted[0].name);
handleMatch(file, sorted[0]);
handleMatch({...file, text}, sorted[0]);

if (sorted.length > 1) {
for (let i = 1; i < sorted.length; i++) {
Expand All @@ -180,7 +184,7 @@ namespace ScanCategorizer {
}

Logger.log('Creating shortcuts for category: ' + category.name);
handleMatch(file, category, true);
handleMatch({...file, text}, category, true);
}
}

Expand All @@ -190,7 +194,7 @@ namespace ScanCategorizer {
};

const handleMatch = (
file: GoogleAppsScript.Drive.File,
file: GoogleAppsScript.Drive.File & {text: string},
category: Query.Category,
onlyShortcut?: boolean,
) => {
Expand All @@ -201,7 +205,7 @@ namespace ScanCategorizer {
category.rename !== undefined
? typeof category.rename === 'string'
? Helpers.populate(category.rename, date)
: category.rename(file)
: category.rename({...file})
: file.getName();

if (!/\.pdf$/.test(name)) {
Expand Down
4 changes: 3 additions & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export namespace Query {
/**
* Optional custom rename function.
*/
rename?: ((document: GoogleAppsScript.Drive.File) => string) | string;
rename?:
| ((document: GoogleAppsScript.Drive.File & {text: string}) => string)
| string;

/**
* Allow secondary match. When this option is set `true`,
Expand Down

0 comments on commit f4603b5

Please sign in to comment.