From 8430ad8ec6b7dabbf95fab1c1dae258f8dd319a9 Mon Sep 17 00:00:00 2001 From: Anantha Kumaran Date: Sun, 22 Sep 2024 11:37:56 +0530 Subject: [PATCH] Add option to not trim spaces This will allow to import at posting level --- docs/reference/import.md | 4 ++++ src/lib/import.test.ts | 2 +- src/lib/spreadsheet.ts | 14 +++++++++++--- src/lib/template_helpers.ts | 3 +++ src/routes/(app)/ledger/import/+page.svelte | 18 +++++++++++++++--- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/docs/reference/import.md b/docs/reference/import.md index cd02e4b5..105b2351 100644 --- a/docs/reference/import.md +++ b/docs/reference/import.md @@ -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 diff --git a/src/lib/import.test.ts b/src/lib/import.test.ts index b4b797e4..9dc04532 100644 --- a/src/lib/import.test.ts +++ b/src/lib/import.test.ts @@ -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)); } diff --git a/src/lib/spreadsheet.ts b/src/lib/spreadsheet.ts index 677078c1..8d14d4d9 100644 --- a/src/lib/spreadsheet.ts +++ b/src/lib/spreadsheet.ts @@ -44,11 +44,14 @@ const COLUMN_REFS = _.chain(_.range(65, 90)) export function render( rows: Array>, 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); } @@ -56,7 +59,12 @@ export function render( 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 { diff --git a/src/lib/template_helpers.ts b/src/lib/template_helpers.ts index 999e5b43..f15dde96 100644 --- a/src/lib/template_helpers.ts +++ b/src/lib/template_helpers.ts @@ -263,5 +263,8 @@ export default { }, toUpperCase(str: string) { return str.toUpperCase(); + }, + capitalize(str: string) { + return _.capitalize(str); } }; diff --git a/src/routes/(app)/ledger/import/+page.svelte b/src/routes/(app)/ledger/import/+page.svelte index 49e21773..436ceaf2 100644 --- a/src/routes/(app)/ledger/import/+page.svelte +++ b/src/routes/(app)/ledger/import/+page.svelte @@ -30,7 +30,7 @@ let data: any[][] = []; let rows: Array> = []; 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; @@ -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; @@ -368,7 +371,7 @@ Drag 'n' drop CSV, TXT, XLS, XLSX, PDF file here or click to select -
+
+
+ + +
{#if parseErrorMessage}