TERP Funding Logic
JavaScript to retrieve funding information from the TERP data file.
funding-logic.js
— 5.4 KB
File contents
(() => {
"use strict";
/*
* Change this URL to the actual location of the JSON file.
*/
const FUNDING_DATA_URL =
"/downloads/air-quality/terp/application-status.json";
/*
* Makes matching case-insensitive and ignores extra spaces.
*
* "Small Business"
* "small business"
* " SMALL BUSINESS "
*
* are all treated as the same value.
*/
function normalizeFundingValue(value) {
return String(value ?? "")
.trim()
.toLocaleLowerCase("en-US");
}
/*
* Formats:
*
* 30000000
*
* as:
*
* $30,000,000
*/
function formatFundingAmount(value, format = "currency") {
if (value === null || value === undefined || String(value).trim() === "") {
return "";
}
const numericValue =
typeof value === "number"
? value
: Number(
String(value)
.replace(/[$,\s]/g, "")
.trim(),
);
if (!Number.isFinite(numericValue)) {
return String(value);
}
return new Intl.NumberFormat("en-US", {
style: format === "number" ? "decimal" : "currency",
currency: "USD",
minimumFractionDigits: 0,
maximumFractionDigits: 0,
}).format(numericValue);
}
/*
* Finds one funding record using:
*
* Program
* Category
* Type
*/
function getFundingRecord(fundingData, program, category, type) {
const normalizedProgram = normalizeFundingValue(program);
const normalizedCategory = normalizeFundingValue(category);
const normalizedType = normalizeFundingValue(type);
return fundingData.find(
(item) =>
normalizeFundingValue(item.Program) === normalizedProgram &&
normalizeFundingValue(item.Category) === normalizedCategory &&
normalizeFundingValue(item.Type) === normalizedType,
);
}
/*
* Populates every element with the class:
*
* funding-amount (currency by default)
* funding-number (whole number with commas)
*
* Optional data-format="number" or data-format="currency"
* overrides the default. Type "awarded applications" defaults
* to a whole number, including on funding-amount elements.
*/
function populateFundingAmounts(fundingData) {
const fundingElements = document.querySelectorAll(
".funding-amount, .funding-number",
);
fundingElements.forEach((element) => {
const program = element.dataset.program;
const category = element.dataset.category;
const type = element.dataset.type;
if (!program || !category || !type) {
console.warn(
"Funding element is missing Program, Category, or Type:",
element,
);
return;
}
const record = getFundingRecord(fundingData, program, category, type);
if (!record) {
console.warn("No funding record found:", {
program,
category,
type,
});
element.textContent = "";
return;
}
const requestedFormat = normalizeFundingValue(element.dataset.format);
const defaultFormat =
element.classList.contains("funding-number") ||
normalizeFundingValue(type) === "awarded applications"
? "number"
: "currency";
const format =
requestedFormat === "number" || requestedFormat === "currency"
? requestedFormat
: defaultFormat;
element.textContent = formatFundingAmount(record.Amount, format);
});
}
/*
* Formats the JSON lastUpdated property.
*
* 2026-08-28T12:57:27.3699125Z
*
* becomes:
*
* Aug. 28, 2026
*/
function formatLastUpdated(value) {
const match = String(value ?? "").match(/^(\d{4})-(\d{2})-(\d{2})T/);
if (!match) {
return "";
}
const [, year, month, day] = match;
const apMonths = [
"Jan.", "Feb.", "March", "April", "May", "June",
"July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec.",
];
const formattedMonth = apMonths[Number(month) - 1];
if (!formattedMonth) {
return "";
}
return `${formattedMonth} ${Number(day)}, ${year}`;
}
/*
* Optional.
*
* Any element with:
*
* class="funding-last-updated"
*
* will receive the last updated date.
*/
function populateLastUpdated(value) {
const formattedDate = formatLastUpdated(value);
document.querySelectorAll(".funding-last-updated").forEach((element) => {
element.textContent = formattedDate;
});
}
/*
* Downloads the JSON file and populates the page.
*/
async function loadFundingData() {
try {
const response = await fetch(FUNDING_DATA_URL, {
cache: "no-store",
});
if (!response.ok) {
throw new Error(
`Unable to load ${FUNDING_DATA_URL}: ` + `HTTP ${response.status}`,
);
}
const report = await response.json();
if (!Array.isArray(report.fundingData)) {
throw new TypeError(
'The JSON property "fundingData" ' + "must contain an array.",
);
}
populateFundingAmounts(report.fundingData);
populateLastUpdated(report.lastUpdated);
} catch (error) {
console.error("Unable to load funding data:", error);
}
}
/*
* This lets the shared code work whether it is
* included before or after DOMContentLoaded.
*/
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", loadFundingData);
} else {
loadFundingData();
}
})();
