Skip to content

Commit bc2a861

Browse files
committed
wip to parse partial dates
1 parent 17ad3cc commit bc2a861

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010

11-
const ymdRegex = /^\W*([0-9]{4})(?:\W+(0?[1-9]|1[0-2])(?:\W+(0?[1-9]|[12][0-9]|3[01]))?)?\W*$/;
11+
const ymdRegex = /^[^\w?]*([0-9]{4}|\?{4})(?:[^\w?]+(0?[1-9]|1[0-2]|\?{2})(?:[^\w?]+(0?[1-9]|[12][0-9]|3[01]|\?{2}))?)?[^\w?]*$/;
1212
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*$/;
1313

1414
function cleanDateString(
@@ -69,11 +69,15 @@ export default function parseNaturalDate(
6969
cleanedString.match(ymdRegex) ||
7070
[];
7171

72+
console.log(match);
73+
const year = match[1] === '????' ? '' : match[1];
74+
const month = match[2] === '??' ? '' : match[2];
75+
const day = match[3] === '??' ? '' : match[3];
7276
return {
7377
/* eslint-disable sort-keys */
74-
year: match[1] || '',
75-
month: match[2] || '',
76-
day: match[3] || '',
78+
year: year || '',
79+
month: month || '',
80+
day: day || '',
7781
/* eslint-enable sort-keys */
7882
};
7983
}

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

Lines changed: 7 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(15);
15+
t.plan(21);
1616

1717
/* eslint-disable sort-keys */
1818
const parseDateTests = [
@@ -24,10 +24,16 @@ test('parseNaturalDate', function (t) {
2424
{date: '1999-01-02', expected: {year: '1999', month: '01', day: '02'}},
2525
{date: '1999-01', expected: {year: '1999', month: '01', day: ''}},
2626
{date: '1999', expected: {year: '1999', month: '', day: ''}},
27+
{date: '1999-??-22', expected: {year: '1999', month: '', day: '22'}},
28+
{date: '????-??-22', expected: {year: '', month: '', day: '22'}},
29+
{date: '????-01-??', expected: {year: '', month: '01', day: ''}},
2730

2831
// Y M D
2932
{date: '1999 01 02', expected: {year: '1999', month: '01', day: '02'}},
3033
{date: '1999 01', expected: {year: '1999', month: '01', day: ''}},
34+
{date: '1999 ?? 22', expected: {year: '1999', month: '', day: '22'}},
35+
{date: '???? ?? 22', expected: {year: '', month: '', day: '22'}},
36+
{date: '???? 01 ??', expected: {year: '', month: '01', day: ''}},
3137

3238
// Fullwidth numbers
3339
{

0 commit comments

Comments
 (0)