All files / owid-grapher/coreTable CoreTableColumns.ts

91.09% Statements 675/741
86.86% Branches 119/137
83.33% Functions 75/90
91.09% Lines 675/741

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 7621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2575x 2575x 2575x 1x 1x 1x 1x     1x 1x 35x 35x 1x 1x 1x 1x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x     1x 1x 26x 26x 1x 1x     1x 1x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 92x 92x 1x 1x 11x 11x 1x 1x 92x 92x 1x 1x 1x 5x 5x 1x 1x 18x 18x 1x 1x 1x 1x 1x 1x 92x 92x 92x 92x 92x 92x 92x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 5x 5x 1x 1x 1x 6x 6x 6x 6x 1x 1x 765x 765x 1x 1x 2x 2x 4x 4x 4x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 1x 1x 1x 1x 2x 2x 1x 1x 131x 131x 1x 1x     1x 1x 1x 1x 1x 1x 1x 460x 460x 460x 1x 1x 332x 332x 332x 332x 1x 1x 137x 137x 137x 1x 1x 18x 18x 1x 1x 2x 2x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 2x 2x 1x 1x 3x 3x 1x 1x 3x 3x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 1x 6x 6x 6x 1x 1x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 1x 1x 1x 66x 66x 1x 1x 1x 66x 66x 1x 1x 1x 4x 4x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 75x 75x 75x 75x 75x 75x 4x 1x 1x 1x 9x 9x 9x 9x 9x 536x 536x 536x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 50x 50x 50x 50x 50x 50x 1x 1x 1x 1x 1x 1x 159x 159x 159x 159x     159x 1x 838x 838x 838x 838x 12x 12x 838x 838x 421x 421x 421x 421x 838x 1x 1x 1x 1x 1x 1x                       829x 829x 829x 829x 94x 94x 94x 94x 94x 94x     829x 829x                   829x 829x 92x 92x 92x 92x 92x 92x 92x 829x 829x 11x 11x 11x 11x 11x 11x 11x 829x 829x 2x 2x 829x 829x 274x 273x 274x 274x 208x 208x 208x 274x 274x 208x 208x 208x 829x 829x 208x 208x 829x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 1x 1x 80x 80x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 68x 68x 68x 68x 68x 68x 1x 1x 1x 1x 1x 1x 1x 1x 68x 68x 68x 68x 68x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 749x 749x 749x 749x 220x 220x 749x 1x 1x 1x 16x 16x 16x 1x 1x 1x 1x 9x 9x 1x 1x     1x 1x 2x 2x 1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 6x 6x 6x 9x 3x 3x 1x 1x 6x 6x 6x 6x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                          
import {
    anyToString,
    csvEscape,
    formatYear,
    formatDay,
    isString,
    last,
    uniq,
    sortNumeric,
    range,
    union,
    dateDiffInDays,
    isNumber,
    omitUndefinedValues,
} from "../clientUtils/Util"
import { isPresent } from "../clientUtils/isPresent"
import { CoreTable } from "./CoreTable"
import {
    CoreRow,
    Time,
    PrimitiveType,
    JsTypes,
    CoreValueType,
} from "./CoreTableConstants"
import { ColumnTypeNames, CoreColumnDef } from "./CoreColumnDef"
import { EntityName, OwidVariableRow } from "./OwidTableConstants" // todo: remove. Should not be on CoreTable
import { ErrorValue, ErrorValueTypes, isNotErrorValue } from "./ErrorValues"
import { getOriginalTimeColumnSlug } from "./OwidTableUtil"
import { imemo } from "./CoreTableUtils"
import moment from "moment"
import { OwidSource } from "../clientUtils/OwidSource"
import { formatValue, TickFormattingOptions } from "../clientUtils/formatValue"
import { OwidVariableDisplayConfigInterface } from "../clientUtils/OwidVariableDisplayConfigInterface"
import { ColumnSlug } from "../clientUtils/owidTypes"
 
interface ColumnSummary {
    numErrorValues: number
    numUniqs: number
    numValues: number
    median: PrimitiveType
    sum: number
    mean: number
    min: PrimitiveType
    max: PrimitiveType
    range: number
    mode: PrimitiveType
    modeSize: number
    deciles: { [decile: number]: PrimitiveType }
}
 
