From 2c9ecee464b880be21ed31f07978fb2ca38292a8 Mon Sep 17 00:00:00 2001 From: "Arthur E. Jones" Date: Fri, 8 Jul 2022 06:58:03 -0500 Subject: [PATCH] refactor: split groups with slice --- .../components/modals/ImportTransactions.js | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/loot-design/src/components/modals/ImportTransactions.js b/packages/loot-design/src/components/modals/ImportTransactions.js index e1ad225..5795367 100644 --- a/packages/loot-design/src/components/modals/ImportTransactions.js +++ b/packages/loot-design/src/components/modals/ImportTransactions.js @@ -31,18 +31,6 @@ let dateFormats = [ { format: 'dd mm yy', label: 'DD MM YY' } ]; -const reYearFirst = /^(\d{4})(?:[^\d]*)?(\d{2})(?:[^\d]*)?(\d{2})$/; -const reYearLast = /^(\d{2})(?:[^\d]*)?(\d{2})(?:[^\d]*)?(\d{4})$/; -const reTwoDig = /^(\d{2})(?:[^\d]*)?(\d{2})(?:[^\d]*)?(\d{2})$/; -const re = { - 'yyyy mm dd': reYearFirst, - 'mm dd yyyy': reYearLast, - 'dd mm yyyy': reYearLast, - 'yy mm dd': reTwoDig, - 'mm dd yy': reTwoDig, - 'dd mm yy': reTwoDig -}; - export function parseDate(str, order) { if (typeof str !== 'string') { return null; @@ -52,42 +40,48 @@ export function parseDate(str, order) { return v && v.length === 1 ? '0' + v : v; } - let parts = str.replace(/\s+/g, '').match(re[order]); - if (!parts) return null; + const dateGroups = (a, b) => str => { + const digits = str.replace(/[^\d]/g, ''); + return [digits.slice(0, a), digits.slice(a, a + b), digits.slice(a + b)]; + }; + const yearFirst = dateGroups(4, 2); + const twoDig = dateGroups(2, 2); - // We're only interested in the groups so slice the full matched string off - // of the array. - parts = parts.slice(1); - - let year, month, day; + let parts, year, month, day; switch (order) { case 'dd mm yyyy': + parts = twoDig(str); year = parts[2]; month = parts[1]; day = parts[0]; break; case 'dd mm yy': + parts = twoDig(str); year = `20${parts[2]}`; month = parts[1]; day = parts[0]; break; case 'yyyy mm dd': + parts = yearFirst(str); year = parts[0]; month = parts[1]; day = parts[2]; break; case 'yy mm dd': + parts = twoDig(str); year = `20${parts[0]}`; month = parts[1]; day = parts[2]; break; case 'mm dd yy': + parts = twoDig(str); year = `20${parts[2]}`; month = parts[0]; day = parts[1]; break; default: case 'mm dd yyyy': + parts = twoDig(str); year = parts[2]; month = parts[0]; day = parts[1];