Class Timeseries<T>

An immutable series of dates and values, representing a value that changes over time.

Type Parameters

  • T = NonNullable<unknown>

Hierarchy

Constructors

Properties

points: TimeseriesPoint<T>[]

Accessors

  • get firstValue(): undefined | T
  • The first value in the timeseries, or undefined if the timeseries is empty.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns undefined | T

  • get lastValue(): undefined | T
  • The last value in the timeseries or undefined if the timeseries is empty.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns undefined | T

  • get maxDate(): undefined | Date
  • The maximum (latest) date in the timeseries.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns undefined | Date

  • get maxValue(): undefined | T
  • The maximum value in the timeseries.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns undefined | T

  • get minDate(): undefined | Date
  • The minimum (earliest) date in the timeseries.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns undefined | Date

  • get minValue(): undefined | T
  • The minimum value in the timeseries.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns undefined | T

Methods

  • Throws an exception if any values in the timeseries are not boolean.

    The returned timeseries will cast to Timeseries<boolean> so any subsequent code doesn't need to deal with non-boolean values.

    Returns Timeseries<boolean>

  • Throws an exception if any values in the timeseries are not finite numbers (e.g. strings, null, undefined, infinity, NaN).

    The returned timeseries will cast to Timeseries<number> so any subsequent code doesn't need to deal with non-number values.

    Returns Timeseries<number>

  • Throws an exception if any values in the timeseries are not of type string.

    The returned timeseries will cast to Timeseries<string> so any subsequent code doesn't need to deal with non-string values.

    Returns Timeseries<string>

  • Computes deltas between each point in the timeseries.

    Returns

    A timeseries made up of the deltas between timeseries points.

    Parameters

    • Optional opts: {
          keepInitialValue?: boolean;
          minDeltaToKeep?: number;
      }
      • Optional keepInitialValue?: boolean

        Whether to include the initial value in the series as a delta (with an assumed prior value of 0).

      • Optional minDeltaToKeep?: number

        The minimum delta to keep. By default all deltas are kept, but can be set to 0 to drop negative deltas or 1 to only keep positive deltas.

    Returns Timeseries<number>

  • Filters the timeseries by calling the provided filterFn on each value in the timeseries.

    Returns

    A new timeseries with the filtered points.

    Parameters

    • filterFn: ((val: T) => boolean)

      The function to call on each value in the timeseries. If it returns true, the point will be kept, else discarded.

        • (val: T): boolean
        • Parameters

          • val: T

          Returns boolean

    Returns Timeseries<T>

  • Returns the point with the date closest to the provided date, or undefined if the timeseries is empty.

    You can use hasData to guard against the timeseries being empty and ensure this can't return undefined.

    Returns

    The point with the date closest to the provided date or undefined if the timeseries is empty.

    Parameters

    • date: Date

      The date to find the closest point to.

    Returns undefined | TimeseriesPoint<T>

  • Returns a new timeseries with the values in the timeseries mapped to new values using the provided mapFn function.

    Returns

    A new timeseries with the mapped points.

    Type Parameters

    • R = T

    Parameters

    • mapFn: ((val: T) => R)

      The function to call on each value in the timeseries. The return value is used as the point's value in the new timeseries.

        • (val: T): R
        • Parameters

          • val: T

          Returns R

    Returns Timeseries<R>

  • Computes a rolling average of the timeseries where each point represents the average of the prior opts.days days of data points.

    Returns

    A new timeseries containing the rolling average of the original one.

    Parameters

    • opts: {
          days: number;
          treatMissingDatesAsZero?: boolean;
      }
      • days: number

        The number of days to average over.

      • Optional treatMissingDatesAsZero?: boolean

        Whether to treat missing dates as 0. This is typically what you want for "incidence" metrics (like "daily new cases") but not "current" metrics (like "% of beds in use").

    Returns Timeseries<number>

  • Returns a new Timeseries from the slice of data points indicated by the provided start and end indices.

    Example

    ts.slice(2, 4); // Returns a Timeseries with the 3rd and 4th points.
    

    Returns

    A new Timeseries with the sliced data points.

    Parameters

    • start: number

      The first index to be included in the new Timeseries.

    • Optional end: number

      The first index not to be included in the new Timeseries.

    Returns Timeseries<T>

  • Breaks the timeseries into overlapping windows of the specified size and makes a new timeseries from them. Handy for computing aggregations like a rolling average.

    Returns

    A new timeseries that has a point for every point in the original timeseries, but contains a TimeseriesWindow object containing the window of points leading up to that point.

    Parameters

    • opts: {
          days: number;
      }
      • days: number

        The number of days to include in each window. Note that the points at the beginning of the timeseries will have a smaller window due to no prior history.

    Returns Timeseries<TimeseriesWindow<T>>

  • Static constructor to help create a timeseries from a range of dates and some values.

    Returns

    A new timeseries.

    Type Parameters

    • T

    Parameters

    • startDate: Date
    • endDate: Date
    • valueProvider: T[] | ((date: Date, index: number) => T)

      An array of values or iteratee function that returns values for the dates in the date range.

    Returns Timeseries<T>

Generated using TypeDoc