Skip to content

Commit

Permalink
Add more data to the returned json
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz committed Aug 6, 2021
1 parent 61d133d commit 8324e6a
Show file tree
Hide file tree
Showing 3 changed files with 391 additions and 80 deletions.
30 changes: 28 additions & 2 deletions src/generate-field-json.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import { spawn } from "child_process";

export interface FormField {
fieldDefault: string;
fieldFlags: string;
fieldMaxLength: string | number;
fieldOptions: string[];
fieldType: string;
fieldValue: string | boolean;
title: string;
}

const getFieldOptions = (field: string): string[] => {
const regOptions = /(FieldStateOption: ([^\n]*))/g;
const matches = field.match(regOptions);
const options: string[] = [];
if (matches) {
for (const match of matches) {
options.push(
/FieldStateOption: ([^\n]*)/.exec(match)?.[1].trim() as string
);
}
}
return options;
};

/**
* Extracts the Form Fields from a PDF Form
* @param sourceFile
* @returns A FormField object
*/
let regName = /FieldName: ([^\n]*)/;
export default (sourceFile: string): Promise<FormField[]> => {
const regName = /FieldName: ([^\n]*)/;
const regType = /FieldType: ([\t .A-Za-z]+)/;
const regFlags = /FieldFlags: ([\d\t .]+)/;
const regMaxLength = /FieldMaxLength: ([\d\t .]+)/;
const regValue = /FieldValue: ([^\n]*)/;
const regDefault = /FieldValueDefault: ([^\n]*)/;
const regOptions = /(FieldStateOption: ([^\n]*))/g;
const fieldArray: FormField[] = [];

return new Promise((resolve, reject) => {
Expand All @@ -39,13 +59,19 @@ export interface FormField {
const fields = output.split("---").slice(1);
for (const field of fields) {
fieldArray.push({
fieldDefault:
(regDefault.exec(field)?.[1].trim() as string) ?? "",
fieldFlags:
(regFlags.exec(field)?.[1].trim() as string) ?? "",
fieldMaxLength:
(regMaxLength.exec(field)?.[1].trim() as string) ?? "",
fieldOptions: regOptions.test(field)
? getFieldOptions(field)
: [],
fieldType:
(regType.exec(field)?.[1].trim() as string) ?? "",
fieldValue: "",
fieldValue:
(regValue.exec(field)?.[1].trim() as string) ?? "",
title: (regName.exec(field)?.[1].trim() as string) ?? "",
});
}
Expand Down
Loading

0 comments on commit 8324e6a

Please sign in to comment.