Skip to content

Commit

Permalink
remove hours to en lang and fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Colom committed Jan 24, 2025
1 parent a860733 commit 36063c5
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

[Unreleased]

## [1.2.0] - 2025-01-24

### Removed

- word hours only for en locale

## [1.1.0] - 2025-01-21

### Changed
Expand Down
6 changes: 4 additions & 2 deletions lib/helpers/formatRangeDates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import isSameDay from './isSameDay.js';
import sameDayRangeDate from './sameDayRangeDate.js';
import formatDate from './formatDate.js';
import translate from './translate.js';
import isSameYear from './isSameYear.js';

/**
Expand Down Expand Up @@ -29,10 +30,11 @@ const formatRangeDates = (dates = [], options) => {
? options?.locale?.customFormat?.singleDate
: options?.locale?.customFormat?.dateWithYear;

return [firstDate, secondDate]
const dateFormatted = [firstDate, secondDate]
.map((date) => formatDate(date, formatFromLocale, options))
.join(' - ')
.concat(' hs');
.concat(' hours');
return translate(dateFormatted, options.locale);
};

export default formatRangeDates;
9 changes: 6 additions & 3 deletions lib/helpers/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
* @returns The `translate` function is being returned.
*/
const translate = (string, locale) => {
return string.replace(/\b[a-zA-Z]+\b/g, (word) => {
return locale?.words?.[word] || word;
});
return string
.replace(/\b[a-zA-Z]+\b/g, (word) => {
const translation = locale?.words?.[word];
return translation !== undefined ? translation : word;
})
.trim();
};

export default translate;
4 changes: 2 additions & 2 deletions lib/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export default {
singleDate: 'MMM dd, HH:mm'
},
words: {
hours: 'hs',
hours: '',
from: 'from',
to: 'to',
today: 'today',
Today: 'today',
Today: 'Today',
tomorrow: 'tomorrow',
Tomorrow: 'Tomorrow',
at: 'at',
Expand Down
144 changes: 141 additions & 3 deletions tests/formatDelivery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ describe('formatDelivery method', () => {
describe('with lang en', () => {
const Dates = new DateHandler({ locale: 'en' });
it('expect to return formatted date with string or one string inside array', () => {
assert.equal(Dates.formatDelivery(['2024-10-15T22:50:03.553Z']), 'Until Oct 15, 19:50 hs');
assert.equal(Dates.formatDelivery(['2024-10-15T22:50:03.553Z']), 'Until Oct 15, 19:50');
});

it('expect to return relative date when is today or tomorrow', () => {
assert.equal(Dates.formatDelivery('2024-10-18T22:50:03.553Z'), 'Until today, 19:50 hs');
assert.equal(Dates.formatDelivery('2024-10-19T22:50:03.553Z'), 'Until tomorrow, 19:50 hs');
assert.equal(Dates.formatDelivery('2024-10-18T22:50:03.553Z'), 'Until today, 19:50');
assert.equal(Dates.formatDelivery('2024-10-19T22:50:03.553Z'), 'Until tomorrow, 19:50');
});
});

Expand Down Expand Up @@ -133,5 +133,143 @@ describe('formatDelivery method', () => {
);
});
});

describe('with lang en', () => {
const Dates = new DateHandler({ locale: 'en' });

it('expect to return relative date when the two dates are equals', () => {
assert.equal(
Dates.formatDelivery(['2024-10-18T22:50:03.553Z', '2024-10-18T22:50:03.553Z']),
'Until today, 19:50'
);

assert.equal(
Dates.formatDelivery(['2024-10-19T22:50:03.553Z', '2024-10-19T22:50:03.553Z']),
'Until tomorrow, 19:50'
);
});

it('expect to return range date when is relative date and range time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-18T20:50:03.553Z', '2024-10-18T22:50:03.553Z']),
'Today, 17:50 - 19:50'
);

assert.equal(
Dates.formatDelivery(['2024-10-19T20:50:03.553Z', '2024-10-19T22:50:03.553Z']),
'Tomorrow, 17:50 - 19:50'
);
});

it('expect to return range date when is not relative date and single time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2024-10-21T22:50:03.553Z']),
'Oct 20, 19:50 - Oct 21, 19:50'
);
});

it('expect to return range date when is not relative but same date with range time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2024-10-20T23:50:03.553Z']),
'Oct 20, 19:50 - 20:50'
);
});

it('expect to return range date when is not relative date with range time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2024-10-21T23:50:03.553Z']),
'Oct 20, 19:50 - Oct 21, 20:50'
);
});

it('expect to return range date with year when dates are from different year', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2025-10-21T23:50:03.553Z']),
'Oct 20 2024, 19:50 - Oct 21 2025, 20:50'
);
});

it('expect to return range date with not undefined value and take first two value ', () => {
assert.equal(
Dates.formatDelivery([
'2024-10-20T22:50:03.553Z',
undefined,
'2024-10-21T23:50:03.553Z',
[],
'2024-10-21T23:50:03.553Z'
]),
'Oct 20, 19:50 - Oct 21, 20:50'
);
});
});

describe('with lang pt', () => {
const Dates = new DateHandler({ locale: 'pt' });

it('expect to return relative date when the two dates are equals', () => {
assert.equal(
Dates.formatDelivery(['2024-10-18T22:50:03.553Z', '2024-10-18T22:50:03.553Z']),
'Até hoje, 19:50 hs'
);

assert.equal(
Dates.formatDelivery(['2024-10-19T22:50:03.553Z', '2024-10-19T22:50:03.553Z']),
'Até amanhã, 19:50 hs'
);
});

it('expect to return range date when is relative date and range time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-18T20:50:03.553Z', '2024-10-18T22:50:03.553Z']),
'Hoje, 17:50 - 19:50 hs'
);

assert.equal(
Dates.formatDelivery(['2024-10-19T20:50:03.553Z', '2024-10-19T22:50:03.553Z']),
'Amanhã, 17:50 - 19:50 hs'
);
});

it('expect to return range date when is not relative date and single time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2024-10-21T22:50:03.553Z']),
'20 out, 19:50 - 21 out, 19:50 hs'
);
});

it('expect to return range date when is not relative but same date with range time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2024-10-20T23:50:03.553Z']),
'20 out, 19:50 - 20:50 hs'
);
});

it('expect to return range date when is not relative date with range time', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2024-10-21T23:50:03.553Z']),
'20 out, 19:50 - 21 out, 20:50 hs'
);
});

it('expect to return range date with year when dates are from different year', () => {
assert.equal(
Dates.formatDelivery(['2024-10-20T22:50:03.553Z', '2025-10-21T23:50:03.553Z']),
'20 out 2024, 19:50 - 21 out 2025, 20:50 hs'
);
});

it('expect to return range date with not undefined value and take first two value ', () => {
assert.equal(
Dates.formatDelivery([
'2024-10-20T22:50:03.553Z',
undefined,
'2024-10-21T23:50:03.553Z',
[],
'2024-10-21T23:50:03.553Z'
]),
'20 out, 19:50 - 21 out, 20:50 hs'
);
});
});
});
});

0 comments on commit 36063c5

Please sign in to comment.