export abstract class AbstractCoreColumn<JS_TYPE extends PrimitiveType> {
    def: CoreColumnDef
    table: CoreTable
 
    constructor(table: CoreTable, def: CoreColumnDef) {
        this.table = table
        this.def = def
    }
 
    abstract jsType: JsTypes
 
    parse(val: any): any {
        return val
    }
 
    @imemo get isMissing(): boolean {
        return this instanceof MissingColumn
    }
 
    get sum(): number | undefined {
        return this.summary.sum
    }
 
    get median(): PrimitiveType | undefined {
        return this.summary.median
    }
 
    get max(): PrimitiveType | undefined {
        return this.summary.max
    }
 
    get min(): PrimitiveType | undefined {
        return this.summary.min
    }
 
    // todo: switch to a lib and/or add tests for this. handle non numerics better.
    @imemo get summary(): Partial<ColumnSummary> {
        const { numErrorValues, numValues, numUniqs } = this
        const basicSummary: Partial<ColumnSummary> = {
            numErrorValues,
            numUniqs,
            numValues,
        }
        if (!numValues) return basicSummary
        const summary: Partial<ColumnSummary> = { ...basicSummary }
        const arr = this.valuesAscending
        const isNumeric = this.jsType === "number"
 
        let min = arr[0]
        let max = arr[0]
        let sum = 0
        let mode = undefined
        let modeSize = 0
        let currentBucketValue = undefined
        let currentBucketSize = 0
        for (let index = 0; index < numValues; index++) {
            const value = arr[index] as any
            sum += value
            if (value > max) max = value
            if (value < min) min = value
            if (value === currentBucketValue) currentBucketSize++
            else {
                currentBucketValue = value
                currentBucketSize = 1
            }
            if (currentBucketSize > modeSize) {
                modeSize = currentBucketSize
                mode = currentBucketValue
            }
        }
 
        const medianIndex = Math.floor(numValues / 2)
        summary.sum = sum
        summary.median = arr[medianIndex]
        summary.mean = sum / numValues
        summary.min = min
        summary.max = max
        summary.range = (max as any) - (min as any)
        summary.mode = mode
        summary.modeSize = modeSize
        if (!isNumeric) {
            summary.sum = undefined
            summary.mean = undefined
        }
 
        summary.deciles = {}
        const deciles = [10, 20, 30, 40, 50, 60, 70, 80, 90, 99, 100]
        deciles.forEach((decile) => {
            let index = Math.floor(numValues * (decile / 100))
            index = index === numValues ? index - 1 : index
            summary.deciles![decile] = arr[index]
        })
 
        return summary
    }
 
    // todo: migrate from unitConversionFactor to computed columns instead. then delete this.
    // note: unitConversionFactor is used >400 times in charts and >800 times in variables!!!
    @imemo get unitConversionFactor(): number {
        return this.display?.conversionFactor ?? 1
    }
 
    @imemo get isAllIntegers(): boolean {
        return false
    }
 
    @imemo get tolerance(): number {
        return this.display?.tolerance ?? this.def.tolerance ?? 0
    }
 
    @imemo get domain(): [JS_TYPE, JS_TYPE] {
        return [this.minValue, this.maxValue]
    }
 
    @imemo get display(): OwidVariableDisplayConfigInterface | undefined {
        return this.def.display
    }
 
    abstract formatValue(value: any, options?: TickFormattingOptions): string
 
    formatValueForMobile(value: any, options?: TickFormattingOptions): string {
        return this.formatValue(value, options)
    }
 
    formatValueShortWithAbbreviations(
        value: any,
        options?: TickFormattingOptions
    ): string {
        return this.formatValue(value, options)
    }
 
    formatValueShort(value: any, options?: TickFormattingOptions): string {
        return this.formatValue(value, options)
    }
 
    formatValueLong(value: any, options?: TickFormattingOptions): string {
        return this.formatValue(value, options)
    }
 
    formatForTick(value: any, options?: TickFormattingOptions): string {
        return this.formatValueShort(value, options)
    }
 
    // A method for formatting for CSV
    formatForCsv(value: JS_TYPE): string {
        return csvEscape(anyToString(value))
    }
 
    @imemo get numDecimalPlaces(): number {
        return this.display?.numDecimalPlaces ?? 2
    }
 
