Skip to content

Parse Utility

A collection of standalone functions for type checking, safe type conversion, and string/object/array transformation - the small helpers repositories and controllers reach for when normalizing loosely typed input.

In one example

typescript
import { int, float, toBoolean, toCamel, keysToCamel } from '@venizia/ignis-helpers';

const myInt = int('1,000'); // => 1000
const myFloat = float('1,234.567', 2); // => 1234.57
const myBool = toBoolean('true'); // => true

const camelObject = keysToCamel({ 'first-name': 'John', 'last_name': 'Doe' });
// => { firstName: 'John', lastName: 'Doe' }

Functions

FunctionSignatureWhat it does
isIntisInt(n: any): booleantrue when n is (or coerces to) an integer.
isFloatisFloat(input: any): booleantrue when input is (or coerces to) a non-integer number.
intint(input: any): numberParses input to an integer. Strips commas first; returns 0 for empty/invalid input.
floatfloat(input: any, digit = 2): numberParses input to a float, rounded to digit places (via lodash round). Strips commas first; returns 0 for empty/invalid input.
toBooleantoBoolean(input: any): booleanfalse for '', 'false', '0', false, 0, null, undefined; true for everything else.
toCameltoCamel(s: string): stringConverts a snake_case or kebab-case string to camelCase.
keysToCamelkeysToCamel(object: object): anyRecursively camelizes every key in object. Arrays stay arrays (their object elements are still camelized); Date values pass through untouched.
parseArrayToMapWithKeyparseArrayToMapWithKey<T, K>(arr: T[], keyMap: K): Map<T[K], T>Turns an array of objects into a Map keyed by keyMap. Positional arguments, not an options object. Throws if keyMap is missing from an element; last element wins on duplicate keys.
toDelimitedArraytoDelimitedArray(input: unknown, separator = ','): string[]Splits input on separator into trimmed, non-empty entries. null/undefined input returns [].
toTrimmedtoTrimmed(input: unknown): stringStringifies and trims input; null/undefined returns ''.
getUIDgetUID(): stringGenerates a short, uppercase, Math.random()-based ID - not cryptographically unique.

Notes

  • int/float are comma-tolerant. Both strip , before parsing, so '1,000' and 1000 behave the same - useful for user-typed numeric input.
  • parseArrayToMapWithKey breaks the options-object convention on purpose - it takes (arr, keyMap) positionally, unlike every other function on this page.
  • toDelimitedArray and toTrimmed are the transform functions for list-shaped and string env values - typical use is applicationEnvironment.get(KEY, { transform: toDelimitedArray }).
  • getUID is for short, human-glanceable identifiers, not for anything that needs global uniqueness guarantees - use the UID helper (Snowflake-based) for that.

See also

Files: