@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.
54 lines (52 loc) • 3.08 kB
JavaScript
import { parseInstantNanoseconds } from "../../internal/instantNanoseconds.js";
import { isObject } from "../../internal/isObject.js";
import { parsePunctualityTolerance, punctualityOf } from "../../internal/punctuality.js";
// src/transport/calculate/punctualityRate.ts
/**
* 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)
*/
function punctualityRate(pairs, tolerance) {
try {
if (!Array.isArray(pairs) || pairs.length === 0) return null;
const bounds = parsePunctualityTolerance(tolerance);
if (bounds === null) return null;
let onTime = 0;
for (const pair of pairs) {
if (!isObject(pair)) return null;
const { planned, actual } = pair;
const plannedNanoseconds = parseInstantNanoseconds(planned);
const actualNanoseconds = parseInstantNanoseconds(actual);
if (plannedNanoseconds === null || actualNanoseconds === null) return null;
if (punctualityOf(actualNanoseconds - plannedNanoseconds, bounds) === "onTime") onTime += 1;
}
return {
onTime,
total: pairs.length,
rate: onTime / pairs.length
};
} catch {
return null;
}
}
export { punctualityRate };