@@ -101,6 +101,25 @@ function handleImgError(event) {
101101 imgNode . setAttribute ( "src" , SmallIcon ) ;
102102}
103103
104+ function filterByAge ( result , age ) {
105+ const now = Date . now ( ) ;
106+ return result . filter (
107+ ( item ) =>
108+ item . firstDonation && now - new Date ( item . firstDonation ) . getTime ( ) < age ,
109+ ) ;
110+ }
111+
112+ function shuffleSlice ( arr , n ) {
113+ const result = [ ...arr ] ;
114+ for ( let i = 0 ; i < n ; i ++ ) {
115+ const other = Math . floor ( Math . random ( ) * ( result . length - i ) ) ;
116+ const temp = result [ other ] ;
117+ result [ other ] = result [ i ] ;
118+ result [ i ] = temp ;
119+ }
120+ return result . slice ( 0 , n ) ;
121+ }
122+
104123export default function Support ( { rank, type } ) {
105124 const [ inView , setInView ] = useState ( false ) ;
106125 const containerRef = useRef ( null ) ;
@@ -156,30 +175,15 @@ export default function Support({ rank, type }) {
156175 }
157176
158177 if ( typeof age === "number" ) {
159- // eslint-disable-next-line react-hooks/purity
160- const now = Date . now ( ) ;
161- result = result . filter (
162- ( item ) =>
163- item . firstDonation &&
164- now - new Date ( item . firstDonation ) . getTime ( ) < age ,
165- ) ;
178+ result = filterByAge ( result , age ) ;
166179 }
167180
168181 if ( typeof lim === "number" ) {
169182 result = result . slice ( 0 , lim ) ;
170183 }
171184
172185 if ( typeof rand === "number" && result . length >= rand ) {
173- // Pick n random items
174- result = [ ...result ] ;
175- for ( let i = 0 ; i < rand ; i ++ ) {
176- // eslint-disable-next-line react-hooks/purity
177- const other = Math . floor ( Math . random ( ) * ( result . length - i ) ) ;
178- const temp = result [ other ] ;
179- result [ other ] = result [ i ] ;
180- result [ i ] = temp ;
181- }
182- result = result . slice ( 0 , rand ) ;
186+ result = shuffleSlice ( result , rand ) ;
183187 }
184188
185189 // resort to keep order
0 commit comments