    @imemo get unit(): string | undefined {
        return this.display?.unit ?? this.def.unit
    }
 
    @imemo get shortUnit(): string | undefined {
        const shortUnit =
            this.display?.shortUnit ?? this.def.shortUnit ?? undefined
        if (shortUnit !== undefined) return shortUnit
 
        const unit = this.unit
 
        if (!unit) return undefined

        if (unit.length < 3) return unit
        if (new Set(["$", "£", "€", "%"]).has(unit[0])) return unit[0]

        return undefined
    }
 
    // Returns a map where the key is a series slug such as "name" and the value is a set
    // of all the unique values that this column has for that particular series.
    getUniqueValuesGroupedBy(
        indexColumnSlug: ColumnSlug
    ): Map<PrimitiveType, Set<PrimitiveType>> {
        const map = new Map<PrimitiveType, Set<PrimitiveType>>()
        const values = this.values
        const indexValues = this.table.getValuesAtIndices(
            indexColumnSlug,
            this.validRowIndices
        ) as PrimitiveType[]
        indexValues.forEach((indexVal, index) => {
            if (!map.has(indexVal)) map.set(indexVal, new Set())
            map.get(indexVal)!.add(values[index])
        })
        return map
    }
 
    @imemo get description(): string | undefined {
        return this.def.description
    }
 
    @imemo get isEmpty(): boolean {
        return this.valuesIncludingErrorValues.length === 0
    }
 
    @imemo get name(): string {
        return this.def.name ?? this.def.slug
    }
 
    @imemo get displayName(): string {
        return this.display?.name ?? this.name ?? ""
    }
 
    // todo: is the isString necessary?
    @imemo get sortedUniqNonEmptyStringVals(): JS_TYPE[] {
        return Array.from(
            new Set(this.values.filter(isString).filter((i) => i))
        ).sort()
    }
 
    @imemo get slug(): string {
        return this.def.slug
    }
 
    @imemo get valuesToIndices(): Map<any, number[]> {
        const map = new Map<any, number[]>()
        this.valuesIncludingErrorValues.forEach((value, index) => {
            if (!map.has(value)) map.set(value, [])
            map.get(value)!.push(index)
        })
        return map
    }
 
    indicesWhere(value: JS_TYPE | JS_TYPE[]): any {
        const queries = Array.isArray(value) ? value : [value]
        return union(
            ...queries
                .map((val) => this.valuesToIndices.get(val))
                .filter(isPresent)
        )
    }
 
    // We approximate whether a column is parsed simply by looking at the first row.
    needsParsing(value: any): boolean {
        // Skip parsing if explicit flag is set
        if (this.def.skipParsing) return false
 
        // Never parse computeds. The computed should return the correct JS type. Ideally we can provide some error messaging around this.
        if (this.def.transform) return false
 
        // If we already tried to parse it and failed we consider it "parsed"
        if (value instanceof ErrorValue) return false
 
        // If the passed value is of the correct type consider the column parsed.
        if (typeof value === this.jsType) return false
        return true
    }
 
    @imemo get isProjection(): boolean {
        return !!this.display?.isProjection
    }
 
    @imemo get uniqValues(): JS_TYPE[] {
        return uniq(this.values)
    }
 
    @imemo get uniqValuesAsSet(): Set<JS_TYPE> {
        return new Set(this.uniqValues)
    }
 
    /**
     * Returns all values including ErrorValues..
     * Normally you want just the valid values, like `[45000, 50000, ...]`. But sometimes you
     * need the ErrorValues too like `[45000, DivideByZeroError, 50000,...]`
     */
    @imemo get valuesIncludingErrorValues(): CoreValueType[] {
        const { table, slug } = this
        return table.has(slug) ? table.columnStore[slug] : []
    }
 
    @imemo get validRowIndices(): number[] {
        return this.valuesIncludingErrorValues
            .map((value, index) => (isNotErrorValue(value) ? index : undefined))
            .filter(isPresent)
    }
 
    @imemo get values(): JS_TYPE[] {
        const values = this.valuesIncludingErrorValues
        return this.validRowIndices.map((index) => values[index]) as JS_TYPE[]
    }
 
