Skip to content

Commit

Permalink
Merge pull request #637 from carstingaxion/fix/632
Browse files Browse the repository at this point in the history
Allow the date- and time-format strings to contain escaped characters
  • Loading branch information
mauteri authored May 3, 2024
2 parents bc4d916 + 842570e commit a2aa5fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/helpers/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,16 @@ export function convertPHPToMomentFormat(format) {
r: '', // no equivalent
U: 'X',
};

return String(format)
.split('')
.map((chr) => (chr in replacements ? replacements[chr] : chr))
.map((chr, index, elements) => {
// Allow the format string to contain escaped chars, like ES or DE needs
const last = elements[index - 1];
if (chr in replacements && last !== '\\') {
return replacements[chr];
}
return chr;
})
.join('');
}

Expand Down
6 changes: 6 additions & 0 deletions test/unit/js/src/helpers/datetime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,9 @@ test('convertPHPToMomentFormat returns correct time format', () => {

expect(format).toBe('h:mm a');
});

test('convertPHPToMomentFormat returns correct format that contains escaped chars, like ES or DE needs', () => {
const format = convertPHPToMomentFormat('G:i \\U\\h\\r'); // "20 Uhr" is german for "8 o'clock" (in the evening).

expect(format).toBe('H:mm \\U\\h\\r');
});

0 comments on commit a2aa5fa

Please sign in to comment.