poe-api-manager
Version:
poe.ninja and poe.watch API
41 lines (40 loc) • 1.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ValidationError_js_1 = __importDefault(require("../errors/ValidationError.js"));
/**
* Filters properties of objects in an array based on the specified properties.
* @param {Record<string, any> []} data The array of objects to filter.
* @param {string} requestedProperties The array of property names to include in the result.
* @returns {object[]} An array of objects with only the specified properties.
*/
function filterProperties(data, requestedProperties) {
// Validate input first
if (!Array.isArray(data)) {
throw new ValidationError_js_1.default("Input data must be an array", 400, {
providedType: typeof data,
});
}
if (!Array.isArray(requestedProperties)) {
throw new ValidationError_js_1.default("requestedProperties must be an array", 400, {
providedType: typeof requestedProperties,
});
}
const result = [];
data.forEach((item) => {
const itemResult = {};
requestedProperties.forEach((prop) => {
if (Object.prototype.hasOwnProperty.call(item, prop)) {
itemResult[prop] = item[prop];
}
else {
console.warn(`Property not found: ${prop}`);
}
});
result.push(itemResult);
});
return result;
}
exports.default = filterProperties;