@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
43 lines • 2.94 kB
TypeScript
import { PunctualityTolerance } from "../../types/transport-timestamps.js";
import "../../types/index.js";
/** One planned/actual pair for `punctualityRate`. */
export interface PlannedActual {
/** The planned time, as an ISO 8601 instant string (`Z` or `±HH:MM`); a zoned string is not one. */
planned: string;
/** The actual time, as an ISO 8601 instant string (`Z` or `±HH:MM`); a zoned string is not one. */
actual: string;
}
/** What `punctualityRate` returns: the on-time count, the total, and their ratio. */
export interface OnTimeRate {
/** The number of pairs `classifyPunctuality` calls `"onTime"`. */
onTime: number;
/** Every pair counted. */
total: number;
/** The on-time share, `onTime / total`, from 0 to 1. */
rate: number;
}
/**
* The share of planned/actual pairs that were on time under one tolerance.
*
* A rate is a count under a stated tolerance: the same arrivals give a different rate under a
* 60-minute tolerance than under a 120-minute one, so the tolerance travels with the number.
*
* - Each pair is classified by `classifyPunctuality`'s rule under the same tolerance: `late` and
* `early` are read once, before any pair, so the whole list is judged under one tolerance.
* Only `"onTime"` counts: with an `early` tolerance, an early arrival is **not** on time;
* without one, it is.
* - `rate` is `onTime / total` as a plain number (`0.75`), not rounded.
* - Every pair must be valid: one invalid pair returns `null`, never a rate over the rest.
* - An empty list returns `null`: no arrivals have no rate.
* - Anything beyond a rate — percentiles, causes — is the consumer's.
*
* @param pairs the planned and actual time of each arrival
* @param tolerance late (required) and early (optional) tolerances, as classifyPunctuality takes them
* @returns the on-time count, the total and the rate (null for an empty list), or null on invalid input
*
* @example punctualityRate([{ planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:05:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:14:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:16:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T09:57:00Z" }], { late: "PT15M" }) // { onTime: 3, total: 4, rate: 0.75 }
* @example punctualityRate([{ planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T09:48:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:01:00Z" }], { late: "PT15M", early: "PT10M" }) // { onTime: 1, total: 2, rate: 0.5 } (12 minutes early is not on time under a 10-minute early tolerance)
* @example punctualityRate([], { late: "PT15M" }) // null
* @example punctualityRate([{ planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:05:00" }], { late: "PT15M" }) // null (a zoneless actual is not a moment)
*/
export declare function punctualityRate(pairs: PlannedActual[], tolerance: PunctualityTolerance): OnTimeRate | null;