File tree Expand file tree Collapse file tree 2 files changed +22
-33
lines changed Expand file tree Collapse file tree 2 files changed +22
-33
lines changed Original file line number Diff line number Diff line change @@ -11,21 +11,21 @@ const quoteRecords = await getCollection("quoteRecords");
11
11
const quoteDates = new Set (
12
12
quoteRecords . flatMap ( ( quoteRecord ) => Object . keys ( quoteRecord . data ) ) ,
13
13
) ;
14
- const quoteYears = new Set (
14
+ export const quoteYears = new Set (
15
15
[ ...quoteDates ] . map ( ( date ) => new Date ( date ) . getUTCFullYear ( ) ) ,
16
16
) ;
17
17
18
- export const getStaticPaths = ( ( ) => {
19
- const quotesByYearBySymbol = new Map (
20
- quoteRecords . map ( ( quoteRecord ) => {
21
- const quotes = Object . entries ( quoteRecord . data ) ;
22
- return [
23
- quoteRecord . id ,
24
- Map . groupBy ( quotes , ( [ date ] ) => new Date ( date ) . getUTCFullYear ( ) ) ,
25
- ] ;
26
- } ) ,
27
- ) ;
18
+ export const quotesByYearBySymbol = new Map (
19
+ quoteRecords . map ( ( quoteRecord ) => {
20
+ const quotes = Object . entries ( quoteRecord . data ) ;
21
+ return [
22
+ quoteRecord . id ,
23
+ Map . groupBy ( quotes , ( [ date ] ) => new Date ( date ) . getUTCFullYear ( ) ) ,
24
+ ] ;
25
+ } ) ,
26
+ ) ;
28
27
28
+ export const getStaticPaths = ( ( ) => {
29
29
return [ ...quoteYears ] . map ( ( year ) => ( {
30
30
params : { year : year . toString ( ) } ,
31
31
props : Object . fromEntries (
Original file line number Diff line number Diff line change 1
1
import type { APIRoute } from "astro" ;
2
- import { getCollection } from "astro:content" ;
3
2
4
- const quoteRecords = await getCollection ( "quoteRecords" ) ;
5
-
6
- const symbolsByYear = new Map < number , string [ ] > ( ) ;
7
- for ( const quoteRecord of quoteRecords ) {
8
- const years = [
9
- ...new Set (
10
- Object . keys ( quoteRecord . data ) . map ( ( date ) =>
11
- new Date ( date ) . getUTCFullYear ( ) ,
12
- ) ,
13
- ) ,
14
- ] ;
15
- for ( const year of years ) {
16
- let symbols = symbolsByYear . get ( year ) ;
17
- if ( symbols == null ) {
18
- symbols = [ ] ;
19
- symbolsByYear . set ( year , symbols ) ;
20
- }
21
- symbols . push ( quoteRecord . id ) ;
22
- }
23
- }
3
+ import { quoteYears , quotesByYearBySymbol } from "./[year].json" ;
24
4
25
5
export const GET : APIRoute = ( ) =>
26
- Response . json ( Object . fromEntries ( symbolsByYear ) ) ;
6
+ Response . json (
7
+ Object . fromEntries (
8
+ [ ...quoteYears ] . map ( ( year ) => [
9
+ year ,
10
+ [ ...quotesByYearBySymbol ]
11
+ . filter ( ( [ , quotesByYear ] ) => quotesByYear . has ( year ) )
12
+ . map ( ( [ symbol ] ) => symbol ) ,
13
+ ] ) ,
14
+ ) ,
15
+ ) ;
You can’t perform that action at this time.
0 commit comments