Skip to content

Commit cb3c4d3

Browse files
committed
MBS-12342: Allow parsing CJK dates
This adds support for Chinese, Taiwanese, Japanese and Korean dates.
1 parent 51fd632 commit cb3c4d3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

root/static/scripts/common/utility/parseNaturalDate.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
const ymdRegex = /^\W*([0-9]{4})(?:\W+(0?[1-9]|1[0-2])(?:\W+(0?[1-9]|[12][0-9]|3[01]))?)?\W*$/;
12+
const cjkRegex = /^\W*([0-9]{2}|[0-9]{4})(?:(?:\u5E74|\uB144\W?)(0?[1-9]|1[0-2])(?:(?:\u6708|\uC6D4\W?)(0?[1-9]|[12][0-9]|3[01])(?:\u65E5|\uC77C)?)?)?\W*$/;
1213

1314
function cleanDateString(
1415
str: string,
@@ -48,6 +49,14 @@ function cleanDateString(
4849
},
4950
);
5051

52+
// RoC year numbering - http://en.wikipedia.org/wiki/Minguo_calendar
53+
cleanedString = cleanedString.replace(
54+
/([0-9]{1,3})/,
55+
function (match, year) {
56+
return String(parseInt(year, 10) + 1911);
57+
},
58+
);
59+
5160
return cleanedString;
5261
}
5362

@@ -56,7 +65,10 @@ export function parseNaturalDate(
5665
): PartialDateStringsT {
5766
const cleanedString = cleanDateString(str);
5867

59-
const match = cleanedString.match(ymdRegex) || [];
68+
const match = cleanedString.match(cjkRegex) ||
69+
cleanedString.match(ymdRegex) ||
70+
[];
71+
6072
return {
6173
/* eslint-disable sort-keys */
6274
year: match[1] || '',

root/static/scripts/tests/utility/parseNaturalDate.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import test from 'tape';
1212
import parseNaturalDate from '../../common/utility/parseNaturalDate.js';
1313

1414
test('parseNaturalDate', function (t) {
15-
t.plan(12);
15+
t.plan(15);
1616

1717
/* eslint-disable sort-keys */
1818
const parseDateTests = [
@@ -46,6 +46,14 @@ test('parseNaturalDate', function (t) {
4646
// Japanese year codes
4747
{date: 'O-2-25', expected: {year: '1987', month: '2', day: '25'}},
4848
{date: 'D-12-10', expected: {year: '1991', month: '12', day: '10'}},
49+
50+
// CJK dates
51+
{date: '2022年4月29日', expected: {year: '2022', month: '4', day: '29'}},
52+
{date: '2021년2월1일', expected: {year: '2021', month: '2', day: '1'}},
53+
{
54+
date: '民國99年12月31日',
55+
expected: {year: '2010', month: '12', day: '31'},
56+
},
4957
];
5058
/* eslint-enable sort-keys */
5159

0 commit comments

Comments
 (0)