11import { computedEquivalents } from 'src/providers/equivalents'
22
3- const getRandomEquivalent = ( toIgnore : string [ ] , category ?: number ) => {
4- const meaningfullEquivalents = computedEquivalents
3+ const getRandomEquivalent = ( value : number , toIgnore : string [ ] , category ?: number ) => {
4+ const allEquivalents = computedEquivalents
55 . filter ( ( equivalent ) => equivalent . value )
66 . filter ( ( equivalent ) => ! toIgnore . includes ( equivalent . slug ) )
77
8+ let meaningfullEquivalents = allEquivalents . filter (
9+ ( equivalent ) => value / equivalent . value >= 0.1 && value / equivalent . value <= 99_999
10+ )
11+ if ( meaningfullEquivalents . length === 0 ) {
12+ meaningfullEquivalents = allEquivalents
13+ }
14+
815 let categoryEquivalents : typeof computedEquivalents = [ ]
916 if ( category ) {
1017 categoryEquivalents = meaningfullEquivalents . filter ( ( equivalent ) => equivalent . category === category )
1118 }
19+
1220 if ( categoryEquivalents . length === 0 ) {
1321 const categories = [ ...new Set ( meaningfullEquivalents . map ( ( equivalent ) => equivalent . category ) ) ]
1422 const randomCategory = categories [ Math . floor ( Math . random ( ) * categories . length ) ]
@@ -18,40 +26,46 @@ const getRandomEquivalent = (toIgnore: string[], category?: number) => {
1826 return categoryEquivalents [ Math . floor ( Math . random ( ) * categoryEquivalents . length ) ] . slug
1927}
2028
21- export const getRandomEquivalents = ( current : string | undefined , length : number ) => {
22- const casPratique = getRandomEquivalent ( current ? [ current ] : [ ] , 13 )
29+ export const getRandomEquivalents = ( value : number , current : string | undefined , length : number ) => {
30+ const casPratique = getRandomEquivalent ( value , current ? [ current ] : [ ] , 13 )
2331 if ( length === 1 ) {
2432 return [ casPratique ]
2533 }
26- const numerique = getRandomEquivalent ( current ? [ current ] : [ ] , 1 )
34+ const numerique = getRandomEquivalent ( value , current ? [ casPratique , current ] : [ casPratique ] , 1 )
2735 if ( length === 2 ) {
2836 return [ casPratique , numerique ]
2937 }
3038
31- const repas = getRandomEquivalent ( current ? [ current ] : [ ] , 2 )
39+ const repas = getRandomEquivalent ( value , current ? [ casPratique , numerique , current ] : [ casPratique , numerique ] , 2 )
3240 if ( length === 3 ) {
3341 return [ casPratique , numerique , repas ]
3442 }
3543
3644 const objects = [ casPratique , numerique , repas ]
3745 for ( let i = 3 ; i < length ; i ++ ) {
38- objects . push ( getRandomEquivalent ( current ? [ ...objects , current ] : objects ) )
46+ objects . push ( getRandomEquivalent ( value , current ? [ ...objects , current ] : objects ) )
3947 }
4048 return objects
4149}
4250
43- export const getFullRandomEquivalents = ( ) => {
51+ export const getFullRandomEquivalents = ( value : number ) => {
4452 const objects : string [ ] = [ ]
4553 for ( let i = 0 ; i < 3 ; i ++ ) {
46- objects . push ( getRandomEquivalent ( objects ) )
54+ objects . push ( getRandomEquivalent ( value , objects ) )
4755 }
4856 return objects
4957}
5058
51- export const getRandomEquivalentsInCategory = ( current : string | undefined , category : number ) => {
52- const meaningfullEquivalents = computedEquivalents . filter (
59+ export const getRandomEquivalentsInCategory = ( value : number , current : string | undefined , category : number ) => {
60+ const allEquivalents = computedEquivalents . filter (
5361 ( equivalent ) => equivalent . category === category && current !== equivalent . slug
5462 )
63+ let meaningfullEquivalents = allEquivalents . filter (
64+ ( equivalent ) => value / equivalent . value >= 0.1 && value / equivalent . value <= 99_999
65+ )
66+ if ( meaningfullEquivalents . length === 0 ) {
67+ meaningfullEquivalents = allEquivalents
68+ }
5569 const shuffled = [ ...meaningfullEquivalents ] . sort ( ( ) => 0.5 - Math . random ( ) )
5670 return shuffled . slice ( 0 , Math . min ( meaningfullEquivalents . length , 8 ) )
5771}
0 commit comments