All files / owid-grapher/grapher/selection SelectionArray.ts

86.52% Statements 154/178
100% Branches 24/24
78.26% Functions 18/23
86.52% Lines 154/178

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 227x 227x 227x 227x 227x 227x 227x 227x 1x 1x 1x 1x 1x 1x 49x 49x 1x 1x 46x 46x 1x 1x 106x 106x 106x 106x 106x 106x 1x 1x             1x 1x 9x 9x 9x 9x 9x 9x 1x 1x 51x 51x 1x 1x 3x 3x 1x 1x 8x 8x 1x 1x 8x 8x 1x 1x 106x 106x 106x 106x 106x 1x 1x 1x 9x 9x 9x 1x 1x 12x 12x 12x 1x 1x 46x 46x 46x 1x 1x             1x 1x 9x 9x 9x 1x 1x 3x 3x 1x 1x 15x 15x 1x 1x         1x 1x 1x 1x 1x 1x         1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x
import {
    EntityCode,
    EntityId,
    EntityName,
} from "../../coreTable/OwidTableConstants"
import { difference, mapBy } from "../../clientUtils/Util"
import { isPresent } from "../../clientUtils/isPresent"
import { action, computed, observable } from "mobx"
 
interface Entity {
    entityName: EntityName
    entityId?: EntityId
    entityCode?: EntityCode
}
 
export class SelectionArray {
    constructor(
        selectedEntityNames: EntityName[] = [],
        availableEntities: Entity[] = [],
        entityType = "country"
    ) {
        this.selectedEntityNames = selectedEntityNames.slice()
        this.availableEntities = availableEntities.slice()
        this.entityType = entityType
    }
 
    @observable entityType: string
    @observable selectedEntityNames: EntityName[]
    @observable private availableEntities: Entity[]
 
    @computed get availableEntityNames(): string[] {
        return this.availableEntities.map((entity) => entity.entityName)
    }
 
    @computed get availableEntityNameSet(): Set<string> {
        return new Set(this.availableEntityNames)
    }
 
    @computed get entityNameToIdMap(): Map<string, number | undefined> {
        return mapBy(
            this.availableEntities,
            (e) => e.entityName,
            (e) => e.entityId
        )
    }
 
    @computed get entityCodeToNameMap(): Map<string | undefined, string> {
        return mapBy(
            this.availableEntities,
            (e) => e.entityCode,
            (e) => e.entityName
        )
    }
 
    @computed get entityIdToNameMap(): Map<number | undefined, string> {
        return mapBy(
            this.availableEntities,
            (e) => e.entityId,
            (e) => e.entityName
        )
    }
 
    @computed get hasSelection(): boolean {
        return this.selectedEntityNames.length > 0
    }
 
    @computed get unselectedEntityNames(): string[] {
        return difference(this.availableEntityNames, this.selectedEntityNames)
    }
 
    @computed get numSelectedEntities(): number {
        return this.selectedEntityNames.length
    }
 
    @computed get selectedSet(): Set<EntityName> {
        return new Set<EntityName>(this.selectedEntityNames)
    }
 
    @computed get allSelectedEntityIds(): EntityId[] {
        const map = this.entityNameToIdMap
        return this.selectedEntityNames
            .map((name) => map.get(name))
            .filter(isPresent)
    }
 
    // Clears and sets selected entities
    @action.bound setSelectedEntities(entityNames: EntityName[]): this {
        this.clearSelection()
        return this.addToSelection(entityNames)
    }
 
    @action.bound addToSelection(entityNames: EntityName[]): this {
        this.selectedEntityNames = this.selectedEntityNames.concat(entityNames)
        return this
    }
 
    @action.bound addAvailableEntityNames(entities: Entity[]): this {
        this.availableEntities.push(...entities)
        return this
    }
 
    @action.bound setSelectedEntitiesByCode(entityCodes: EntityCode[]): this {
        const map = this.entityCodeToNameMap
        const codesInData = entityCodes.filter((code) => map.has(code))
        return this.setSelectedEntities(
            codesInData.map((code) => map.get(code)!)
        )
    }
 
    @action.bound setSelectedEntitiesByEntityId(entityIds: EntityId[]): this {
        const map = this.entityIdToNameMap
        return this.setSelectedEntities(entityIds.map((id) => map.get(id)!))
    }
 
    @action.bound selectAll(): this {
        return this.addToSelection(this.unselectedEntityNames)
    }
 
    @action.bound clearSelection(): void {
        this.selectedEntityNames = []
    }
 
    @action.bound toggleSelection(entityName: EntityName): this {
        return this.selectedSet.has(entityName)
            ? this.deselectEntity(entityName)
            : this.selectEntity(entityName)
    }
 
    @computed get numAvailableEntityNames(): number {
        return this.availableEntityNames.length
    }
 
    @action.bound selectEntity(entityName: EntityName): this {
        if (!this.selectedSet.has(entityName))
            return this.addToSelection([entityName])
        return this
    }
 
    // Mainly for testing
    @action.bound selectSample(howMany = 1): this {
        return this.setSelectedEntities(
            this.availableEntityNames.slice(0, howMany)
        )
    }
 
    @action.bound deselectEntity(entityName: EntityName): this {
        this.selectedEntityNames = this.selectedEntityNames.filter(
            (name) => name !== entityName
        )
        return this
    }
}