    @imemo get originalTimeColumnSlug(): string {
        return getOriginalTimeColumnSlug(this.table, this.slug)
    }
 
    @imemo get originalTimeColumn(): CoreColumn {
        return this.table.get(this.originalTimeColumnSlug)
    }
 
    @imemo get originalTimes(): number[] {
        const { originalTimeColumnSlug } = this
        if (!originalTimeColumnSlug) return []
        return this.table.getValuesAtIndices(
            originalTimeColumnSlug,
            this.validRowIndices
        ) as number[]
    }
 
    /**
     * True if the column has only 1 unique value. ErrorValues are counted as values, so
     * something like [DivideByZeroError, 2, 2] would not be constant.
     */
    @imemo get isConstant(): boolean {
        return new Set(this.valuesIncludingErrorValues).size === 1
    }
 
    @imemo get minValue(): JS_TYPE {
        return this.valuesAscending[0]
    }
 
    @imemo get maxValue(): JS_TYPE {
        return last(this.valuesAscending)!
    }
 
    @imemo get numErrorValues(): number {
        return this.valuesIncludingErrorValues.length - this.numValues
    }
 
    // Number of correctly parsed values
    @imemo get numValues(): number {
        return this.values.length
    }
 
    @imemo get numUniqs(): number {
        return this.uniqValues.length
    }
 
    @imemo get valuesAscending(): JS_TYPE[] {
        const values = this.values.slice()
        return this.jsType === "string" ? values.sort() : sortNumeric(values)
    }
 
    get source(): OwidSource {
        const { def } = this
        return {
            name: def.sourceName,
            link: def.sourceLink,
            dataPublishedBy: def.dataPublishedBy,
            dataPublisherSource: def.dataPublisherSource,
            retrievedDate: def.retrievedDate,
            additionalInfo: def.additionalInfo,
        }
    }
 
    // todo: remove. should not be on coretable
    @imemo private get allTimes(): Time[] {
        return this.table.getTimesAtIndices(this.validRowIndices)
    }
 
    // todo: remove. should not be on coretable
    @imemo get uniqTimesAsc(): Time[] {
        return sortNumeric(uniq(this.allTimes))
    }
 
    // todo: remove. should not be on coretable
    @imemo get maxTime(): Time {
        return last(this.uniqTimesAsc) as Time
    }
 
    // todo: remove. should not be on coretable
    @imemo get minTime(): Time {
        return this.uniqTimesAsc[0]
    }
 
    // todo: remove? Should not be on CoreTable
    @imemo get uniqEntityNames(): EntityName[] {
        return uniq(this.allEntityNames)
    }
 
    // todo: remove? Should not be on CoreTable
    @imemo private get allEntityNames(): EntityName[] {
        return this.table.getValuesAtIndices(
            this.table.entityNameSlug,
            this.validRowIndices
        ) as EntityName[]
    }
 
    // todo: remove? Should not be on CoreTable
    // assumes table is sorted by time
    @imemo get owidRows(): OwidVariableRow<JS_TYPE>[] {
        const times = this.originalTimes
        const values = this.values
        const entities = this.allEntityNames
        return range(0, times.length).map((index) => {
            return {
                entityName: entities[index],
                time: times[index],
                value: values[index],
            }
        })
    }
 
    // todo: remove? Should not be on CoreTable
    @imemo get owidRowsByEntityName(): Map<
        EntityName,
        OwidVariableRow<JS_TYPE>[]
    > {
        const map = new Map<EntityName, OwidVariableRow<JS_TYPE>[]>()
        this.owidRows.forEach((row) => {
            if (!map.has(row.entityName)) map.set(row.entityName, [])
            map.get(row.entityName)!.push(row)
        })
        return map
    }
 
    // todo: remove? Should not be on CoreTable
    // NOTE: this uses the original times, so any tolerance is effectively unapplied.
    @imemo get valueByEntityNameAndOriginalTime(): Map<
        EntityName,
        Map<Time, JS_TYPE>
    > {
        const valueByEntityNameAndTime = new Map<
            EntityName,
            Map<Time, JS_TYPE>
        >()
        this.owidRows.forEach((row) => {
            if (!valueByEntityNameAndTime.has(row.entityName))
                valueByEntityNameAndTime.set(row.entityName, new Map())
            valueByEntityNameAndTime
                .get(row.entityName)!
                .set(row.time, row.value)
        })
        return valueByEntityNameAndTime
    }
}
 
