mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Simplified code and removed duplication
This commit is contained in:
parent
1b6028ae6d
commit
ef42dcd666
1 changed files with 6 additions and 8 deletions
|
@ -32,7 +32,7 @@ const STEPS_MAP = {
|
||||||
hourly: 'Hour',
|
hourly: 'Hour',
|
||||||
};
|
};
|
||||||
|
|
||||||
const STEP_TO_DIFF_UNIT_MAP = {
|
const STEP_TO_DATE_UNIT_MAP = {
|
||||||
hourly: 'hour',
|
hourly: 'hour',
|
||||||
daily: 'day',
|
daily: 'day',
|
||||||
weekly: 'week',
|
weekly: 'week',
|
||||||
|
@ -60,17 +60,15 @@ const groupVisitsByStep = (step, visits) => visits.reduce((acc, visit) => {
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
const generateLabels = (step, visits) => {
|
const generateLabels = (step, visits) => {
|
||||||
|
const unit = STEP_TO_DATE_UNIT_MAP[step];
|
||||||
|
const formatter = STEP_TO_DATE_FORMAT[step];
|
||||||
const newerDate = moment(visits[0].date);
|
const newerDate = moment(visits[0].date);
|
||||||
const oldestDate = moment(visits[visits.length - 1].date);
|
const oldestDate = moment(visits[visits.length - 1].date);
|
||||||
const size = newerDate.diff(oldestDate, STEP_TO_DIFF_UNIT_MAP[step]);
|
const size = newerDate.diff(oldestDate, unit);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
STEP_TO_DATE_FORMAT[step](oldestDate),
|
formatter(oldestDate),
|
||||||
...rangeOf(size, () => {
|
...rangeOf(size, () => formatter(oldestDate.add(1, unit))),
|
||||||
const date = oldestDate.add(1, STEP_TO_DIFF_UNIT_MAP[step]);
|
|
||||||
|
|
||||||
return STEP_TO_DATE_FORMAT[step](date);
|
|
||||||
}),
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue