import { TypedArray , DemographicCounts , IStaticMetadata , IStaticFile , MetricsList , VotingMetricsList , VotingMetricField , DemographicsGroup } from "../shared/entities" ; import { CORE_METRIC_FIELDS , DEMOGRAPHIC_FIELDS_ORDER } from "./constants" ; export function getAllIndices ( arrayBuf , vals ) { let indices = [ ] ; arrayBuf . forEach ( ( el , ind ) => { if ( vals . has ( el ) ) { indices . push ( ind ) ; } } ) ; return indices ; } export function getAllBaseIndices ( descGeoLevels , levelIndex , vals ) { if ( vals . length === 0 || levelIndex === descGeoLevels . length ) { return vals ; } return getAllBaseIndices ( descGeoLevels , levelIndex + 1 , getAllIndices ( descGeoLevels [ levelIndex ] , new Set ( vals ) ) ) ; } export function getDemographics ( baseIndices , staticMetadata , staticDemographics ) { return getAggregatedCounts ( baseIndices , staticMetadata , staticDemographics , staticMetadata . demographics ) ; } export function getVoting ( baseIndices , staticMetadata , staticVoting ) { return staticMetadata . voting ? getAggregatedCounts ( baseIndices , staticMetadata , staticVoting , staticMetadata . voting ) : { } ; } export function getAggregatedCounts ( baseIndices , staticMetadata , staticFiles , fileProperties ) { return fileProperties . reduce ( ( data , props , ind ) => { let count = 0 ; baseIndices . forEach ( ( v ) => { if ( ! isNaN ( staticFiles [ ind ] [ v ] ) ) { count += staticFiles [ ind ] [ v ] ; } } ) ; return { ... data , [ props . id ] : count } ; } , { } as DemographicCounts ) ; } export function getDemographicLabel ( id ) { return id === "native" ? "Native American" : id === "pacific" ? "Pacific Islander" : id . split ( /(?=[A-Z])/ ) . join ( " " ) ; } export const getMetricFieldForDemographicsId = ( id ) => id === "population" ? id : `${ id }Population` ; export function getDemographicsMetricFields ( staticMetadata ) { const data = staticMetadata . demographics . flatMap ( file => CORE_METRIC_FIELDS . includes ( file . id ) ? [ ] : [ [ file . id , getMetricFieldForDemographicsId ( file . id ) ] ] ) ; const order = staticMetadata . demographicsGroups ?. flatMap ( g => g . total ? [ g . total , ... g . subgroups ] : g . subgroups ) || DEMOGRAPHIC_FIELDS_ORDER ; data . sort ( ( [ a ] , [ b ] ) => order . indexOf ( a ) - order . indexOf ( b ) ) ; return data ; } export function getVotingMetricFields ( staticMetadata ) { const data = staticMetadata . voting ?. flatMap ( file => { const field = file . id === "democrat" || file . id === "democrat16" ? "dem16" : file . id === "republican" || file . id === "republican16" ? "rep16" : file . id === "other party" || file . id === "other party16" ? "other16" : file . id === "democrat20" ? "dem20" : file . id === "republican20" ? "rep20" : file . id === "other party20" ? "other20" : undefined ; return field !== undefined ? [ [ file . id , field ] ] : [ ] ; } ) || [ ] ; const order = [ "dem16" , "rep16" , "other16" , "dem20" , "rep20" , "other20" ] ; data . sort ( ( [ , a ] , [ , b ] ) => order . indexOf ( a ) - order . indexOf ( b ) ) ; return data ; } export function getDemographicsGroups ( staticMetadata ) { const demographicsMetricFields = getDemographicsMetricFields ( staticMetadata ) ; return ( staticMetadata . demographicsGroups || [ { total : "population" , subgroups : demographicsMetricFields ?. map ( ( [ id ] ) => id ) || [ ] } ] ) ; }