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
  • parseNumericObj(obj)
  • Description
  • Arguments
  • Returns
  • Example
  • Usage
  • Notes

Was this helpful?

  1. Functions
  2. Object Methods

parseNumericObj

parseNumericObj(obj)

Converts numeric string values in an object to numbers, preserving non-numeric values.

Description

The parseNumericObj function iterates through the input object and converts numeric string values to numbers while preserving non-numeric values. It creates a new object with the converted numeric values and the original non-numeric values.

Arguments

  • obj (Record<string, any>): The input object to parse numeric values from.

Returns

  • Record<string, any>: A new object with numeric string values converted to numbers.

Example

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

const data = {
  count: '10',
  price: '19.99',
  name: 'Alice',
  active: 'true'
};

const parsedData = parseNumericObj(data); // ==> { count: 10, price: 19.99, name: 'Alice', active: 'true' }

Usage

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

const details = {
  quantity: '5',
  weight: '2.5',
  inStock: 'true',
  category: 'electronics'
};

const parsedDetails = parseNumericObj(details); // ==> { quantity: 5, weight: 2.5, inStock: true, category: 'electronics' }

Notes

  • The parseNumericObj function selectively converts numeric string values in an object to numbers while preserving non-numeric values.

  • It ensures that only valid numeric strings are converted, maintaining the integrity of non-numeric data.

  • This function is useful for scenarios where specific values need to be treated as numbers for calculations or comparisons while keeping other values unchanged.

PreviousquerylizeNextreserveKeys

Last updated 11 months ago

Was this helpful?