quantitivecalc
Version:
A TypeScript library providing advanced quantitative finance functions for risk analysis, performance metrics, and technical indicators. (Currently in development)
79 lines (78 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = filterByRange;
/**
* Filters a list of objects by a numeric or date range on a specified property.
*
* @template T - The type of objects in the list.
* @param list - The array of objects to filter.
* @param columnName - The key of the property to filter by.
* @param minValue - The minimum value (inclusive) for the property. If omitted or null, no minimum is applied.
* @param maxValue - The maximum value (inclusive) for the property. If omitted or null, no maximum is applied.
* @returns A new array containing only the objects where the specified property is within the given range.
*
* @remarks
* - Properties with `null`, `undefined`, or non-numeric/non-date values are excluded from the result.
* - Both `minValue` and `maxValue` are optional; if not provided or null, the corresponding bound is not checked.
* - Date values can be provided as Date objects or ISO strings.
* - Invalid date strings or non-numeric strings in min/max parameters are treated as no bound.
*/
function filterByRange(list, columnName, minValue, maxValue) {
return list.filter(item => {
const value = item[columnName];
// Handle null/undefined values in the data
if (value == null)
return false;
let comparable;
// Handle Date objects
if (value instanceof Date) {
comparable = value.getTime();
}
// Handle date strings
else if (typeof value === 'string' && !isNaN(Date.parse(value))) {
comparable = Date.parse(value);
}
// Handle numbers and numeric strings
else if (typeof value === 'number' || (typeof value === 'string' && !isNaN(Number(value)))) {
comparable = Number(value);
}
else {
return false;
}
// Convert min/max to comparable values, handling null explicitly
let minComp;
let maxComp;
// Handle minValue - treat null, undefined, or invalid values as no minimum bound
if (minValue != null) {
if (minValue instanceof Date) {
minComp = minValue.getTime();
}
else if (typeof minValue === 'string' && !isNaN(Date.parse(minValue))) {
minComp = Date.parse(minValue);
}
else if (!isNaN(Number(minValue))) {
minComp = Number(minValue);
}
// If minValue is invalid (NaN after conversion), minComp remains undefined
}
// Handle maxValue - treat null, undefined, or invalid values as no maximum bound
if (maxValue != null) {
if (maxValue instanceof Date) {
maxComp = maxValue.getTime();
}
else if (typeof maxValue === 'string' && !isNaN(Date.parse(maxValue))) {
maxComp = Date.parse(maxValue);
}
else if (!isNaN(Number(maxValue))) {
maxComp = Number(maxValue);
}
// If maxValue is invalid (NaN after conversion), maxComp remains undefined
}
// Apply range filters only if bounds are valid
if (minComp !== undefined && comparable < minComp)
return false;
if (maxComp !== undefined && comparable > maxComp)
return false;
return true;
});
}