@rxap/utilities
Version:
A collection of utility functions, types and interfaces.
224 lines • 10.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IDENTIFIER_PROPERTY_KEYS = void 0;
exports.isObject = isObject;
exports.applyContextToFunctionOrConstant = applyContextToFunctionOrConstant;
exports.ToControlOptions = ToControlOptions;
exports.hasIdentifierProperty = hasIdentifierProperty;
exports.getIdentifierProperties = getIdentifierProperties;
exports.getIdentifierPropertyValue = getIdentifierPropertyValue;
exports.NormalizeIconConfig = NormalizeIconConfig;
exports.IsSvgIcon = IsSvgIcon;
exports.IsMaterialIcon = IsMaterialIcon;
const has_property_1 = require("./has-property");
/**
* This function checks if a given value is an object.
*
* @export
* @function isObject
* @param {any} value - The value to be checked.
* @returns {boolean} - Returns true if the value is an object (excluding arrays, functions, and null), otherwise returns false.
*
* @description
* The function uses the JavaScript `typeof` operator to determine the type of the input value. It checks against a number of primitive types (number, bigint, boolean, function, string, symbol, undefined) and returns false if the value is of any of these types.
* If the value is of type 'object', the function further checks if the value is not null, not undefined, and not an array. If all these conditions are met, the function returns true, indicating that the value is indeed an object.
* For any other type not covered by the above cases, the function returns false.
*
* Note: This function uses TypeScript's type guard syntax (`value is object`) in its return type. This means that if the function returns true, TypeScript will treat the value as an object in the subsequent code.
*/
function isObject(value) {
switch (typeof value) {
case 'number':
case 'bigint':
case 'boolean':
case 'function':
case 'string':
case 'symbol':
case 'undefined':
return false;
case 'object':
return value !== null && value !== undefined && !Array.isArray(value);
default:
return false;
}
}
/**
* This function applies a given context to a function or a constant. If the provided argument is a function, it will be invoked with the provided arguments. If it's a constant, the constant itself will be returned. If the function or constant is not defined or returns undefined, a default value will be returned instead.
*
* @template T - The type of the return value of the function or the type of the constant.
* @template A - The type of the array of arguments that the function can accept.
*
* @param {FunctionOrConstant<T, A>} [functionOrConstant] - The function or constant to which the context should be applied. This parameter is optional.
* @param {A} [args] - An array of arguments to be passed to the function if the first parameter is a function. This parameter is optional.
* @param {T | null} [defaultValue=null] - The default value to be returned if the function or constant is not defined or returns undefined. This parameter is optional and defaults to null.
*
* @returns {T | null} - The result of the function invocation or the constant itself, or the default value if the function or constant is not defined or returns undefined.
*
* @example
* // If functionOrConstant is a function:
* applyContextToFunctionOrConstant((x, y) => x + y, [1, 2], 0); // Returns 3
* // If functionOrConstant is a constant:
* applyContextToFunctionOrConstant(5, null, 0); // Returns 5
* // If functionOrConstant is undefined:
* applyContextToFunctionOrConstant(undefined, null, 0); // Returns 0
*/
function applyContextToFunctionOrConstant(functionOrConstant, args, defaultValue = null) {
let value;
if (typeof functionOrConstant === 'function') {
value = functionOrConstant(...(args || []));
}
else {
value = functionOrConstant;
}
return value === undefined ? defaultValue : value;
}
/**
* Converts an array of strings into an array of ControlOptions objects.
* Each ControlOptions object consists of two properties: 'value' and 'display'.
* Both properties are assigned the same string value from the input array.
*
* @export
* @function ToControlOptions
* @param {string[]} values - An array of strings to be converted into ControlOptions objects.
* @returns {ControlOptions<string>[]} - An array of ControlOptions objects. Each object has 'value' and 'display' properties, both assigned the same string value from the input array.
*
* @example
* // returns [{value: 'example1', display: 'example1'}, {value: 'example2', display: 'example2'}]
* ToControlOptions(['example1', 'example2']);
*
* @see ControlOptions
*/
function ToControlOptions(values) {
return values.map(value => ({
value,
display: value,
}));
}
exports.IDENTIFIER_PROPERTY_KEYS = [
'id',
'uuid',
'_id',
'__id',
'key',
];
/**
* Checks if the provided object has any of the properties defined in the IDENTIFIER_PROPERTY_KEYS array.
*
* @export
* @function hasIdentifierProperty
* @param {any} obj - The object to be checked for identifier properties. This can be of any type.
* @returns {boolean} - Returns true if the object has at least one property that matches a key in the IDENTIFIER_PROPERTY_KEYS array. Otherwise, it returns false.
*
* @example
* // Assuming IDENTIFIER_PROPERTY_KEYS = ['id', 'key', 'identifier']
* let obj = { id: 1, name: 'John Doe' };
* console.log(hasIdentifierProperty(obj)); // Outputs: true
*
* @example
* let obj = { name: 'John Doe' };
* console.log(hasIdentifierProperty(obj)); // Outputs: false
*
* @note The function uses the 'some' method of the Array prototype, which tests whether at least one element in the array passes the test implemented by the provided function.
* @note The function uses the 'hasProperty' function to check if the object has a property. The 'hasProperty' function is not defined in this documentation.
*/
function hasIdentifierProperty(obj) {
return exports.IDENTIFIER_PROPERTY_KEYS.some(pk => (0, has_property_1.hasProperty)(obj, pk));
}
/**
* This function retrieves the identifier properties from a given object.
*
* @export
* @function getIdentifierProperties
* @param {any} obj - The object from which to extract identifier properties. This can be of any type that is an object (e.g., Array, Function, Object, Date, null).
* @returns {string[]} - An array of strings representing the keys of the identifier properties found in the input object. If no identifier properties are found, an empty array is returned.
*
* @description
* This function uses the `Object.keys()` method to get an array of the object's own enumerable property names, iterated in the same order that a normal loop would. It then filters this array to only include keys that are also present in the `IDENTIFIER_PROPERTY_KEYS` array.
*
* Note: The `IDENTIFIER_PROPERTY_KEYS` array is not defined within this function, and must be available in the scope where this function is called.
*
* This function does not modify the input object.
*/
function getIdentifierProperties(obj) {
return Object.keys(obj).filter(pk => exports.IDENTIFIER_PROPERTY_KEYS.includes(pk));
}
/**
* This function is used to retrieve the identifier property value from an object. The identifier property can be one of the following: 'id', 'uuid', '_id', '__id', 'key', or 'index'. If the object is a string, the function will return the string itself. If none of these properties exist, the function will return null.
*
* @template T - The type of the identifier property value. By default, it is a string.
*
* @param {any} obj - The object from which to retrieve the identifier property value. This can be of any type, but typically it would be an object or a string.
*
* @returns {T | null} - The value of the identifier property if it exists, otherwise null. If the object is a string, the function will return the string itself. The return type will be of type T, or null if no identifier property exists.
*
* @example
* // returns '123'
* getIdentifierPropertyValue<{id: '123'}>({id: '123'});
*
* @example
* // returns 'abc'
* getIdentifierPropertyValue('abc');
*
* @example
* // returns null
* getIdentifierPropertyValue({});
*/
function getIdentifierPropertyValue(obj) {
return obj.id || obj.uuid || obj._id || obj.__id || obj.key || obj.index || (typeof obj === 'string' && obj) || null;
}
function NormalizeIconConfig(icon) {
if (typeof icon === 'string') {
return {
icon,
};
}
return icon;
}
/**
* This function checks if the provided icon is of type SvgIcon.
*
* @export
* @function IsSvgIcon
* @param {IconConfig} [icon] - An optional parameter that represents the icon configuration.
* @returns {boolean} - The function returns a boolean value. If the icon is of type SvgIcon, it returns true. Otherwise, it returns false.
*
* The function works by first checking if the icon is not undefined. If the icon is undefined, it immediately returns false.
* If the icon is defined, it then checks if the 'svgIcon' property exists on the icon object. If the 'svgIcon' property exists, it returns true, indicating that the icon is of type SvgIcon.
* If the 'svgIcon' property does not exist, it returns false, indicating that the icon is not of type SvgIcon.
*
* @example
*
* let icon1 = { svgIcon: 'home' };
* let icon2 = { pngIcon: 'home' };
*
* console.log(IsSvgIcon(icon1)); // Outputs: true
* console.log(IsSvgIcon(icon2)); // Outputs: false
*
* @see {@link IconConfig} for more information about the icon configuration object.
* @see {@link SvgIcon} for more information about the SvgIcon type.
*/
function IsSvgIcon(icon) {
return !!icon && typeof icon === 'object' && 'svgIcon' in icon;
}
/**
* This function checks if the provided icon is a MaterialIcon.
*
* @export
* @function IsMaterialIcon
* @param {IconConfig} [icon] - An optional parameter that represents the icon configuration.
* @returns {boolean} - Returns true if the provided icon is a MaterialIcon, otherwise returns false.
*
* The function works by checking if the icon parameter is not undefined and if the 'icon' property of the icon parameter is not undefined.
* If both conditions are met, it means that the icon is a MaterialIcon, so the function returns true.
* If any of the conditions is not met, it means that the icon is not a MaterialIcon, so the function returns false.
*
* @example
*
* IsMaterialIcon({icon: 'home', color: 'primary'}); // returns true
* IsMaterialIcon(); // returns false
* IsMaterialIcon({color: 'primary'}); // returns false
*/
function IsMaterialIcon(icon) {
return !!icon && typeof icon === 'object' && 'icon' in icon;
}
//# sourceMappingURL=helpers.js.map