@akadenia/helpers
Version:
Akadenia helpers
124 lines (123 loc) • 5.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findEntry = exports.objectPropHasValue = exports.parseCookie = exports.isPureObject = void 0;
exports.filterObjectsByProperty = filterObjectsByProperty;
exports.containsSubObject = containsSubObject;
exports.findObjectBySubObject = findObjectBySubObject;
exports.filterObjectsBySubObject = filterObjectsBySubObject;
exports.convertArrayToMap = convertArrayToMap;
/**
*
* @function
* @param {any} object - The object to check if it is a pure object (not an array, date, or function)
* @returns {boolean} - Whether or not the provided object is a pure object
*/
const isPureObject = function (object) {
const isDateObject = object instanceof Date;
return object === Object(object) && !Array.isArray(object) && typeof object !== "function" && !isDateObject;
};
exports.isPureObject = isPureObject;
/**
* @function
* @param {string} str - The string of cookies to parse
* @returns {Object} - The object containing key-value pairs of the parsed cookies
*/
const parseCookie = (str) => str
.split(";")
.map((v) => v.split("="))
.reduce((acc, v) => {
if (v.length > 1) {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
}
return acc;
}, {});
exports.parseCookie = parseCookie;
/**
* Filters an array of objects based on a specified property and value.
* @template T extends Record<string, any> - The type of objects in the array.
* @param {T[]} array - The array of objects to filter.
* @param {keyof T} propertyName - The name of the property to filter on.
* @param {T[keyof T]} propertyValue - The value of the property to filter on.
* @returns {T[]} - The filtered array of objects.
*/
function filterObjectsByProperty(array, propertyName, propertyValue) {
const predicate = (obj) => Object.getOwnPropertyDescriptor(obj, propertyName) && obj[propertyName] === propertyValue;
return array.filter(predicate);
}
/**
* Checks if the main object contains the sub object.
* @template T extends Record<string, any> - The type of the main object.
* @param {T} mainObject - The main object to check.
* @param {Partial<T>} subObject - The sub object to look for in the main object.
* @returns {boolean} - True if the main object contains the sub object, false otherwise.
*/
function containsSubObject(mainObject, subObject) {
if (!((0, exports.isPureObject)(mainObject) && (0, exports.isPureObject)(subObject))) {
return false;
}
for (let key of Object.keys(subObject)) {
if (!(Object.getOwnPropertyDescriptor(mainObject, key) && mainObject[key] === subObject[key])) {
return false;
}
}
return true;
}
/**
* Finds the object containing the given sub object in the array.
* @template T extends Record<string, any> - The type of objects in the array.
* @param {T[]} array - The array of objects.
* @param {Partial<T>} subObject - The sub object used to search the array.
* @returns {T | null} - The object containing the given sub object or null.
*/
function findObjectBySubObject(array, subObject) {
const predicate = (obj) => containsSubObject(obj, subObject);
return array.find(predicate) || null;
}
/**
* Filters an array of objects based on the objects containing the given sub object
* @template T extends Record<string, any>
* @param {T[]} array - The array of objects
* @param {Partial<T>} subObject - The sub object used to search the array
* @returns {Object | null} - An array of objects containing the given sub object
*/
function filterObjectsBySubObject(array, subObject) {
const predicate = (obj) => containsSubObject(obj, subObject);
return array.filter(predicate);
}
/**
* Checks if the object has the key with the value.
* @param {Object} object - The object to check
* @param {string} key - The key to check
* @param {any} value - The value to check
* @returns {boolean} - Whether or not the object has the key with the value
* @example
* const object = { a: 1, b: 2, c: 3 }
* objectPropHasValue(object, 'a', 1) // true
* objectPropHasValue(object, 'b', 3) // false
*/
const objectPropHasValue = ({ object, propName, value, }) => object[propName] === value;
exports.objectPropHasValue = objectPropHasValue;
/**
* Finds the first entry in the array that satisfies the predicate.
* @template EntryType - The type of the entries in the array.
* @callback PredicateFunction - The predicate function to use to search the array.
* @param {EntryType} item - The entry in the array to search.
* @returns {boolean} - True if the entry satisfies the predicate, false otherwise.
* @param {EntryType[]} array - The array of entries to search.
* @param {PredicateFunction} predicate - The predicate function to use to search the array.
* @returns {EntryType | null} - The first entry that satisfies the predicate, or null if not found.
*/
const findEntry = (array, predicate) => array.find(predicate) || null;
exports.findEntry = findEntry;
function convertArrayToMap(arrayData, keyProp) {
if (typeof keyProp !== "string") {
throw new Error("keyProp can only be strings or numbers");
}
const dataAsObject = {};
arrayData.reduce((prev, curr) => {
const key = curr[keyProp];
prev[key] = curr;
return prev;
}, dataAsObject);
return dataAsObject;
}