import { SearchResult , HitDatabaseEntry , RequesterAttributes , TOpticonResponse , TOpticonRequester , Requester , RequesterMap , TOpticonAttributes , AttributeWeights , GroupId } from '../types' ; import { turkopticonBaseUrl } from '../constants/urls' ; import { Map } from 'immutable' ; export const noTurkopticon = ( hit ) => ! hit . requester . turkopticon ; export const selectHitRequesterId = ( hit ) => hit . requester . id ; export const selectRequesterId = ( requester ) => requester . id ; export const invalidGroupId = ( hit ) => ! hit . groupId . startsWith ( '[Error:groupId]-' ) ; export const calculateWeightedAverageScore = ( scores , weights ) : number | null => { const categories = filterCategories ( scores ) ; const total = Object . keys ( categories ) . reduce ( ( acc , category ) => acc + categories [ category ] * weights [ `${ category }Weight` ] , 0 ) ; return total / sumOfApplicableWeights ( categories , weights ) || null ; } ; export const objectValueSumation = < T > ( obj ) : number => Object . values ( obj ) . reduce ( ( acc , cur ) : number => acc + cur , 0 ) ; export const sumOfApplicableWeights = ( categories , weights ) : number => Object . keys ( categories ) . reduce ( ( acc , cur ) => acc + weights [ `${ cur }Weight` ] , 0 ) ; param scores export const filterCategories = ( scores ) : Partial < RequesterAttributes > => Object . keys ( scores ) . reduce ( ( acc , category ) => scores [ category ] !== null ? { ... acc , [ category ] : scores [ category ] } : acc , { } ) ; export const hasAValidScore = ( scores ) => Object . values ( scores ) . some ( value => value !== null ) ; export const topticonMapFromTO = ( data ) : RequesterMap => Object . keys ( data ) . reduce ( ( acc , id ) => data [ id ] ? acc . set ( id , topticonRequesterToRequester ( id , data [ id ] ) ) : acc , Map < string , Requester > ( ) ) ; export const topticonRequesterToRequester = ( id , requester ) : Requester => { const scores = stringScoresToNumbers ( requester . attrs ) ; return { id , name : requester . name , turkopticon : { scores , numReviews : requester . reviews , numTosFlags : requester . tos_flags } } ; } ; const stringScoresToNumbers = ( scores ) : RequesterAttributes => { return Object . keys ( scores ) . filter ( validTurkopticonAttributeScore ) . reduce ( ( acc , category ) : RequesterAttributes => ( { ... acc , [ category ] : + scores [ category ] || null } ) , { } ) ; } ; const validTurkopticonAttributeScore = ( score ) => score !== '0.00' ; export const generateReviewLink = ( hit ) : string => { const { requester } = hit ; return ( turkopticonBaseUrl + 'report?requester[amzn_id]=' + requester . id + '&requester[amzn_name]=' + requester . name + '&report[hit_names]=' + hit . title ) ; } ; export const sortByWeightedTo = ( weightedToMap , minScore ) => ( hit ) : boolean => { if ( ! hit . requester . turkopticon ) { return true ; } const averageScore = weightedToMap . get ( hit . groupId ) ; if ( ! averageScore ) { return true ; } return averageScore >= minScore ; } ;