austrian-cpi
Version:
Query and merge Austrian CPI time series data for economic modeling
63 lines (54 loc) • 1.88 kB
JavaScript
import { extractSeriesValues } from "./cpiUtils.js";
/**
* Get the latest available numeric value for a CPI series
* @param {string} seriesKey
* @param {'monthly'|'yearly'} [frequency='monthly']
* @returns {{ date: string, value: number } | null}
*/
function getLatestValue(seriesKey, frequency = "monthly") {
const values = extractSeriesValues(seriesKey, frequency);
return values.length ? values[values.length - 1] : null;
}
/**
* Get the %CH between the latest two CPI values
* @param {string} seriesKey
* @param {'monthly'|'yearly'} [frequency='monthly']
* @returns {{ from: string, to: string, rateOfChange: number } | null}
*/
function getLatestRateOfChange(seriesKey, frequency = "monthly") {
const values = extractSeriesValues(seriesKey, frequency);
if (values.length < 2) return null;
const prev = values[values.length - 2];
const curr = values[values.length - 1];
const rate = ((curr.value - prev.value) / prev.value) * 100;
return {
from: prev.date,
to: curr.date,
rateOfChange: parseFloat(rate.toFixed(2)),
};
}
/**
* Generic RoC for custom date ranges
* @param {string} seriesKey
* @param {object} options
* @param {'monthly'|'yearly'} [options.frequency='monthly']
* @param {string} [options.start]
* @param {string} [options.end]
* @returns {{ from: string, to: string, rateOfChange: number } | null}
*/
function getRateOfChange(
seriesKey,
{ frequency = "monthly", start, end } = {},
) {
const values = extractSeriesValues(seriesKey, frequency, start, end);
if (values.length < 2) return null;
const first = values[0];
const last = values[values.length - 1];
const rate = ((last.value - first.value) / first.value) * 100;
return {
from: first.date,
to: last.date,
rateOfChange: parseFloat(rate.toFixed(2)),
};
}
export { getLatestValue, getRateOfChange, getLatestRateOfChange };