Skip to content
Snippets Groups Projects
Unverified Commit 8f2dc5d5 authored by Matiss Janis Aboltins's avatar Matiss Janis Aboltins Committed by GitHub
Browse files

:bug: (import) date formats supporting digits without leading zeros (#692)

* :bug: (import) date formats supporting digits without leading zeros

Closes #607

* ISO test case
parent 081b98cc
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,14 @@ export function parseDate(str, order) { ...@@ -43,6 +43,14 @@ export function parseDate(str, order) {
} }
const dateGroups = (a, b) => str => { const dateGroups = (a, b) => str => {
const parts = str
.replace(/^[^\d]+/, '')
.replace(/[^\d]+$/, '')
.split(/[^\d]+/);
if (parts.length >= 3) {
return parts.slice(0, 3);
}
const digits = str.replace(/[^\d]/g, ''); const digits = str.replace(/[^\d]/g, '');
return [digits.slice(0, a), digits.slice(a, a + b), digits.slice(a + b)]; return [digits.slice(0, a), digits.slice(a, a + b), digits.slice(a + b)];
}; };
......
...@@ -2,122 +2,141 @@ import { parseDate } from './ImportTransactions'; ...@@ -2,122 +2,141 @@ import { parseDate } from './ImportTransactions';
describe('Import transactions', function () { describe('Import transactions', function () {
describe('date parsing', function () { describe('date parsing', function () {
it('should not parse', function () { const invalidInputs = [
const invalidInputs = [ { str: '', order: 'yyyy mm dd' },
{ str: '', order: 'yyyy mm dd' }, { str: null, order: 'yyyy mm dd' },
{ str: null, order: 'yyyy mm dd' }, { str: 42, order: 'yyyy mm dd' },
{ str: 42, order: 'yyyy mm dd' }, { str: {}, order: 'yyyy mm dd' },
{ str: {}, order: 'yyyy mm dd' }, { str: [], order: 'yyyy mm dd' },
{ str: [], order: 'yyyy mm dd' }, { str: 'invalid', order: 'yyyy mm dd' },
{ str: 'invalid', order: 'yyyy mm dd' }, { str: '2020 Dec 24', order: 'yyyy mm dd' },
{ str: '2020 Dec 24', order: 'yyyy mm dd' }, { str: '12 24 20', order: 'mm dd yyyy' },
{ str: '12 24 20', order: 'mm dd yyyy' }, { str: '20 12 24', order: 'yyyy mm dd' },
{ str: '20 12 24', order: 'yyyy mm dd' }, { str: '2020 12 24', order: 'yy mm dd' },
{ str: '2020 12 24', order: 'yy mm dd' }, { str: '12 24 2020', order: 'mm dd yy' },
{ str: '12 24 2020', order: 'mm dd yy' }, { str: '12 00 2020', order: 'mm dd yyyy' },
{ str: '12 00 2020', order: 'mm dd yyyy' }, { str: '12 32 2020', order: 'mm dd yyyy' },
{ str: '12 32 2020', order: 'mm dd yyyy' }, { str: '13 24 2020', order: 'mm dd yyyy' },
{ str: '13 24 2020', order: 'mm dd yyyy' }, { str: '00 24 2020', order: 'mm dd yyyy' },
{ str: '00 24 2020', order: 'mm dd yyyy' }, { str: '02 30 2020', order: 'mm dd yyyy' },
{ str: '02 30 2020', order: 'mm dd yyyy' }, { str: '04 31 2020', order: 'mm dd yyyy' },
{ str: '04 31 2020', order: 'mm dd yyyy' }, { str: '04 31 2020', order: 'mm dd yyyy' },
{ str: '04 31 2020', order: 'mm dd yyyy' }, { str: '06 31 2020', order: 'mm dd yyyy' },
{ str: '06 31 2020', order: 'mm dd yyyy' }, { str: '09 31 2020', order: 'mm dd yyyy' },
{ str: '09 31 2020', order: 'mm dd yyyy' }, { str: '11 31 2020', order: 'mm dd yyyy' },
{ str: '11 31 2020', order: 'mm dd yyyy' }, { str: '2046 31 2020', order: 'mm dd yyyy' },
{ str: '2046 31 2020', order: 'mm dd yyyy' }, { str: '2011 31 2020', order: 'mm dd yy' },
{ str: '2011 31 2020', order: 'mm dd yy' }, { str: '2020', order: 'mm dd yy' },
{ str: '2020', order: 'mm dd yy' }, ];
];
for (const { str, order } of invalidInputs) { it.each(invalidInputs)(
'should not parse string "$str" with order "$order"',
({ str, order }) => {
expect(parseDate(str, order)).toBe(null); expect(parseDate(str, order)).toBe(null);
} },
}); );
it('should parse', function () { const validInputs = [
const validInputs = [ {
{ order: 'yyyy mm dd',
order: 'yyyy mm dd', cases: [
cases: [ ['20201224', '2020-12-24'],
'20201224', ['2020 12 24', '2020-12-24'],
'2020 12 24', ['2020-1-2', '2020-01-02'],
'2020-12-24', ['2020-12-24', '2020-12-24'],
'2020/12/24', ['2020/12/24', '2020-12-24'],
' 2020 / 12 / 24', [' 2020 / 12 / 24', '2020-12-24'],
'2020/12/24 ', ['2020/12/24 ', '2020-12-24'],
'2020 12-24 ', ['2020 12-24 ', '2020-12-24'],
], ['2023-01-19T02:36:52', '2023-01-19'],
}, ],
{ },
order: 'yy mm dd', {
cases: [ order: 'yy mm dd',
'201224', cases: [
'20 12 24', ['201224', '2020-12-24'],
'20-12-24', ['20 12 24', '2020-12-24'],
'20/12/24', ['20-12-24', '2020-12-24'],
'20/12/24', ['20/12/24', '2020-12-24'],
'20/12/24 ', ['20/12/24', '2020-12-24'],
'20 12-24 ', ['20/12/24 ', '2020-12-24'],
], ['20/1/2 ', '2020-01-02'],
}, ['20 12-24 ', '2020-12-24'],
{ ],
order: 'mm dd yyyy', },
cases: [ {
'12242020', order: 'mm dd yyyy',
'12 24 2020 ', cases: [
'12-24-2020', ['12242020', '2020-12-24'],
'12/24/2020', ['1 24 2020', '2020-01-24'],
' 12/24/2020', ['01 24 2020', '2020-01-24'],
'12/24/2020', ['12 24 2020 ', '2020-12-24'],
'12 24-2020', ['12-24-2020', '2020-12-24'],
], ['12/24/2020', '2020-12-24'],
}, [' 12/24/2020', '2020-12-24'],
{ ['12/24/2020', '2020-12-24'],
order: 'mm dd yy', ['12 24-2020', '2020-12-24'],
cases: [ ],
'122420', },
'12 24 20 ', {
'12-24-20', order: 'mm dd yy',
'12/24/20', cases: [
' 12/24/20', ['122420', '2020-12-24'],
'12/24/20', ['12 24 20 ', '2020-12-24'],
'12 24-20', ['12-24-20', '2020-12-24'],
], ['1-24-20', '2020-01-24'],
}, ['01-24-20', '2020-01-24'],
{ ['12/24/20', '2020-12-24'],
order: 'dd mm yyyy', [' 12/24/20', '2020-12-24'],
cases: [ ['12/24/20', '2020-12-24'],
'24122020', ['1/24/20', '2020-01-24'],
'24 12 2020 ', ['12 24-20', '2020-12-24'],
'24-12-2020', ],
'24/12/2020', },
' 24/12/2020', {
'24/12/2020 ', order: 'dd mm yyyy',
'24-12 2020', cases: [
], ['24122020', '2020-12-24'],
}, ['24 12 2020 ', '2020-12-24'],
{ ['2 12 2020', '2020-12-02'],
order: 'dd mm yy', ['02 12 2020 ', '2020-12-02'],
cases: [ ['24-12-2020', '2020-12-24'],
'241220', ['02-12-2020', '2020-12-02'],
'2412 20 ', ['24/12/2020', '2020-12-24'],
'24-12-20', [' 24/12/2020', '2020-12-24'],
'24/12/20', ['24/12/2020 ', '2020-12-24'],
' 24/12/20', ['2/12/2020 ', '2020-12-02'],
'24/12/20 ', ['24-12 2020', '2020-12-24'],
'24 12-20 ', ],
], },
}, {
]; order: 'dd mm yy',
cases: [
['241220', '2020-12-24'],
['2412 20 ', '2020-12-24'],
['24-12-20', '2020-12-24'],
['2-12-20', '2020-12-02'],
['02-12-20', '2020-12-02'],
['24/12/20', '2020-12-24'],
[' 24/12/20', '2020-12-24'],
['24/12/20 ', '2020-12-24'],
['2/12/20', '2020-12-02'],
['02/12/20', '2020-12-02'],
['24 12-20 ', '2020-12-24'],
],
},
];
describe.each(validInputs)(
'should parse with order "$order"',
({ order, cases }) => {
it.each(cases)('given input %j expects output %j', (input, output) => {
const parsed = parseDate(input, order);
for (const { order, cases } of validInputs) {
for (const str of cases) {
const parsed = parseDate(str, order);
expect(typeof parsed).toBe('string'); expect(typeof parsed).toBe('string');
expect(parsed).toBe('2020-12-24'); expect(parsed).toBe(output);
} });
} },
}); );
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment