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 | 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 24x 24x 24x 1x 1x 60x 1x 1x 1x 350x 1x 1x 1x 259x 1x 1x 24x 24x 1x 1x 45x 45x 33x 45x 45x 20x 20x 20x 1x 1x 1x 870x 1x 1x 12x 12x 12x 6x 6x 1x 1x 1x 405x 1x 1x 1x 465x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 60x 60x 36x 21x 15x 60x 1x 1x 57x 45x 45x 1x 1x | import {
parseIntOrUndefined,
isString,
diffDateISOStringInDays,
formatDay,
} from "./Util"
import { EPOCH_DATE, Time } from "./owidTypes"
/**
* An unbounded value (±Infinity) or a concrete point in time (year or date).
*/
export type TimeBound = number
export type TimeBounds = [TimeBound, TimeBound]
/**
* The two special TimeBound values: unbounded left & unbounded right.
*/
export enum TimeBoundValue {
negativeInfinity = -Infinity,
positiveInfinity = Infinity,
}
enum TimeBoundValueStr {
unboundedLeft = "earliest",
unboundedRight = "latest",
}
export const timeFromTimebounds = (
timeBound: TimeBound,
fallbackTime: Time
): number => (Math.abs(timeBound) !== Infinity ? timeBound : fallbackTime)
const hasAnInfinity = (timeBound: TimeBound): timeBound is TimeBoundValue =>
isNegativeInfinity(timeBound) || isPositiveInfinity(timeBound)
export const isNegativeInfinity = (
timeBound: TimeBound
): timeBound is TimeBoundValue => timeBound === TimeBoundValue.negativeInfinity
export const isPositiveInfinity = (
timeBound: TimeBound
): timeBound is TimeBoundValue => timeBound === TimeBoundValue.positiveInfinity
const formatTimeBound = (timeBound: TimeBound): string => {
if (isNegativeInfinity(timeBound)) return TimeBoundValueStr.unboundedLeft
if (isPositiveInfinity(timeBound)) return TimeBoundValueStr.unboundedRight
return `${timeBound}`
}
const parseTimeBound = (str: string): TimeBound | undefined => {
if (str === TimeBoundValueStr.unboundedLeft)
return TimeBoundValue.negativeInfinity
if (str === TimeBoundValueStr.unboundedRight)
return TimeBoundValue.positiveInfinity
return parseIntOrUndefined(str)
}
// Use this to not repeat logic
const fromJSON = (value: TimeBound | string | undefined): number | undefined =>
isString(value) ? parseTimeBound(value) : value
const toJSON = (bound: TimeBound | undefined): string | number | undefined => {
if (bound === undefined) return undefined
if (isNegativeInfinity(bound)) return TimeBoundValueStr.unboundedLeft
if (isPositiveInfinity(bound)) return TimeBoundValueStr.unboundedRight
return bound
}
export const minTimeBoundFromJSONOrNegativeInfinity = (
minTime: TimeBound | string | undefined
): TimeBound => fromJSON(minTime) ?? TimeBoundValue.negativeInfinity
export const maxTimeBoundFromJSONOrPositiveInfinity = (
maxTime: TimeBound | string | undefined
): TimeBound => fromJSON(maxTime) ?? TimeBoundValue.positiveInfinity
export const minTimeToJSON = toJSON
export const maxTimeToJSON = toJSON
const reISODateComponent = new RegExp("\\d{4}-[01]\\d-[0-3]\\d")
const reISODate = new RegExp(`^(${reISODateComponent.source})$`)
export const timeBoundToTimeBoundString = (
timeBound: TimeBound,
isDate: boolean
): string => {
if (hasAnInfinity(timeBound)) return formatTimeBound(timeBound)
return isDate
? formatDay(timeBound, { format: "YYYY-MM-DD" })
: `${timeBound}`
}
const parseTimeURIComponent = (param: string): TimeBound | undefined =>
reISODate.test(param)
? diffDateISOStringInDays(param, EPOCH_DATE)
: parseTimeBound(param)
const upgradeLegacyTimeString = (time: string): string => {
// In the past we supported unbounded time parameters like time=2015.. which would be
// equivalent to time=2015..latest. We don't actively generate these kinds of URL any
// more because URLs ending with dots are not interpreted correctly by many services
// (Twitter, Facebook and others) - but we still want to recognize incoming requests
// for these "legacy" URLs!
if (time === "..") return "earliest..latest"
return time.endsWith("..")
? time + "latest"
: time.startsWith("..")
? "earliest" + time
: time
}
export const getTimeDomainFromQueryString = (
time: string
): [number, number] => {
time = upgradeLegacyTimeString(time)
const reIntComponent = new RegExp("\\-?\\d+")
const reIntRange = new RegExp(
`^(${reIntComponent.source}|earliest)\\.\\.(${reIntComponent.source}|latest)$`
)
const reDateRange = new RegExp(
`^(${reISODateComponent.source}|earliest)\\.\\.(${reISODateComponent.source}|latest)$`
)
if (reIntRange.test(time) || reDateRange.test(time)) {
const [start, end] = time.split("..")
return [
parseTimeURIComponent(start) ?? TimeBoundValue.negativeInfinity,
parseTimeURIComponent(end) ?? TimeBoundValue.positiveInfinity,
]
}
const timebound =
parseTimeURIComponent(time) ?? TimeBoundValue.positiveInfinity
return [timebound, timebound]
}
|