Skip to content

Commit

Permalink
chore: fix case for multi-character date segment separator
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Oct 16, 2024
1 parent e7a63ea commit 786fb5d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function parseDateTimeString(
'',
).length;
const [date = ''] =
new RegExp(`(\\d\\D?){0,${dateDigitsCount - 1}}\\d?`).exec(dateTime) || [];
new RegExp(`(\\d[^\\d]*){0,${dateDigitsCount - 1}}\\d?`).exec(dateTime) || [];
const [dateTimeSeparator = ''] =
LEADING_NON_DIGIT_RE.exec(dateTime.slice(date.length)) || [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
import {parseDateTimeString} from '../parse-date-time-string';

describe('parseDateTimeString', () => {
const parse = (value: string): [string, string] =>
parseDateTimeString(value, 'dd.mm.yyyy');
describe('dd.mm.yyyy', () => {
const parse = (value: string): [string, string] =>
parseDateTimeString(value, 'dd.mm.yyyy');

(
[
{input: '', output: ['', '']},
{input: '02', output: ['02', '']},
{input: '02.', output: ['02.', '']},
{input: '0211', output: ['0211', '']},
{input: '0211.', output: ['0211.', '']},
{input: '02.112018', output: ['02.112018', '']},
{input: '02.112018,', output: ['02.112018', '']},
{input: '02.112018, ', output: ['02.112018', '']},
{input: '02.11.2018, ', output: ['02.11.2018', '']},
{input: '021120181620', output: ['02112018', '1620']},
{input: '02112018,1620', output: ['02112018', '1620']},
{input: '02112018, 1620', output: ['02112018', '1620']},
{input: '02112018, 16:20', output: ['02112018', '16:20']},
{input: '02112018,16:20', output: ['02112018', '16:20']},
{input: '02.11.2018,1620', output: ['02.11.2018', '1620']},
{input: '02.11.2018, 1620', output: ['02.11.2018', '1620']},
{input: '02.11.2018, 16:20', output: ['02.11.2018', '16:20']},
{input: '02.11.2018,16:20', output: ['02.11.2018', '16:20']},
] as const
).forEach(({input, output}) => {
it(`${input} -> ${JSON.stringify(output)}`, () => {
expect(parse(input)).toEqual(output);
(
[
{input: '', output: ['', '']},
{input: '02', output: ['02', '']},
{input: '02.', output: ['02.', '']},
{input: '0211', output: ['0211', '']},
{input: '0211.', output: ['0211.', '']},
{input: '02.112018', output: ['02.112018', '']},
{input: '02.112018,', output: ['02.112018', '']},
{input: '02.112018, ', output: ['02.112018', '']},
{input: '02.11.2018, ', output: ['02.11.2018', '']},
{input: '021120181620', output: ['02112018', '1620']},
{input: '02112018,1620', output: ['02112018', '1620']},
{input: '02112018, 1620', output: ['02112018', '1620']},
{input: '02112018, 16:20', output: ['02112018', '16:20']},
{input: '02112018,16:20', output: ['02112018', '16:20']},
{input: '02.11.2018,1620', output: ['02.11.2018', '1620']},
{input: '02.11.2018, 1620', output: ['02.11.2018', '1620']},
{input: '02.11.2018, 16:20', output: ['02.11.2018', '16:20']},
{input: '02.11.2018,16:20', output: ['02.11.2018', '16:20']},
] as const
).forEach(({input, output}) => {
it(`${input} -> ${JSON.stringify(output)}`, () => {
expect(parse(input)).toEqual(output);
});
});
});

describe('dd. mm. yyyy (date segment separator consists of space and dot)', () => {
const parse = (value: string): [string, string] =>
parseDateTimeString(value, 'dd. mm. yyyy');

(
[
{input: '', output: ['', '']},
{input: '02', output: ['02', '']},
{input: '02.', output: ['02.', '']},
{input: '02. ', output: ['02. ', '']},
{input: '0211', output: ['0211', '']},
{input: '0211. ', output: ['0211. ', '']},
{input: '02. 112018', output: ['02. 112018', '']},
{input: '02. 112018,', output: ['02. 112018', '']},
{input: '02. 112018, ', output: ['02. 112018', '']},
{input: '02. 11. 2018, ', output: ['02. 11. 2018', '']},
{input: '02. 11. 2018,1620', output: ['02. 11. 2018', '1620']},
{input: '02. 11. 2018, 1620', output: ['02. 11. 2018', '1620']},
{input: '02. 11. 2018, 16:20', output: ['02. 11. 2018', '16:20']},
{input: '02. 11. 2018,16:20', output: ['02. 11. 2018', '16:20']},
] as const
).forEach(({input, output}) => {
it(`${input} -> ${JSON.stringify(output)}`, () => {
expect(parse(input)).toEqual(output);
});
});
});
});

0 comments on commit 786fb5d

Please sign in to comment.