Skip to content

Commit 19e0ae5

Browse files
committed
refactor(dateFieldName): method accepts DateFieldNameOptions instead of string.
Moved the method to the cldr folder
1 parent c92253c commit 19e0ae5

File tree

8 files changed

+173
-166
lines changed

8 files changed

+173
-166
lines changed

src/cldr.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,51 @@ export interface DateFormatNameOptions {
3232
*/
3333
export function dateFormatNames(locale: string, options: DateFormatNameOptions): any;
3434

35+
/**
36+
* Settings for the dateFieldName function.
37+
*/
38+
export interface DateFieldNameOptions {
39+
/**
40+
* Specifies the type of names.
41+
*/
42+
type: 'dayPeriods' |
43+
'era' |
44+
'year' |
45+
'quarter' |
46+
'month' |
47+
'week' |
48+
'day' |
49+
'weekday' |
50+
'dayperiod' |
51+
'hour' |
52+
'minute' |
53+
'second' |
54+
'millisecond' |
55+
'zone';
56+
57+
/**
58+
* Specifies the names form.
59+
*/
60+
nameType?: 'wide' | 'narrow' | 'short';
61+
}
62+
63+
/**
64+
* Returns a localized date field name based on a specific format specifier.
65+
*
66+
* @param formatSpecifier The dateFieldName options.
67+
* @param locale The optional locale id. If not specified, the `"en"` locale id is used.
68+
* @returns The localized date field name determined by the dateFieldName options.
69+
*
70+
* @example
71+
* ```
72+
* dateFieldName({ type: 'day' }); //returns 'day';
73+
* dateFieldName({ type: 'day', nameType: 'wide' }); //returns 'day';
74+
* dateFieldName({ type: 'month', nameType: 'short' }); //returns 'mo.';
75+
* dateFieldName({ type: 'month', nameType: 'wide' }); //returns 'month';
76+
* ```
77+
*/
78+
export function dateFieldName(formatSpecifier: DateFieldNameOptions, locale?: string): string;
79+
3580
/**
3681
* Returns the first day index starting from Sunday based on the specified locale.
3782
*

src/cldr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { default as load } from './cldr/load';
2+
export { default as dateFieldName } from './cldr/date-field-name';
23
export { default as dateFormatNames } from './cldr/date-format-names';
34
export { cldr, localeInfo } from './cldr/info';
45
export { currencyDisplays, currencyDisplay, currencyFractionOptions, territoryCurrencyCode, localeCurrency } from './cldr/currency';
56
export { default as firstDay } from './cldr/first-day';
6-
export { default as numberSymbols } from './cldr/number-symbols';
7+
export { default as numberSymbols } from './cldr/number-symbols';

src/cldr/date-field-name.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { localeInfo } from './info';
2+
3+
export default function(options, locale = "en") {
4+
const info = localeInfo(locale);
5+
const dateFields = info.calendar.dateFields || {};
6+
const fieldNameInfo = dateFields[options.type] || {};
7+
8+
return fieldNameInfo[options.nameType] || fieldNameInfo['wide'];
9+
}

src/dates.d.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,3 @@ export interface DateFormatPart {
125125
* @returns The date format parts.
126126
*/
127127
export function splitDateFormat(format: string|DateFormatOptions, locale?: string): DateFormatPart[];
128-
129-
/**
130-
* Returns a localized date field name based on a specific format specifier.
131-
*
132-
* @param formatSpecifier The format specifier.
133-
* @param locale The optional locale id. If not specified, the `"en"` locale id is used.
134-
* @returns The localized date field name determined by the format specifier.
135-
*
136-
* @example
137-
* ```
138-
* dateFieldName('d'); //returns 'day';
139-
* dateFieldName('M'); //returns 'mo.';
140-
* dateFieldName('MMMM'); //returns 'month';
141-
* ```
142-
*/
143-
export function dateFieldName(formatSpecifier: string, locale?: string): string;

src/dates.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { default as formatDate } from './dates/format-date';
22
export { default as parseDate } from './dates/parse-date';
33
export { default as splitDateFormat } from './dates/split-date-format';
4-
export { default as dateFieldName } from './dates/date-field-name';

src/dates/date-field-name.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

test/cldr.js

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { load, cldr, localeInfo, dateFormatNames, firstDay, localeCurrency, currencyDisplay, currencyFractionOptions, currencyDisplays, numberSymbols } from '../src/cldr';
1+
import { load, cldr, localeInfo, dateFieldName, dateFormatNames, firstDay, localeCurrency, currencyDisplay, currencyFractionOptions, currencyDisplays, numberSymbols } from '../src/cldr';
22
import { errors } from '../src/errors';
33

44
const likelySubtags = require("cldr-data/supplemental/likelySubtags.json");
@@ -366,6 +366,120 @@ describe('dateFormatNames', () => {
366366
});
367367
});
368368

