Module:London Underground passenger data

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

-- Module:Station passenger data
local p = {}

-- Data: station -> year -> { passengers = number }
local data = {
  ["Oxford Circus"] = {
    [2019] = { passengers = 43000000 },
    [2020] = { passengers = 15000000 },
    [2021] = { passengers = 28000000 },
  },
  ["Waterloo"] = {
    [2019] = { passengers = 100000000 },
    [2020] = { passengers = 25000000 },
    [2021] = { passengers = 50000000 },
  },
}

-- Utility: format number with thousands separators
local function format_num(n)
  local s = tostring(n)
  local k
  while true do
    s, k = s:gsub("^(-?%d+)(%d%d%d)", "%1,%2")
    if k == 0 then break end
  end
  return s
end

-- Get a specific year
function p.get(frame)
  local station = frame.args[1]
  local year = tonumber(frame.args[2])
  if not station or not year then
    return "Data not available"
  end
  local entry = data[station] and data[station][year]
  if not entry or not entry.passengers then
    return "Data not available"
  end
  return format_num(entry.passengers)
end

-- Get the latest available year automatically
function p.getLatest(frame)
  local station = frame.args[1]
  if not station or not data[station] then
    return "Data not available"
  end
  -- Find max year key
  local latest
  for y, _ in pairs(data[station]) do
    if not latest or y > latest then
      latest = y
    end
  end
  local entry = data[station][latest]
  if not entry or not entry.passengers then
    return "Data not available"
  end
  return format_num(entry.passengers) .. " (" .. latest .. ")"
end

-- Optional: show ▲/▼ trend by comparing to previous year
function p.getWithTrend(frame)
  local station = frame.args[1]
  local year = tonumber(frame.args[2])
  if not station or not year or not data[station] then
    return "Data not available"
  end
  local cur = data[station][year]
  local prev = data[station][year - 1]
  if not cur or not cur.passengers then
    return "Data not available"
  end
  local arrow = ""
  if prev and prev.passengers then
    if cur.passengers > prev.passengers then
      arrow = " ▲"
    elseif cur.passengers < prev.passengers then
      arrow = " ▼"
    end
  end
  return format_num(cur.passengers) .. arrow
end

return p