Add more zlib utils

This commit is contained in:
Zerebos 2024-12-05 16:31:31 -05:00
parent 542d0c0ee4
commit 53b4bed979
No known key found for this signature in database
2 changed files with 34 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import Utilities from "@modules/utilities";
import {comparator} from "@structs/semver";
/**
@ -71,6 +72,27 @@ const Utils = {
*/
className() {
return Utilities.className(...arguments);
},
/**
* Gets a nested value (if it exists) of an object safely. keyPath should be something like `key.key2.key3`.
* Numbers can be used for arrays as well like `key.key2.array.0.id`.
* @param {object} obj - object to get nested value from
* @param {string} keyPath - key path to the desired value
*/
getNestedValue(obj, keyPath) {
return Utilities.getNestedValue(obj, keyPath);
},
/**
* This works on semantic versioning e.g. "1.0.0".
*
* @param {string} currentVersion
* @param {string} newVersion
* @returns {number} 0 indicates equal, -1 indicates left hand greater, 1 indicates right hand greater
*/
semverCompare(currentVersion, newVersion) {
return comparator(currentVersion, newVersion);
}
};

View File

@ -216,4 +216,16 @@ export default class Utilities {
return classes.join(" ");
}
/**
* Gets a nested value (if it exists) safely. Path should be something like `prop.prop2.prop3`.
* Numbers can be used for arrays as well like `prop.prop2.array.0.id`.
* @param {Object} obj - object to get nested value of
* @param {string} path - representation of the key path to obtain
*/
static getNestedValue(obj, path) {
return path.split(".").reduce(function(ob, prop) {
return ob && ob[prop];
}, obj);
}
}