ts-utils
  • Getting Started
  • Contributing
  • Testing
  • Functions
    • Array Methods
      • isArr
      • isArrEmpty
      • pushUniqueValue
      • pushOrUpdate
    • Date Methods
      • toNow
      • formatDate
      • fromNow
    • Number Methods
      • toFixed
      • parseNumber
      • parseFormat
      • addUnit
      • addSeparators
      • formatNumber
      • ensureNumber
      • numberEmptyState
    • Object Methods
      • parseJSON
      • deepClone
      • parseNumValues
      • removeEmptyKeys
      • sanitizeQuery
      • removeKeys
      • renameKey
      • renameKeys
      • sortByKeyLength
      • querylize
      • parseNumericObj
      • reserveKeys
      • hasKeysAndValues
    • String Methods
      • randomStr
      • toUpperCase
      • toLowerCase
      • toFullUrl
      • initials
      • generateUuid
Powered by GitBook
On this page
  • numberEmptyState(value, format)
  • Arguments
  • Returns
  • Example
  • Usage
  • Notes

Was this helpful?

  1. Functions
  2. Number Methods

numberEmptyState

numberEmptyState(value, format)

Takes an input value and a fallback format as arguments. It checks if the input value is considered "empty" or invalid (i.e., "0" or "NaN") and returns the fallback format if so. Otherwise, it returns the original input value. This function is useful for handling cases where a numeric value may be empty or invalid and a fallback display is needed.

Arguments

  • value (string | number): The input value to be displayed.

  • format (any, optional): The fallback format to use when the input value is considered "empty" or invalid. Defaults to "---" if not provided.

Returns

  • (any): The formatted value or the fallback format.

Example

typescript
import { numberEmptyState } from 'check-engineering/ts-utils';

const validInput = 42;
const invalidInput = "NaN";
const fallbackFormat = "---";

const validDisplay = numberEmptyState(validInput, fallbackFormat); // => 42
const fallbackDisplay = numberEmptyState(invalidInput, fallbackFormat); // Output: "---"

Usage

typescript
import { numberEmptyState } from 'check-engineering/ts-utils';

const userInput = document.getElementById('user-input').value;
const fallbackDisplay = "N/A";

const safeDisplay = numberEmptyState(userInput, fallbackDisplay); // => "N/A"
document.getElementById('display').innerHTML = safeDisplay;

Notes

  • The numberEmptyState function provides a way to handle "empty" or invalid numeric values by returning a fallback format.

  • It checks if the input value is "0" or "NaN" and returns the fallback format if so.

  • This function is particularly useful for displaying numeric values in a user interface, where a fallback display is needed for empty or invalid values.

PreviousensureNumberNextObject Methods

Last updated 11 months ago

Was this helpful?