Get Full Access

len() function

/**
 * Returns the length of a collection.
 * For arrays, returns the number of elements.
 * For strings, returns the length of the string.
 * For objects, returns the number of properties.
 * Throws an error for other types.
 *
 * @param {any} value - The value to get the length of.
 * @returns {number} The length of the value.
 */
function len(value) {
  if (Array.isArray(value)) {
    return value.length;
  } else if (typeof value === "string") {
    return value.length;
  } else if (typeof value === "object") {
    return Object.keys(value).length;
  }

  throw new Error("Invalid type");
}