HasValue in Typescript

by Terrance Smith

TypeScript

/** Class with a view defined helper methods. */
class ObjectHelper {
  /**
   * Checks to see if the passed in object exists.
   *
   * @param {Object} object is the object being validated.
   * @returns {boolean} true/false.
   */
  static isEmpty(object: Object): boolean {
    if (object === null || object === undefined) {
      return false;
    }
    return Object.keys(object).length === 0;
  }

  /**
   * Checks to see if the passed in object exists.
   *
   * @param {Any} object is the object being validated.
   * @returns {boolean} true/false.
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static hasValue(object: any): boolean {
    return !(!object);
  }
}

const a = null;
const b = undefined;
const c = '';
const d = [];
const e = 0;

const emptyValTestVals = [
  null,
  undefined,
  '',
  [],
  0
];

emptyValTestVals.forEach((v) => {
  const isEmptyOut = ObjectHelper.isEmpty(v);
  const hasValueOut = ObjectHelper.hasValue(v);
  console.log('Value : ' + v + ' || hasValue : ' + hasValueOut +' || isEmpty : ' + isEmptyOut);  
});