hfs-utilities
Version:
Health Fund Solution's internal utilities library for Typescript projects
104 lines • 4.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDate = exports.getFloat = exports.isNumber = exports.isString = exports.isNotEmptyString = exports.isEmptyString = exports.asArray = exports.isEmpty = exports.isNotNullAndNotUndefined = exports.isNullOrUndefined = void 0;
const moment_timezone_1 = __importDefault(require("moment-timezone"));
/**
* @description - Checks if a value is null or undefined.
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is null or undefined.
*/
const isNullOrUndefined = (value) => {
return value === undefined || value === null;
};
exports.isNullOrUndefined = isNullOrUndefined;
/**
* @description - Checks if a value is not null and not undefined.
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is not null and not undefined.
*/
const isNotNullAndNotUndefined = (value) => {
return value !== null && value !== undefined;
};
exports.isNotNullAndNotUndefined = isNotNullAndNotUndefined;
/**
* @description - Checks if a value is null, undefined, an empty string, an empty array, an object without properties, or 0.
* @param value - The value to check.
* @returns - Returns true if the value is null, undefined, an empty string, an empty array, an object without properties, or 0.
*/
const isEmpty = (value) => {
return ((0, exports.isNullOrUndefined)(value) ||
value === '' ||
(Array.isArray(value) && value.length === 0) ||
(typeof value === 'object' && Object.keys(value).length === 0) ||
value === 0);
};
exports.isEmpty = isEmpty;
/**
* @description - coerces a value to an array if it is not already an array.
* @param value - The value to coerce.
* @returns - Returns the value as an array or an empty array if null or undefined is passed.
*/
const asArray = (value) => {
return (0, exports.isNullOrUndefined)(value) ? [] : Array.isArray(value) ? value : [value];
};
exports.asArray = asArray;
/**
* @description - Checks if a value is an empty string
* @param value - The value to check.
* @returns {boolean} - Returns true if the value is an empty string.
*/
const isEmptyString = (value) => (0, exports.isString)(value) && value.trim() === '';
exports.isEmptyString = isEmptyString;
/**
* @description - Checks if a value is a none empty string
* @param value - The value to check.
* @returns { boolean } - Returns true if the value is a none empty string.
*/
const isNotEmptyString = (value) => (0, exports.isString)(value) && value.trim().length > 0;
exports.isNotEmptyString = isNotEmptyString;
/**
* @description - Checks if a value is a string
* @param value - The value to check.
* @returns { boolean } - Returns true if the value is a string.
*/
const isString = (value) => typeof value === 'string';
exports.isString = isString;
/**
* @description - Checks if a value is a number
* @param value - The value to check.
* @returns { boolean } - Returns true if the value is a number.
*/
const isNumber = (value) => typeof value === 'number';
exports.isNumber = isNumber;
/**
* @description - Returns a float value or undefined if the value is not a number.
* @param number - The value to convert to a float.
* @returns { number | undefined } - Returns a float value or undefined if the value is not a number.
*/
const getFloat = (number) => {
if ((0, exports.isNullOrUndefined)(number)) {
return undefined;
}
else {
const result = parseFloat(number.toString().replace(/[$,]+/g, ''));
return isNaN(result) ? undefined : result;
}
};
exports.getFloat = getFloat;
/**
* @description - Returns a date object from a string or null if the value is not a valid date.
* @param date - The value to convert to a date.
* @returns { Date | null } - Returns a date object or null if the value is not a valid date.
*/
const getDate = (date, hoursToAdd) => {
if ((0, exports.isNullOrUndefined)(date))
return null;
if ((0, exports.isString)(date))
return (0, moment_timezone_1.default)(date, 'YYYY-MM-DD').add(hoursToAdd !== null && hoursToAdd !== void 0 ? hoursToAdd : 0, 'hours');
return (0, moment_timezone_1.default)(date).add(hoursToAdd !== null && hoursToAdd !== void 0 ? hoursToAdd : 0, 'hours');
};
exports.getDate = getDate;
//# sourceMappingURL=utilities.js.map