Branch data Line data Source code
1 [ + ]: 2 : /** 2 : 2 : * @module utilities 3 : 2 : * @desc A set of utilities functions. 4 : 2 : * @version 1.0.0 5 : 2 : * @author Essam A. El-Sherif 6 : 2 : */ 7 : 2 : 8 : 2 : /** 9 : 2 : * @func updateObject 10 : 2 : * @static 11 : 2 : * @param {Object} original - The original Object to update. 12 : 2 : * @param {Object} updates - The Object with update values. 13 : 2 : * @param {Array} excludeKeys - A list of keys to exclude from updating. 14 : 2 : * @desc Update the original Object with values from the updates Object, excluding keys specified in excludeKeys. 15 : 2 : */ 16 [ + ]: 2 : export function updateObject(original, updates, excludeKeys = []){ 17 [ + ]: 1 : for(const [key, value] of Object.entries(updates)){ 18 [ + ]: 4 : if(!excludeKeys.includes(key)){ 19 : 3 : original[key] = value; 20 : 3 : } 21 : 4 : } 22 : 1 : } 23 : 2 : 24 : 2 : /** 25 : 2 : * @func isValidObject 26 : 2 : * @static 27 : 2 : * @param {Object} variable - The variable to check. 28 : 2 : * @param {Array} allowedKeys - A list of allowed keys. 29 : 2 : * @return {boolean} - True if variable is an Object and all keys are in allowedKeys, false otherwise. 30 : 2 : * @desc Check if the variable is an Object and if all its keys are in the allowed keys. 31 : 2 : */ 32 [ + ]: 2 : export function isValidObject(variable, allowedKeys){ 33 [ + ][ + ]: 3 : if(typeof variable === 'object' && !Array.isArray(variable) && variable !== null){ 34 [ + ]: 2 : return Object.keys(variable).every(key => allowedKeys.includes(key)); 35 [ + ]: 2 : } 36 : 1 : return false; 37 : 3 : } 38 : 2 : 39 : 2 : /** 40 : 2 : * @func extractSubobjectByKeys 41 : 2 : * @static 42 : 2 : * @param {Object} originalDict - The original Object to extract from. 43 : 2 : * @param {Array} keysList - A list of keys to include in the sub-Object. 44 : 2 : * @return {Object} - A sub-Object containing only the specified keys. 45 : 2 : * @desc Extract a sub-Object from the original Object with keys specified in keysList. 46 : 2 : */ 47 [ + ]: 2 : export function extractSubobjectByKeys(originalDict, keysList){ 48 : 32 : const subDict = {}; 49 [ + ]: 32 : keysList.forEach((key) => { 50 : 37 : if(key in originalDict){ 51 : 37 : subDict[key] = originalDict[key]; 52 : 37 : } 53 : 32 : }); 54 : 32 : return subDict; 55 : 32 : }