export type CoreColumn = AbstractCoreColumn<any>
 
export class MissingColumn extends AbstractCoreColumn<any> {
    jsType = JsTypes.string
 
    formatValue(): string {
        return ""
    }
}
 
class StringColumn extends AbstractCoreColumn<string> {
    jsType = JsTypes.string
 
    formatValue(value: any): string {
        return anyToString(value)
    }
 
    parse(val: any): any {
        if (val === null) return ErrorValueTypes.NullButShouldBeString
        if (val === undefined) return ErrorValueTypes.UndefinedButShouldBeString
        return val.toString() || ""
    }
}
 
class SeriesAnnotationColumn extends StringColumn {}
class CategoricalColumn extends StringColumn {}
class RegionColumn extends CategoricalColumn {}
class ContinentColumn extends RegionColumn {}
class ColorColumn extends CategoricalColumn {}
class BooleanColumn extends AbstractCoreColumn<boolean> {
    jsType = JsTypes.boolean

    formatValue(value: any): "true" | "false" {
        return value ? "true" : "false"
    }

    parse(val: any): boolean {
        return !!val
    }
}
abstract class AbstractNumericColumn extends AbstractCoreColumn<number> {
    jsType = JsTypes.number
 
    formatValue(value: number, options?: TickFormattingOptions): string {
        if (isNumber(value)) {
            return formatValue(value, {
                numDecimalPlaces: this.numDecimalPlaces,
                ...options,
            })
        }
        return ""
    }
 
    formatValueShortWithAbbreviations(
        value: number,
        options?: TickFormattingOptions
    ): string {
        return super.formatValueShortWithAbbreviations(value, {
            shortNumberPrefixes: true,
            // do not set a unit
            ...options,
        })
    }
 
    formatValueShort(value: number, options?: TickFormattingOptions): string {
        return super.formatValueShort(value, {
            ...omitUndefinedValues({
                unit: this.shortUnit,
            }),
            ...options,
        })
    }
 
    formatValueLong(value: number, options?: TickFormattingOptions): string {
        return super.formatValueLong(value, {
            ...omitUndefinedValues({
                unit: this.unit,
            }),
            ...options,
        })
    }
 
    @imemo get isAllIntegers(): boolean {
        return this.values.every((val) => val % 1 === 0)
    }
 
    parse(val: any): number | ErrorValue {
        if (val === null) return ErrorValueTypes.NullButShouldBeNumber
        if (val === undefined) return ErrorValueTypes.UndefinedButShouldBeNumber
        if (val === "") return ErrorValueTypes.BlankButShouldBeNumber
        if (isNaN(val)) return ErrorValueTypes.NaNButShouldBeNumber
 
        const res = this._parse(val)
 
        if (isNaN(res))
            return ErrorValueTypes.NotAParseableNumberButShouldBeNumber
 
        return res
    }
 
    protected _parse(val: any): number {
        return parseFloat(val)
    }
}
 
class NumericColumn extends AbstractNumericColumn {}
class NumericCategoricalColumn extends AbstractNumericColumn {}
 
class IntegerColumn extends NumericColumn {
    formatValue(value: any, options?: TickFormattingOptions): string {
        return super.formatValue(value, {
            numDecimalPlaces: 0,
            ...options,
        })
    }
 
    protected _parse(val: any): number {
        return parseInt(val)
    }
}
 
class CurrencyColumn extends NumericColumn {
    formatValue(value: any, options?: TickFormattingOptions): string {
        return super.formatValue(value, {
            numDecimalPlaces: 0,
            unit: "$",
            ...options,
        })
    }
}
// Expects 50% to be 50
class PercentageColumn extends NumericColumn {
    formatValue(value: number, options?: TickFormattingOptions): string {
        return super.formatValue(value, {
            numberPrefixes: false,
            unit: "%",
            ...options,
        })
    }
}
 
// Same as %, but indicates it's part of a group of columns that add up to 100%.
// Might not need this.
class RelativePercentageColumn extends PercentageColumn {}
 
class PercentChangeOverTimeColumn extends PercentageColumn {
    formatValue(value: number, options?: TickFormattingOptions): string {
        return super.formatValue(value, {
            showPlus: true,
            ...options,
        })
    }
}
 
