Skip to content

Instantly share code, notes, and snippets.

@burn2delete
Last active December 23, 2023 17:07
Show Gist options
  • Save burn2delete/ec9b5bb148a7170cfc962c3fdc66c49d to your computer and use it in GitHub Desktop.
Save burn2delete/ec9b5bb148a7170cfc962c3fdc66c49d to your computer and use it in GitHub Desktop.
Utilities written in Typescript
/**
* Checks if the given value is null or undefined.
*
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is null or undefined, false otherwise.
*/
const isNullOrUndefined = (value: any): boolean => value === null || value === undefined;
/**
* Checks if the given value is an array.
*
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is an array, false otherwise.
*/
const isArray = (value: any): boolean => Array.isArray(value);
/**
* Checks if the given value is an object.
*
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is an object, false otherwise.
*/
const isObject = (value: any): boolean => typeof value === 'object';
/**
* Checks if the given value is a string.
*
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is a string, false otherwise.
*/
const isString = (value: any): boolean => typeof value === 'string';
/**
* Checks if the given array is empty.
*
* @param {any[]} arr - The array to check.
* @returns {boolean} - Returns true if the array is empty, false otherwise.
*/
const isArrayEmpty = (arr: any[]): boolean => !isNullOrUndefined(arr) && !isArray(arr) || !arr.length;
/**
* Checks if the given object is empty.
*
* @param {object} obj - The object to check.
* @returns {boolean} - Returns true if the object is empty, false otherwise.
*/
const isObjectEmpty = (obj: object): boolean => !isNullOrUndefined(obj) && !isObject(obj) || !Object.keys(obj).length;
/**
* Checks if the given string is empty.
*
* @param {string} str - The string to check.
* @returns {boolean} - Returns true if the string is empty, false otherwise.
*/
const isStringEmpty = (str: string): boolean => !isNullOrUndefined(str) && !isString(str) || !str.length;
/**
* Checks if the given value is empty.
*
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is empty, false otherwise.
*/
const isEmpty = (value: any): boolean => isNullOrUndefined(value) || isArrayEmpty(value) || isObjectEmpty(value) || isStringEmpty(value);
/**
* Checks if the given value is a Function.
*
* @param {any} value - The value to check.
* @returns {boolean} - Returns true if the value is null or undefined, false otherwise.
*/
const isFunction = (value: any): boolean => typeof value === 'function';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment