From 94f9b837b1e3028e30eb78c2de0c30fdc6ec9b27 Mon Sep 17 00:00:00 2001 From: Csongor Zih Date: Tue, 27 Dec 2022 12:51:36 +0100 Subject: [PATCH] Fix parsing errors #7 and #11 Change `trimEnclosing` implementation, fix `-scale` to fit specification --- src/export/processLayer.ts | 8 ++++++-- src/import/importTH2.ts | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/export/processLayer.ts b/src/export/processLayer.ts index e268f5e..f4a3bc1 100644 --- a/src/export/processLayer.ts +++ b/src/export/processLayer.ts @@ -16,10 +16,14 @@ export function processLayer(layer: paper.Layer) { const s = settings; const o = []; - if (s.scale !== "") o.push(`-scale [${s.scale}]`); + if (s.scale !== "") { + const val = s.scale.includes(" ") ? `[${s.scale}]` : s.scale; + o.push(`-scale ${val}`); + } + for (const setting of ScrapSettings.stringSettings.slice(1)) { if (s[setting]) - o.push(`-${setting} "${s[setting]}"`); + o.push(`-${setting} ${s[setting]}`); } if (s.otherSettings !== "") diff --git a/src/import/importTH2.ts b/src/import/importTH2.ts index 444f3b4..6200c15 100644 --- a/src/import/importTH2.ts +++ b/src/import/importTH2.ts @@ -22,11 +22,17 @@ const toPoint = function(global: string[], global2: string[] = undefined) { ]); }; -const trimEnclosing = (text: string) => - text.trim() - // eslint-disable-next-line no-useless-escape - .replace(/^["'\[]/, "") - .replace(/["'\]]$/, ""); +function trimEnclosing(text: string): string { + text = text.trim(); + const starting = text.charAt(0); + const ending = text.charAt(text.length - 1); + if ([`"`, `'`].includes(starting) && starting === ending) { + return text.substring(1, text.length - 1); + } else if (starting === `[` && ending === `]`) { + return text.substring(1, text.length - 1); + } + return text; +} const getOptions = function(source: string) { const options: Record = {}; @@ -293,7 +299,7 @@ function savePointSettings(point: paper.Shape, options: Record) for (const key of PointSettings.stringSettings) { if (key in options) { - s[key] = o[key]; delete o[key]; + s[key] = trimEnclosing(o[key]); delete o[key]; } }