Skip to content

Commit db87152

Browse files
committed
feat(dates): introduce dateFormatPlaceholder method
The method will return localized format specifier name: d -> day; M -> mo.; MMMM -> month and etc
1 parent 7c5b18a commit db87152

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

src/dates.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as formatDate } from './dates/format-date';
22
export { default as parseDate } from './dates/parse-date';
3-
export { default as splitDateFormat } from './dates/split-date-format';
3+
export { default as splitDateFormat } from './dates/split-date-format';
4+
export { default as dateFormatPlaceholder } from './dates/date-format-placeholder';
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { localeInfo } from '../cldr';
2+
3+
const dateNameType = function(formatLength) {
4+
let nameType;
5+
if (formatLength <= 3) {
6+
nameType = "short";
7+
} else if (formatLength === 4) {
8+
nameType = "wide";
9+
} else {
10+
nameType = "narrow";
11+
}
12+
13+
return nameType;
14+
};
15+
16+
const dateFieldName = function(formatChar) {
17+
switch (formatChar) {
18+
case 'd':
19+
case 'E':
20+
return 'day';
21+
case 'M':
22+
case 'L':
23+
return 'month';
24+
case 'y':
25+
return 'year';
26+
case 'h':
27+
case 'H':
28+
return 'hour';
29+
case 'm':
30+
return 'minute';
31+
case 's':
32+
return 'second';
33+
case 'a':
34+
return 'dayperiod';
35+
case 'x':
36+
case 'X':
37+
case 'z':
38+
case 'Z':
39+
return 'zone';
40+
case 'G':
41+
return 'era';
42+
case 'c':
43+
case 'e':
44+
return 'weekday';
45+
46+
case 'q':
47+
case 'Q':
48+
return 'quarter';
49+
default:
50+
return ''; //XXX: Only milliseconds are not handled here!
51+
}
52+
};
53+
54+
const dateFieldDisplayName = function(info, match, matchLength) {
55+
const fieldName = dateFieldName(match);
56+
const nameType = dateNameType(matchLength);
57+
const fieldNameInfo = info.calendar.dateFields[fieldName];
58+
59+
return fieldNameInfo[nameType] || fieldNameInfo['wide'];
60+
};
61+
62+
63+
export default function(specifier = "", locale = "en") {
64+
const info = localeInfo(locale);
65+
return dateFieldDisplayName(info, specifier[0], specifier.length);
66+
}
67+
68+
/*
69+
//TODO: export in common file
70+
import datePattern from './date-pattern';
71+
72+
const dateFormatRegExp = /d{1,2}|E{1,6}|e{1,6}|c{3,6}|c{1}|M{1,5}|L{1,5}|y{1,4}|H{1,2}|h{1,2}|m{1,2}|a{1,5}|s{1,2}|S{1,3}|z{1,4}|Z{1,5}|x{1,5}|X{1,5}|G{1,5}|q{1,5}|Q{1,5}|"[^"]*"|'[^']*'/g;
73+
74+
export default function(format, locale = "en") {
75+
const info = localeInfo(locale);
76+
const pattern = datePattern(format, info);
77+
const formatLength = format.length;
78+
79+
console.log(pattern);
80+
81+
return pattern.replace(dateFormatRegExp, function(match) {
82+
if (match.includes("'") || match.includes("\"")) {
83+
return match.slice(1, formatLength - 1);
84+
}
85+
86+
return dateFieldDisplayName(info, match[0], match.length);
87+
});
88+
}*/

test/dates.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { load, localeInfo, dateFormatNames } from '../src/cldr';
2-
import { formatDate, parseDate, splitDateFormat } from '../src/dates';
2+
import { dateFormatPlaceholder, formatDate, parseDate, splitDateFormat } from '../src/dates';
33
import { convertTimeZone } from '../src/dates/time-utils';
44
import pad from '../src/common/pad';
55

@@ -24,6 +24,68 @@ function date(year, month, day, hour, minute, second, millisecond) {
2424
return d;
2525
}
2626

27+
describe('dateFormatPlaceholder', () => {
28+
it('should return placeholder for wide year name', () => {
29+
expect(dateFormatPlaceholder("yyyy")).toEqual("year");
30+
});
31+
32+
it('should return placeholder for short year name', () => {
33+
expect(dateFormatPlaceholder("y")).toEqual("yr.");
34+
});
35+
36+
it('should return placeholder for wide month name', () => {
37+
expect(dateFormatPlaceholder("MMMM")).toEqual("month");
38+
});
39+
40+
it('should return placeholder for short month name', () => {
41+
expect(dateFormatPlaceholder("M")).toEqual("mo.");
42+
});
43+
44+
it('should return placeholder for wide day name', () => {
45+
expect(dateFormatPlaceholder("EEEE", "bg")).toEqual("ден");
46+
});
47+
48+
it('should return placeholder for short day name', () => {
49+
expect(dateFormatPlaceholder("d", "bg")).toEqual("д");
50+
});
51+
52+
it('should return placeholder for short hour name', () => {
53+
expect(dateFormatPlaceholder("h")).toEqual("hr.");
54+
expect(dateFormatPlaceholder("h", "bg")).toEqual("ч");
55+
});
56+
57+
it('should return placeholder for short minute name', () => {
58+
expect(dateFormatPlaceholder("m")).toEqual("min.");
59+
expect(dateFormatPlaceholder("m", "bg")).toEqual("мин");
60+
});
61+
62+
it('should return placeholder for short second name', () => {
63+
expect(dateFormatPlaceholder("s")).toEqual("sec.");
64+
expect(dateFormatPlaceholder("s", "bg")).toEqual("с");
65+
});
66+
67+
it('should return placeholder for day period', () => {
68+
expect(dateFormatPlaceholder("a")).toEqual("AM/PM");
69+
});
70+
71+
it('should return placeholder for era', () => {
72+
expect(dateFormatPlaceholder("G")).toEqual("era");
73+
});
74+
75+
it('should return placeholder for zone', () => {
76+
expect(dateFormatPlaceholder("x")).toEqual("time zone");
77+
expect(dateFormatPlaceholder("z")).toEqual("time zone");
78+
});
79+
80+
it('should return placeholder for wide quarter name', () => {
81+
expect(dateFormatPlaceholder("QQQQ")).toEqual("quarter");
82+
});
83+
84+
it('should return placeholder for short quarter name', () => {
85+
expect(dateFormatPlaceholder("q")).toEqual("qtr.");
86+
});
87+
});
88+
2789
describe('date formatting', () => {
2890
it('returns value if it is not a date', () => {
2991
expect(formatDate("foo")).toEqual("foo");

0 commit comments

Comments
 (0)