369+
describe('dateFieldName', () => {
370+
it('should return placeholder for the era type', () => {
371+
expect(dateFieldName({ type: 'era', nameType: 'wide' })).toEqual("era");
372+
expect(dateFieldName({ type: 'era', nameType: 'narrow' })).toEqual("era");
373+
expect(dateFieldName({ type: 'era', nameType: 'short' })).toEqual("era");
374+
});
375+
376+
it('should return placeholder for the year type', () => {
377+
expect(dateFieldName({ type: 'year', nameType: 'wide' })).toEqual("year");
378+
expect(dateFieldName({ type: 'year', nameType: 'narrow' })).toEqual("yr.");
379+
expect(dateFieldName({ type: 'year', nameType: 'short' })).toEqual("yr.");
380+
});
381+
382+
it('should return placeholder for the quarter type', () => {
383+
expect(dateFieldName({ type: 'quarter', nameType: 'wide' })).toEqual("quarter");
384+
expect(dateFieldName({ type: 'quarter', nameType: 'narrow' })).toEqual("qtr.");
385+
expect(dateFieldName({ type: 'quarter', nameType: 'short' })).toEqual("qtr.");
386+
});
387+
388+
it('should return placeholder for the month type', () => {
389+
expect(dateFieldName({ type: 'month', nameType: 'wide' })).toEqual("month");
390+
expect(dateFieldName({ type: 'month', nameType: 'narrow' })).toEqual("mo.");
391+
expect(dateFieldName({ type: 'month', nameType: 'short' })).toEqual("mo.");
392+
});
393+
394+
it('should return placeholder for the month type', () => {
395+
expect(dateFieldName({ type: 'month', nameType: 'wide' })).toEqual("month");
396+
expect(dateFieldName({ type: 'month', nameType: 'narrow' })).toEqual("mo.");
397+
expect(dateFieldName({ type: 'month', nameType: 'short' })).toEqual("mo.");
398+
});
399+
400+
it('should return placeholder for the week type', () => {
401+
expect(dateFieldName({ type: 'week', nameType: 'wide' })).toEqual("week");
402+
expect(dateFieldName({ type: 'week', nameType: 'narrow' })).toEqual("wk.");
403+
expect(dateFieldName({ type: 'week', nameType: 'short' })).toEqual("wk.");
404+
});
405+
406+
it('should return placeholder for the day type', () => {
407+
expect(dateFieldName({ type: 'day', nameType: 'wide' })).toEqual("day");
408+
expect(dateFieldName({ type: 'day', nameType: 'narrow' })).toEqual("day");
409+
expect(dateFieldName({ type: 'day', nameType: 'short' })).toEqual("day");
410+
});
411+
412+
it('should return localized placeholder for the day type', () => {
413+
expect(dateFieldName({ type: 'day', nameType: 'wide' }, 'bg')).toEqual("ден");
414+
expect(dateFieldName({ type: 'day', nameType: 'narrow' }, 'bg')).toEqual("д");
415+
expect(dateFieldName({ type: 'day', nameType: 'short' }, 'bg')).toEqual("д");
416+
});
417+
418+
it('should return placeholder for the weekday type', () => {
419+
expect(dateFieldName({ type: 'weekday', nameType: 'wide' })).toEqual("day of the week");
420+
expect(dateFieldName({ type: 'weekday', nameType: 'narrow' })).toEqual("day of the week");
421+
expect(dateFieldName({ type: 'weekday', nameType: 'short' })).toEqual("day of the week");
422+
});
423+
424+
it('should return localized placeholder for the weekday type', () => {
425+
expect(dateFieldName({ type: 'weekday', nameType: 'wide' }, 'bg')).toEqual("ден от седмицата");
426+
expect(dateFieldName({ type: 'weekday', nameType: 'narrow' }, 'bg')).toEqual("ден от седмицата");
427+
expect(dateFieldName({ type: 'weekday', nameType: 'short' }, 'bg')).toEqual("ден от седмицата");
428+
});
429+
430+
it('should return placeholder for the dayperiod type', () => {
431+
expect(dateFieldName({ type: 'dayperiod', nameType: 'wide' })).toEqual("AM/PM");
432+
expect(dateFieldName({ type: 'dayperiod', nameType: 'narrow' })).toEqual("AM/PM");
433+
expect(dateFieldName({ type: 'dayperiod', nameType: 'short' })).toEqual("AM/PM");
434+
});
435+
436+
it('should return localized placeholder for the dayperiod type', () => {
437+
expect(dateFieldName({ type: 'dayperiod', nameType: 'wide' }, 'bg')).toEqual("пр.об./сл.об.");
438+
expect(dateFieldName({ type: 'dayperiod', nameType: 'narrow' }, 'bg')).toEqual("пр.об./сл.об.");
439+
expect(dateFieldName({ type: 'dayperiod', nameType: 'short' }, 'bg')).toEqual("пр.об./сл.об.");
440+
});
441+
442+
it('should return placeholder for hour type', () => {
443+
expect(dateFieldName({ type: 'hour', nameType: 'wide' })).toEqual("hour");
444+
expect(dateFieldName({ type: 'hour', nameType: 'narrow' })).toEqual("hr.");
445+
expect(dateFieldName({ type: 'hour', nameType: 'short' })).toEqual("hr.");
446+
});
447+
448+
it('should return localized placeholder for hour type', () => {
449+
expect(dateFieldName({ type: 'hour', nameType: 'wide' }, 'bg')).toEqual("час");
450+
expect(dateFieldName({ type: 'hour', nameType: 'narrow' }, 'bg')).toEqual("ч");
451+
expect(dateFieldName({ type: 'hour', nameType: 'short' }, 'bg')).toEqual("ч");
452+
});
453+
454+
it('should return placeholder for minute type', () => {
455+
expect(dateFieldName({ type: 'minute', nameType: 'wide' })).toEqual("minute");
456+
expect(dateFieldName({ type: 'minute', nameType: 'narrow' })).toEqual("min.");
457+
expect(dateFieldName({ type: 'minute', nameType: 'short' })).toEqual("min.");
458+
});
459+
460+
it('should return placeholder for second type', () => {
461+
expect(dateFieldName({ type: 'second', nameType: 'wide' })).toEqual("second");
462+
expect(dateFieldName({ type: 'second', nameType: 'narrow' })).toEqual("sec.");
463+
expect(dateFieldName({ type: 'second', nameType: 'short' })).toEqual("sec.");
464+
});
465+
466+
it('should return placeholder for zone type', () => {
467+
expect(dateFieldName({ type: 'zone', nameType: 'wide' })).toEqual("time zone");
468+
expect(dateFieldName({ type: 'zone', nameType: 'narrow' })).toEqual("time zone");
469+
expect(dateFieldName({ type: 'zone', nameType: 'short' })).toEqual("time zone");
470+
});
471+
472+
it('should return undefined for missing fieldName type', () => {
473+
expect(dateFieldName({ type: 'millisecond', nameType: 'wide' })).toEqual(undefined);
474+
expect(dateFieldName({ type: 'millisecond', nameType: 'narrow' })).toEqual(undefined);
475+
expect(dateFieldName({ type: 'millisecond', nameType: 'short' })).toEqual(undefined);
476+
});
477+
478+
it('should return wide placeholder by default', () => {
479+
expect(dateFieldName({ type: 'year' })).toEqual('year');
480+
});
481+
});
482+
369483
describe('firstDay', () => {
370484
it('should return first day name based on locale', () => {
371485
expect(firstDay('en')).toBe(0);

test/dates.js

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

66
const likelySubtags = require("cldr-data/supplemental/likelySubtags.json");
77
const timeZoneNames = require("cldr-data/main/bg/timeZoneNames.json");
88
const calendar = require("cldr-data/main/bg/ca-gregorian.json");
99

10-
load(likelySubtags, timeZoneNames, calendar, require("cldr-data/main/ko/timeZoneNames.json"), require("cldr-data/main/ko/ca-gregorian.json"), require("cldr-data/supplemental/weekData.json"), require("cldr-data/main/ko/dateFields.json"));
10+
load(likelySubtags, timeZoneNames, calendar, require("cldr-data/main/ko/timeZoneNames.json"), require("cldr-data/main/ko/ca-gregorian.json"), require("cldr-data/supplemental/weekData.json"));
1111

1212
Date.prototype.getTimezoneOffset = function() {
1313
return -120;
@@ -24,74 +24,6 @@ function date(year, month, day, hour, minute, second, millisecond) {
2424
return d;
2525
}
2626

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

0 commit comments

Comments
 (0)