class DecimalPercentageColumn extends PercentageColumn {}
class RatioColumn extends NumericColumn {}
 
// todo: remove. should not be in coretable
class EntityIdColumn extends NumericCategoricalColumn {}
class EntityCodeColumn extends CategoricalColumn {}
class EntityNameColumn extends CategoricalColumn {}
 
// todo: cleanup time columns. current schema is a little incorrect.
abstract class TimeColumn extends AbstractCoreColumn<number> {
    jsType = JsTypes.number
 
    parse(val: any): number | ErrorValue {
        return parseInt(val)
    }
}
 
class YearColumn extends TimeColumn {
    formatValue(value: number): string {
        // Include BCE
        return formatYear(value)
    }
}
 
class DayColumn extends TimeColumn {
    formatValue(value: number): string {
        return formatDay(value)
    }
 
    formatValueForMobile(value: number): string {
        return formatDay(value, { format: "MMM D, 'YY" })
    }
 
    formatForCsv(value: number): string {
        return formatDay(value, { format: "YYYY-MM-DD" })
    }
}
 
const dateToTimeCache = new Map<string, Time>() // Cache for performance
class DateColumn extends DayColumn {
    parse(val: any): number {
        // skip parsing if a date is a number, it's already been parsed
        if (typeof val === "number") return val
        if (!dateToTimeCache.has(val))
            dateToTimeCache.set(
                val,
                dateDiffInDays(
                    moment.utc(val).toDate(),
                    moment.utc("2020-01-21").toDate()
                )
            )
        return dateToTimeCache.get(val)!
    }
}
 
class QuarterColumn extends TimeColumn {
    private static regEx = /^([+-]?\d+)-Q([1-4])$/
 
    parse(val: any): number | ErrorValue {
        // skip parsing if a date is a number, it's already been parsed
        if (typeof val === "number") return val
        if (typeof val === "string") {
            const match = val.match(QuarterColumn.regEx)
            if (match) {
                const [, year, quarter] = match
                return parseInt(year) * 4 + (parseInt(quarter) - 1)
            }
        }
        return ErrorValueTypes.InvalidQuarterValue
    }
 
    private static numToQuarter(value: number): number[] {
        const year = Math.floor(value / 4)
        const quarter = (Math.abs(value) % 4) + 1
        return [year, quarter]
    }
 
    formatValue(value: number): string {
        const [year, quarter] = QuarterColumn.numToQuarter(value)
        return `Q${quarter}/${year}`
    }
 
    formatForCsv(value: number): string {
        const [year, quarter] = QuarterColumn.numToQuarter(value)
        return `${year}-Q${quarter}`
    }
}
 
class PopulationColumn extends IntegerColumn {}
class PopulationDensityColumn extends NumericColumn {}
 
class AgeColumn extends NumericColumn {}
 
export const ColumnTypeMap = {
    String: StringColumn,
    SeriesAnnotation: SeriesAnnotationColumn,
    Categorical: CategoricalColumn,
    Region: RegionColumn,
    Continent: ContinentColumn,
    Numeric: NumericColumn,
    Day: DayColumn,
    Date: DateColumn,
    Year: YearColumn,
    Quarter: QuarterColumn,
    Time: TimeColumn,
    Boolean: BooleanColumn,
    Currency: CurrencyColumn,
    Percentage: PercentageColumn,
    RelativePercentage: RelativePercentageColumn,
    Integer: IntegerColumn,
    DecimalPercentage: DecimalPercentageColumn,
    PercentChangeOverTime: PercentChangeOverTimeColumn,
    Ratio: RatioColumn,
    Color: ColorColumn,
    EntityCode: EntityCodeColumn,
    EntityId: EntityIdColumn,
    EntityName: EntityNameColumn,
    Population: PopulationColumn,
    PopulationDensity: PopulationDensityColumn,
    Age: AgeColumn,
}
 
// Keep this in. This is used as a compile-time check that ColumnTypeMap covers all
// column names defined in ColumnTypeNames, since that is quite difficult to ensure
// otherwise without losing inferred type information.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _ColumnTypeMap: {
    [key in ColumnTypeNames]: unknown
} = ColumnTypeMap