Skip to content

Commit

Permalink
Merge branch 'release/0.7.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveryh committed Mar 29, 2022
2 parents 77c1880 + 373a013 commit f9156b5
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clear-habits-client",
"version": "0.7.1",
"version": "0.7.2",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
36 changes: 35 additions & 1 deletion client/src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ const getWeekNumber = d => {
return [year, weekNo]
}

const monthSpread = (endDate, numMonths) => {
const endDateObj = new Date(endDate)
const endMonth = endDateObj.getMonth() + 1
const endYear = endDateObj.getYear() + 1900
const startMonth = (12 % (endMonth - numMonths)) + 1
const startYear = endYear - Math.floor(numMonths / 12)
const endMonthString = `${endYear}-${String(endMonth).padStart(2, '0')}`
const startMonthString = `${startYear}-${String(startMonth).padStart(2, '0')}`
return monthSpreadSequential(startMonthString, endMonthString)
}

const weekSpread = (endDate, numWeeks) => {
if (endDate != null) {
const endDateObj = new Date(endDate)
Expand Down Expand Up @@ -56,4 +67,27 @@ const weekSpreadSequential = (startYearWeek, endYearWeek) => {
}, [])
}

module.exports = { weekSpread, weekSpreadSequential }
const monthSpreadSequential = (startYearMonth, endYearMonth) => {
const startYear = Number(startYearMonth.slice(0, 4))
const startMonth = Number(startYearMonth.slice(5, 7))
const endYear = Number(endYearMonth.slice(0, 4))
const endMonth = Number(endYearMonth.slice(5, 7))
const years =
startYear != endYear
? [...Array(endYear - startYear + 1).keys()].map(i => endYear + i - 1)
: [startYear]
return years.reduce((monthList, year) => {
const numMonthsInYear = 12
let monthsInYear = Array.from({ length: numMonthsInYear }, (_, ac) => ac + 1)
if (year == startYear) {
monthsInYear = monthsInYear.filter(a => a >= startMonth)
}
if (year == endYear) {
monthsInYear = monthsInYear.filter(a => a <= endMonth)
}
monthsInYear = monthsInYear.map(a => `${year}-${String(a).padStart(2, '0')}`)
return [...monthList, ...monthsInYear]
}, [])
}

module.exports = { monthSpread, monthSpreadSequential, weekSpread, weekSpreadSequential }
33 changes: 30 additions & 3 deletions client/src/common/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { weekSpread, weekSpreadSequential } from './utils'
import { monthSpread, monthSpreadSequential, weekSpread, weekSpreadSequential } from './utils'

describe('utils', () => {
describe('weekSpread', () => {
Expand All @@ -23,14 +23,41 @@ describe('utils', () => {
])
})
})
describe('weekSpreadSequential', () => {
describe('monthSpread', () => {
it('basic case', () => {
expect(weekSpreadSequential('2021-01', '2021-03')).toEqual([
expect(monthSpread('2021-05-15', 12)).toEqual([
'2020-06',
'2020-07',
'2020-08',
'2020-09',
'2020-10',
'2020-11',
'2020-12',
'2021-01',
'2021-02',
'2021-03',
'2021-04',
'2021-05',
])
})
})
describe('monthSpreadSequential', () => {
it('basic case', () => {
expect(monthSpreadSequential('2021-01', '2021-03')).toEqual(['2021-01', '2021-02', '2021-03'])
})
it('across years', () => {
expect(monthSpreadSequential('2021-11', '2022-02')).toEqual([
'2021-11',
'2021-12',
'2022-01',
'2022-02',
])
})
})
describe('weekSpreadSequential', () => {
it('basic case', () => {
expect(weekSpreadSequential('2021-01', '2021-03')).toEqual(['2021-01', '2021-02', '2021-03'])
})
it('across years', () => {
expect(weekSpreadSequential('2020-52', '2021-02')).toEqual([
'2020-52',
Expand Down
10 changes: 3 additions & 7 deletions client/src/views/Stats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ let generateQuery = (graphType, level) => {
}
statFilter['entryDate'] = {
greaterThanOrEqualTo: '2021-01-01',
lessThan: '2022-01-01',
lessThan: '2023-01-01',
}
}
groupBy.push(`${level.toUpperCase()}_DESCRIPTION`)
Expand Down Expand Up @@ -397,18 +397,14 @@ export default {
this.$apollo.queries.statsPieCategory.refetch()
},
weekSpread: utils.weekSpread,
monthSpread(year) {
return [...Array(12).keys()].map(
(j) => `${year}-${String(j + 1).padStart(2, '0')}`,
)
},
monthSpread: utils.monthSpread,
getSpread() {
if (this.period == 'daily') {
return this.dateSpread(this.settings.startDate)
} else if (this.period == 'weekly') {
return this.weekSpread(this.settings.startDate, this.numWeeks)
} else if (this.period == 'monthly') {
return this.monthSpread(2021)
return this.monthSpread(this.settings.startDate, 12)
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion server-postgraphile/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server-postgraphile/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clear-habits-server",
"version": "0.7.0",
"version": "0.7.2",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f9156b5

Please sign in to comment.