Skip to content

Commit

Permalink
Merge 8686094 into 99bcaaf
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiter committed Nov 12, 2018
2 parents 99bcaaf + 8686094 commit 457da00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import moment from 'moment';
import RelativeRange from './relative-range';
import type { FormatStaticOptionsType } from './relative-range';

export const DEFAULT_STATIC_RANGE_LOCALE = {
separator: '-',
Expand Down Expand Up @@ -36,7 +37,11 @@ moment.updateLocale('en', {
relativeRange: DEFAULT_RELATIVE_RANGE_LOCALE,
});

const formatStatic = (range: RelativeRange, format: string = 'll') => {
const formatStatic = (
range: RelativeRange,
format: string = 'll',
options: FormatStaticOptionsType = {},
) => {
const {
date,
start,
Expand All @@ -47,6 +52,9 @@ const formatStatic = (range: RelativeRange, format: string = 'll') => {
otherYear: otherYearConfig = '%s YYYY',
// eslint-disable-next-line no-underscore-dangle
} = moment.localeData()._config.staticRange || DEFAULT_STATIC_RANGE_LOCALE;
const {
attemptYearHiding = true,
} = options;

const result = [];

Expand Down Expand Up @@ -78,16 +86,16 @@ const formatStatic = (range: RelativeRange, format: string = 'll') => {
endFormat = longMonthFormat.replace(/([^D.]*)(M+)([^D.]*)/, '');
}
} else {
startFormat = startThisYear && sameYear ? longMonthFormat : longDateFormat;
endFormat = endThisYear && sameYear ? longMonthFormat : longDateFormat;
startFormat = startThisYear && sameYear && attemptYearHiding ? longMonthFormat : longDateFormat;
endFormat = endThisYear && sameYear && attemptYearHiding ? longMonthFormat : longDateFormat;
}

result.push(start.format(startFormat));

if (!sameDay) {
result.push(separator);

if (monthsMergable && !endThisYear && endFormat !== longDateFormat) {
if (monthsMergable && (!endThisYear || !attemptYearHiding) && endFormat !== longDateFormat) {
endFormat = otherYearConfig.replace('%s', endFormat);
}
result.push(end.format(endFormat));
Expand Down
8 changes: 6 additions & 2 deletions src/relative-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const DAY_FORMAT = 'YYYY-MM-DD';

type RangeSchemaTypeEnum = typeof Date | typeof String | typeof Number | typeof Boolean;

export type FormatStaticOptionsType = {
attemptYearHiding?: boolean;
}

function isDateType(Type: RangeSchemaTypeEnum): boolean {
return Object.prototype.toString.call(new Type()) === '[object Date]';
}
Expand Down Expand Up @@ -304,13 +308,13 @@ class RelativeRange {
return parts.every((key: RangePartEnum) => this.data[key]);
}

format(format?: string): string {
format(format?: string, options?: FormatStaticOptionsType): string {
switch (format) {
case 'R':
case 'RR':
return formatRelative(this, format);
default:
return formatStatic(this, format);
return formatStatic(this, format, options);
}
}

Expand Down
32 changes: 30 additions & 2 deletions tests/format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ describe('RelativeRange#format', function () {
nl: '1 jan.',
es: '1 de ene.',
},
}, {
range: range.current('day'),
format: 'll',
locales: {
en: 'Jan 1, 3000',
nl: '1 jan. 3000',
es: '1 de ene. de 3000',
},
options: {
attemptYearHiding: false,
},
}, {
range: range.next(1, 'day'),
format: 'll',
Expand All @@ -100,6 +111,17 @@ describe('RelativeRange#format', function () {
nl: '1 t/m 28 feb.',
es: '1 al 28 de feb.',
},
}, {
range: range.next('month'),
format: 'll',
locales: {
en: 'Feb 1 - 28, 3000',
nl: '1 t/m 28 feb. 3000',
es: '1 al 28 de feb. de 3000',
},
options: {
attemptYearHiding: false,
},
}, {
range: range.previous(1, 'month'),
format: 'll',
Expand Down Expand Up @@ -320,6 +342,7 @@ describe('RelativeRange#format', function () {
range: localeRange,
format,
locales,
options,
} = translation;

moment.locale('en');
Expand All @@ -328,6 +351,7 @@ describe('RelativeRange#format', function () {
describe(`(${format}) ${englishFormat}`, function () {
Object.keys(locales).forEach((locale) => {
const expectation = locales[locale];

before(function () {
moment.locale(locale);

Expand All @@ -337,9 +361,13 @@ describe('RelativeRange#format', function () {
}
});

it(`[${locale}]: ${expectation}`, function () {
after(function () {
moment.locale('en');
});

it(`[${locale}]: ${expectation}${options ? ' (with options)' : ''}`, function () {
moment.locale(locale);
const formatted = localeRange.format(format);
const formatted = localeRange.format(format, options);

expect(formatted).to.equal(expectation);
});
Expand Down

0 comments on commit 457da00

Please sign in to comment.