Skip to content

Commit a545632

Browse files
refactor: include standAlone option for the part names
1 parent b8718c9 commit a545632

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

src/dates/split-date-format.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,40 @@ import { localeInfo } from '../cldr';
55

66
const NAME_TYPES = {
77
month: {
8-
nameType: 'months',
9-
minLength: 3
8+
type: 'months',
9+
minLength: 3,
10+
standAlone: 'L'
1011
},
1112

1213
quarter: {
13-
nameType: 'quarters',
14-
minLength: 3
14+
type: 'quarters',
15+
minLength: 3,
16+
standAlone: 'q'
1517
},
1618

1719
weekday: {
18-
nameType: 'days',
19-
minLength: 0
20+
type: 'days',
21+
minLength: {
22+
E: 0,
23+
c: 3,
24+
e: 3
25+
},
26+
standAlone: 'c'
2027
},
2128

2229
dayperiod: {
23-
nameType: 'dayPeriods',
30+
type: 'dayPeriods',
2431
minLength: 0
2532
},
2633

2734
era: {
28-
nameType: 'eras',
35+
type: 'eras',
2936
minLength: 0
3037
}
3138
};
39+
3240
const LITERAL = 'literal';
41+
const NUMBER = 'number';
3342

3443
function addLiteral(parts, value) {
3544
const lastPart = parts[parts.length - 1];
@@ -60,17 +69,26 @@ export default function splitDateFormat(format, locale = 'en') {
6069
if (value.startsWith('"') || value.startsWith("'")) {
6170
addLiteral(parts, value);
6271
} else {
63-
const type = DATE_FIELD_MAP[value[0]];
72+
const specifier = value[0];
73+
const type = DATE_FIELD_MAP[specifier];
6474
const part = {
6575
type: type,
6676
pattern: value
6777
};
6878

69-
if (NAME_TYPES[type] && value.length >= NAME_TYPES[type].minLength) {
70-
part.names = {
71-
type: NAME_TYPES[type].nameType,
72-
nameType: dateNameType(value.length)
73-
};
79+
const names = NAME_TYPES[type];
80+
81+
if (names) {
82+
const minLength = typeof names.minLength === NUMBER ? names.minLength : names.minLength[specifier];
83+
const patternLength = value.length;
84+
85+
if (patternLength >= minLength) {
86+
part.names = {
87+
type: names.type,
88+
nameType: dateNameType(patternLength),
89+
standAlone: names.standAlone === specifier
90+
};
91+
}
7492
}
7593

7694
parts.push(part);

test/dates.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,20 +1216,36 @@ describe('splitDateFormat', () => {
12161216
type: "month",
12171217
names: {
12181218
type: "months",
1219-
nameType: "abbreviated"
1219+
nameType: "abbreviated",
1220+
standAlone: false
12201221
},
12211222
pattern: "MMM"
12221223
}];
12231224

12241225
expect(splitDateFormat('MMM')).toEqual(expected);
12251226
});
12261227

1228+
it('part names standAlone options is set to true if specifier is for standAlone names', () => {
1229+
const expected = [{
1230+
type: "month",
1231+
names: {
1232+
type: "months",
1233+
nameType: "abbreviated",
1234+
standAlone: true
1235+
},
1236+
pattern: "LLL"
1237+
}];
1238+
1239+
expect(splitDateFormat('LLL')).toEqual(expected);
1240+
});
1241+
12271242
it('includes all specifiers', () => {
12281243
const expected = [{
12291244
type: "era",
12301245
names: {
12311246
type: "eras",
1232-
nameType: "abbreviated"
1247+
nameType: "abbreviated",
1248+
standAlone: false
12331249
},
12341250
pattern: "G"
12351251
}, {
@@ -1242,21 +1258,24 @@ describe('splitDateFormat', () => {
12421258
type: "quarter",
12431259
names: {
12441260
type: "quarters",
1245-
nameType: "abbreviated"
1261+
nameType: "abbreviated",
1262+
standAlone: false
12461263
},
12471264
pattern: "QQQ"
12481265
}, {
12491266
type: "month",
12501267
names: {
12511268
type: "months",
1252-
nameType: "wide"
1269+
nameType: "wide",
1270+
standAlone: false
12531271
},
12541272
pattern: "MMMM"
12551273
}, {
12561274
type: "month",
12571275
names: {
12581276
type: "months",
1259-
nameType: "narrow"
1277+
nameType: "narrow",
1278+
standAlone: true
12601279
},
12611280
pattern: "LLLLL"
12621281
}, {
@@ -1266,21 +1285,24 @@ describe('splitDateFormat', () => {
12661285
type: "weekday",
12671286
names: {
12681287
type: "days",
1269-
nameType: "abbreviated"
1288+
nameType: "abbreviated",
1289+
standAlone: false
12701290
},
12711291
pattern: "EE"
12721292
}, {
12731293
type: "weekday",
12741294
names: {
12751295
type: "days",
1276-
nameType: "abbreviated"
1296+
nameType: "abbreviated",
1297+
standAlone: true
12771298
},
12781299
pattern: "ccc"
12791300
}, {
12801301
type: "weekday",
12811302
names: {
12821303
type: "days",
1283-
nameType: "wide"
1304+
nameType: "wide",
1305+
standAlone: false
12841306
},
12851307
pattern: "eeee"
12861308
}, {
@@ -1299,7 +1321,8 @@ describe('splitDateFormat', () => {
12991321
type: "dayperiod",
13001322
names: {
13011323
type: "dayPeriods",
1302-
nameType: "abbreviated"
1324+
nameType: "abbreviated",
1325+
standAlone: false
13031326
},
13041327
pattern: "a"
13051328
}, {

0 commit comments

Comments
 (0)