Skip to content

Commit

Permalink
Add option to not trim spaces
Browse files Browse the repository at this point in the history
This will allow to import at posting level
  • Loading branch information
ananthakumaran committed Sep 22, 2024
1 parent 5fd2d45 commit 8430ad8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/reference/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ Converts the given string to lower case.

Converts the given string to upper case.

#### `#!typescript capitalize(str: string): string`

Capitalize the given string.

#### `#!typescript match(str: string, {[string]: string}): string`

Let's say you are trying to import your Credit Card bill and you want
Expand Down
2 changes: 1 addition & 1 deletion src/lib/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("import", () => {
const result = await parse(new File([input], inputFile));
const rows = asRows(result);

const actual = render(rows, compiled);
const actual = render(rows, compiled, { trim: true });

expect(actual).toBe(_.trim(output));
}
Expand Down
14 changes: 11 additions & 3 deletions src/lib/spreadsheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,27 @@ const COLUMN_REFS = _.chain(_.range(65, 90))
export function render(
rows: Array<Record<string, any>>,
template: Handlebars.TemplateDelegate,
options: { reverse?: boolean } = {}
options: { reverse?: boolean; trim?: boolean } = {}
) {
const output: string[] = [];
_.each(rows, (row) => {
const rendered = _.trim(template(_.assign({ ROW: row, SHEET: rows }, COLUMN_REFS)));
let rendered = template(_.assign({ ROW: row, SHEET: rows }, COLUMN_REFS));
if (options.trim) {
rendered = _.trim(rendered);
}
if (!_.isEmpty(rendered)) {
output.push(rendered);
}
});
if (options.reverse) {
output.reverse();
}
return format(output.join("\n\n"));

if (options.trim) {
return format(output.join("\n\n"));
} else {
return format(output.join(""));
}
}

function parseCSV(file: File): Promise<Result> {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/template_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,8 @@ export default {
},
toUpperCase(str: string) {
return str.toUpperCase();
},
capitalize(str: string) {
return _.capitalize(str);
}
};
18 changes: 15 additions & 3 deletions src/routes/(app)/ledger/import/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
let data: any[][] = [];
let rows: Array<Record<string, any>> = [];
let lastOptions: any;
let options: { reverse: boolean } = { reverse: false };
let options: { reverse: boolean; trim: boolean } = { reverse: false, trim: true };
let templateEditorDom: Element;
let templateEditor: EditorView;
Expand Down Expand Up @@ -122,7 +122,10 @@
lastOptions != options
) {
try {
preview = renderJournal(rows, $templateEditorState.template, { reverse: options.reverse });
preview = renderJournal(rows, $templateEditorState.template, {
reverse: options.reverse,
trim: options.trim
});
updatePreviewContent(previewEditor, preview);
lastTemplate = $templateEditorState.template;
lastData = data;
Expand Down Expand Up @@ -368,7 +371,7 @@
Drag 'n' drop CSV, TXT, XLS, XLSX, PDF file here or click to select
</Dropzone>
</div>
<div class="is-flex justify-end mb-3">
<div class="is-flex justify-end mb-3 gap-4">
<div class="field color-switch">
<input
id="import-reverse"
Expand All @@ -378,6 +381,15 @@
/>
<label for="import-reverse">Reverse</label>
</div>
<div class="field color-switch">
<input
id="trim-reverse"
type="checkbox"
bind:checked={options.trim}
class="switch is-rounded is-small"
/>
<label for="trim-reverse">Trim</label>
</div>
</div>
{#if parseErrorMessage}
<div class="message invertable is-danger">
Expand Down

0 comments on commit 8430ad8

Please sign in to comment.