Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ejemplo: (con base en `new Date(2020,0,1)` como fecha de entrada)
{ label: '2019', startAt: '2019/01/01', endAt: '2019/12/31' },
{ label: '2018', startAt: '2018/01/01', endAt: '2018/12/31' },
{ label: '2017', startAt: '2017/01/01', endAt: '2017/12/31' },

{ label: 'diciembre', startAt: '2019/12/01', endAt: '2019/12/31' },
{ label: 'noviembre', startAt: '2019/11/01', endAt: '2019/11/30' },
{ label: 'octubre', startAt: '2019/10/01', endAt: '2019/10/31' }
Expand Down
57 changes: 56 additions & 1 deletion src/filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
const sub = require('date-fns/sub');
const format = require('date-fns/format');
const startOfMonth = require('date-fns/startOfMonth')
const endOfMonth = require('date-fns/endOfMonth')
const startOfYear = require('date-fns/startOfYear')
const endOfYear = require('date-fns/endOfYear')

const EXPECTED_FORMAT = 'yyyy/MM/dd';

const makeFilter = (date) => {
let filters = [];
const filters = [];
const days = [7,28,90,365];

const objGen = (label,startAt,endAt)=>{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puedes hacer una clase

return {
label: label,
startAt: startAt,
endAt: endAt
}
}

const filterObj = (label, date, subtract) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usa nombres más descriptivos a la función que desempeña

const newFilter = objGen(
label,
format(sub(date, { days: subtract }), EXPECTED_FORMAT),
format(date, EXPECTED_FORMAT)
)
return newFilter;
}

const filtersByAmountDays = (i) =>{
const filter = filterObj(`Últimos ${i} días`, date, i);
filters.push(filter);
}

days.forEach(filtersByAmountDays);

for (let i = 1; i <= 3; i++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usas funciones y de momento cambias ciclos. Esto puede ser confuso para alguien que lea tu código.

const yearlyRange = sub(date, {years: i});
const obj = objGen(
format(yearlyRange, 'yyyy'),
format(startOfYear(yearlyRange),EXPECTED_FORMAT),
format(endOfYear(yearlyRange),EXPECTED_FORMAT)
)
filters.push(obj);
}

for (let i = 1; i <= 3; i++) {
const monthlyRange = sub(date, {months: i});
const obj = objGen(
monthlyRange.toLocaleString('es-CO',{month:"long"}),
format(startOfMonth(monthlyRange), EXPECTED_FORMAT),
format(endOfMonth(monthlyRange), EXPECTED_FORMAT)
)
filters.push(obj);
}

return filters;
}

Expand Down