-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export { createYearBuckets } | ||
|
||
function sortByDateOrYear(a, b) { | ||
if (a.hasOwnProperty('year') && b.hasOwnProperty('year')) { | ||
return new Date(b.year) - new Date(a.year) | ||
} else { | ||
return new Date(b.attributes.date) - new Date(a.attributes.date) | ||
} | ||
} | ||
|
||
function createYearBuckets(entries) { | ||
const bucketsYear = {} | ||
|
||
// sort entries by date in descending order | ||
entries.sort(sortByDateOrYear) | ||
|
||
// loop them to gather them into yearly buckets | ||
for (const entry of entries) { | ||
const { attributes } = entry | ||
const { date } = attributes | ||
|
||
const year = String(new Date(date).getFullYear()) | ||
if (bucketsYear[year] === undefined) { | ||
bucketsYear[year] = { | ||
year: year, | ||
items: [entry] | ||
} | ||
} else { | ||
bucketsYear[year].items.push(entry) | ||
} | ||
} | ||
|
||
// loop over the yearly buckets and sort those in yearly descending order | ||
const yearlyItems = Object.values(bucketsYear) | ||
yearlyItems.sort(sortByDateOrYear) | ||
|
||
return yearlyItems | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters